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                         
550                         
551                         try { 
552                                 success += repo.commit(
553                                         GitMonitorQueue.messageToString(messages),
554                                         add_files_f
555                                 );
556                                 success += repo.push();
557
558                         } catch(Error e) {
559                                 this.paused = false;                    
560                                 this.pauseError(e.message);
561                                 
562                                 return;
563                                 //failure += e.message;
564                                 //print("Push failed:\n");
565                                 
566                         }   
567                         this.paused = false;                                            
568                 }
569                 
570                 
571                 // finally merge all the commit messages.
572                  
573                 try {
574                         // catch notification failures.. so we can carry on..
575                         if (success.length > 0) {
576
577                                 
578                                 var notification = new Notify.Notification(
579                                         "Git Live Commited",
580                                         string.joinv("\n",success),
581                                          "dialog-information"
582                                         
583                                 );
584         
585                                 notification.set_timeout(5);
586                                 notification.show();   
587                         }
588                         
589                         //if (failure.length > 0) {
590
591                                 // should never get this far...
592                         //      this.pauseError();   
593                         //}
594                 } catch(Error e) {
595                         print(e.message);
596                         
597                 }
598                 this.queueRunning = false;
599         }
600         
601
602
603         
604
605         //string[] just_created;
606  
607  
608
609
610
611    
612
613
614         public override  void onChanged(MonitorNamePathDir src) 
615         { 
616                 //print("GitMonitor.onChanged\n");        
617                 return; // always ignore this..?
618                 //this.parsePath(src);
619         }
620         
621
622  
623         /**
624          *  results in  git add  + git commit..
625          *
626          */
627         public override void onChangesDoneHint(MonitorNamePathDir src)  
628         { 
629
630                 if (this.paused) {
631                         return;
632                 }
633                 print("GitMonitor.onChangedHint\n");                            
634
635                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
636                 var cmd = new GitMonitorQueue(src);
637                 if (cmd.shouldIgnore()) {
638                         return;
639                 }
640                 
641            
642                 //var add_it = false;
643                 /*
644                 if (this.is_just_created(cmd.path)) {
645                         
646                 if (typeof(this.just_created[src.path]) !='undefined') {
647                         delete this.just_created[src.path];
648                         
649                         this.queue.push( 
650                                 [ src.gitpath,  'add', src.vpath ],
651                                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
652                                 
653                         );
654                  
655                         return;
656                 }
657                 */
658                 cmd.action = "add";
659                 this.queue.append_val(cmd);
660
661                 cmd = new GitMonitorQueue(src);
662                 cmd.action = "commit";
663                 cmd.message = cmd.vname;
664                 this.queue.append_val(cmd);
665  
666                  
667         }
668         public override  void onDeleted(MonitorNamePathDir src) 
669    { 
670
671                 if (this.paused) {
672                         return;
673                 }
674                 print("GitMonitor.onDeleted\n");                        
675                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
676                 var cmd = new GitMonitorQueue(src);
677                 if (cmd.shouldIgnore()) {
678                         return;
679                 }
680                 // should check if monitor needs removing..
681                 // it should also check if it was a directory.. - so we dont have to commit all..
682                 cmd.action = "rm";
683                 this.queue.append_val(cmd);
684
685                 cmd = new GitMonitorQueue(src);
686                 cmd.action = "commit";
687                 cmd.message = cmd.vname;
688                 cmd.commit_all = true;
689
690                 this.queue.append_val(cmd);
691  
692         }
693         public override  void onCreated(MonitorNamePathDir src) {
694
695                 if (this.paused) {
696                         return;
697                 }
698                 print("GitMonitor.onCreated\n");                        
699                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
700                 var cmd = new GitMonitorQueue(src);
701                 if (cmd.shouldIgnore()) {
702                         return;
703                 }
704
705                 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
706                    // this.just_created[src.path] = true;
707                         return; // we do not handle file create flags... - use done hint.
708                 }
709                 // directory has bee created
710                 this.monitor(src.path);
711                 //this.top.append_val(src.path);
712                 //this.monitor(src.path );
713
714
715 // -- no point in adding a dir.. as git does not handle them...
716 //        this.queue.push( 
717   //          [ src.gitpath, 'add' , src.vpath,  { all: true } ],
718  //           [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
719   //          
720    //     );
721
722         }
723
724         public  override void onAttributeChanged(MonitorNamePathDir src) { 
725
726                 if (this.paused) {
727                         return;
728                 }
729                 print("GitMonitor.onAttributeChanged\n");                       
730                 if (src.dir == GitMonitor.gitlive) {
731                    return; // attribute on top level..
732                 }
733                 
734                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
735                 var cmd = new GitMonitorQueue(src);
736                 if (cmd.shouldIgnore()) {
737                         return;
738                 }
739                 cmd.action = "add";
740                 this.queue.append_val(cmd);
741
742                 cmd = new GitMonitorQueue(src);
743                 cmd.action = "commit";
744                 cmd.message = "Attribute changed " + cmd.vname;
745                 this.queue.append_val(cmd);
746         }
747  
748    public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
749         { 
750
751                 if (this.paused) {
752                         return;
753                 }
754                 print("GitMonitor.onMoved\n");                  
755                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
756                 var cmd_s = new GitMonitorQueue(src);
757
758                 var cmd_d = new GitMonitorQueue(dest);
759    
760                 
761                 if (cmd_d.gitpath != cmd_s.gitpath) {
762                         this.onDeleted(src);
763                         this.onCreated(dest);
764                         this.onChangesDoneHint(dest);
765                         return;
766                 }
767                 // needs to handle move to/from unsupported types..
768                 
769                 if (cmd_s.shouldIgnore()) {
770                         this.onCreated(dest);
771                         this.onChangesDoneHint(dest);
772                         return;
773
774                 }
775                 if (cmd_d.shouldIgnore()) {
776                         
777                         this.onDeleted(src);
778  
779
780                         return;
781                 }
782                 
783                 
784                 print("RM: %s\n", cmd_s.vname);
785                 cmd_s.action = "rm";
786                 this.queue.append_val(cmd_s);
787
788                 
789                 
790                 print("ADD: %s\n", cmd_d.vname);
791                 cmd_d.action = "add";
792                 this.queue.append_val(cmd_d);
793
794
795                 var cmd = new GitMonitorQueue(dest);
796                 cmd.action = "commit";
797                 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
798                 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
799                         cmd.message = cmd_d.vname;
800                 }
801                 
802                 this.queue.append_val(cmd);
803
804
805                  
806         }
807            
808 }