2 represent a git branch..
5 Does repo require branching? - flag in config?
6 ** list of repo's ?? with ability to turn on/off
8 Start editing without branch?
9 -> show prompt to start branch
10 -> flag a ticket? optional ??
12 Once editing branch...
13 -> merge with squash / ticket...
14 ** show list of repo's with 'working' branches?
15 ** select some/all to merge with a issue fix..
17 ?? closing ticket in system ??
20 need to push all? / fetch all?
28 public class GitBranch : Object
32 public bool active = false;
33 public string lastrev = "";
34 public string name = ""; // human friendly name...
35 public bool is_remote;
36 public string remote = "";
37 public string remoterev = "";
38 public string age = "";
40 public int behind = 0;
44 public string toString()
47 "active: " + (active ? "true" : "false") + "\n" +
48 "is_remote: " + (is_remote ? "true" : "false") + "\n" +
49 "lastrev: " + lastrev + "\n" +
50 "name: " + name + "\n" +
51 "remote: " + remote + "\n" +
52 "remoterev: " + remoterev + "\n" +
53 "age: " + age + "\n" ;
57 public GitBranch(GitRepo repo)
62 public Ggit.Tree getTree()
64 var br = this.repo.repo.lookup_branch(this.name,Ggit.BranchType.LOCAL);
65 return this.repo.repo.lookup_commit(br.get_target()).get_tree();
69 public static void loadBranches(GitRepo repo)
71 repo.branches = new Gee.HashMap<string,GitBranch>();
73 //var branches = new Gee.HashMap<string,GitBranch>();
74 //var local = new Gee.HashMap<string,GitBranch>();
75 var remotes = new Gee.HashMap<string,Ggit.OId>();
76 var remotes_used = new Gee.HashMap<string,bool>();
80 var rem = repo.repo.lookup_remote("origin");
81 var cb = new GitCallbacks(repo);
83 rem.connect(Ggit.Direction.FETCH, cb, null, null);
84 var remote_heads = rem.list();
85 foreach(var rh in remote_heads) {
86 var rn = rh.get_name();
87 if (!rn.has_prefix("refs/heads/")) {
90 remotes.set(rn.substring(11), rh.get_oid());
91 remotes_used.set(rn.substring(11), false);
97 GLib.debug("Failed to fetch remotes continuing on ");
102 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
107 var br = new GitBranch(repo);
108 var gbr = r.get() as Ggit.Branch;
109 if (!gbr.is_branch()) {
113 br.active = gbr.is_head();
114 br.name = gbr.get_name();
115 br.lastrev = gbr.get_target().to_string();
119 THIS IS DISABLED, and get_upstream has a major memor leak.
121 rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
123 GLib.debug("Skip branch = got error");
127 repo.branches.set(gbr.get_name(), br);
128 if (rname.has_prefix("refs/remotes/origin/")) {
129 rname = rname.substring(20);
130 if (remotes.has_key(rname)) {
132 br.remoterev = remotes.get(rname).to_string();
133 remotes_used.set(rname,true);
134 size_t ahead, behind;
138 repo.repo.get_ahead_behind(
144 br.ahead = (int)ahead;
145 br.behind = (int) behind;
148 GLib.debug("we probably need to fetch... %s", repo.name);
155 // behind or infront..
158 GLib.debug("repo: %s currentBranch = %s", repo.name, br.name);
159 repo._currentBranch = br;
164 // find unused remotes.. and track them...
165 foreach(var rn in remotes_used.keys) {
166 if (remotes_used.get(rn)) {
169 if (repo.branches.has_key(rn)) {
170 GLib.debug("skip tracking branch - same name exists?");
173 // not clear how to do this yet...
175 repo.git( { "branch" ,"--track" , rn, "origin/" + rn} );
177 continue; // allow failure?
179 var br = new GitBranch(repo);
181 br.lastrev = ""; // it's behind
182 br.remoterev = remotes.get(rn).to_string();
186 // behind/ahead == 0...
187 repo.branches.set(rn, br);
191 if (repo._currentBranch == null) {
192 GLib.error("could not find active Branch for %s", repo.name);
198 // repo.git( { "fetch", "-a" } ); == done async before...
200 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
201 var res = repo.git( cmd );
202 var lines = res.split("\n");
203 for (var i = 0; i < lines.length ; i++) {
204 var br = new GitBranch(repo);
205 if (!br.parseBranchListItem(lines[i])) {
208 GLib.debug("add branch %s", br.realName());
210 local.set(br.name, br);
212 remotes.set(br.remote, br);
215 branches.set(br.realName(), br);
217 repo.currentBranch = br;
223 "--format", "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
227 foreach(var line in bl) {
228 if (line.length < 1) continue;
230 var ar = line.split(":remotes/");
232 var rname = "remotes/" + ar[1];
236 // we should always have a local version of it.
237 if (branches.has_key(lname)) {
238 branches.get(lname).remote = rname;
239 branches.get(lname).age = ar[2];
242 if (!branches.has_key(rname) || !branches.has_key(lname) ) {
245 branches.get(lname).remoterev = branches.get(rname).lastrev;
246 // flag it for not adding..
248 branches.get(rname).name = lname;
250 foreach(var br in branches.values) {
251 GLib.debug("BRANCH:\n%s\n" , br.toString());
252 if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) {
253 GLib.debug("SKIP - track exists");
256 var newname = br.remote.replace("remotes/origin/","");
257 if (branches.has_key(newname)) {
258 GLib.debug("SKIP - have branch already");
263 repo.git( { "branch" ,"--track" , newname, "origin/" + newname} );
266 local.set(br.name, br);
270 this bit of the code tries to turn a local branch into a track of a remote one.
271 -- that's probably not a good idea.
272 var r_replace = new Regex("^remotes/");
273 var o_replace = new Regex("^origin/");
274 foreach(var r in remotes.values) {
275 if (r.name.length >0) {
278 var name = r_replace.replace(r.name, r.name.length,0, "");
279 name = o_replace.replace(name, name.length,0, "");
280 name = name.replace("/", ".");
284 repo.branches = local;
291 public bool parseBranchListItem(string str)
293 if (str.length < 1) {
296 this.active = str[0] == '*';
298 var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip());
299 if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
302 this.is_remote = false;
303 this.lastrev = parts[1];
304 if (parts[0].has_prefix("remotes/")) {
305 this.is_remote = true;
306 this.remote = parts[0];
308 this.name = parts[0];
312 public string realName()
314 return this.is_remote ? this.remote : this.name;
317 // public void create() { }