GitMonitor.vala
[gitlive] / GitMonitor.vala
index 7a4bda0..9521391 100644 (file)
@@ -7,26 +7,28 @@ public class GitMonitorQueue : MonitorNamePathDir {
         // dir = dir path
     
         public string gitpath;
-        public string vpath;  // relative path (within git)
+        public string vdir;  // relative path (within git)
         public string vname;  // relative filename (within git)
         public string message ; // for commit
         public bool commit_all;
-        
-
-
-
-
+         
         public GitMonitorQueue(MonitorNamePathDir f) {
 
             base(f.name, f.path, f.dir);
-
-
             this.message = "";
             this.commit_all = false;
-           
-            var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
             
+            var vpath_ar = this.dir.substring(GitMonitor.gitlive.length +1).split("/", 0);
+            
+            if (vpath_ar[0].length < 1) {
+
+                this.gitpath = "";
+                this.vdir = "";
+                this.vname = "";
+            }        
+
+
             this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
             
             string[]  vpath = {};
@@ -34,19 +36,27 @@ public class GitMonitorQueue : MonitorNamePathDir {
                 vpath += vpath_ar[i];
             }
 
-            this.vpath =  string.joinv("/", vpath);
+            this.vdir =  string.joinv("/", vpath);
+
+            this.vname =  this.vdir + (this.vdir.length > 0 ? "/" : "") + this.name;
+/*
+            stdout.printf(
+                    "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" + 
+                    "gitpath: %s\nvdir: %s\nvname: %s\n",
+                    this.name, this.path, this.dir,
+                    this.gitpath, this.vdir, this.vname
+            );
+*/
 
-            this.vname =  this.vpath + (this.vpath.length > 0 ? "/" : "") + this.name;
             //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
         
         
         }
 
-        public bool shouldIgnore(GitMonitor gm)
+        public bool shouldIgnore()
         {
             
-            
-            
+             
             // vim.. what a seriously brain dead program..
             if (this.name == "4913") {
                 return true;
@@ -60,6 +70,10 @@ public class GitMonitorQueue : MonitorNamePathDir {
                 
                 return true;
             }
+            if (this.name[this.name.length -1] == '~') {
+                return true;
+            }
+
             //if (f.name.match(/~$/)) {
             //    return true;
             //}
@@ -67,7 +81,7 @@ public class GitMonitorQueue : MonitorNamePathDir {
             //    return true;
             //}
             // ignore anything in top level!!!!
-            if (this.vpath.length < 1) {
+            if (this.gitpath.length < 1) {
                 return true;
             }
             
@@ -76,7 +90,8 @@ public class GitMonitorQueue : MonitorNamePathDir {
         
         /** -- statics --*/
         
-        public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
+        public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
+        {
             for(var i =0; i < add_files.length; i++) {
                 if (add_files.index(i).vname == add) {
                     return i;
@@ -99,7 +114,15 @@ public class GitMonitorQueue : MonitorNamePathDir {
             }
             return string.joinv("\n",ret);
         }
-
+        public static string queueArrayToString(Array<GitMonitorQueue> list) {
+            var ret = "";
+            for(var i =0; i < list.length; i++) {
+                
+                ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
+            }
+            return ret;
+            
+        }
 }
 
 
@@ -107,13 +130,13 @@ public class GitMonitorQueue : MonitorNamePathDir {
 public class GitMonitor : Monitor
 {
 
+    public static GitMonitor gitmonitor;
     /**
      * @property {String} the "gitlive" directory, normally ~/gitlive
      *  dset by OWNER... - we should do this as a CTOR.
      *  
      */
-    public static string gitlive = "";
+    public static string gitlive;
     
     
     public Array<GitMonitorQueue> queue ;
@@ -121,7 +144,15 @@ public class GitMonitor : Monitor
     
     public DateTime lastAdd;
      
-     
+    
+    public GitMonitor () {
+        this.queue = new Array<GitMonitorQueue>();
+        GitMonitor.gitmonitor = this;
+    }
+
+
+
     public new void pause() {
         this.paused = true;
         // what does this do to the old one...
@@ -149,9 +180,10 @@ public class GitMonitor : Monitor
         this.lastAdd = new DateTime.now(new TimeZone.local()); 
         
         Timeout.add_full(Priority.LOW, 500, () => {
-
+            //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
             // call this.monitor on each of 'top'
             for(int i = 0; i < this.top.length ; i++) {
+
                 this.monitor(this.top.index(i) );
             }
             StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
@@ -172,20 +204,24 @@ public class GitMonitor : Monitor
             } catch(Error e) {
                 print(e.message);
             }
+            return false; // do not keep doing this..
 
         });
         
+
         Timeout.add_full(Priority.LOW, 1000, () => {
             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
+
+            //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
             if (this.queue.length < 1  || this.queueRunning) {
                 return true;
             }
-
-            var last = this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
-
-            
-            //print("LAST RUN?" + last);
             
+            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. ????
                 return true;
             }
@@ -193,10 +229,9 @@ public class GitMonitor : Monitor
             //return 1;
         
             this.runQueue();
-            return true;
-        },null,null);
-        
-      
+            return true; //
+        });
+         
     }
 
 
@@ -249,9 +284,12 @@ public class GitMonitor : Monitor
         if (this.paused) {
             return;
         }
+        print("GitMonitor.runQueue\n");
+
         this.queueRunning = true;
 
         var cmds = new Array<GitMonitorQueue>();
+
         for(var i = 0; i < this.queue.length; i++) {
             cmds.append_val(this.queue.index(i));
         }
@@ -261,8 +299,8 @@ public class GitMonitor : Monitor
         
         string[] success = {};
         string[] failure = {};
-        var repos = new Array<GitRepo>(); //??
-        var done = new Array<GitMonitorQueue>();
+       //var repos = new Array<GitRepo>(); //??
+        //var done = new Array<GitMonitorQueue>();
         
         // first build a array of repo's to work with
         var repo_list = new Array<GitRepo>();
@@ -270,19 +308,25 @@ public class GitMonitor : Monitor
         // pull and group.
         
         //print(JSON.stringify(cmds));
+        // make sure nothing get's added to the queue where we are doing this..
+
         this.paused = true;
+
+        print("GitMonitor.runQueue - creating repos\n");
         
         for(var i = 0; i < cmds.length; i++) {
            
             var cmd = cmds.index(i);
         
             var gitpath = cmd.gitpath; 
-            var ix  = GitRepo.indexOf(this.repos,  cmd.gitpath);
+            stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
+        
+            var ix  = GitRepo.indexOf(repo_list,  cmd.gitpath);
             if (ix < 0) {
                 repo_list.append_val(new GitRepo( gitpath ));
-                ix = GitRepo.indexOf(this.repos,  cmd.gitpath);
+                ix = GitRepo.indexOf(repo_list,  cmd.gitpath);
             }
-            
+            stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
 
             //if (typeof(repo_list[gitpath]) == 'undefined') {
             //    repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
@@ -294,13 +338,15 @@ public class GitMonitor : Monitor
         }
         this.paused = false;
         // build add, remove and commit message list..
+
+        print("GitMonitor.runQueue - creating actions\n");
         
         for(var i = 0;i < repo_list.length;i++) {
-    
-
+     
             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>();
             //print(JSON.stringify(repo.cmds,null,4));
@@ -315,18 +361,17 @@ public class GitMonitor : Monitor
                         if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
                            break;
                         }
-        
                         
                         add_files.append_val(cmd);
                         break;
                     
                     case "rm":
-                        if (GitMonitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
+                        if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
                            break;
                         }
                         
                         // if file exists, do not try and delete it.
-                        if (FileUtils.test(cmd.rm, FileTest.EXISTS)) {
+                        if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
                             break;
                         }
                         
@@ -337,37 +382,64 @@ public class GitMonitor : Monitor
                         if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
                            break;
                         }
-                         
                         messages.append_val(cmd);
                         
                         break;
                     default:
-                        print("Opps unmatched action");
+                        stdout.printf("Opps unmatched action %s\n", cmd.action);
                         break;
                 } 
             }
-            
+            print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
+            print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
             //repo.debug = 1;
             // these can fail... at present... as we wildcard stuff.
-            stdout.printf("ADD : %d files"  , add_files.length);
-            
-            // make sure added files do not get removed..
-
+           
+            // make sure added files do not get removed.. ?? 
+            /*
             var remove_files_f = new Array<GitMonitorQueue>();
             for(var ii = 0;ii < remove_files.length;ii++) {
-                if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).rm) > -1 ) {
+                if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
                      continue;
                 }
                 remove_files_f.append_val(remove_files.index(ii));
             };
-            stdout.printf("REMOVE : %u files"  , remove_files.length);
-             
+            stdout.printf("REMOVE : %u files\n"  , remove_files_f.length);
+            */
+            
+            // if file was added, then removed, 
+            var remove_files_f = new Array<GitMonitorQueue>();
+            for(var ii = 0;ii < remove_files.length;ii++) {
+                if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
+                    // in add and remove - do not remvove
+                    continue;
+                }
+                remove_files_f.append_val(remove_files.index(ii));
+            };
+            for(var ii = 0;ii < add_files.length;ii++) {
+                if (GitMonitorQueue.indexOfAdd(remove_files,  add_files.index(ii).vname) > -1 ) {
+                    // in add and remove - do not remvove
+                    continue;
+                }
+                add_files_f.append_val(add_files.index(ii));
+            };
+            
+            print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
+            print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
+           
+            
             // make sure monitoring is paused so it does not recursively pick up
             // deletions
+            try {
+                repo.pull();
+            } catch(Error e) {
+                failure +=  e.message;
+            }
+            
             
-            // -- DO STUFF..
+            // -- DO STUFF..            
             try {
-                repo.add(add_files);
+                repo.add(add_files_f);
             } catch(Error e) {
                 failure +=  e.message;
             }  
@@ -383,7 +455,7 @@ public class GitMonitor : Monitor
             try { 
                 success += repo.commit(
                     GitMonitorQueue.messageToString(messages),
-                    add_files  
+                    add_files_f
                 );
                 success += repo.push();
 
@@ -397,7 +469,7 @@ public class GitMonitor : Monitor
          
         try {
             // catch notification failures.. so we can carry on..
-            if (success.length) {
+            if (success.length > 0) {
 
                 
                 var notification = new Notify.Notification(
@@ -411,14 +483,14 @@ public class GitMonitor : Monitor
                 notification.show();   
             }
             
-            if (failure.length) {
+            if (failure.length > 0) {
 
-                var notification = new Notify.Notification({
-                    summary: "Git Live ERROR!!",
+                var notification = new Notify.Notification(
+                      "Git Live ERROR!!",
                     string.joinv("\n",failure),
                     "dialog-information"
                     
-                });
+                );
     
                 notification.set_timeout(5); // show errros for longer
                 notification.show();   
@@ -443,8 +515,9 @@ public class GitMonitor : Monitor
    
 
 
-    public void onChanged(MonitorNamePathDir src) 
+    public override  void onChanged(MonitorNamePathDir src) 
     { 
+        print("GitMonitor.onChanged\n");        
         return; // always ignore this..?
         //this.parsePath(src);
     }
@@ -455,11 +528,11 @@ public class GitMonitor : Monitor
      *  results in  git add  + git commit..
      *
      */
-    public void onChangesDoneHint(MonitorNamePathDir src)  
+    public override void onChangesDoneHint(MonitorNamePathDir src)  
     { 
-        
+        print("GitMonitor.onChangedHint\n");        
         if (this.paused) {
-            return true;
+            return;
         }
             
 
@@ -470,7 +543,7 @@ public class GitMonitor : Monitor
         }
         
        
-        var add_it = false;
+        //var add_it = false;
         /*
         if (this.is_just_created(cmd.path)) {
             
@@ -486,20 +559,21 @@ public class GitMonitor : Monitor
             return;
         }
         */
-
+        cmd.action = "add";
         this.queue.append_val(cmd);
 
-        var cmd = new GitMonitorQueue(src);
+        cmd = new GitMonitorQueue(src);
         cmd.action = "commit";
-        cmd.message = src.vpath;
+        cmd.message = cmd.vname;
         this.queue.append_val(cmd);
  
          
     }
-    public void onDeleted(MonitorNamePathDir src) 
+    public override  void onDeleted(MonitorNamePathDir src) 
    { 
+        print("GitMonitor.onDeleted\n");        
         if (this.paused) {
-            return true;
+            return;
         }
         this.lastAdd = new DateTime.now(new TimeZone.local()); 
         var cmd = new GitMonitorQueue(src);
@@ -509,21 +583,20 @@ 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";
-        cmd.rm = src.vpath;
         this.queue.append_val(cmd);
 
-        var cmd = new GitMonitorQueue(src);
+        cmd = new GitMonitorQueue(src);
         cmd.action = "commit";
-        cmd.message = src.vpath;
+        cmd.message = cmd.vname;
         cmd.commit_all = true;
 
         this.queue.append_val(cmd);
  
     }
-    public void onCreated(MonitorNamePathDir src) {
-
+    public override  void onCreated(MonitorNamePathDir src) {
+        print("GitMonitor.onCreated\n");        
         if (this.paused) {
-            return true;
+            return;
         }
         this.lastAdd = new DateTime.now(new TimeZone.local()); 
         var cmd = new GitMonitorQueue(src);
@@ -537,8 +610,8 @@ public class GitMonitor : Monitor
         }
         // directory has bee created
         this.monitor(src.path);
-        this.top.append_val(src.path);
-        this.monitor(src.path );
+        //this.top.append_val(src.path);
+        //this.monitor(src.path );
 
 
 // -- no point in adding a dir.. as git does not handle them...
@@ -550,11 +623,15 @@ public class GitMonitor : Monitor
 
     }
 
-    public void onAttributeChanged(MonitorNamePathDir src) { 
-
+    public  override void onAttributeChanged(MonitorNamePathDir src) { 
+        print("GitMonitor.onAttributeChanged\n");        
         if (this.paused) {
-            return true;
+            return;
+        }
+        if (src.dir == GitMonitor.gitlive) {
+           return; // attribute on top level..
         }
+        
         this.lastAdd = new DateTime.now(new TimeZone.local()); 
         var cmd = new GitMonitorQueue(src);
         if (cmd.shouldIgnore()) {
@@ -563,56 +640,57 @@ public class GitMonitor : Monitor
         cmd.action = "add";
         this.queue.append_val(cmd);
 
-        var cmd = new GitMonitorQueue(src);
+        cmd = new GitMonitorQueue(src);
         cmd.action = "commit";
-        cmd.message = "Attribute changed " + cmd.vpath;
+        cmd.message = "Attribute changed " + cmd.vname;
         this.queue.append_val(cmd);
     }
-
-
-   public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
+   public  override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
     { 
+        print("GitMonitor.onMoved\n");        
+        if (this.paused) {
+            return;
+        }
         this.lastAdd = new DateTime.now(new TimeZone.local()); 
         var cmd_s = new GitMonitorQueue(src);
 
-        var cmd_d = new GitMonitorQueue(src);
+        var cmd_d = new GitMonitorQueue(dest);
    
         
         if (cmd_d.gitpath != cmd_s.gitpath) {
             this.onDeleted(src);
             this.onCreated(dest);
-            this.onChangedDoneHint(dest);
+            this.onChangesDoneHint(dest);
             return;
         }
         // needs to handle move to/from unsupported types..
         
-        if (this.shouldIgnore(src)) {
+        if (cmd_s.shouldIgnore()) {
             this.onCreated(dest);
-            this.onChangedDoneHint(dest);
+            this.onChangesDoneHint(dest);
             return;
 
         }
-        if (this.shouldIgnore(dest)) {
+        if (cmd_d.shouldIgnore()) {
             
             this.onDeleted(src);
  
 
             return;
         }
-        
+         
         cmd_s.action = "rm";
         this.queue.append_val(cmd_s);
 
-
-
-
+  
         cmd_d.action = "add";
         this.queue.append_val(cmd_d);
 
 
         var cmd = new GitMonitorQueue(dest);
         cmd.action = "commit";
-        cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
+        cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
         this.queue.append_val(cmd);