GitMonitor.vala
[gitlive] / GitMonitor.vala
index 002e3c7..6a67288 100644 (file)
@@ -18,7 +18,7 @@ public class GitMonitorQueue : MonitorNamePathDir {
  
             this.message = "";
             this.commit_all = false;
-  
+            
             var vpath_ar = this.dir.substring(GitMonitor.gitlive.length +1).split("/", 0);
             
             if (vpath_ar[0].length < 1) {
@@ -39,14 +39,14 @@ public class GitMonitorQueue : MonitorNamePathDir {
             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
             );
-
+*/
 
             //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
         
@@ -56,8 +56,7 @@ public class GitMonitorQueue : MonitorNamePathDir {
         public bool shouldIgnore()
         {
             
-            
-            
+             
             // vim.. what a seriously brain dead program..
             if (this.name == "4913") {
                 return true;
@@ -91,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;
@@ -114,7 +114,33 @@ 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;
+            
+        }
+        
+        public static bool GitMonitorQueue.queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action)) {
+            for(var i =0; i < list.length; i++) {
+                var test = list.index(i);
+                if (list.index(i).gitpath != cmd_s.gitpath) {
+                    continue
+                }
+                if (list.index(i).vname != cmd_s.vname) {
+                    continue
+                }
+                if (list.index(i).action != action) {
+                    continue
+                }
+                return true;
+            }
+            return false;
+        }
+        
 }
 
 
@@ -204,15 +230,15 @@ public class GitMonitor : Monitor
         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);
+            //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 = -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() );
+            // 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;
@@ -292,7 +318,7 @@ public class GitMonitor : Monitor
         string[] success = {};
         string[] failure = {};
        //var repos = new Array<GitRepo>(); //??
-        var done = new Array<GitMonitorQueue>();
+        //var done = new Array<GitMonitorQueue>();
         
         // first build a array of repo's to work with
         var repo_list = new Array<GitRepo>();
@@ -338,6 +364,7 @@ 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>();
             //print(JSON.stringify(repo.cmds,null,4));
@@ -357,7 +384,7 @@ public class GitMonitor : Monitor
                         break;
                     
                     case "rm":
-                        if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
+                        if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
                            break;
                         }
                         
@@ -381,13 +408,13 @@ public class GitMonitor : Monitor
                         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 : %u files\n"  , 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).vname) > -1 ) {
@@ -396,13 +423,41 @@ public class GitMonitor : Monitor
                 remove_files_f.append_val(remove_files.index(ii));
             };
             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;
             }  
@@ -418,7 +473,7 @@ public class GitMonitor : Monitor
             try { 
                 success += repo.commit(
                     GitMonitorQueue.messageToString(messages),
-                    add_files  
+                    add_files_f
                 );
                 success += repo.push();
 
@@ -506,7 +561,7 @@ public class GitMonitor : Monitor
         }
         
        
-        var add_it = false;
+        //var add_it = false;
         /*
         if (this.is_just_created(cmd.path)) {
             
@@ -591,6 +646,10 @@ public class GitMonitor : Monitor
         if (this.paused) {
             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()) {
@@ -614,7 +673,7 @@ public class GitMonitor : Monitor
         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) {
@@ -639,12 +698,14 @@ public class GitMonitor : Monitor
             return;
         }
         
+        
+        print("RM: %s\n", cmd_s.vname);
         cmd_s.action = "rm";
         this.queue.append_val(cmd_s);
 
-
-
-
+        
+        
+        print("ADD: %s\n", cmd_d.vname);
         cmd_d.action = "add";
         this.queue.append_val(cmd_d);
 
@@ -652,6 +713,10 @@ public class GitMonitor : Monitor
         var cmd = new GitMonitorQueue(dest);
         cmd.action = "commit";
         cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
+        if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
+            cmd.message = cmd_d.vname;
+        }
+        
         this.queue.append_val(cmd);