README.txt
[gitlive] / GitMonitor.js
index 0e084c8..df3565a 100644 (file)
@@ -39,39 +39,87 @@ var GitMonitor = new Monitor({
         
     },
     
-    
+    /**
+     * Start the monitoring
+     * and run the queue every 500 milliseconds..
+     *
+     */
     start: function() {
+        imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_REFRESH );
         var _this = this;
         this.lastAdd = new Date();
         
-        this.top.forEach(this.monitor, this);
-         
         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
+            _this.top.forEach(_this.monitor, _this);
+            imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PLAY );
+            
+            try { 
+                var notification = new Notify.Notification({
+                    summary: "Git Live",
+                    body : this.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
+                    timeout : 5
+                });
+        
+                notification.set_timeout(5);
+                notification.show();
+            } catch(e) {
+                print(e.toString());
+            }
+
+        });
+        
+        GLib.timeout_add(GLib.PRIORITY_LOW, 1000, function() {
             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
             if (!_this.queue.length || _this.queueRunning) {
                 return 1;
             }
-            var last = Math.floor(((new Date()) - this.lastAdd) / 100);
-            if (last < 4) { // wait 1/2 a seconnd before running.
+            var last = Math.floor(((new Date()) - _this.lastAdd) / 100);
+            
+            //print("LAST RUN?" + last);
+            
+            if (last < 5) { // wait 1/2 a seconnd before running.
                 return 1;
             }
+            //_this.lastAdd = new Date();
+            //return 1;
+        
             _this.runQueue();
             return 1;
         },null,null);
         
-        try { 
-            var notification = new Notify.Notification({
-                summary: "Git Live",
-                body : this.gitlive + "\nMonitoring " + this.monitors.length + " Directories",
-                timeout : 5
-            });
+      
+    },
     
-            notification.set_timeout(5);
-            notification.show();
-        } catch(e) {
-            print(e.toString());
+    
+    stop: function() {
+        imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
+        Monitor.prototype.stop.call(this);
+    },
+    
+    
+    monitor : function(path, fn, depth)
+    {
+        depth = typeof(depth) == 'number'  ? depth *1 : 0;
+        
+         
+        // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
+        if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
+            return;
         }
-
+        
+        if (depth == 1) {
+            var repo = imports.Scm.Repo.Repo.get(path);
+            if (!repo || !repo.autocommit()) {
+                return;
+            } 
+        }
+        
+        
+        // check if the repo is to be monitored.
+        //print("PATH : " + path);
+        
+        
+        Monitor.prototype.monitor.call(this, path,fn, depth);
     },
     
     /**
@@ -106,7 +154,7 @@ var GitMonitor = new Monitor({
         // pull and group.
         
         //print(JSON.stringify(cmds));
-        
+        this.paused = true;
         cmds.forEach(function(cmd) {
             var gitpath = cmd.shift(); 
             if (typeof(repo_list[gitpath]) == 'undefined') {
@@ -116,17 +164,20 @@ var GitMonitor = new Monitor({
             }
             repo_list[gitpath].cmds.push(cmd);
         });
-        
+        this.paused = false;
         // build add, remove and commit message list..
         
          
+         
         for (var gitpath in repo_list) {
             var repo = repo_list[gitpath];
             var add_files = [];
             var remove_files = [];
             var messages = [];
+            //print(JSON.stringify(repo.cmds,null,4));
+            
             repo.cmds.forEach(function(cmd) {
-                //print(JSON.stringify(cmd));
+                
                 var name = cmd.shift();
                 var arg = cmd.shift();
                 
@@ -138,15 +189,22 @@ var GitMonitor = new Monitor({
                         }
                         
                         // if file does not exist.. s,ip
-                        if (!GLib.file_test(arg, GLib.FileTest.EXISTS)) {
-                            break;
-                        }
+                        
+                        //if (!GLib.file_test(arg, GLib.FileTest.EXISTS)) {
+                             
+                        //    break;
+                         // }
         
                         
                         add_files.push(arg);
                         break;
                     
                     case 'rm':
+                        
+                        if (add_files.indexOf(arg) > -1) {
+                            break;
+                        }
+                        
                         // if file exists, do not try and delete it.
                         if (GLib.file_test(arg, GLib.FileTest.EXISTS)) {
                             break;
@@ -157,21 +215,34 @@ var GitMonitor = new Monitor({
                     
                     case 'commit' :
                         
-                        
-                        messages.push(arg.message);
+                        if (messages.indexOf(arg.message) < 0) { 
+                            messages.push(arg.message);
+                        }
                         break;    
                 } 
             });
+            
             //repo.debug = 1;
             // these can fail... at present... as we wildcard stuff.
-            repo.add(add_files);
+            print("ADD : "  + JSON.stringify(add_files));
             
             // make sure added files do not get removed..
             remove_files  = remove_files.filter(function(v) {
                 return add_files.indexOf(v) < 0;
             });
+            print("REMOVE : "  + JSON.stringify(remove_files));
+            
+            
+            // make sure monitoring is paused so it does not recursively pick up
+            // deletions
+            
+            // -- DO STUFF..
+            
+            repo.add(add_files);
             
             repo.remove(remove_files);
+            this.paused = false;
+            
             
             try { 
                 success.push(repo.commit({
@@ -229,10 +300,13 @@ var GitMonitor = new Monitor({
         if (this.paused) {
             return true;
         }
+        
+        
         // vim.. what a seriously brain dead program..
         if (f.name == '4913') {
-            retun true;
-        } 
+            return true;
+        }
+        
         if (f.name[0] == '.') {
             // except!
             if (f.name == '.htaccess') {
@@ -244,6 +318,9 @@ var GitMonitor = new Monitor({
         if (f.name.match(/~$/)) {
             return true;
         }
+        if (f.name.match(/^nbproject/)) {
+            return true;
+        }
         // ignore anything in top level!!!!
         if (!f.vpath.length) {
             return true;
@@ -286,6 +363,7 @@ var GitMonitor = new Monitor({
      */
     onChangesDoneHint : function(src) 
     { 
+        this.lastAdd = new Date();
         this.parsePath(src);
         if (this.shouldIgnore(src)) {
             return;
@@ -295,7 +373,7 @@ var GitMonitor = new Monitor({
         var add_it = false;
         if (typeof(this.just_created[src.path]) !='undefined') {
             delete this.just_created[src.path];
-            this.lastAdd = new Date();
+            
             this.queue.push( 
                 [ src.gitpath,  'add', src.vpath ],
                 [ src.gitpath,  'commit',    { message: src.vpath} ] 
@@ -304,7 +382,7 @@ var GitMonitor = new Monitor({
          
             return;
         }
-        this.lastAdd = new Date();
+        
         this.queue.push( 
             [ src.gitpath,  'add', src.vpath ],
             [ src.gitpath,  'commit',  {  message: src.vpath} ]
@@ -314,6 +392,7 @@ var GitMonitor = new Monitor({
     },
     onDeleted : function(src) 
     { 
+        this.lastAdd = new Date();
         this.parsePath(src);
         if (this.shouldIgnore(src)) {
             return;
@@ -321,7 +400,7 @@ var GitMonitor = new Monitor({
         // should check if monitor needs removing..
         // it should also check if it was a directory.. - so we dont have to commit all..
         
-        this.lastAdd = new Date();
+        
         this.queue.push( 
             [ src.gitpath, 'rm' , src.vpath ],
             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
@@ -332,6 +411,7 @@ var GitMonitor = new Monitor({
     },
     onCreated : function(src) 
     { 
+        this.lastAdd = new Date();
         this.parsePath(src);
         if (this.shouldIgnore(src)) {
             return;
@@ -343,7 +423,7 @@ var GitMonitor = new Monitor({
         }
         // director has bee created
         this.monitor(src.path);
-        this.lastAdd = new Date();
+        
         this.queue.push( 
             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
@@ -352,12 +432,14 @@ var GitMonitor = new Monitor({
         
         
     },
-    onAttributeChanged : function(src) { 
+    onAttributeChanged : function(src)
+    { 
+        this.lastAdd = new Date();
         this.parsePath(src);
         if (this.shouldIgnore(src)) {
             return;
         }
-        this.lastAdd = new Date();
+        
         this.queue.push(
                         
             //[ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
@@ -370,6 +452,7 @@ var GitMonitor = new Monitor({
     
     onMoved : function(src,dest)
     { 
+        this.lastAdd = new Date();
         this.parsePath(src);
         this.parsePath(dest);
         
@@ -387,7 +470,7 @@ var GitMonitor = new Monitor({
         if (this.shouldIgnore(dest)) {
             return;
         }
-        this.lastAdd = new Date();
+        
         this.queue.push( 
            // [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
              [ src.gitpath, 'add',    dest.vpath ],