GitMonitor.vala
[gitlive] / GitMonitor.vala
1
2
3 public GitMontitorQueue : MonitorNamePathDir {
4         // name = basename
5         // path = full path..
6         // dir = dir path
7     
8         public string gitpath;
9         public string vpath;
10         public string message ; // for commit
11         public bool commit_all
12         public GitMontitorQueue(MonitorNamePathDir f) {
13             this.name = f.name;
14             this.path = f.path;
15             this.dir = f.dir;
16             this.message = "";
17             this.commit_all = false;
18  
19            
20             var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split('/', 0);
21             
22             this.gitpath = GitMonitor.gitlive + '/' + vpath_ar[0];
23             
24             string[]  vpath = {};
25             for (var i = 1; i< vpath_ar.length; i++) {
26                 vpath += vpath_ar[i];
27             }
28             this.vpath =  string.joinv("/", vpath);
29             //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
30         
31         
32         }
33
34         public bool shouldIgnore(GitMonitor gm)
35         {
36             
37             
38             
39             // vim.. what a seriously brain dead program..
40             if (this.name == '4913') {
41                 return true;
42             }
43             
44             if (this.name[0] == '.') {
45                 // except!
46                 if (this.name == '.htaccess') {
47                     return false;
48                 }
49                 
50                 return true;
51             }
52             //if (f.name.match(/~$/)) {
53             //    return true;
54             //}
55             //if (f.name.match(/^nbproject/)) {
56             //    return true;
57             //}
58             // ignore anything in top level!!!!
59             if (!this.vpath.length) {
60                 return true;
61             }
62             
63             return false;
64         }
65         
66         /** -- statics --*/
67         
68         public static int indexOfAdd( Array<GtkMonitorQueuequeue> add_files, string add) {
69             for(var i =0; i < add_files.length; i++) {
70                 if (addfiles.index(i).add == add) {
71                     return i;
72                 }
73             }
74             return -1;
75         }
76         public static  int indexOfMessage(Array<GtkMonitorQueuequeue> messages, message)  {
77             for(var i =0; i < messages.length; i++) {
78                 if (messages.index(i).message == message) {
79                     return i;
80                 }
81             }
82             return -1;
83         }
84         public static string messageToString(Array<GtkMonitorQueuequeue> messages ) {
85             string[] ret = {};
86             for(var i =0; i < messages.length; i++) {
87                 ret+= messages.index(i).message;
88             }
89             return string.joinv(ret,"\n");
90         }
91
92 }
93
94
95
96 public class GitMonitor : Monitor
97 {
98
99  
100     /**
101      * @property {String} the "gitlive" directory, normally ~/gitlive
102      *  dset by OWNER... - we should do this as a CTOR.
103      *  
104      */
105     public static string gitlive = '';
106     
107     
108     public Array<GtkMonitorQueuequeue>;
109     public bool queueRunning = false;
110     
111     public DateTime lastAdd;
112      
113      
114     public void pause() {
115         this.paused = true;
116         // what does this do to the old one...
117         this.queue = new Array<FileMonitor> ();
118         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
119
120     }
121     
122     public void resume () {
123         this.paused = false;
124         this.queue = new Array<FileMonitor> ();
125         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
126         
127         
128     }
129     /**
130      * Start the monitoring
131      * and run the queue every 500 milliseconds..
132      *
133      */
134     public void start() {
135         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
136         
137          
138         this.lastAdd = new DateTime.now(); 
139         
140         Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
141
142             // call this.monitor on each of 'top'
143             for(int i = 0; i < this.top.length ; i++) {
144                 this.monitor(this.top.index(i) );
145             }
146             StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
147              
148            
149             
150             try { 
151                 var notification = new Notify.Notification({
152                     "Git Live",
153                     GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
154                      "dialog-information"
155                 });
156         
157                 notification.set_timeout(5);
158                 notification.show();
159             } catch(Error e) {
160                 print(e.toString());
161             }
162
163         });
164         
165         Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
166             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
167             if (!_this.queue.length || _this.queueRunning) {
168                 return true;
169             }
170
171             var last = this.lastAdd.difference(new DateTime.now());
172
173             
174             //print("LAST RUN?" + last);
175             
176             if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
177                 return 1;
178             }
179             //_this.lastAdd = new Date();
180             //return 1;
181         
182             this.runQueue();
183             return true;
184         },null,null);
185         
186       
187     }
188
189
190     public void stop() {
191         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
192         base.stop();
193     }
194     
195     
196     public void monitor (string path,  int depth = 0)
197     {
198         
199         //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
200         
201          
202         // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
203         if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
204             return;
205         }
206         
207         if (depth == 1) {
208             // FIXME - check if repo is flagged as not autocommit..
209             //var repo = imports.Scm.Repo.Repo.get(path);
210             //if (!repo || !repo.autocommit()) {
211             //    return;
212             //} 
213         }
214         
215         
216         // check if the repo is to be monitored.
217         //print("PATH : " + path);
218         
219         
220         base.monitor(path, depth);
221     }
222
223     
224
225     /**
226      * run the queue.
227      * - pulls the items off the queue 
228      *    (as commands run concurrently and new items may get added while it's running)
229      * - runs the queue items
230      * - pushes upstream.
231      * 
232      */
233     public void runQueue()
234     {
235         
236         if (this.paused) {
237             return;
238         }
239         this.queueRunning = true;
240
241         var cmds = new Array<GitMontitorQueue>();
242         for(var i = 0; i < this.queue.length; i++) {
243             cmds.append_val(this.queue.item(i));
244         }
245
246         this.queue = new Array<GitMontitorQueue>();// empty queue!
247
248         
249         var success = new Array<String>();
250         var failure = new Array<GitMontitorQueue>();
251         var repos = new Array<GitRepo>(); //??
252         var done = new Array<GitMontitorQueue>();
253         
254         // first build a array of repo's to work with
255         var repo_list = new Array<GitRepo>();
256         
257         // pull and group.
258         
259         //print(JSON.stringify(cmds));
260         this.paused = true;
261         
262         for(var i = 0; i < cmds.length; i++) {
263            
264         
265             var gitpath = cmd.gitpath; 
266             var ix  = GitRepo.indexOf(this.repos,  cmd.gitpath);
267             if (ix < 0) {
268                 repo_list.append_val(new GitRepo( gitpath ));
269                 ix = GitRepo.indexOf(this.repos,  cmd.gitpath);
270             }
271             
272
273             //if (typeof(repo_list[gitpath]) == 'undefined') {
274             //    repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
275             //    repo_list[gitpath].cmds = [];
276              //   repo_list[gitpath].pull();
277             //}
278             repo_list.item(ix).cmds.append_val(cmd);
279
280         }
281         this.paused = false;
282         // build add, remove and commit message list..
283         
284         for(var i = 0;i < repo_list.length;i++) {
285     
286
287             var repo = repo_list.item(i);
288
289             var add_files = new Array<GitMontitorQueue>();
290             var remove_files = new Array<GitMontitorQueue>();
291             var messages = new Array<GitMontitorQueue>();
292             //print(JSON.stringify(repo.cmds,null,4));
293             
294             for(var ii = 0;ii < repo.cmds.length;ii++) {
295                 var cmd = repo.cmds.item(ii);
296     
297                 
298                 switch(cmd.action) {
299                     case "add" :
300                         
301                         if (GitMontitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
302                            break;
303                         }
304         
305                         
306                         add_files.append_val(cmd);
307                         break;
308                     
309                     case 'rm':
310                         if (GitMontitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
311                            break;
312                         }
313                         
314                         // if file exists, do not try and delete it.
315                         if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
316                             break;
317                         }
318                         
319                         remove_files.append_val(cmd);
320                         break;
321                     
322                     case 'commit' :
323                         if (GitMontitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
324                            break;
325                         }
326                          
327                         messages.append_val(cmd);
328                         
329                         break;
330                     default:
331                         print("Opps unmatched action");
332                         break;
333                 } 
334             }
335             
336             //repo.debug = 1;
337             // these can fail... at present... as we wildcard stuff.
338             stdout.printf("ADD : %d files"  , add_files.length);
339             
340             // make sure added files do not get removed..
341
342             var remove_files_f = new Array<GitMontitorQueue>();
343             for(var ii = 0;ii < remove_files.length;ii++) {
344                 if (GitMontitorQueue.indexOfAdd(add_files,  remove_files.item(ii).rm) > -1 ) {
345                      continue;
346                 }
347                 remove_files_f.append_val(remove_files.item(ii));
348             };
349             stdout.printf("REMOVE : %d files"  , remove_files.length);
350              
351             // make sure monitoring is paused so it does not recursively pick up
352             // deletions
353             
354             // -- DO STUFF..
355             
356             repo.add(add_files);
357             repo.remove(remove_files);
358             this.paused = false;
359             
360             
361             try { 
362                 success.append_val(repo.commit(
363                     GitMontitorQueue.messageToString(messages)
364                     add_files  
365                 ));
366                 success.push(repo.push());
367
368             } catch(Error e) {
369                 failure.append_val(e.message);
370                 
371             }   
372         }
373         
374         // finally merge all the commit messages.
375          
376         try {
377             // catch notification failures.. so we can carry on..
378             if (success.length) {
379                 var success_str = "";
380                 for(var ii = 0;ii < success.length;ii++) {
381                     success_str+= success.item(ii) + "\n";
382                 }
383                 
384                 var notification = new Notify.Notification(
385                     "Git Live Commited",
386                     success_str,
387                      "dialog-information"
388                     
389                 );
390     
391                 notification.set_timeout(5);
392                 notification.show();   
393             }
394             
395             if (failure.length) {
396                 var failure_str = "";
397                 for(var ii = 0;ii < failure.length;ii++) {
398                     failure_str+= failure.item(ii) + "\n";
399                 }
400                 var notification = new Notify.Notification({
401                     summary: "Git Live ERROR!!",
402                     failure_str,
403                     "dialog-information"
404                     
405                 });
406     
407                 notification.set_timeout(5); // show errros for longer
408                 notification.show();   
409             }
410         } catch(Error e) {
411             print(e.message);
412             
413         }
414         this.queueRunning = false;
415     }
416     
417
418
419     
420
421     //string[] just_created;
422  
423  
424
425
426
427    
428
429
430     public void onChanged(MonitorNamePathDir src) { }
431     { 
432         return; // always ignore this..?
433         //this.parsePath(src);
434     },
435     
436
437  
438     /**
439      *  results in  git add  + git commit..
440      *
441      */
442     public void onChangesDoneHint(MonitorNamePathDir src) { }
443     { 
444         
445         if (this.paused) {
446             return true;
447         }
448             
449
450         this.lastAdd = new DateTime.now(); 
451         var cmd = new GitMontitorQueue(src);
452         if (cmd.shouldIgnore()) {
453             return;
454         }
455         
456        
457         var add_it = false;
458         /*
459         if (this.is_just_created(cmd.path)) {
460             
461         if (typeof(this.just_created[src.path]) !='undefined') {
462             delete this.just_created[src.path];
463             
464             this.queue.push( 
465                 [ src.gitpath,  'add', src.vpath ],
466                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
467                 
468             );
469          
470             return;
471         }
472         */
473         cmd.add = src.vpath;
474         this.queue.append_val(cmd);
475
476         var cmd = new GitMontitorQueue(src);
477         cmd.action = "commit";
478         cmd.message = src.vpath;
479         this.queue.append_val(cmd);
480  
481          
482     }
483     public void onDeleted(MonitorNamePathDir src) 
484    { 
485         if (this.paused) {
486             return true;
487         }
488         this.lastAdd = new DateTime.now(); 
489         var cmd = new GitMontitorQueue(src);
490         if (cmd.shouldIgnore()) {
491             return;
492         }
493         // should check if monitor needs removing..
494         // it should also check if it was a directory.. - so we dont have to commit all..
495         cmd.action = "rm";
496         cmd.rm = src.vpath;
497         this.queue.append_val(cmd);
498
499         var cmd = new GitMontitorQueue(src);
500         cmd.action = "commit";
501         cmd.message = src.vpath;
502         cmd.commit_all = true;
503
504         this.queue.append_val(cmd);
505  
506     }
507     public void onCreated(MonitorNamePathDir src) {
508
509         if (this.paused) {
510             return true;
511         }
512         this.lastAdd = new DateTime.now(); 
513         var cmd = new GitMontitorQueue(src);
514         if (cmd.shouldIgnore()) {
515             return;
516         }
517
518         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
519            // this.just_created[src.path] = true;
520             return; // we do not handle file create flags... - use done hint.
521         }
522         // directory has bee created
523         this.monitor(src.path);
524         this.top.append_val(src.path);
525         this.monitor(src.path );
526
527
528 // -- no point in adding a dir.. as git does not handle them...
529 //        this.queue.push( 
530   //          [ src.gitpath, 'add' , src.vpath,  { all: true } ],
531  //           [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
532   //          
533    //     );
534
535     }
536
537     public void onAttributeChanged(MonitorNamePathDir src) { }
538
539         if (this.paused) {
540             return true;
541         }
542         this.lastAdd = new DateTime.now(); 
543         var cmd = new GitMontitorQueue(src);
544         if (cmd.shouldIgnore()) {
545             return;
546         }
547         cmd.action = "add";
548         cmd.add = src.vpath;
549         this.queue.append_val(cmd);
550
551         var cmd = new GitMontitorQueue(src);
552         cmd.action = "commit";
553         cmd.message = "Attribute changed " + cmd.vpath;
554         this.queue.append_val(cmd);
555     }
556
557
558    public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
559     { 
560         this.lastAdd = new DateTime.now(); 
561         var cmd_s = new GitMontitorQueue(src);
562
563         var cmd_d = new GitMontitorQueue(src);
564    
565         
566         if (cmd_d.gitpath != cmd_s.gitpath) {
567             this.onDeleted(src);
568             this.onCreated(dest);
569             this.onChangedDoneHint(dest);
570             return;
571         }
572         // needs to handle move to/from unsupported types..
573         
574         if (this.shouldIgnore(src)) {
575             this.onCreated(dest);
576             this.onChangedDoneHint(dest);
577             return;
578
579         }
580         if (this.shouldIgnore(dest)) {
581             
582             this.onDeleted(src);
583  
584
585             return;
586         }
587         
588         cmd_s.action = "rm";
589         cmd_s.rm = src.vpath;
590         this.queue.append_val(cmd_s);
591
592
593
594
595         cmd_d.action = "add";
596         cmd_d.add = src.vpath;
597         this.queue.append_val(cmd_d);
598
599
600         var cmd = new GitMontitorQueue(dest);
601         cmd.action = "commit";
602         cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
603         this.queue.append_val(cmd);
604
605
606          
607     }
608        
609 }