sync
[gitlive] / GitRepo.vala
index 2df125a..6374b4f 100644 (file)
@@ -12,7 +12,7 @@ static GitRepo  _GitRepo;
 public class GitRepo : Object
 {
      
-    public Array<GitMonitorQueue> cmds;
+    public Gee.ArrayList<GitMonitorQueue> cmds;
 
     public string name;
     public string gitdir;
@@ -171,7 +171,7 @@ public class GitRepo : Object
     
     public bool is_wip_branch()
     {
-       return this.currentBranch.name.has_prefix("wip_")
+       return this.currentBranch.name.has_prefix("wip_");
                
     }
     
@@ -225,9 +225,118 @@ public class GitRepo : Object
                return ret;
        
     }
-    RooTicket? ticket = null;
+     public static void doMerges(string action, string ticket_id, string commit_message)
+    {
+       GitMonitor.gitmonitor.stop();
+       
+       var commitrevs = "";
+       var sucess = true;
+       foreach(var  repo in GitRepo.singleton().cache.values) {
+               if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
+                       var res = repo.doMerge(action,ticket_id, commit_message);
+                       if (!res) {
+                               sucess = false;
+                               continue;
+                       }
+                       commitrevs += commitrevs.length > 0 ? " " : "";
+                       commitrevs += repo.currentBranch.lastrev;
+               }
+       }
+       if (sucess && action == "CLOSE") {
+               RooTicket.singleton().getById(ticket_id).close(commitrevs);
+       }
+       GitMonitor.gitmonitor.start();
+    }
+    
+
+
+
+    public bool doMerge(string action, string ticket_id, string commit_message)
+    {
+       // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
+       // also need to decide which branch we will merge into?
+       var ret = "";
+       if (action == "CLOSE" || action == "LEAVE") {
+                       
+                       try {
+                               var oldbranch = this.currentBranch.name;
+                               this.setActiveTicket(null, "master");
+                       string [] cmd = { "merge",   "--squash",  oldbranch };
+                       this.git( cmd );
+                        cmd = { "commit",   "--m",  commit_message };
+                       this.git( cmd );
+                       this.loadBranches(); // updates lastrev..
+                       
+                       var notification = new Notify.Notification(
+                                       "Merged branch %s to master".printf(oldbranch),
+                                       "",
+                                        "dialog-information"
+                                       
+                               );
+       
+                               notification.set_timeout(5);
+                               notification.show();   
+                       
+                       // close ticket..
+                       return true; 
+                       
+                   } catch (Error e) {
+
+                       GitMonitor.gitmonitor.pauseError(e.message);
+                       return false;
+                   }
+                   // error~?? -- show the error dialog...
+                   return false;
+       }
+       if (action == "MASTER") {
+               // merge master into ours..
+                       try {
+                       string[] cmd = { "merge",  "master" };
+                       this.git( cmd );
+                       var notification = new Notify.Notification(
+                                       "Merged code from master to %s".printf(this.currentBranch.name),
+                                       "",
+                                        "dialog-information"
+                                       
+                               );
+                               notification.set_timeout(5);
+                               notification.show();   
+                      
+                       return true;
+                       } catch (Error e) {
+                       GitMonitor.gitmonitor.pauseError(e.message);
+                       return false;
+                   }
+           }
+       if (action == "EXIT") {
+                       try {
+                       var oldbranch  = this.currentBranch.name;
+                         this.setActiveTicket(null, "master");
+                       this.loadBranches();
+                       var notification = new Notify.Notification(
+                                       "Left branch %s".printf(oldbranch),
+                                       "",
+                                        "dialog-information"
+                                       
+                               );
+                               notification.set_timeout(5);
+                               notification.show();   
+                       
+                       return true;
+                   } catch (Error e) {
+                       GitMonitor.gitmonitor.pauseError(e.message);
+
+                       return false;                   
+                   }
+                   // error~?? -- show the error dialog...
+
+       }
+       return false;
+    }
     
-    public void setActiveTicket(RooTicket ticket, string branchname)
+    
+   public void setActiveTicket(RooTicket ticket, string branchname)
     {
        this.createBranchNamed(branchname);
        FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
@@ -236,8 +345,13 @@ public class GitRepo : Object
     
     public void createBranchNamed(string branchname)
     {
-        string[] cmd = { "checkout", "-b" , branchname  };
-        this.git(cmd);
+         if (this.branches.has_key(branchname)) {
+                string[] cmd = { "checkout", branchname  };
+                this.git(cmd);         
+       } else {
+                string[] cmd = { "checkout", "-b" , branchname  };
+                this.git(cmd);
+        }
         this.loadBranches(); // update branch list...
         GitMonitor.gitmonitor.runQueue(); // commit any outstanding...
     }
@@ -249,14 +363,14 @@ public class GitRepo : Object
      *
      * @argument {Array} files the files to add.
      */
-    public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
+    public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
     {
         // should really find out if these are untracked files each..
         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
         // not sure if that is how git works.. but just be certian.
         var ret = "";
-        for (var i = 0; i < files.length;i++) {
-            var f = files.index(i).vname;
+        for (var i = 0; i < files.size;i++) {
+            var f = files.get(i).vname;
             try {
                 string[] cmd = { "add",    f  };
                 this.git( cmd );
@@ -333,7 +447,7 @@ public class GitRepo : Object
      * 
      */
      
-    public string commit ( string message, Array<GitMonitorQueue> files  ) throws Error, SpawnError
+    public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files  ) throws Error, SpawnError
     {
         
 
@@ -357,8 +471,8 @@ public class GitRepo : Object
         */
         string[] args = { "commit", "-m" };
         args +=  (message.length > 0  ? message : "Changed" );
-        for (var i = 0; i< files.length ; i++ ) {
-            args += files.index(i).vname; // full path?
+        for (var i = 0; i< files.size ; i++ ) {
+            args += files.get(i).vname; // full path?
         }
          
         return this.git(args);