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