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