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