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             repo.add(add_files);
154             repo.remove(remove_files);
155             
156             success.push(repo.commit({
157                 reason : messages.join("\n"),
158                 files : add_files  
159             }));
160             success.push(repo.push());
161              
162             
163         }
164         
165         // finally merge all the commit messages.
166          
167         try {
168             // catch notification failures.. so we can carry on..
169             if (success.length) {
170                 print(success.join("\n"));
171                 
172                 var notification = new Notify.Notification({
173                     summary: "Git Live Commited",
174                     body : success.join("\n"),
175                     timeout : 5
176                     
177                 });
178     
179                 notification.set_timeout(5);
180                 notification.show();   
181             }
182             
183             if (failure.length) {
184             
185                 var notification = new Notify.Notification({
186                     summary: "Git Live ERROR!!",
187                     body : failure.join("\n"),
188                     timeout : 5
189                     
190                 });
191     
192                 notification.set_timeout(5); // show errros for longer
193                 notification.show();   
194             }
195         } catch(e) {
196             print(e.toString());
197             
198         }
199         this.queueRunning = false;
200     },
201     
202     shouldIgnore: function(f)
203     {
204         if (f.name[0] == '.') {
205             // except!
206             if (f.name == '.htaccess') {
207                 return false;
208             }
209             
210             return true;
211         }
212         if (f.name.match(/~$/)) {
213             return true;
214         }
215         // ignore anything in top level!!!!
216         if (!f.vpath.length) {
217             return true;
218         }
219         
220         return false;
221         
222     },
223     
224     /**
225      * parsePath:
226      * Fill in gitlive, vpath and repo  
227      * 
228      */
229     parsePath: function(f)
230     {
231            
232         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
233         
234         f.gitpath = gitlive + '/' + vpath_ar.shift();
235         f.vpath =  vpath_ar.join('/');
236         //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
237         
238         
239     },
240     
241     just_created : {},
242       
243     onChanged : function(src) 
244     { 
245         return; // always ignore this..?
246         //this.parsePath(src);
247     },
248     
249     
250     
251     
252     /**
253      *  results in  git add  + git commit..
254      *
255      */
256     onChangesDoneHint : function(src) 
257     { 
258         this.parsePath(src);
259         if (this.shouldIgnore(src)) {
260             return;
261         }
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',    { message: src.vpath} ] 
271                 
272             );
273          
274             return;
275         }
276         this.lastAdd = new Date();
277         this.queue.push( 
278             [ src.gitpath,  'add', src.vpath ],
279             [ src.gitpath,  'commit',  {  message: src.vpath} ]
280
281             
282         );
283     },
284     onDeleted : function(src) 
285     { 
286         this.parsePath(src);
287         if (this.shouldIgnore(src)) {
288             return;
289         }
290         // should check if monitor needs removing..
291         // it should also check if it was a directory.. - so we dont have to commit all..
292         
293         this.lastAdd = new Date();
294         this.queue.push( 
295             [ src.gitpath, 'rm' , src.vpath ],
296             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
297             
298         );
299     
300         
301     },
302     onCreated : function(src) 
303     { 
304         this.parsePath(src);
305         if (this.shouldIgnore(src)) {
306             return;
307         }
308         
309         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
310             this.just_created[src.path] = true;
311             return; // we do not handle file create flags... - use done hint.
312         }
313         // director has bee created
314         this.monitor(src.path);
315         this.lastAdd = new Date();
316         this.queue.push( 
317             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
318             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
319             
320         );
321         
322         
323     },
324     onAttributeChanged : function(src) { 
325         this.parsePath(src);
326         if (this.shouldIgnore(src)) {
327             return;
328         }
329         this.lastAdd = new Date();
330         this.queue.push(
331                         
332             //[ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
333             [ src.gitpath, 'add' ,  src.vpath ],
334              [ src.gitpath, 'commit' ,    {  message: "Attribute Changed :" + src.vpath} ]
335         );
336  
337     
338     },
339     
340     onMoved : function(src,dest)
341     { 
342         this.parsePath(src);
343         this.parsePath(dest);
344         
345         if (src.gitpath != dest.gitpath) {
346             this.onDeleted(src);
347             this.onCreated(dest);
348             this.onChangedDoneHint(dest);
349             return;
350         }
351         // needs to handle move to/from unsupported types..
352         
353         if (this.shouldIgnore(src)) {
354             return;
355         }
356         if (this.shouldIgnore(dest)) {
357             return;
358         }
359         this.lastAdd = new Date();
360         this.queue.push( 
361            // [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
362              [ src.gitpath, 'add',    dest.vpath ],
363              [ src.gitpath, 'rm',    src.vpath ],
364              
365             [ src.gitpath, 'commit' , 
366                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath}
367             ]
368         );
369          
370     }
371           
372     
373 });
374  
375  
376   
377
378 function errorDialog(data) {
379     var msg = new Gtk.MessageDialog({
380             message_type: Gtk.MessageType.ERROR, 
381             buttons : Gtk.ButtonsType.OK, 
382             text: data
383     });
384     msg.run();
385     msg.destroy();
386 }
387
388  
389
390
391
392 //
393 // need a better icon...
394
395
396 StatusIcon.init();   
397
398
399 Notify.init("gitlive");
400
401 monitor.add(GLib.get_home_dir() + "/gitlive");
402 monitor.start();
403 Gtk.main();
404 //icon.signal["activate"].connect(on_left_click);
405