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