/** represent a git branch.. Branching / Gitlive: Does repo require branching? - flag in config? ** list of repo's ?? with ability to turn on/off Start editing without branch? -> show prompt to start branch -> flag a ticket? optional ?? Once editing branch... -> merge with squash / ticket... ** show list of repo's with 'working' branches? ** select some/all to merge with a issue fix.. ?? closing ticket in system ?? -> done by the ui? need to push all? / fetch all? list of repo's */ public class GitBranch : Object { public GitRepo repo; public bool active = false; public string lastrev = ""; 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() { 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" + "age: " + age + "\n" ; } public GitBranch(GitRepo repo) { 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 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" }; var res = repo.git( cmd ); var lines = res.split("\n"); for (var i = 0; i < lines.length ; i++) { var br = new GitBranch(repo); if (!br.parseBranchListItem(lines[i])) { 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) { repo.currentBranch = br; } } var bl = repo.git({ "for-each-ref", "--format", "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)", "refs/heads" }).split("\n"); foreach(var line in bl) { if (line.length < 1) continue; var ar = line.split(":remotes/"); 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) ) { continue; } branches.get(lname).remoterev = branches.get(rname).lastrev; // flag it for not adding.. branches.get(rname).name = lname; } foreach(var br in branches.values) { GLib.debug("BRANCH:\n%s\n" , br.toString()); if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) { GLib.debug("SKIP - track exists"); continue; } var newname = br.remote.replace("remotes/origin/",""); if (branches.has_key(newname)) { GLib.debug("SKIP - have branch already"); continue; } repo.git( { "branch" ,"--track" , newname, "origin/" + newname} ); // 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. 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; */ } public bool parseBranchListItem(string str) { if (str.length < 1) { return false; } this.active = str[0] == '*'; var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip()); 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]; } return true; } public string realName() { return this.is_remote ? this.remote : this.name; } // public void create() { } }