X-Git-Url: http://git.roojs.org/?p=gitlive;a=blobdiff_plain;f=GitBranch.vala;h=6a2dfa1357ea5b4486228f44eed72d40a0d485ac;hp=84a833129db97fb0d3722dedf5114407cf1d396f;hb=cf448ce21966e7b9fe9ca9e8894a4d53871aead4;hpb=d33736b91e5bdee2a751dce3d4199d22244ca786 diff --git a/GitBranch.vala b/GitBranch.vala index 84a83312..6a2dfa13 100644 --- a/GitBranch.vala +++ b/GitBranch.vala @@ -31,10 +31,15 @@ public class GitBranch : Object public bool active = false; public string lastrev = ""; - public string name = ""; + public string name = ""; // human friendly name... public bool is_remote; public string remote = ""; public string remoterev = ""; + public string age = ""; + public int ahead = 0; + public int behind = 0; + + public string toString() { @@ -44,7 +49,8 @@ public class GitBranch : Object "lastrev: " + lastrev + "\n" + "name: " + name + "\n" + "remote: " + remote + "\n" + - "remoterev: " + remoterev + "\n"; + "remoterev: " + remoterev + "\n" + + "age: " + age + "\n" ; } @@ -52,15 +58,132 @@ public class GitBranch : Object { this.repo = repo; } + + public Ggit.Tree getTree() + { + var br = this.repo.repo.lookup_branch(this.name,Ggit.BranchType.LOCAL); + return this.repo.repo.lookup_commit(br.get_target()).get_tree(); + } + public static void loadBranches(GitRepo repo) { repo.branches = new Gee.HashMap(); - var branches = new Gee.HashMap(); - var local = new Gee.HashMap(); - var remotes = new Gee.HashMap(); - + //var branches = new Gee.HashMap(); + //var local = new Gee.HashMap(); + var remotes = new Gee.HashMap(); + var remotes_used = new Gee.HashMap(); + + + + var rem = repo.repo.lookup_remote("origin"); + var cb = new GitCallbacks(repo); + rem.connect(Ggit.Direction.FETCH, cb, null, null); + var remote_heads = rem.list(); + foreach(var rh in remote_heads) { + var rn = rh.get_name(); + if (!rn.has_prefix("refs/heads/")) { + continue; + } + remotes.set(rn.substring(11), rh.get_oid()); + remotes_used.set(rn.substring(11), false); + + } + + + var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL); + while (r.next()) { + + + + var br = new GitBranch(repo); + var gbr = r.get() as Ggit.Branch; + if (!gbr.is_branch()) { + continue; + } + + br.active = gbr.is_head(); + br.name = gbr.get_name(); + br.lastrev = gbr.get_target().to_string(); + string rname ; + try { + rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : ""; + } catch(Error e) { + GLib.debug("Skip branch = got error"); + continue; + } + repo.branches.set(gbr.get_name(), br); + if (rname.has_prefix("refs/remotes/origin/")) { + rname = rname.substring(20); + if (remotes.has_key(rname)) { + br.remote = rname; + br.remoterev = remotes.get(rname).to_string(); + remotes_used.set(rname,true); + size_t ahead, behind; + br.behind = 1; + + try { + repo.repo.get_ahead_behind( + gbr.get_target(), + remotes.get(rname), + out ahead, + out behind + ); + br.ahead = (int)ahead; + br.behind = (int) behind; + + } catch(Error e) { + GLib.debug("we probably need to fetch... %s", repo.name); + } + + } + + + // age? + // behind or infront.. + } + if (br.active) { + GLib.debug("repo: %s currentBranch = %s", repo.name, br.name); + repo._currentBranch = br; + } + + } + + // find unused remotes.. and track them... + foreach(var rn in remotes_used.keys) { + if (remotes_used.get(rn)) { + continue; + } + if (repo.branches.has_key(rn)) { + GLib.debug("skip tracking branch - same name exists?"); + continue; + } + // not clear how to do this yet... + try { + repo.git( { "branch" ,"--track" , rn, "origin/" + rn} ); + } catch (Error e) { + continue; // allow failure? + } + var br = new GitBranch(repo); + br.name = rn; + br.lastrev = ""; // it's behind + br.remoterev = remotes.get(rn).to_string(); + br.remote = rn; + br.ahead = 0; + br.behind = 1; + // behind/ahead == 0... + repo.branches.set(rn, br); + + } + + if (repo._currentBranch == null) { + GLib.error("could not find active Branch for %s", repo.name); + } + + + + /* // repo.git( { "fetch", "-a" } ); == done async before... string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" }; @@ -86,7 +209,7 @@ public class GitBranch : Object var bl = repo.git({ "for-each-ref", - "--format", "%(refname:short):remotes/%(upstream:short)", + "--format", "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)", "refs/heads" }).split("\n"); @@ -97,10 +220,12 @@ public class GitBranch : Object var lname= ar[0]; var rname = "remotes/" + ar[1]; + //print(rname); // we should always have a local version of it. if (branches.has_key(lname)) { branches.get(lname).remote = rname; + branches.get(lname).age = ar[2]; } if (!branches.has_key(rname) || !branches.has_key(lname) ) { @@ -129,7 +254,7 @@ public class GitBranch : Object br.name = newname; local.set(br.name, br); } - + */ /* 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. @@ -144,9 +269,9 @@ public class GitBranch : Object name = name.replace("/", "."); } - */ + repo.branches = local; - + */ } @@ -178,10 +303,7 @@ public class GitBranch : Object return this.is_remote ? this.remote : this.name; } - public void create() - { - - } +// public void create() { } }