sync
[gitlive] / GitMonitor.vala
index 92733e3..7e42154 100644 (file)
@@ -1,5 +1,26 @@
+/**
+
+The monitor suffers from various issues - basically event flows while it's running...
+
+normall operations
+
+- monitors for file changes
+ -- adds to QUEUE when occurs.
+- queue runs in background.
+ - if it's got stuff in it..
+ - COMMIT (normally)
+ - now?? - if on master - try and branch
+   == do user selection, until we have branched
+   == then start monitoring again..
+   
+
+
+
+*/
+
+
 
-  
 
 public class GitMonitor : Monitor
 {
@@ -13,7 +34,7 @@ public class GitMonitor : Monitor
        public static string gitlive;
        
        
-       public Array<GitMonitorQueue> queue ;
+       private Gee.ArrayList<GitMonitorQueue> queue ;
        public bool queueRunning = false;
        
        public DateTime lastAdd;
@@ -23,23 +44,29 @@ public class GitMonitor : Monitor
        public GitMonitor ()
        {
        
-               this.queue = new Array<GitMonitorQueue>();
+               this.queue = new Gee.ArrayList<GitMonitorQueue>();
                GitMonitor.gitmonitor = this;
                Timeout.add_full(Priority.LOW, 500, () => {
 
                        //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
 
                        //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
-                       if (this.queue.length < 1  || this.queueRunning) {
+                       if (this.queue.size < 1  || this.queueRunning) {
                                return true;
                        }
+                       var first = this.queue.get(0);
+                       var delay = true;
+                       if (!first.repo.is_wip_branch() && first.repo.is_auto_branch()) {
+                               delay = false;
+                       }
+                       
                        
                        var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
  
                        // stdout.printf("LAST RUN: %s (expect %s) \n" ,
                        //         last.to_string(),   (5 * TimeSpan.SECOND).to_string() );
  
-                       if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
+                       if (delay && last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
                                return true;
                        }
                        //_this.lastAdd = new Date();
@@ -82,7 +109,7 @@ public class GitMonitor : Monitor
                this.paused = true;
                this.queueRunning = false;
                // what does this do to the old one...
-               this.queue = new Array<GitMonitorQueue> ();
+               //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
                this.stop();
                StatusIconA.statusicon.pauseError();
                
@@ -103,11 +130,17 @@ public class GitMonitor : Monitor
        public new void pause() {
                this.paused = true;
                // what does this do to the old one...
-               this.queue = new Array<GitMonitorQueue> ();
+               //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
                StatusIconA.statusicon.pause();
 
        }
        
+       public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
+       {
+               //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
+               this.queue = queue;
+       }
+       
        /*
        public new void resume () {
                this.paused = false;
@@ -170,9 +203,10 @@ public class GitMonitor : Monitor
        }
        
        
-       public new void monitor (string path,  int depth = 0)
+       public override void monitor (string path,  int depth = 0)
        {
                
+               //GLib.debug("GitMonitor : monitor %d %s", depth, path);
                //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
                
                 
@@ -182,6 +216,13 @@ public class GitMonitor : Monitor
                }
                
                if (depth == 1) {
+               
+                       if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
+                               return; // skip non-git directories..
+                       }
+               
+                       GitRepo.get(path);
+               
                        // FIXME - check if repo is flagged as not autocommit..
                        //var repo = imports.Scm.Repo.Repo.get(path);
                        //if (!repo || !repo.autocommit()) {
@@ -210,22 +251,35 @@ public class GitMonitor : Monitor
        public void runQueue()
        {
                
-               if (this.paused) {
+               if (this.paused || this.queue.size < 1 ) {
                        return;
                }
-               GLib.debug("GitMonitor.runQueue\n");
+               
+               foreach(var q in this.queue) {
+                       if (!q.shouldIgnore() && !q.repo.is_wip_branch() && q.repo.is_auto_branch()) {
+                               var oldq = this.queue;
+                               this.queue =  new Gee.ArrayList<GitMonitorQueue>(); 
+                               NewBranch.singleton().show(q.repo, oldq);
+                               
+                               return;
+                       }
+               
+               }
+               
+                
+               
+               GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
 
                this.queueRunning = true;
 
-               var cmds = new Array<GitMonitorQueue>();
+               var cmds = new Gee.ArrayList<GitMonitorQueue>();
 
-               for(var i = 0; i < this.queue.length; i++) {
-                       cmds.append_val(this.queue.index(i));
+               for(var i = 0; i < this.queue.size; i++) {
+                       cmds.add(this.queue.get(i));
                }
 
-               this.queue = new Array<GitMonitorQueue>();// empty queue!
-
-               
+               this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
                string[] success = {};
                string[] failure = {};
           //var repos = new Array<GitRepo>(); //??
@@ -244,14 +298,14 @@ public class GitMonitor : Monitor
                var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
                GLib.debug("GitMonitor.runQueue - creating repos");
                
-               for(var i = 0; i < cmds.length; i++) {
+               for(var i = 0; i < cmds.size; i++) {
                   
-                       var cmd = cmds.index(i);
+                       var cmd = cmds.get(i);
                
                        var gitpath = cmd.gitpath; 
                        
                        var repo = GitRepo.get( gitpath );
-                       if ( !repo.is_wip_branch()) {
+                       if ( !repo.is_wip_branch() && repo.is_auto_branch()) {
                                leave_queued.add(cmd);
                                continue;
                        }
@@ -270,9 +324,11 @@ public class GitMonitor : Monitor
                        //    repo_list[gitpath].cmds = [];
                         //   repo_list[gitpath].pull();
                        //}
-                       repo_list.index(ix).cmds.append_val(cmd);
+                       repo_list.index(ix).cmds.add(cmd);
 
                }
+               this.queue = leave_queued;
+               
                this.paused = false;
                // build add, remove and commit message list..
 
@@ -282,14 +338,14 @@ public class GitMonitor : Monitor
         
                        var repo = repo_list.index(i);
 
-                       var add_files = new Array<GitMonitorQueue>();
-                       var add_files_f = new Array<GitMonitorQueue>();
-                       var remove_files = new Array<GitMonitorQueue>();
-                       var messages = new Array<GitMonitorQueue>();
+                       var add_files = new Gee.ArrayList<GitMonitorQueue>();
+                       var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
+                       var remove_files = new Gee.ArrayList<GitMonitorQueue>();
+                       var messages = new Gee.ArrayList<GitMonitorQueue>();
                        //print(JSON.stringify(repo.cmds,null,4));
                        
-                       for(var ii = 0;ii < repo.cmds.length;ii++) {
-                               var cmd = repo.cmds.index(ii);
+                       for(var ii = 0;ii < repo.cmds.size;ii++) {
+                               var cmd = repo.cmds.get(ii);
        
                                if (repo.is_ignore(cmd.vname)) {
                                        continue;
@@ -303,7 +359,7 @@ public class GitMonitor : Monitor
                                                   break;
                                                }
                                                
-                                               add_files.append_val(cmd);
+                                               add_files.add(cmd);
                                                break;
                                        
                                        case "rm":
@@ -316,14 +372,14 @@ public class GitMonitor : Monitor
                                                        break;
                                                }
                                                
-                                               remove_files.append_val(cmd);
+                                               remove_files.add(cmd);
                                                break;
                                        
                                        case "commit" :
                                                if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
                                                   break;
                                                }
-                                               messages.append_val(cmd);
+                                               messages.add(cmd);
                                                
                                                break;
                                        default:
@@ -353,37 +409,35 @@ public class GitMonitor : Monitor
                        */
                        
                        // if file was added, then removed, 
-                       var remove_files_f = new Array<GitMonitorQueue>();
-                       for(var ii = 0;ii < remove_files.length;ii++) {
+                       var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
+                       for(var ii = 0;ii < remove_files.size;ii++) {
                                
                                
                                
-                               if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
+                               if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.get(ii).vname) > -1 ) {
                                        // in add and remove - do not remvove
                                        continue;
                                }
-                               remove_files_f.append_val(remove_files.index(ii));
+                               remove_files_f.add(remove_files.get(ii));
                        };
-                       for(var ii = 0;ii < add_files.length;ii++) {
-                               if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.index(ii).vname) > -1 ) {
+                       for(var ii = 0;ii < add_files.size;ii++) {
+                               if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.get(ii).vname) > -1 ) {
                                        // the add file is in the remove list, and it does not exist - do not add it..
-                                       print("check exists ? %s\n",add_files.index(ii).fullpath());
+                                       print("check exists ? %s\n",add_files.get(ii).fullpath());
                                        
-                                       if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
+                                       if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
                                                        continue;
                                        }
                                         
-                               }
-                               
-                               
+                               } 
                                
-                               add_files_f.append_val(add_files.index(ii));
+                               add_files_f.add(add_files.get(ii));
                        };
                        
                        GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
                        GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
                    
