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