Clone.bjs
[gitlive] / GitBranch.vala
index 0015aef..a30808b 100644 (file)
@@ -32,9 +32,22 @@ public class GitBranch : Object
        public bool active = false;
        public string lastrev = "";
        public string name = "";
+       public bool is_remote;
        public string remote = "";
        public string remoterev = "";
        
+       public string toString()
+       {
+               return 
+                       "active: " + (active ? "true" : "false") + "\n" +
+                       "is_remote: " + (is_remote ? "true" : "false") + "\n" +                 
+                       "lastrev: " + lastrev + "\n" +
+                       "name: " + name + "\n" +
+                       "remote: " + remote + "\n" +
+                       "remoterev: " + remoterev + "\n";
+       }
+       
+       
        public GitBranch(GitRepo repo)
        {
                this.repo = repo;
@@ -45,8 +58,11 @@ public class GitBranch : Object
                repo.branches = new Gee.HashMap<string,GitBranch>();
        
        var branches =  new Gee.HashMap<string,GitBranch>();
-       var bmap =  new Gee.HashMap<string,GitBranch>();        
-       
+               var local =  new Gee.HashMap<string,GitBranch>();
+               var remotes  =  new Gee.HashMap<string,GitBranch>();
+               
+      //        repo.git( { "fetch",  "-a" } ); == done async before...
+               
        string[] cmd = { "branch",   "--no-color", "--verbose", "--no-abbrev" , "-a"  };
         var res = repo.git( cmd );
         var lines = res.split("\n");
@@ -56,6 +72,11 @@ public class GitBranch : Object
                        continue;
                }
                GLib.debug("add branch %s", br.realName());
+               if (!br.is_remote) {
+                       local.set(br.name, br);
+               } else {
+                       remotes.set(br.remote, br);
+                       }
                 
                branches.set(br.realName(), br);
                if (br.active) {
@@ -63,29 +84,53 @@ public class GitBranch : Object
                }
         }
         
-         var bl = repo.git(
-                "for-each-ref'"
+         var bl = repo.git({
+                "for-each-ref",
                 "--format",   "%(refname:short):remotes/%(upstream:short)",
                 "refs/heads"
-               ).split("\n");
+               }).split("\n");
         
-        for(var line in bl) {
+        foreach(var line in bl) {
                if (line.length < 1) continue;
                
                var ar = line.split(":remotes/");
                var lname= ar[0];
-            var rname = "remotes/' + ar[1]";
+            var rname = "remotes/" + ar[1];
+            
                 //print(rname);
                 // we should always have a local version of it.
-                bmap[lname].remote = rname;
-                if (typeof(bmap[rname]) == 'undefined') {
-                    return;
-                }
-                bmap[lname].remoterev =  bmap[rname].lastrev;
+            if (branches.has_key(lname)) {     
+                   branches.get(lname).remote = rname;
+            }
+            
+            if (!branches.has_key(rname)) {
+                    continue;
+            }
+            branches.get(lname).remoterev =  branches.get(rname).lastrev;
                 // flag it for not adding..
                 
-                bmap[rname].name = lname;
-        repo.branches = branches;
+            branches.get(rname).name = lname;
+        }
+        foreach(var br in local.values) {
+               GLib.debug("BRANCH:\n%s\n" , br.toString());
+        }
+        
+        /*
+        this bit of the code tries to turn a local branch into a track of a remote one.
+        -- that's probably not a good idea.
+        var r_replace = new Regex("^remotes/");
+        var o_replace = new Regex("^origin/");
+        foreach(var r in remotes.values) {
+               if (r.name.length >0) {
+                       return;
+               }
+               var name = r_replace.replace(r.name, r.name.length,0, "");
+               name = o_replace.replace(name, name.length,0, "");
+               name = name.replace("/", ".");
+               
+        }
+        */
+        repo.branches = local;
         
     
        
@@ -103,9 +148,10 @@ public class GitBranch : Object
                if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
                        return false;
                }
-               
+               this.is_remote = false;
                this.lastrev = parts[1];
                if (parts[0].has_prefix("remotes/")) {
+                       this.is_remote  = true;
                        this.remote = parts[0];
                } else {
                        this.name = parts[0];
@@ -114,7 +160,7 @@ public class GitBranch : Object
        }
        public string  realName()
        {
-               return this.name == "" ? this.remote : this.name;
+               return this.is_remote ? this.remote : this.name;
        }
        
        public void create()