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 GIRepository      = imports.gi.GIRepository
14 GLib        = imports.gi.GLib;
15
16 //print(JSON.stringify(GI, null,4));
17 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
18 //GI.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
19 GIRepository.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.2');
20
21 var Gio      = imports.gi.Gio;
22 var Gtk      = imports.gi.Gtk;
23 var Notify = imports.gi.Notify;
24
25 var Spawn = imports.Spawn;
26 var Git = imports.Git;
27 var StatusIcon = imports.StatusIcon.StatusIcon;
28 var Monitor = imports.Monitor.Monitor;
29
30
31 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
32 Gtk.init (null, null);
33
34 var gitlive = GLib.get_home_dir() + "/gitlive";
35
36 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
37     var msg = new Gtk.MessageDialog({message_type:
38         Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
39     msg.run();
40     msg.destroy();
41     
42     Seed.quit();
43 }
44
45  
46 var monitor = new Monitor({
47     
48     queue : [],
49     queueRunning : false,
50      
51     start: function() {
52         var _this = this;
53         this.lastAdd = new Date();
54         
55         this.top.forEach(this.monitor, this);
56          
57         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
58             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
59             if (!_this.queue.length || _this.queueRunning) {
60                 return 1;
61             }
62             var last = Math.floor(((new Date()) - this.lastAdd) / 100);
63             if (last < 4) { // wait 1/2 a seconnd before running.
64                 return 1;
65             }
66             _this.runQueue();
67             return 1;
68         },null,null);
69         
70         try { 
71             var notification = new Notify.Notification({
72                 summary: "Git Live",
73                 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories",
74                 timeout : 5
75             });
76     
77             notification.set_timeout(5);
78             notification.show();
79         } catch(e) {
80             print(e.toString());
81         }
82
83     },
84     
85     /**
86      * run the queue.
87      * - pulls the items off the queue 
88      *    (as commands run concurrently and new items may get added while it's running)
89      * - runs the queue items
90      * - pushes upstream.
91      * 
92      */
93     runQueue: function()
94     {
95         this.queueRunning = true;
96         var cmds = [];
97         this.queue.forEach(function (q) {
98             cmds.push(q);
99         });
100         this.queue = []; // empty queue!
101         
102         var success = [];
103         var failure = [];
104         var repos = [];
105         var done = [];
106         
107         // first build a array of repo's to work with
108         var repo_list = {};
109         
110         // pull and group.
111         
112         //print(JSON.stringify(cmds));
113         
114         cmds.forEach(function(cmd) {
115             var gitpath = cmd.shift(); 
116             if (typeof(repo_list[gitpath]) == 'undefined') {
117                 repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
118                 repo_list[gitpath].cmds = [];
119                 repo_list[gitpath].pull();
120             }
121             repo_list[gitpath].cmds.push(cmd);
122         });
123         
124         // build add, remove and commit message list..
125         
126          
127         for (var gitpath in repo_list) {
128             var repo = repo_list[gitpath];
129             var add_files = [];
130             var remove_files = [];
131             var messages = [];
132             repo.cmds.forEach(function(cmd) {
133                 //print(JSON.stringify(cmd));
134                 var name = cmd.shift();
135                 var arg = cmd.shift();
136                 
137                 switch(name) {
138                     case 'add' :
139                         if (add_files.indexOf(arg) > -1) break;
140                         add_files.push(arg);
141                         break;
142                     
143                     case 'rm':
144                         remove_files.push(arg);
145                         break;
146                     
147                     case 'commit' :
148                         messages.push(arg.message);
149                         break;    
150                 } 
151             });
152             //repo.debug = 1;
153             // these can fail... at present... as we wildcard stuff.
154             repo.add(add_files); 
155             repo.remove(remove_files);
156             
157             success.push(repo.commit({
158                 reason : messages.join("\n"),
159                 files : add_files  
160             }));
161             success.push(repo.push());
162              
163             
164         }
165         
166         // finally merge all the commit messages.
167          
168         try {
169             // catch notification failures.. so we can carry on..
170             if (success.length) {
171                 print(success.join("\n"));
172                 
173                 var notification = new Notify.Notification({
174                     summary: "Git Live Commited",
175                     body : success.join("\n"),
176                     timeout : 5
177                     
178                 });
179     
180                 notification.set_timeout(5);
181                 notification.show();   
182             }
183             
184             if (failure.length) {
185             
186                 var notification = new Notify.Notification({
187                     summary: "Git Live ERROR!!",
188                     body : failure.join("\n"),
189                     timeout : 5
190                     
191                 });
192     
193                 notification.set_timeout(5); // show errros for longer
194                 notification.show();   
195             }
196         } catch(e) {
197             print(e.toString());
198             
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      * parsePath:
227      * Fill in gitlive, vpath and repo  
228      * 
229      */
230     parsePath: function(f)
231     {
232            
233         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
234         
235         f.gitpath = gitlive + '/' + vpath_ar.shift();
236         f.vpath =  vpath_ar.join('/');
237         //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
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     
252     
253     /**
254      *  results in  git add  + git commit..
255      *
256      */
257     onChangesDoneHint : function(src) 
258     { 
259         this.parsePath(src);
260         if (this.shouldIgnore(src)) {
261             return;
262         }
263         
264        
265         var add_it = false;
266         if (typeof(this.just_created[src.path]) !='undefined') {
267             delete this.just_created[src.path];
268             this.lastAdd = new Date();
269             this.queue.push( 
270                 [ src.gitpath,  'add', src.vpath ],
271                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
272                 
273             );
274          
275             return;
276         }
277         this.lastAdd = new Date();
278         this.queue.push( 
279             [ src.gitpath,  'add', src.vpath ],
280             [ src.gitpath,  'commit',  {  message: src.vpath} ]
281
282             
283         );
284     },
285     onDeleted : function(src) 
286     { 
287         this.parsePath(src);
288         if (this.shouldIgnore(src)) {
289             return;
290         }
291         // should check if monitor needs removing..
292         // it should also check if it was a directory.. - so we dont have to commit all..
293         
294         this.lastAdd = new Date();
295         this.queue.push( 
296             [ src.gitpath, 'rm' , src.vpath ],
297             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
298             
299         );
300     
301         
302     },
303     onCreated : function(src) 
304     { 
305         this.parsePath(src);
306         if (this.shouldIgnore(src)) {
307             return;
308         }
309         
310         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
311             this.just_created[src.path] = true;
312             return; // we do not handle file create flags... - use done hint.
313         }
314         // director has bee created
315         this.monitor(src.path);
316         this.lastAdd = new Date();
317         this.queue.push( 
318             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
319             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
320             
321         );
322         
323         
324     },
325     onAttributeChanged : function(src) { 
326         this.parsePath(src);
327         if (this.shouldIgnore(src)) {
328             return;
329         }
330         this.lastAdd = new Date();
331         this.queue.push(
332                         
333             //[ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
334             [ src.gitpath, 'add' ,  src.vpath ],
335              [ src.gitpath, 'commit' ,    {  message: "Attribute Changed :" + src.vpath} ]
336         );
337  
338     
339     },
340     
341     onMoved : function(src,dest)
342     { 
343         this.parsePath(src);
344         this.parsePath(dest);
345         
346         if (src.gitpath != dest.gitpath) {
347             this.onDeleted(src);
348             this.onCreated(dest);
349             this.onChangedDoneHint(dest);
350             return;
351         }
352         // needs to handle move to/from unsupported types..
353         
354         if (this.shouldIgnore(src)) {
355             return;
356         }
357         if (this.shouldIgnore(dest)) {
358             return;
359         }
360         this.lastAdd = new Date();
361         this.queue.push( 
362            // [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
363              [ src.gitpath, 'add',    dest.vpath ],
364              [ src.gitpath, 'rm',    src.vpath ],
365              
366             [ src.gitpath, 'commit' , 
367                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath}
368             ]
369         );
370          
371     }
372           
373     
374 });
375  
376  
377   
378
379 function errorDialog(data) {
380     var msg = new Gtk.MessageDialog({
381             message_type: Gtk.MessageType.ERROR, 
382             buttons : Gtk.ButtonsType.OK, 
383             text: data
384     });
385     msg.run();
386     msg.destroy();
387 }
388
389  
390
391
392
393 //
394 // need a better icon...
395
396
397 StatusIcon.init();   
398
399
400 Notify.init("gitlive");
401
402 monitor.add(GLib.get_home_dir() + "/gitlive");
403 monitor.start();
404 Gtk.main();
405 //icon.signal["activate"].connect(on_left_click);
406