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                         
74                         if (this.name[this.name.length -1] == '~') {
75                                 return true;
76                         }
77                         // netbeans / android studio.. silly temp files..
78                         
79                         if (Regex.match_simple("___jb_old___$", this.name)) {
80                             return true;
81                         }
82                         if (Regex.match_simple("___jb_bak___$", this.name)) {
83                             return true;
84                         }
85                         //if (f.name.match(/^nbproject/)) {
86                         //    return true;
87                         //}
88                         // ignore anything in top level!!!!
89                         if (this.gitpath.length < 1) {
90                                 return true;
91                         }
92                         
93                         return false;
94                 }
95                 
96                 /** -- statics --*/
97                 
98                 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
99                 {
100                         for(var i =0; i < add_files.length; i++) {
101                                 if (add_files.index(i).vname == add) {
102                                         return i;
103                                 }
104                         }
105                         return -1;
106                 }
107                 public static  int indexOfMessage(Array<GitMonitorQueue> messages, string message)  {
108                         for(var i =0; i < messages.length; i++) {
109                                 if (messages.index(i).message == message) {
110                                         return i;
111                                 }
112                         }
113                         return -1;
114                 }
115                 public static string messageToString(Array<GitMonitorQueue> messages ) {
116                         string[] ret = {};
117                         for(var i =0; i < messages.length; i++) {
118                                 ret+= messages.index(i).message;
119                         }
120                         return string.joinv("\n",ret);
121                 }
122                 public static string queueArrayToString(Array<GitMonitorQueue> list) {
123                         var ret = "";
124                         for(var i =0; i < list.length; i++) {
125                                 
126                                 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
127                         }
128                         return ret;
129                         
130                 }
131                 
132                 public static bool  queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
133                         for(var i =0; i < list.length; i++) {
134                                 var test = list.index(i);
135                                 if (list.index(i).gitpath != cmd_s.gitpath) {
136                                         continue;
137                                 }
138                                 if (list.index(i).vname != cmd_s.vname) {
139                                         continue;
140                                 }
141                                 if (list.index(i).action != action) {
142                                         continue;
143                                 }
144                                 return true;
145                         }
146                         return false;
147                 }
148                 public string fullpath()
149                 {
150                         return this.gitpath + "/" + this.vname;
151                 }
152                 
153                         
154                         
155 }
156
157
158
159 public class GitMonitor : Monitor
160 {
161
162         public static GitMonitor gitmonitor;
163         /**
164          * @property {String} the "gitlive" directory, normally ~/gitlive
165          *  dset by OWNER... - we should do this as a CTOR.
166          *  
167          */
168         public static string gitlive;
169         
170         
171         public Array<GitMonitorQueue> queue ;
172         public bool queueRunning = false;
173         
174         public DateTime lastAdd;
175          
176         
177         public GitMonitor () {
178                 this.queue = new Array<GitMonitorQueue>();
179                 GitMonitor.gitmonitor = this;
180                 
181                 Timeout.add_full(Priority.LOW, 1000, () => {
182                         
183                         //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
184
185                         //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
186                         if (this.queue.length < 1  || this.queueRunning) {
187                                 return true;
188                         }
189                         
190                         var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
191  
192                         // stdout.printf("LAST RUN: %s (expect %s) \n" ,
193                         //         last.to_string(),   (5 * TimeSpan.SECOND).to_string() );
194  
195                         if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
196                                 return true;
197                         }
198                         //_this.lastAdd = new Date();
199                         //return 1;
200                 
201                         this.runQueue();
202                         return true; //
203                 });
204                 
205                 
206         }
207
208         public new void pauseError(string failure) 
209         {
210         
211                 var notification = new Notify.Notification(
212                           "Git Live ERROR!!",
213                         failure,
214                         "dialog-information"
215                         
216                 );
217                         
218                 notification.set_timeout(60); // show errros for longer
219                 notification.show();
220                 
221                 
222
223                         
224         
225                 this.paused = true;
226                 this.queueRunning = false;
227                 // what does this do to the old one...
228                 this.queue = new Array<GitMonitorQueue> ();
229                 this.stop();
230                 StatusIconA.statusicon.pauseError();
231                 
232                 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
233                         "A Serious problem occured running git, you will probably have to resolve this manually\n" + 
234                         "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" + 
235                         failure
236                 );
237                 m.set_keep_above(true);
238                 m.show();
239                 m.set_position(Gtk.WindowPosition.CENTER);
240                 m.response.connect( (id_pressed) => {
241                         m.hide();
242                 });
243                 
244         }
245  
246         public new void pause() {
247                 this.paused = true;
248                 // what does this do to the old one...
249                 this.queue = new Array<GitMonitorQueue> ();
250                 StatusIconA.statusicon.pause();
251
252         }
253         
254         /*
255         public new void resume () {
256                 this.paused = false;
257                 this.queue = new Array<GitMonitorQueue> ();
258                 StatusIconA.statusicon.resume();
259                 
260                 
261         }
262         */
263         /**
264          * Start the monitoring
265          * and run the queue every 500 milliseconds..
266          *
267          */
268         public new void start() 
269         {
270                 StatusIconA.statusicon.refreshing();
271                 
272                  
273                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
274                 
275                 Timeout.add_full(Priority.LOW, 500, () => {
276                         //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
277                         // call this.monitor on each of 'top'
278                         for(int i = 0; i < this.top.length ; i++) {
279
280                                 this.monitor(this.top.index(i) );
281                         }
282                         StatusIconA.statusicon.resume();
283                     this.paused = false;
284                    
285                         
286                         try {
287
288                           
289                                 var notification = new Notify.Notification(
290                                         "Git Live",
291                                         "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length), 
292                                          "dialog-information"
293                                 );
294                 
295                                 notification.set_timeout(5);
296                                 notification.show();
297                         } catch(Error e) {
298                                 print("Error %s\n",e.message);
299                         }
300                         return false; // do not keep doing this..
301
302                 });
303                 
304
305                 
306                  
307         }
308
309
310         public new void stop() {
311                 StatusIconA.statusicon.pause();
312                 base.stop();
313         }
314         
315         
316         public new void monitor (string path,  int depth = 0)
317         {
318                 
319                 //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
320                 
321                  
322                 // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
323                 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
324                         return;
325                 }
326                 
327                 if (depth == 1) {
328                         // FIXME - check if repo is flagged as not autocommit..
329                         //var repo = imports.Scm.Repo.Repo.get(path);
330                         //if (!repo || !repo.autocommit()) {
331                         //    return;
332                         //} 
333                 }
334                 
335                 
336                 // check if the repo is to be monitored.
337                 //print("PATH : " + path);
338                 
339                 
340                 base.monitor(path, depth);
341         }
342
343         
344
345         /**
346          * run the queue.
347          * - pulls the items off the queue 
348          *    (as commands run concurrently and new items may get added while it's running)
349          * - runs the queue items
350          * - pushes upstream.
351          * 
352          */
353         public void runQueue()
354         {
355                 
356                 if (this.paused) {
357                         return;
358                 }
359                 print("GitMonitor.runQueue\n");
360
361                 this.queueRunning = true;
362
363                 var cmds = new Array<GitMonitorQueue>();
364
365                 for(var i = 0; i < this.queue.length; i++) {
366                         cmds.append_val(this.queue.index(i));
367                 }
368
369                 this.queue = new Array<GitMonitorQueue>();// empty queue!
370
371                 
372                 string[] success = {};
373                 string[] failure = {};
374            //var repos = new Array<GitRepo>(); //??
375                 //var done = new Array<GitMonitorQueue>();
376                 
377                 // first build a array of repo's to work with
378                 var repo_list = new Array<GitRepo>();
379                 
380                 // pull and group.
381                 
382                 //print(JSON.stringify(cmds));
383                 // make sure nothing get's added to the queue where we are doing this..
384
385                 this.paused = true;
386
387                 print("GitMonitor.runQueue - creating repos\n");
388                 
389                 for(var i = 0; i < cmds.length; i++) {
390                    
391                         var cmd = cmds.index(i);
392                 
393                         var gitpath = cmd.gitpath; 
394                         stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
395                 
396                         var ix  = GitRepo.indexOf(repo_list,  cmd.gitpath);
397                         if (ix < 0) {
398                                 repo_list.append_val(new GitRepo( gitpath ));
399                                 ix = GitRepo.indexOf(repo_list,  cmd.gitpath);
400                         }
401                         stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
402
403                         //if (typeof(repo_list[gitpath]) == 'undefined') {
404                         //    repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
405                         //    repo_list[gitpath].cmds = [];
406                          //   repo_list[gitpath].pull();
407                         //}
408                         repo_list.index(ix).cmds.append_val(cmd);
409
410                 }
411                 this.paused = false;
412                 // build add, remove and commit message list..
413
414                 print("GitMonitor.runQueue - creating actions\n");
415                 
416                 for(var i = 0;i < repo_list.length;i++) {
417          
418                         var repo = repo_list.index(i);
419
420                         var add_files = new Array<GitMonitorQueue>();
421                         var add_files_f = new Array<GitMonitorQueue>();
422                         var remove_files = new Array<GitMonitorQueue>();
423                         var messages = new Array<GitMonitorQueue>();
424                         //print(JSON.stringify(repo.cmds,null,4));
425                         
426                         for(var ii = 0;ii < repo.cmds.length;ii++) {
427                                 var cmd = repo.cmds.index(ii);
428         
429                                 
430                                 switch(cmd.action) {
431                                         case "add" :
432                                                 
433                                                 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
434                                                    break;
435                                                 }
436                                                 
437                                                 add_files.append_val(cmd);
438                                                 break;
439                                         
440                                         case "rm":
441                                                 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
442                                                    break;
443                                                 }
444                                                 
445                                                 // if file exists, do not try and delete it.
446                                                 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
447                                                         break;
448                                                 }
449                                                 
450                                                 remove_files.append_val(cmd);
451                                                 break;
452                                         
453                                         case "commit" :
454                                                 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
455                                                    break;
456                                                 }
457                                                 messages.append_val(cmd);
458                                                 
459                                                 break;
460                                         default:
461                                                 stdout.printf("Opps unmatched action %s\n", cmd.action);
462                                                 break;
463                                 } 
464                         }
465                         print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
466                         print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
467                         
468                         //repo.debug = 1;
469                         // these can fail... at present... as we wildcard stuff.
470                    
471                         // make sure added files do not get removed.. ?? 
472                         /*
473                         var remove_files_f = new Array<GitMonitorQueue>();
474                         for(var ii = 0;ii < remove_files.length;ii++) {
475                                 if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
476                                          continue;
477                                 }
478                                 remove_files_f.append_val(remove_files.index(ii));
479                         };
480                         stdout.printf("REMOVE : %u files\n"  , remove_files_f.length);
481                         */
482                         
483                         // if file was added, then removed, 
484                         var remove_files_f = new Array<GitMonitorQueue>();
485                         for(var ii = 0;ii < remove_files.length;ii++) {
486                                 
487                                 
488                                 
489                                 if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
490                                         // in add and remove - do not remvove
491                                         continue;
492                                 }
493                                 remove_files_f.append_val(remove_files.index(ii));
494                         };
495                         for(var ii = 0;ii < add_files.length;ii++) {
496                                 if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.index(ii).vname) > -1 ) {
497                                         // the add file is in the remove list, and it does not exist - do not add it..
498                                         print("check exists ? %s\n",add_files.index(ii).fullpath());
499                                         
500                                         if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
501                                                         continue;
502                                         }
503                                          
504                                 }
505                                 
506                                 
507                                 
508                                 add_files_f.append_val(add_files.index(ii));
509                         };
510                         
511                         print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
512                         print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
513                    
514                         
515                         // make sure monitoring is paused so it does not recursively pick up
516                         // deletions
517                         this.paused = true; 
518                          
519                          
520                         try {
521                                 
522                                 repo.pull();
523                         } catch(Error e) {
524                                 this.pauseError(e.message);
525                                 //failure +=  e.message;
526                                 //print("Pull failed:\n");
527                                 return;
528                         }
529                                                 
530                         // -- DO STUFF..            
531                         try {
532                                 repo.add(add_files_f);
533                         } catch(Error e) {
534                                 this.pauseError(e.message);
535                                 return;
536                                 failure +=  e.message;
537                                 print("Add failed:\n");
538                         }  
539                         try {
540                                  repo.remove(remove_files_f);
541                         } catch(Error e) {
542                                 this.pauseError(e.message);
543                                 return;
544                                 failure +=  e.message;
545                                 print("Remove failed:\n");
546                         }  
547
548
549                         try { 
550                                 success += repo.commit(
551                                         GitMonitorQueue.messageToString(messages),
552                                         add_files_f
553                                 );
554                                 success += repo.push();
555
556                         } catch(Error e) {
557                         
558                                 // if the error is 'nothing to commit, working tree clean'
559                                 // then it's not an error, - just continue;
560                                 
561                                 if (/nothing to commit, working tree clean/.match(e.message)) {
562                                         success += e.message;
563                                         continue;
564                                 }
565                                 
566                         
567                         
568                                 this.paused = false;                    
569                                 this.pauseError(e.message);
570                                 
571                                 return;
572                                 //failure += e.message;
573                                 //print("Push failed:\n");
574                                 
575                         }   
576                         this.paused = false;                                            
577                 }
578                 
579                 
580                 // finally merge all the commit messages.
581                  
582                 try {
583                         // catch notification failures.. so we can carry on..
584                         if (success.length > 0) {
585
586                                 
587                                 var notification = new Notify.Notification(
588                                         "Git Live Commited",
589                                         string.joinv("\n",success),
590                                          "dialog-information"
591                                         
592                                 );
593         
594                                 notification.set_timeout(5);
595                                 notification.show();   
596                         }
597                         
598                         //if (failure.length > 0) {
599
600                                 // should never get this far...
601                         //      this.pauseError();   
602                         //}
603                 } catch(Error e) {
604                         print(e.message);
605                         
606                 }
607                 this.queueRunning = false;
608         }
609         
610
611
612         
613
614         //string[] just_created;
615  
616  
617
618
619
620    
621
622
623         public override  void onChanged(MonitorNamePathDir src) 
624         { 
625                 //print("GitMonitor.onChanged\n");        
626                 return; // always ignore this..?
627                 //this.parsePath(src);
628         }
629         
630
631  
632         /**
633          *  results in  git add  + git commit..
634          *
635          */
636         public override void onChangesDoneHint(MonitorNamePathDir src)  
637         { 
638
639                 if (this.paused) {
640                         return;
641                 }
642                 print("GitMonitor.onChangedHint\n");                            
643
644                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
645                 var cmd = new GitMonitorQueue(src);
646                 if (cmd.shouldIgnore()) {
647                         return;
648                 }
649                 
650            
651                 //var add_it = false;
652                 /*
653                 if (this.is_just_created(cmd.path)) {
654                         
655                 if (typeof(this.just_created[src.path]) !='undefined') {
656                         delete this.just_created[src.path];
657                         
658                         this.queue.push( 
659                                 [ src.gitpath,  'add', src.vpath ],
660                                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
661                                 
662                         );
663                  
664                         return;
665                 }
666                 */
667                 cmd.action = "add";
668                 this.queue.append_val(cmd);
669
670                 cmd = new GitMonitorQueue(src);
671                 cmd.action = "commit";
672                 cmd.message = cmd.vname;
673                 this.queue.append_val(cmd);
674  
675                  
676         }
677         public override  void onDeleted(MonitorNamePathDir src) 
678    { 
679
680                 if (this.paused) {
681                         return;
682                 }
683                 print("GitMonitor.onDeleted\n");                        
684                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
685                 var cmd = new GitMonitorQueue(src);
686                 if (cmd.shouldIgnore()) {
687                         return;
688                 }
689                 // should check if monitor needs removing..
690                 // it should also check if it was a directory.. - so we dont have to commit all..
691                 cmd.action = "rm";
692                 this.queue.append_val(cmd);
693
694                 cmd = new GitMonitorQueue(src);
695                 cmd.action = "commit";
696                 cmd.message = cmd.vname;
697                 cmd.commit_all = true;
698
699                 this.queue.append_val(cmd);
700  
701         }
702         public override  void onCreated(MonitorNamePathDir src) {
703
704                 if (this.paused) {
705                         return;
706                 }
707                 print("GitMonitor.onCreated\n");                        
708                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
709                 var cmd = new GitMonitorQueue(src);
710                 if (cmd.shouldIgnore()) {
711                         return;
712                 }
713
714                 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
715                    // this.just_created[src.path] = true;
716                         return; // we do not handle file create flags... - use done hint.
717                 }
718                 // directory has bee created
719                 this.monitor(src.path);
720                 //this.top.append_val(src.path);
721                 //this.monitor(src.path );
722
723
724 // -- no point in adding a dir.. as git does not handle them...
725 //        this.queue.push( 
726   //          [ src.gitpath, 'add' , src.vpath,  { all: true } ],
727  //           [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
728   //          
729    //     );
730
731         }
732
733         public  override void onAttributeChanged(MonitorNamePathDir src) { 
734
735                 if (this.paused) {
736                         return;
737                 }
738                 print("GitMonitor.onAttributeChanged\n");                       
739                 if (src.dir == GitMonitor.gitlive) {
740                    return; // attribute on top level..
741                 }
742                 
743                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
744                 var cmd = new GitMonitorQueue(src);
745                 if (cmd.shouldIgnore()) {
746                         return;
747                 }
748                 cmd.action = "add";
749                 this.queue.append_val(cmd);
750
751                 cmd = new GitMonitorQueue(src);
752                 cmd.action = "commit";
753                 cmd.message = "Attribute changed " + cmd.vname;
754                 this.queue.append_val(cmd);
755         }
756  
757    public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
758         { 
759
760                 if (this.paused) {
761                         return;
762                 }
763                 print("GitMonitor.onMoved\n");                  
764                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
765                 var cmd_s = new GitMonitorQueue(src);
766
767                 var cmd_d = new GitMonitorQueue(dest);
768    
769                 
770                 if (cmd_d.gitpath != cmd_s.gitpath) {
771                         this.onDeleted(src);
772                         this.onCreated(dest);
773                         this.onChangesDoneHint(dest);
774                         return;
775                 }
776                 // needs to handle move to/from unsupported types..
777                 
778                 if (cmd_s.shouldIgnore()) {
779                         this.onCreated(dest);
780                         this.onChangesDoneHint(dest);
781                         return;
782
783                 }
784                 if (cmd_d.shouldIgnore()) {
785                         
786                         this.onDeleted(src);
787  
788
789                         return;
790                 }
791                 
792                 
793                 print("RM: %s\n", cmd_s.vname);
794                 cmd_s.action = "rm";
795                 this.queue.append_val(cmd_s);
796
797                 
798                 
799                 print("ADD: %s\n", cmd_d.vname);
800                 cmd_d.action = "add";
801                 this.queue.append_val(cmd_d);
802
803
804                 var cmd = new GitMonitorQueue(dest);
805                 cmd.action = "commit";
806                 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
807                 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
808                         cmd.message = cmd_d.vname;
809                 }
810                 
811                 this.queue.append_val(cmd);
812
813
814                  
815         }
816            
817 }