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