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
13 GI      = imports.gi.GIRepository
14 GLib        = imports.gi.GLib;
15
16 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
17 GI.IRepository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
18
19 Gio         = imports.gi.Gio;
20 Gtk         = imports.gi.Gtk;
21 Notify      = imports.gi.Notify;
22
23 Spawn       = imports.Spawn;
24 Git         = imports.Git;
25 StatusIcon  = imports.StatusIcon.StatusIcon;
26 Monitor     = imports.Monitor.Monitor;
27
28
29 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
30 Gtk.init (null, null);
31
32 var gitlive = GLib.get_home_dir() + "/gitlive";
33
34 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
35     var msg = new Gtk.MessageDialog({message_type:
36         Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
37     msg.run();
38     msg.destroy();
39     
40     Seed.quit();
41 }
42
43  
44 var monitor = new Monitor({
45     /**
46      *
47      * queue objects
48      *  action: 'add' | rm | update
49      *  repo : 'gitlive'
50      *  file : XXXXX
51      *
52      *
53      *
54      *  
55      * 
56      *
57      */
58     action_queue : [],
59     queue : [],
60     queueRunning : false,
61      
62     start: function()
63     {
64         var _this = this;
65         this.lastAdd = new Date();
66          
67         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
68             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
69             if (!_this.queue.length || _this.queueRunning) {
70                 return 1;
71             }
72             var last = Math.floor(((new Date()) - this.lastAdd) / 100);
73             if (last < 4) { // wait 1/2 a seconnd before running.
74                 return 1;
75             }
76             _this.runQueue();
77             return 1;
78         },null,null);
79         
80         Monitor.prototype.start.call(this);
81         var notification = new Notify.Notification({
82             summary: "Git Live",
83             body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
84         });
85
86         notification.set_timeout(2000);
87         notification.show();   
88     },
89     /**
90      * run the queue.
91      * - pulls the items off the queue 
92      *    (as commands run concurrently and new items may get added while it's running)
93      * - runs the queue items
94      * - pushes upstream.
95      * 
96      */
97     runQueue: function()
98     {
99         this.queueRunning = true;
100         var cmds = [];
101         //this.queue.forEach(function (q) {
102         //    cmds.push(q);
103         //});
104         
105         this.action_queue.forEach(function (q) {
106             cmds.push(q);
107         });
108         //this.queue = []; // empty queue!
109         this.action_queue = [];
110         var success = [];
111         var failure = [];
112         var repos = [];
113         var done = [];
114         
115         function readResult(sp) {
116             switch (sp.result * 1) {
117                 case 0: // success:
118                     success.push(sp.args.join(' '));
119                     if (sp.output.length) success.push(sp.output + '');
120                   // if (sp.stderr.length) success.push(sp.stderr + '');
121                     break;
122                 default: 
123                     failure.push(sp.args.join(' '));
124                     if (sp.output.length) failure.push(sp.output);
125                     if (sp.stderr.length) failure.push(sp.stderr);
126                     break;
127             }
128         }
129             
130         cmds.forEach(function(cmd) {
131             // prevent duplicate calls..
132             if (done.indexOf(JSON.stringify(cmd)) > -1) {
133                 return;
134             }
135             done.push(JSON.stringify(cmd));
136             // --- we keep a list of repositories that will be pushed to at the end..
137             
138             if (repos.indexOf(cmd.repos) < 0) {
139                 repos.push(cmd.repos);
140                 //    Git.run(cmd.repos , 'pull'); // pull before we push!
141             }
142             
143             
144             
145             switch( cmd.action ) {
146                 case 'add':
147                     readResult(Git.run(gitlive + '/' + cmd.repo, 'add',  cmd.file ));
148                     readResult(Git.run(gitlive + '/' + cmd.repo, 'commit',  src.file, { message: cmd.file}  ));
149                     break;
150                     
151                 case 'rm':
152                     readResult(Git.run(gitlive + '/' + cmd.repo, 'rm',  cmd.file ));
153                     readResult(Git.run(gitlive + '/' + cmd.repo, 'commit',  { all: true, message: cmd.file}  ));
154                     break;
155                      
156                 case 'update':
157                     readResult(Git.run(gitlive + '/' + cmd.repo, 'commit', cmd.file  , {   message: cmd.file}  ));
158                     break;
159                     
160                 case 'mv':
161                 
162                 
163             }
164             
165              
166             
167         });
168          
169         // push upstream.
170         repos.forEach(function(r) {
171             var sp = Git.run(r , 'push', { all: true } );
172             if (sp.length) {
173                 success.push(sp);
174             }
175             
176         });
177         
178         if (success.length) {
179             print(success.join("\n"));
180             var notification = new Notify.Notification({
181                 summary: "Git Live Commited",
182                 body : success.join("\n")
183                 
184             });
185
186             notification.set_timeout(2000);
187             notification.show();   
188         }
189         if (failure.length) {
190         
191             var notification = new Notify.Notification({
192                 summary: "Git Live ERROR!!",
193                 body : failure.join("\n")
194                 
195             });
196
197             notification.set_timeout(5000); // show errros for longer
198             notification.show();   
199         }
200         this.queueRunning = false;
201     },
202     
203     shouldIgnore: function(f)
204     {
205         if (f.name[0] == '.') {
206             // except!
207             if (f.name == '.htaccess') {
208                 return false;
209             }
210             
211             return true;
212         }
213         if (f.name.match(/~$/)) {
214             return true;
215         }
216         // ignore anything in top level!!!!
217         if (!f.vpath.length) {
218             return true;
219         }
220         
221         return false;
222         
223     },
224     
225     /**
226      * set gitpath and vpath
227      * 
228      * 
229      */
230     
231     parsePath: function(f)
232     {
233            
234         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
235         f.repo = vpath_ar.shift();
236         f.gitpath = gitlive + '/' + f.repo;
237         f.vpath =  vpath_ar.join('/');
238         
239         
240     },
241     
242     just_created : {},
243       
244     onChanged : function(src) 
245     { 
246         return; // always ignore this..?
247         //this.parsePath(src);
248     },
249     
250     /**
251      *  results in  git add  + git commit..
252      *  
253      *
254      *
255      */
256     
257     onChangesDoneHint : function(src) 
258     { 
259         this.parsePath(src);
260         if (this.shouldIgnore(src)) {
261             return;
262         }
263         
264         var add_it = false;
265         if (typeof(this.just_created[src.path]) !='undefined') {
266             delete this.just_created[src.path];
267             this.lastAdd = new Date();
268             //this.queue.push( 
269             //    [ src.gitpath,  'add', src.vpath ],
270             //    [ src.gitpath,  'commit',  src.vpath, { message: src.vpath} ] 
271             //    
272             //);
273             this.action_queue.push({
274                 action: 'add',
275                 repo : src.repo,
276                 file : src.vpath
277             });
278             
279             
280          
281             return;
282         }
283         this.lastAdd = new Date();
284         //this.queue.push( 
285         //    [ src.gitpath,  'add', src.vpath ],
286         //    [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ]
287         //
288         //);
289         
290         this.action_queue.push({
291             action: 'add',
292             repo : src.repo,
293             file : src.vpath
294         });
295         
296
297     },
298     onDeleted : function(src) 
299     { 
300         this.parsePath(src);
301         if (this.shouldIgnore(src)) {
302             return;
303         }
304         // should check if monitor needs removing..
305         // it should also check if it was a directory.. - so we dont have to commit all..
306         
307         this.lastAdd = new Date();
308         //this.queue.push( 
309         //    [ src.gitpath, 'rm' , src.vpath ],
310         //    [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
311         //    
312         //);
313         this.action_queue.push({
314             action: 'rm',
315             repo : src.repo,
316             file : src.vpath
317         });
318         
319     },
320     onCreated : function(src) 
321     { 
322         this.parsePath(src);
323         if (this.shouldIgnore(src)) {
324             return;
325         }
326         
327         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
328             this.just_created[src.path] = true;
329             return; // we do not handle file create flags... - use done hint.
330         }
331         // director has bee created
332         this.monitor(src.path);
333         
334         /*
335           since git does not really handle directory adds...
336          
337         this.lastAdd = new Date();
338         this.action_queue.push({
339             action: 'add',
340             repo : src.repo,
341             file : src.vpath
342         });
343         
344         this.queue.push( 
345             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
346             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
347             
348         );
349         */
350         
351         
352     },
353     onAttributeChanged : function(src) { 
354         this.parsePath(src);
355         if (this.shouldIgnore(src)) {
356             return;
357         }
358         this.lastAdd = new Date();
359         
360         
361         //this.queue.push( 
362        //     [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
363        // );
364         this.action_queue.push({
365             action: 'update',
366             repo : src.repo,
367             file : src.vpath
368         });
369  
370     
371     },
372     
373     onMoved : function(src,dest)
374     { 
375         this.parsePath(src);
376         this.parsePath(dest);
377         
378         if (src.gitpath != dest.gitpath) {
379             this.onDeleted(src);
380             this.onCreated(dest);
381             this.onChangedDoneHint(dest);
382             return;
383         }
384         // needs to handle move to/from unsupported types..
385         
386         if (this.shouldIgnore(src)) {
387             return;
388         }
389         if (this.shouldIgnore(dest)) {
390             return;
391         }
392         this.lastAdd = new Date();
393        // this.queue.push( 
394        //     [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
395        //     [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
396        //         { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ]
397        // );
398         
399         this.action_queue.push({
400             action: 'mv',
401             repo : src.repo,
402             file : src.vpath,
403             target : dest.vpath
404             
405         });
406         
407     }
408           
409     
410 });
411  
412  
413   
414
415 function errorDialog(data) {
416     var msg = new Gtk.MessageDialog({
417             message_type: Gtk.MessageType.ERROR, 
418             buttons : Gtk.ButtonsType.OK, 
419             text: data
420     });
421     msg.run();
422     msg.destroy();
423 }
424
425  
426
427
428
429 //
430 // need a better icon...
431
432
433 StatusIcon.init();   
434
435
436 Notify.init("gitlive");
437
438 monitor.add(GLib.get_home_dir() + "/gitlive");
439 monitor.start();
440 Gtk.main();
441 //icon.signal["activate"].connect(on_left_click);
442