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