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