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