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