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