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.vdir  + 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.vname, 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                                 if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
438                                         // in add and remove - do not remvove
439                                         continue;
440                                 }
441                                 remove_files_f.append_val(remove_files.index(ii));
442                         };
443                         for(var ii = 0;ii < add_files.length;ii++) {
444                                 if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.index(ii).vname) > -1 ) {
445                                         // the add file is in the remove list, and it does not exist - do not add it..
446                                         if (!FileUtils.test(add_files.index(ii).vname, FileTest.EXISTS)) {
447                                                         continue;
448                                         }
449                                          
450                                 }
451                                 
452                                 
453                                 
454                                 add_files_f.append_val(add_files.index(ii));
455                         };
456                         
457                         print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
458                         print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
459                    
460                         
461                         // make sure monitoring is paused so it does not recursively pick up
462                         // deletions
463                         try {
464                                 repo.pull();
465                         } catch(Error e) {
466                                 failure +=  e.message;
467                         }
468                         
469                         
470                         // -- DO STUFF..            
471                         try {
472                                 repo.add(add_files_f);
473                         } catch(Error e) {
474                                 failure +=  e.message;
475                         }  
476                         try {
477                                  repo.remove(remove_files_f);
478                         } catch(Error e) {
479                                 failure +=  e.message;
480                         }  
481
482                         this.paused = false;
483                         
484                         
485                         try { 
486                                 success += repo.commit(
487                                         GitMonitorQueue.messageToString(messages),
488                                         add_files_f
489                                 );
490                                 success += repo.push();
491
492                         } catch(Error e) {
493                                 failure += e.message;
494                                 
495                         }   
496                 }
497                 
498                 // finally merge all the commit messages.
499                  
500                 try {
501                         // catch notification failures.. so we can carry on..
502                         if (success.length > 0) {
503
504                                 
505                                 var notification = new Notify.Notification(
506                                         "Git Live Commited",
507                                         string.joinv("\n",success),
508                                          "dialog-information"
509                                         
510                                 );
511         
512                                 notification.set_timeout(5);
513                                 notification.show();   
514                         }
515                         
516                         if (failure.length > 0) {
517
518                                 var notification = new Notify.Notification(
519                                           "Git Live ERROR!!",
520                                         string.joinv("\n",failure),
521                                         "dialog-information"
522                                         
523                                 );
524         
525                                 notification.set_timeout(5); // show errros for longer
526                                 notification.show();   
527                         }
528                 } catch(Error e) {
529                         print(e.message);
530                         
531                 }
532                 this.queueRunning = false;
533         }
534         
535
536
537         
538
539         //string[] just_created;
540  
541  
542
543
544
545    
546
547
548         public override  void onChanged(MonitorNamePathDir src) 
549         { 
550                 print("GitMonitor.onChanged\n");        
551                 return; // always ignore this..?
552                 //this.parsePath(src);
553         }
554         
555
556  
557         /**
558          *  results in  git add  + git commit..
559          *
560          */
561         public override void onChangesDoneHint(MonitorNamePathDir src)  
562         { 
563                 print("GitMonitor.onChangedHint\n");        
564                 if (this.paused) {
565                         return;
566                 }
567                         
568
569                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
570                 var cmd = new GitMonitorQueue(src);
571                 if (cmd.shouldIgnore()) {
572                         return;
573                 }
574                 
575            
576                 //var add_it = false;
577                 /*
578                 if (this.is_just_created(cmd.path)) {
579                         
580                 if (typeof(this.just_created[src.path]) !='undefined') {
581                         delete this.just_created[src.path];
582                         
583                         this.queue.push( 
584                                 [ src.gitpath,  'add', src.vpath ],
585                                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
586                                 
587                         );
588                  
589                         return;
590                 }
591                 */
592                 cmd.action = "add";
593                 this.queue.append_val(cmd);
594
595                 cmd = new GitMonitorQueue(src);
596                 cmd.action = "commit";
597                 cmd.message = cmd.vname;
598                 this.queue.append_val(cmd);
599  
600                  
601         }
602         public override  void onDeleted(MonitorNamePathDir src) 
603    { 
604                 print("GitMonitor.onDeleted\n");        
605                 if (this.paused) {
606                         return;
607                 }
608                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
609                 var cmd = new GitMonitorQueue(src);
610                 if (cmd.shouldIgnore()) {
611                         return;
612                 }
613                 // should check if monitor needs removing..
614                 // it should also check if it was a directory.. - so we dont have to commit all..
615                 cmd.action = "rm";
616                 this.queue.append_val(cmd);
617
618                 cmd = new GitMonitorQueue(src);
619                 cmd.action = "commit";
620                 cmd.message = cmd.vname;
621                 cmd.commit_all = true;
622
623                 this.queue.append_val(cmd);
624  
625         }
626         public override  void onCreated(MonitorNamePathDir src) {
627                 print("GitMonitor.onCreated\n");        
628                 if (this.paused) {
629                         return;
630                 }
631                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
632                 var cmd = new GitMonitorQueue(src);
633                 if (cmd.shouldIgnore()) {
634                         return;
635                 }
636
637                 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
638                    // this.just_created[src.path] = true;
639                         return; // we do not handle file create flags... - use done hint.
640                 }
641                 // directory has bee created
642                 this.monitor(src.path);
643                 //this.top.append_val(src.path);
644                 //this.monitor(src.path );
645
646
647 // -- no point in adding a dir.. as git does not handle them...
648 //        this.queue.push( 
649   //          [ src.gitpath, 'add' , src.vpath,  { all: true } ],
650  //           [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
651   //          
652    //     );
653
654         }
655
656         public  override void onAttributeChanged(MonitorNamePathDir src) { 
657                 print("GitMonitor.onAttributeChanged\n");        
658                 if (this.paused) {
659                         return;
660                 }
661                 if (src.dir == GitMonitor.gitlive) {
662                    return; // attribute on top level..
663                 }
664                 
665                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
666                 var cmd = new GitMonitorQueue(src);
667                 if (cmd.shouldIgnore()) {
668                         return;
669                 }
670                 cmd.action = "add";
671                 this.queue.append_val(cmd);
672
673                 cmd = new GitMonitorQueue(src);
674                 cmd.action = "commit";
675                 cmd.message = "Attribute changed " + cmd.vname;
676                 this.queue.append_val(cmd);
677         }
678  
679    public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
680         { 
681                 print("GitMonitor.onMoved\n");        
682                 if (this.paused) {
683                         return;
684                 }
685                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
686                 var cmd_s = new GitMonitorQueue(src);
687
688                 var cmd_d = new GitMonitorQueue(dest);
689    
690                 
691                 if (cmd_d.gitpath != cmd_s.gitpath) {
692                         this.onDeleted(src);
693                         this.onCreated(dest);
694                         this.onChangesDoneHint(dest);
695                         return;
696                 }
697                 // needs to handle move to/from unsupported types..
698                 
699                 if (cmd_s.shouldIgnore()) {
700                         this.onCreated(dest);
701                         this.onChangesDoneHint(dest);
702                         return;
703
704                 }
705                 if (cmd_d.shouldIgnore()) {
706                         
707                         this.onDeleted(src);
708  
709
710                         return;
711                 }
712                 
713                 
714                 print("RM: %s\n", cmd_s.vname);
715                 cmd_s.action = "rm";
716                 this.queue.append_val(cmd_s);
717
718                 
719                 
720                 print("ADD: %s\n", cmd_d.vname);
721                 cmd_d.action = "add";
722                 this.queue.append_val(cmd_d);
723
724
725                 var cmd = new GitMonitorQueue(dest);
726                 cmd.action = "commit";
727                 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
728                 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
729                         cmd.message = cmd_d.vname;
730                 }
731                 
732                 this.queue.append_val(cmd);
733
734
735                  
736         }
737            
738 }