2 ///<script type="text/javascript">
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
12 GI = imports.gi.GIRepository;
14 GLib = imports.gi.GLib;
16 Notify = imports.gi.Notify;
18 Spawn = imports.Spawn;
20 StatusIcon = imports.StatusIcon.StatusIcon;
21 Monitor = imports.Monitor.Monitor;
24 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
25 Gtk.init (null, null);
27 var gitlive = GLib.get_home_dir() + "/gitlive";
29 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
30 var msg = new Gtk.MessageDialog({message_type:
31 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
39 var monitor = new Monitor({
43 nqv : false, // temp var while I switch to queued version.
47 this.lastAdd = new Date();
49 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
50 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
51 if (!_this.queue.length || _this.queueRunning) {
54 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
55 if (last < 4) { // wait 1/2 a seconnd before running.
62 Monitor.prototype.start.call(this);
63 var notification = new Notify.Notification({
65 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
68 notification.set_timeout(500);
73 * - pulls the items off the queue
74 * (as commands run concurrently and new items may get added while it's running)
75 * - runs the queue items
81 this.queueRunning = true;
83 this.queue.forEach(function (q) {
86 this.queue = []; // empty queue!
91 cmds.forEach(function(cmd) {
92 if (repos.indexOf(cmd[0]) < 0) {
95 var sp = Git.run.apply(Git,cmd);
99 success.push(sp.args.join(' '));
100 if (sp.output.length) success.push(sp.output + '');
101 // if (sp.stderr.length) success.push(sp.stderr + '');
104 failure.push(sp.args.join(' '));
105 if (sp.output.length) failure.push(sp.output);
106 if (sp.stderr.length) failure.push(sp.stderr);
114 repos.forEach(function(r) {
115 var sp = Git.run(r , 'push', { all: true } );
122 if (success.length) {
123 print(success.join("\n"));
124 var notification = new Notify.Notification({
125 summary: "Git Live Commited",
126 body : success.join("\n")
130 notification.set_timeout(500);
133 if (failure.length) {
135 var notification = new Notify.Notification({
136 summary: "Git Live ERROR!!",
137 body : failure.join("\n")
141 notification.set_timeout(500);
144 this.queueRunning = false;
147 shouldIgnore: function(f)
149 if (f.name[0] == '.') {
151 if (f.name == '.htaccess') {
157 if (f.name.match(/~$/)) {
160 // ignore anything in top level!!!!
161 if (!f.vpath.length) {
169 parsePath: function(f) {
171 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
173 f.gitpath = gitlive + '/' + vpath_ar.shift();
174 f.vpath = vpath_ar.join('/');
181 onChanged : function(src)
183 return; // always ignore this..?
184 //this.parsePath(src);
186 onChangesDoneHint : function(src)
189 if (this.shouldIgnore(src)) {
194 if (typeof(this.just_created[src.path]) !='undefined') {
195 delete this.just_created[src.path];
196 this.lastAdd = new Date();
198 [ src.gitpath, 'add', src.vpath ],
199 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
204 Git.run(src.gitpath, 'add', src.vpath);
205 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
206 Git.run(src.gitpath , 'push', { all: true } );
207 notify(src.name,"CHANGED", sp);
211 this.lastAdd = new Date();
213 [ src.gitpath, 'add', src.vpath ],
214 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
219 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
220 Git.run(src.gitpath , 'push', '--all' );
221 notify(src.name,"CHANGED", sp);
225 onDeleted : function(src)
228 if (this.shouldIgnore(src)) {
231 // should check if monitor needs removing..
232 // it should also check if it was a directory.. - so we dont have to commit all..
234 this.lastAdd = new Date();
236 [ src.gitpath, 'rm' , src.vpath ],
237 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
244 var sp = Git.run(src.gitpath,'rm' , src.vpath);
245 Git.run(src.gitpath , 'push', { all: true } );
247 notify(src.name,"DELETED", sp);
250 sp = Git.run(src.gitpath,'commit' ,{ all: true, message: src.vpath});
251 Git.run(src.gitpath , 'push',{ all: true });
252 notify(src.name,"DELETED", sp);
256 onCreated : function(src)
259 if (this.shouldIgnore(src)) {
263 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
264 this.just_created[src.path] = true;
265 return; // we do not handle file create flags... - use done hint.
267 // director has bee created
268 this.monitor(src.path);
269 this.lastAdd = new Date();
271 [ src.gitpath, 'add' , src.vpath, { all: true } ],
272 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
278 var sp = Git.run(src.gitpath, 'add', src.vpath);
279 Git.run(src.gitpath , 'push', { all: true } );
282 notify(src.path,"CREATED", sp);
285 //uh.call(fm,f,of, event_type);
286 sp = Git.run(src.gitpath,'commit' , { all: true, message: src.vpath});
287 Git.run(src.gitpath , 'push', { all: true } );
288 notify(src.path,"CREATED", sp);
292 onAttributeChanged : function(src) {
294 if (this.shouldIgnore(src)) {
297 this.lastAdd = new Date();
299 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
304 var sp = Git.run(src.gitpath, 'commit',{ all: true, message: src.vpath});
305 Git.run(src.gitpath , 'push', { all: true } );
306 notify(src.path,"ATTRIBUTE_CHANGED", sp);
311 onMoved : function(src,dest)
314 this.parsePath(dest);
316 if (src.gitpath != dest.gitpath) {
318 this.onCreated(dest);
319 this.onChangedDoneHint(dest);
322 // needs to handle move to/from unsupported types..
324 if (this.shouldIgnore(src)) {
327 if (this.shouldIgnore(dest)) {
330 this.lastAdd = new Date();
332 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
333 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
334 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
341 var sp = Git.run(src.gitpath, 'mv', '-k', src.vpath, dest.vpath);
343 notify(dest.path,"MOVED", sp);
346 sp = Git.run(src.gitpath,'commit' , { all: true, message: 'MOVED ' + src.vpath +' to ' + dest.vpath} );
347 Git.run(src.gitpath , 'push', { all: true } );
348 notify(src.path,"MOVED", sp);
358 function notify(fn, act , sp)
360 var sum = act + " " + fn;
362 var notification = new Notify.Notification({
367 notification.set_timeout(500);
375 function errorDialog(data) {
376 var msg = new Gtk.MessageDialog({
377 message_type: Gtk.MessageType.ERROR,
378 buttons : Gtk.ButtonsType.OK,
390 // need a better icon...
393 Notify.init("gitlive");
394 monitor.add(GLib.get_home_dir() + "/gitlive");
397 //icon.signal["activate"].connect(on_left_click);