GitMonitor.vala
[gitlive] / GitMonitor.vala
1 /**
2
3 The monitor suffers from various issues - basically event flows while it's running...
4
5 normall operations
6
7 - monitors for file changes
8  -- adds to QUEUE when occurs.
9  
10 - queue runs in background.
11  - if it's got stuff in it..
12  - COMMIT (normally)
13  - now?? - if on master - try and branch
14    == do user selection, until we have branched
15    == then start monitoring again..
16    
17
18
19
20 */
21
22
23
24
25 public class GitMonitor : Monitor
26 {
27
28         public static GitMonitor gitmonitor;
29         /**
30          * @property {String} the "gitlive" directory, normally ~/gitlive
31          *  dset by OWNER... - we should do this as a CTOR.
32          *  
33          */
34         public static string gitlive;
35         
36         
37         private Gee.ArrayList<GitMonitorQueue> queue ;
38         public bool queueRunning = false;
39         
40         public DateTime lastAdd;
41
42  
43
44         public GitMonitor ()
45         {
46         
47                 this.queue = new Gee.ArrayList<GitMonitorQueue>();
48                 GitMonitor.gitmonitor = this;
49                 Timeout.add_full(Priority.LOW, 500, () => {
50
51                         //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
52
53                         //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
54                         if (this.queue.size < 1  || this.queueRunning) {
55                                 return true;
56                         }
57                         var first = this.queue.get(0);
58                         var delay = true;
59                         if (!first.repo.is_wip_branch() && first.repo.is_auto_branch()) {
60                                 delay = false;
61                         }
62                         
63                         
64                         
65                         var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
66  
67                         // stdout.printf("LAST RUN: %s (expect %s) \n" ,
68                         //         last.to_string(),   (5 * TimeSpan.SECOND).to_string() );
69  
70                         if (delay && last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
71                                 return true;
72                         }
73                         //_this.lastAdd = new Date();
74                         //return 1;
75                 
76                         this.runQueue();
77                         return true; //
78                 });
79                 
80         }
81
82         public new void pauseError(string failure) 
83         {
84         
85                 var notification = new Notify.Notification(
86                           "Git Live ERROR!!",
87                         failure,
88                         "dialog-information"
89                         
90                 );
91                         
92                 notification.set_timeout(60); // show errros for longer
93                 notification.show();
94                 
95         Canberra.Context context;
96         Canberra.Proplist props;
97
98         Canberra.Context.create (out context);
99         Canberra.Proplist.create (out props);
100
101         props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
102         props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
103  
104
105         context.play_full (0, props, null);
106
107
108                         
109         
110                 this.paused = true;
111                 this.queueRunning = false;
112                 // what does this do to the old one...
113                 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
114                 this.stop();
115                 StatusIconA.statusicon.pauseError();
116                 
117                 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
118                         "A Serious problem occured running git, you will probably have to resolve this manually\n" + 
119                         "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" + 
120                         failure
121                 );
122                 m.set_keep_above(true);
123                 m.show();
124                 m.set_position(Gtk.WindowPosition.CENTER);
125                 m.response.connect( (id_pressed) => {
126                         m.hide();
127                 });
128                 
129         }
130  
131         public new void pause() {
132                 this.paused = true;
133                 // what does this do to the old one...
134                 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
135                 StatusIconA.statusicon.pause();
136
137         }
138         
139         public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
140         {
141                 //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
142                 this.queue = queue;
143         }
144         
145         /*
146         public new void resume () {
147                 this.paused = false;
148                 this.queue = new Array<GitMonitorQueue> ();
149                 StatusIconA.statusicon.resume();
150                 
151                 
152         }
153         */
154         /**
155          * Start the monitoring
156          * and run the queue every 500 milliseconds..
157          *
158          */
159         public new void start() 
160         {
161                 StatusIconA.statusicon.refreshing();
162                 
163                  
164                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
165                 
166                 Timeout.add_full(Priority.LOW, 500, () => {
167                         //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
168                         // call this.monitor on each of 'top'
169                         for(int i = 0; i < this.top.length ; i++) {
170
171                                 this.monitor(this.top.index(i) );
172                         }
173                         StatusIconA.statusicon.resume();
174                     this.paused = false;
175                    
176                         
177                         try {
178
179                           
180                                 var notification = new Notify.Notification(
181                                         "Git Live",
182                                         "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length), 
183                                          "dialog-information"
184                                 );
185                 
186                                 notification.set_timeout(5);
187                                 notification.show();
188                         } catch(Error e) {
189                                 GLib.debug("Error sending notification to screen: %s",e.message);
190                         }
191                         return false; // do not keep doing this..
192
193                 });
194                 
195
196                 
197                  
198         }
199
200
201         public new void stop() {
202                 StatusIconA.statusicon.pause();
203                 base.stop();
204         }
205         
206         
207         public override void monitor (string path,  int depth = 0)
208         {
209                 
210                 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
211                 //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
212                 
213                  
214                 // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
215                 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
216                         return;
217                 }
218                 
219                 if (depth == 1) {
220                 
221                         if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
222                                 return; // skip non-git directories..
223                         }
224                 
225                         GitRepo.get(path);
226                 
227                         // FIXME - check if repo is flagged as not autocommit..
228                         //var repo = imports.Scm.Repo.Repo.get(path);
229                         //if (!repo || !repo.autocommit()) {
230                         //    return;
231                         //} 
232                 }
233                 
234                 
235                 // check if the repo is to be monitored.
236                 //print("PATH : " + path);
237                 
238                 
239                 base.monitor(path, depth);
240         }
241
242         
243
244         /**
245          * run the queue.
246          * - pulls the items off the queue 
247          *    (as commands run concurrently and new items may get added while it's running)
248          * - runs the queue items
249          * - pushes upstream.
250          * 
251          */
252         public void runQueue()
253         {
254                 
255                 if (this.paused || this.queue.size < 1 ) {
256                         return;
257                 }
258                 
259                 foreach(var q in this.queue) {
260                         if (!q.shouldIgnore() && !q.repo.is_wip_branch() && q.repo.is_auto_branch()) {
261                                 var oldq = this.queue;
262                                 this.queue =  new Gee.ArrayList<GitMonitorQueue>(); 
263                                 NewBranch.singleton().show(q.repo, oldq);
264                                 
265                                 return;
266                         }
267                 
268                 }
269                 
270                  
271                 
272                 GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
273
274                 this.queueRunning = true;
275
276                 var cmds = new Gee.ArrayList<GitMonitorQueue>();
277
278                 for(var i = 0; i < this.queue.size; i++) {
279                         cmds.add(this.queue.get(i));
280                 }
281
282                 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
283  
284                 string[] success = {};
285                 string[] failure = {};
286            //var repos = new Array<GitRepo>(); //??
287                 //var done = new Array<GitMonitorQueue>();
288                 
289                 // first build a array of repo's to work with
290                 var repo_list = new Array<GitRepo>();
291                 
292                 // pull and group.
293                 
294                 //print(JSON.stringify(cmds));
295                 // make sure nothing get's added to the queue where we are doing this..
296
297                 this.paused = true;
298
299                 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
300                 GLib.debug("GitMonitor.runQueue - creating repos");
301                 
302                 for(var i = 0; i < cmds.size; i++) {
303                    
304                         var cmd = cmds.get(i);
305                 
306                         var gitpath = cmd.gitpath; 
307                         
308                         var repo = GitRepo.get( gitpath );
309                         if ( !repo.is_wip_branch() && repo.is_auto_branch()) {
310                                 leave_queued.add(cmd);
311                                 continue;
312                         }
313                         
314                         GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
315                 
316                         var ix  = GitRepo.indexOf(repo_list,   gitpath);
317                         if (ix < 0) {
318                                 repo_list.append_val( GitRepo.get( gitpath ));
319                                 ix = GitRepo.indexOf(repo_list,  cmd.gitpath);
320                         }
321                         GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
322
323                         //if (typeof(repo_list[gitpath]) == 'undefined') {
324                         //    repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
325                         //    repo_list[gitpath].cmds = [];
326                          //   repo_list[gitpath].pull();
327                         //}
328                         repo_list.index(ix).cmds.add(cmd);
329
330                 }
331                 this.queue = leave_queued;
332                 
333                 this.paused = false;
334                 // build add, remove and commit message list..
335
336                 GLib.debug("GitMonitor.runQueue - creating actions");
337                 
338                 for(var i = 0;i < repo_list.length;i++) {
339          
340                         var repo = repo_list.index(i);
341
342                         var add_files = new Gee.ArrayList<GitMonitorQueue>();
343                         var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
344                         var remove_files = new Gee.ArrayList<GitMonitorQueue>();
345                         var messages = new Gee.ArrayList<GitMonitorQueue>();
346                         //print(JSON.stringify(repo.cmds,null,4));
347                         
348                         for(var ii = 0;ii < repo.cmds.size;ii++) {
349                                 var cmd = repo.cmds.get(ii);
350         
351                                 if (repo.is_ignore(cmd.vname)) {
352                                         continue;
353                                 }
354         
355                                 
356                                 switch(cmd.action) {
357                                         case "add" :
358                                                 
359                                                 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
360                                                    break;
361                                                 }
362                                                 
363                                                 add_files.add(cmd);
364                                                 break;
365                                         
366                                         case "rm":
367                                                 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
368                                                    break;
369                                                 }
370                                                 
371                                                 // if file exists, do not try and delete it.
372                                                 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
373                                                         break;
374                                                 }
375                                                 
376                                                 remove_files.add(cmd);
377                                                 break;
378                                         
379                                         case "commit" :
380                                                 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
381                                                    break;
382                                                 }
383                                                 messages.add(cmd);
384                                                 
385                                                 break;
386                                         default:
387                                                 stdout.printf("Opps unmatched action %s\n", cmd.action);
388                                                 break;
389                                 } 
390                         }
391                         
392                         repo.cmds.clear(); // reset the repo's command list..
393                         
394                         GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
395                         GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
396                         
397                         //repo.debug = 1;
398                         // these can fail... at present... as we wildcard stuff.
399                    
400                         // make sure added files do not get removed.. ?? 
401                         /*
402                         var remove_files_f = new Array<GitMonitorQueue>();
403                         for(var ii = 0;ii < remove_files.length;ii++) {
404                                 if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
405                                          continue;
406                                 }
407                                 remove_files_f.append_val(remove_files.index(ii));
408                         };
409                         stdout.printf("REMOVE : %u files\n"  , remove_files_f.length);
410                         */
411                         
412                         // if file was added, then removed, 
413                         var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
414                         for(var ii = 0;ii < remove_files.size;ii++) {
415                                 
416                                 
417                                 
418                                 if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.get(ii).vname) > -1 ) {
419                                         // in add and remove - do not remvove
420                                         continue;
421                                 }
422                                 remove_files_f.add(remove_files.get(ii));
423                         };
424                         for(var ii = 0;ii < add_files.size;ii++) {
425                                 if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.get(ii).vname) > -1 ) {
426                                         // the add file is in the remove list, and it does not exist - do not add it..
427                                         print("check exists ? %s\n",add_files.get(ii).fullpath());
428                                         
429                                         if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
430                                                         continue;
431                                         }
432                                          
433                                 } 
434                                 
435                                 add_files_f.add(add_files.get(ii));
436                         };
437                         
438                         GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
439                         GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
440                     
441                     if (add_files_f.size < 1 && remove_files_f.size < 1) {
442                                 continue;
443                     }
444                     
445                         // make sure monitoring is paused so it does not recursively pick up
446                         // deletions
447                         this.paused = true; 
448                           
449                           
450                         try {
451                                 
452                                 repo.pull();
453                         } catch(Error e) {
454                                 this.pauseError(e.message);
455                                 //failure +=  e.message;
456                                 //print("Pull failed:\n");
457                                 return;
458                         }
459                                                                         
460                         // -- DO STUFF..            
461                         try {
462                                 repo.add(add_files_f);
463                         } catch(Error e) {
464                                 this.pauseError(e.message);
465                                 return;
466                                 failure +=  e.message;
467                                 GLib.debug("Add failed:");
468                         }  
469                         try {
470                                  repo.remove(remove_files_f);
471                         } catch(Error e) {
472                                 this.pauseError(e.message);
473                                 return;
474                                 failure +=  e.message;
475                                 GLib.debug("Remove failed:");
476                         }  
477  
478                         try { 
479                                 success += repo.commit(
480                                         GitMonitorQueue.messageToString(messages),
481                                         add_files_f
482                                 );
483                                 success += repo.push();
484
485
486                         } catch(Error e) {
487                         
488                                 // if the error is 'nothing to commit, working tree clean'
489                                 // then it's not an error, - just continue;
490                                 if (/nothing to commit, working tree clean/.match(e.message)) {
491                                         GLib.debug("%s",e.message);
492                                         success += e.message;
493                                         this.paused = false; 
494                                         continue;
495                                 }
496                                 
497                         
498                         
499                                 this.paused = false;                    
500                                 this.pauseError(e.message);
501                                 
502                                 return;
503                                 //failure += e.message;
504                                 //print("Push failed:\n");
505                                 
506                         }   
507                         this.paused = false;                                            
508                 }
509                 
510                 
511                 // finally merge all the commit messages.
512                  
513                 try {
514                         // catch notification failures.. so we can carry on..
515                         if (success.length > 0) {
516
517                                 
518                                 var notification = new Notify.Notification(
519                                         "Git Live Commited",
520                                         string.joinv("\n",success),
521                                          "dialog-information"
522                                         
523                                 );
524         
525                                 notification.set_timeout(5);
526                                 notification.show();   
527                         }
528                         
529                         //if (failure.length > 0) {
530
531                                 // should never get this far...
532                         //      this.pauseError();   
533                         //}
534                 } catch(Error e) {
535                         GLib.debug(e.message);
536                         
537                 }
538                 this.queueRunning = false;
539         }
540         
541
542
543         
544
545         //string[] just_created;
546  
547  
548
549
550
551    
552
553
554         public override  void onChanged(MonitorNamePathDir src) 
555         { 
556                 //print("GitMonitor.onChanged\n");        
557                 return; // always ignore this..?
558                 //this.parsePath(src);
559         }
560         
561
562  
563         /**
564          *  results in  git add  + git commit..
565          *
566          */
567         public override void onChangesDoneHint(MonitorNamePathDir src)  
568         { 
569
570                 if (this.paused) {
571                         return;
572                 }
573                 GLib.debug("GitMonitor.onChangedHint");                         
574
575                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
576                 var cmd = new GitMonitorQueue(src);
577                 if (cmd.shouldIgnore()) {
578                         GLib.debug("GitMonitor.onChangedHint - ignored");
579                         return;
580                 }
581                 
582                 //var add_it = false;
583                 /*
584                 if (this.is_just_created(cmd.path)) {
585                         
586                 if (typeof(this.just_created[src.path]) !='undefined') {
587                         delete this.just_created[src.path];
588                         
589                         this.queue.push( 
590                                 [ src.gitpath,  'add', src.vpath ],
591                                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
592                                 
593                         );
594                  
595                         return;
596                 }
597                 */
598                 cmd.action = "add";
599                 this.queue.add(cmd);
600
601                 cmd = new GitMonitorQueue(src);
602                 cmd.action = "commit";
603                 cmd.message = cmd.vname;
604                 this.queue.add(cmd);
605  
606                  
607         }
608         public override  void onDeleted(MonitorNamePathDir src) 
609    { 
610
611                 if (this.paused) {
612                         return;
613                 }
614                 GLib.debug("GitMonitor.onDeleted");                     
615                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
616                 var cmd = new GitMonitorQueue(src);
617                 if (cmd.shouldIgnore()) {
618                         return;
619                 }
620                 // should check if monitor needs removing..
621                 // it should also check if it was a directory.. - so we dont have to commit all..
622                 cmd.action = "rm";
623                 this.queue.add(cmd);
624
625                 cmd = new GitMonitorQueue(src);
626                 cmd.action = "commit";
627                 cmd.message = cmd.vname;
628                 cmd.commit_all = true;
629
630                 this.queue.add(cmd);
631  
632         }
633         public override  void onCreated(MonitorNamePathDir src) {
634
635                 if (this.paused) {
636                         return;
637                 }
638                 GLib.debug("GitMonitor.onCreated");                     
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         { 
666
667                 if (this.paused) {
668                         return;
669                 }
670                 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);                       
671                 if (src.dir == GitMonitor.gitlive) {
672                    return; // attribute on top level..
673                 }
674                 
675                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
676                 var cmd = new GitMonitorQueue(src);
677                 if (cmd.shouldIgnore()) {
678                         return;
679                 }
680                 cmd.action = "add";
681                 this.queue.add(cmd);
682
683                 cmd = new GitMonitorQueue(src);
684                 cmd.action = "commit";
685                 cmd.message = "Attribute changed " + cmd.vname;
686                 this.queue.add(cmd);
687         }
688  
689    public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
690         { 
691
692                 if (this.paused) {
693                         return;
694                 }
695                 GLib.debug("GitMonitor.onMoved");                       
696                 this.lastAdd = new DateTime.now(new TimeZone.local()); 
697                 var cmd_s = new GitMonitorQueue(src);
698
699                 var cmd_d = new GitMonitorQueue(dest);
700    
701                 
702                 if (cmd_d.gitpath != cmd_s.gitpath) {
703                         this.onDeleted(src);
704                         this.onCreated(dest);
705                         this.onChangesDoneHint(dest);
706                         return;
707                 }
708                 // needs to handle move to/from unsupported types..
709                 
710                 if (cmd_s.shouldIgnore()) {
711                         this.onCreated(dest);
712                         this.onChangesDoneHint(dest);
713                         return;
714
715                 }
716                 if (cmd_d.shouldIgnore()) {
717                         
718                         this.onDeleted(src);
719                         return;
720                 }
721                  
722                 GLib.debug("RM: %s", cmd_s.vname);
723                 cmd_s.action = "rm";
724                 this.queue.add(cmd_s);
725
726                 
727                 
728                 GLib.debug("ADD: %s", cmd_d.vname);
729                 cmd_d.action = "add";
730                 this.queue.add(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.add(cmd);
741
742
743                  
744         }
745            
746 }