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