-                   if (add_files_f.length < 1 && remove_files_f.length < 1) {
+                   if (add_files_f.size < 1 && remove_files_f.size < 1) {
                                continue;
                    }
                    
@@ -419,8 +473,7 @@ public class GitMonitor : Monitor
                                failure +=  e.message;
                                GLib.debug("Remove failed:");
                        }  
-
-
                        try { 
                                success += repo.commit(
                                        GitMonitorQueue.messageToString(messages),
@@ -428,6 +481,7 @@ public class GitMonitor : Monitor
                                );
                                success += repo.push();
 
+
                        } catch(Error e) {
                        
                                // if the error is 'nothing to commit, working tree clean'
@@ -541,12 +595,12 @@ public class GitMonitor : Monitor
                }
                */
                cmd.action = "add";
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
 
                cmd = new GitMonitorQueue(src);
                cmd.action = "commit";
                cmd.message = cmd.vname;
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
  
                 
        }
@@ -565,14 +619,14 @@ public class GitMonitor : Monitor
                // should check if monitor needs removing..
                // it should also check if it was a directory.. - so we dont have to commit all..
                cmd.action = "rm";
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
 
                cmd = new GitMonitorQueue(src);
                cmd.action = "commit";
                cmd.message = cmd.vname;
                cmd.commit_all = true;
 
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
  
        }
        public override  void onCreated(MonitorNamePathDir src) {
@@ -606,7 +660,8 @@ public class GitMonitor : Monitor
 
        }
 
