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