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