-       public  override void onAttributeChanged(MonitorNamePathDir src) { 
+       public  override void onAttributeChanged(MonitorNamePathDir src) 
+       { 
 
                if (this.paused) {
                        return;
@@ -622,12 +677,12 @@ public class GitMonitor : Monitor
                        return;
                }
                cmd.action = "add";
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
 
                cmd = new GitMonitorQueue(src);
                cmd.action = "commit";
                cmd.message = "Attribute changed " + cmd.vname;
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);
        }
  
    public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
@@ -660,21 +715,18 @@ public class GitMonitor : Monitor
                if (cmd_d.shouldIgnore()) {
                        
                        this.onDeleted(src);
-
                        return;
                }
-               
-               
+                
                GLib.debug("RM: %s", cmd_s.vname);
                cmd_s.action = "rm";
-               this.queue.append_val(cmd_s);
+               this.queue.add(cmd_s);
 
                
                
                GLib.debug("ADD: %s", cmd_d.vname);
                cmd_d.action = "add";
-               this.queue.append_val(cmd_d);
+               this.queue.add(cmd_d);
 
 
                var cmd = new GitMonitorQueue(dest);
@@ -684,7 +736,7 @@ public class GitMonitor : Monitor
                        cmd.message = cmd_d.vname;
                }
                
-               this.queue.append_val(cmd);
+               this.queue.add(cmd);