gitlive.js
[gitlive] / gitlive.js
1 #!/usr/bin/seed
2 ///<script type="text/javascript">
3 /**
4 * Git Live
5
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
9
10
11 */
12 GI      = imports.gi.GIRepository;
13 Gio      = imports.gi.Gio;
14 GLib      = imports.gi.GLib;
15 Gtk      = imports.gi.Gtk;
16 Notify = imports.gi.Notify;
17
18 Spawn = imports.Spawn;
19 Git = imports.Git;
20 StatusIcon = imports.StatusIcon.StatusIcon;
21 Monitor = imports.Monitor.Monitor;
22
23
24 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
25 Gtk.init (null, null);
26  
27 var gitlive = GLib.get_home_dir() + "/gitlive";
28
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."});
32     msg.run();
33     msg.destroy();
34     
35     Seed.quit();
36 }
37
38  
39 var monitor = new Monitor({
40     
41     queue : [],
42     queueRunning : false,
43     nqv : false, // temp var while I switch to queued version.
44     
45     start: function() {
46         var _this = this;
47         this.lastAdd = new Date();
48          
49         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
50             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
51             if (!_this.queue.length || _this.queueRunning) {
52                 return 1;
53             }
54             var last = Math.floor(((new Date()) - this.lastAdd) / 100);
55             if (last < 4) { // wait 1/2 a seconnd before running.
56                 return 1;
57             }
58             _this.runQueue();
59             return 1;
60         },null,null);
61         
62         Monitor.prototype.start.call(this);
63         var notification = new Notify.Notification({
64             summary: "Git Live",
65             body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
66         });
67
68         notification.set_timeout(2000);
69         notification.show();   
70     },
71     /**
72      * run the queue.
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
76      * - pushes upstream.
77      * 
78      */
79     runQueue: function()
80     {
81         this.queueRunning = true;
82         var cmds = [];
83         this.queue.forEach(function (q) {
84             cmds.push(q);
85         });
86         this.queue = []; // empty queue!
87         
88         var success = [];
89         var failure = [];
90         var repos = [];
91         cmds.forEach(function(cmd) {
92             if (repos.indexOf(cmd[0]) < 0) {
93                 repos.push(cmd[0]);
94             }
95             var sp = Git.run.apply(Git,cmd);
96              
97             switch (sp.result * 1) {
98                 case 0: // success:
99                     success.push(sp.args.join(' '));
100                     if (sp.output.length) success.push(sp.output + '');
101                   // if (sp.stderr.length) success.push(sp.stderr + '');
102                     break;
103                 default: 
104                     failure.push(sp.args.join(' '));
105                     if (sp.output.length) failure.push(sp.output);
106                     if (sp.stderr.length) failure.push(sp.stderr);
107                     break;
108             }
109             
110         });
111          
112         // push upstream.
113         repos.forEach(function(r) {
114             var sp = Git.run(r , 'push', { all: true } );
115             if (sp.length) {
116                 success.push(sp);
117             }
118             
119         });
120         
121         if (success.length) {
122             print(success.join("\n"));
123             var notification = new Notify.Notification({
124                 summary: "Git Live Commited",
125                 body : success.join("\n")
126                 
127             });
128
129             notification.set_timeout(2000);
130             notification.show();   
131         }
132         if (failure.length) {
133         
134             var notification = new Notify.Notification({
135                 summary: "Git Live ERROR!!",
136                 body : failure.join("\n")
137                 
138             });
139
140             notification.set_timeout(2000);
141             notification.show();   
142         }
143         this.queueRunning = false;
144     },
145     
146     shouldIgnore: function(f)
147     {
148         if (f.name[0] == '.') {
149             // except!
150             if (f.name == '.htaccess') {
151                 return false;
152             }
153             
154             return true;
155         }
156         if (f.name.match(/~$/)) {
157             return true;
158         }
159         // ignore anything in top level!!!!
160         if (!f.vpath.length) {
161             return true;
162         }
163         
164         return false;
165         
166     },
167     
168     parsePath: function(f) {
169            
170         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
171         
172         f.gitpath = gitlive + '/' + vpath_ar.shift();
173         f.vpath =  vpath_ar.join('/');
174         
175         
176     },
177     
178     just_created : {},
179       
180     onChanged : function(src) 
181     { 
182         return; // always ignore this..?
183         //this.parsePath(src);
184     },
185     onChangesDoneHint : function(src) 
186     { 
187         this.parsePath(src);
188         if (this.shouldIgnore(src)) {
189             return;
190         }
191         
192         var add_it = false;
193         if (typeof(this.just_created[src.path]) !='undefined') {
194             delete this.just_created[src.path];
195             this.lastAdd = new Date();
196             this.queue.push( 
197                 [ src.gitpath,  'add', src.vpath ],
198                 [ src.gitpath,  'commit',  src.vpath, { message: src.vpath} ] 
199                 
200             );
201             if (this.nqv) {
202                 
203                 Git.run(src.gitpath, 'add', src.vpath);
204                 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
205                 Git.run(src.gitpath , 'push', { all: true } );
206                 notify(src.name,"CHANGED", sp);
207             }
208             return;
209         }
210         this.lastAdd = new Date();
211         this.queue.push( 
212             [ src.gitpath,  'add', src.vpath ],
213             [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ]
214
215             
216         );
217         if (this.nqv) {
218             var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
219             Git.run(src.gitpath , 'push', '--all' );
220             notify(src.name,"CHANGED", sp);
221         }
222
223     },
224     onDeleted : function(src) 
225     { 
226         this.parsePath(src);
227         if (this.shouldIgnore(src)) {
228             return;
229         }
230         // should check if monitor needs removing..
231         // it should also check if it was a directory.. - so we dont have to commit all..
232         
233         this.lastAdd = new Date();
234         this.queue.push( 
235             [ src.gitpath, 'rm' , src.vpath ],
236             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
237             
238         );
239         if (!this.nqv) {
240             return;
241         }
242         
243         var sp = Git.run(src.gitpath,'rm' , src.vpath);
244         Git.run(src.gitpath , 'push', { all: true } );
245         if (sp.status !=0) {
246             notify(src.name,"DELETED", sp);
247             return;
248         }
249         sp = Git.run(src.gitpath,'commit' ,{ all: true, message: src.vpath});
250         Git.run(src.gitpath , 'push',{ all: true });
251         notify(src.name,"DELETED", sp);
252         return;
253         
254     },
255     onCreated : function(src) 
256     { 
257         this.parsePath(src);
258         if (this.shouldIgnore(src)) {
259             return;
260         }
261         
262         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
263             this.just_created[src.path] = true;
264             return; // we do not handle file create flags... - use done hint.
265         }
266         // director has bee created
267         this.monitor(src.path);
268         this.lastAdd = new Date();
269         this.queue.push( 
270             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
271             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
272             
273         );
274         if (!this.nqv) {
275             return;
276         }
277         var sp = Git.run(src.gitpath, 'add', src.vpath);
278         Git.run(src.gitpath , 'push', { all: true } );
279
280         if (sp.status !=0) {
281             notify(src.path,"CREATED", sp);
282             return;
283         }
284         //uh.call(fm,f,of, event_type);
285         sp = Git.run(src.gitpath,'commit' , { all: true, message: src.vpath});
286         Git.run(src.gitpath , 'push', { all: true } );
287         notify(src.path,"CREATED", sp);
288         return;
289         
290     },
291     onAttributeChanged : function(src) { 
292         this.parsePath(src);
293         if (this.shouldIgnore(src)) {
294             return;
295         }
296         this.lastAdd = new Date();
297         this.queue.push( 
298             [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
299         );
300         if (!this.nqv) {
301             return;
302         }
303         var sp = Git.run(src.gitpath, 'commit',{ all: true, message: src.vpath});
304         Git.run(src.gitpath , 'push', { all: true } );
305         notify(src.path,"ATTRIBUTE_CHANGED", sp);
306         return;
307     
308     },
309     
310     onMoved : function(src,dest)
311     { 
312         this.parsePath(src);
313         this.parsePath(dest);
314         
315         if (src.gitpath != dest.gitpath) {
316             this.onDeleted(src);
317             this.onCreated(dest);
318             this.onChangedDoneHint(dest);
319             return;
320         }
321         // needs to handle move to/from unsupported types..
322         
323         if (this.shouldIgnore(src)) {
324             return;
325         }
326         if (this.shouldIgnore(dest)) {
327             return;
328         }
329         this.lastAdd = new Date();
330         this.queue.push( 
331             [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
332             [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
333                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ]
334         );
335         
336         if (!this.nqv) {
337             return;
338         }
339         
340         var sp = Git.run(src.gitpath,  'mv',  '-k', src.vpath, dest.vpath);
341         if (sp.status !=0) {
342             notify(dest.path,"MOVED", sp);
343             return;
344         }
345         sp = Git.run(src.gitpath,'commit' , { all: true, message:   'MOVED ' + src.vpath +' to ' + dest.vpath} );
346         Git.run(src.gitpath , 'push', { all: true } );
347         notify(src.path,"MOVED", sp);
348         
349     }
350           
351     
352 });
353  
354   
355 function notify(fn, act , sp)
356 {
357     var sum = act + " " + fn;
358     
359     var notification = new Notify.Notification({
360         summary: sum,
361                 body : sp
362         });
363
364     notification.set_timeout(2000);
365     notification.show();
366 }
367
368
369
370   
371
372 function errorDialog(data) {
373     var msg = new Gtk.MessageDialog({
374             message_type: Gtk.MessageType.ERROR, 
375             buttons : Gtk.ButtonsType.OK, 
376             text: data
377     });
378     msg.run();
379     msg.destroy();
380 }
381
382  
383
384
385
386 //
387 // need a better icon...
388
389 StatusIcon.init(); 
390 Notify.init("gitlive");
391 monitor.add(GLib.get_home_dir() + "/gitlive");
392 monitor.start();
393 Gtk.main();
394 //icon.signal["activate"].connect(on_left_click);
395