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)
65 public static void loadBranches(GitRepo repo)
67 repo.branches = new Gee.HashMap<string,GitBranch>();
69 //var branches = new Gee.HashMap<string,GitBranch>();
70 //var local = new Gee.HashMap<string,GitBranch>();
71 var remotes = new Gee.HashMap<string,Ggit.OId>();
72 var remotes_used = new Gee.HashMap<string,bool>();
76 var rem = repo.repo.lookup_remote("origin");
77 var cb = new GitCallbacks(repo);
78 rem.connect(Ggit.Direction.FETCH, cb, null, null);
79 var remote_heads = rem.list();
80 foreach(var rh in remote_heads) {
81 var rn = rh.get_name();
82 if (!rn.has_prefix("refs/heads/")) {
85 remotes.set(rn.substring(11), rh.get_oid());
86 remotes_used.set(rn.substring(11), false);
91 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
96 var br = new GitBranch(repo);
97 var gbr = r.get() as Ggit.Branch;
98 if (!gbr.is_branch()) {
102 br.active = gbr.is_head();
103 br.name = gbr.get_name();
104 br.lastrev = gbr.get_target().to_string();
107 rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
109 GLib.debug("Skip branch = got error");
112 repo.branches.set(gbr.get_name(), br);
113 if (rname.has_prefix("refs/remotes/origin/")) {
114 rname = rname.substring(20);
115 if (remotes.has_key(rname)) {
117 br.remoterev = remotes.get(rname).to_string();
118 remotes_used.set(rname,true);
119 size_t ahead, behind;
120 repo.repo.get_ahead_behind(
126 br.ahead = (int)ahead;
127 br.behind = (int) behind;
133 // behind or infront..
136 GLib.debug("repo: %s currentBranch = %s", repo.name, br.name);
137 repo.currentBranch = br;
142 // find unused remotes.. and track them...
143 foreach(var rn in remotes_used.keys) {
144 if (remotes_used.get(rn)) {
147 if (repo.branches.has_key(rn)) {
148 GLib.debug("skip tracking branch - same name exists?");
151 // not clear how to do this yet...
153 repo.git( { "branch" ,"--track" , rn, "origin/" + rn} );
155 continue; // allow failure?
157 var br = new GitBranch(repo);
159 br.lastrev = ""; // it's behind
160 br.remoterev = remotes.get(rn).to_string();
164 // behind/ahead == 0...
165 repo.branches.set(rn, br);
168 if (repo.currentBranch == null) {
169 GLib.error("could not find active Branch for %s", repo.name);
175 // repo.git( { "fetch", "-a" } ); == done async before...
177 string[] cmd = { "branch", "--no-color", "--verbose", "--no-abbrev" , "-a" };
178 var res = repo.git( cmd );
179 var lines = res.split("\n");
180 for (var i = 0; i < lines.length ; i++) {
181 var br = new GitBranch(repo);
182 if (!br.parseBranchListItem(lines[i])) {
185 GLib.debug("add branch %s", br.realName());
187 local.set(br.name, br);
189 remotes.set(br.remote, br);
192 branches.set(br.realName(), br);
194 repo.currentBranch = br;
200 "--format", "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
204 foreach(var line in bl) {
205 if (line.length < 1) continue;
207 var ar = line.split(":remotes/");
209 var rname = "remotes/" + ar[1];
213 // we should always have a local version of it.
214 if (branches.has_key(lname)) {
215 branches.get(lname).remote = rname;
216 branches.get(lname).age = ar[2];
219 if (!branches.has_key(rname) || !branches.has_key(lname) ) {
222 branches.get(lname).remoterev = branches.get(rname).lastrev;
223 // flag it for not adding..
225 branches.get(rname).name = lname;
227 foreach(var br in branches.values) {
228 GLib.debug("BRANCH:\n%s\n" , br.toString());
229 if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) {
230 GLib.debug("SKIP - track exists");
233 var newname = br.remote.replace("remotes/origin/","");
234 if (branches.has_key(newname)) {
235 GLib.debug("SKIP - have branch already");
240 repo.git( { "branch" ,"--track" , newname, "origin/" + newname} );
243 local.set(br.name, br);
247 this bit of the code tries to turn a local branch into a track of a remote one.
248 -- that's probably not a good idea.
249 var r_replace = new Regex("^remotes/");
250 var o_replace = new Regex("^origin/");
251 foreach(var r in remotes.values) {
252 if (r.name.length >0) {
255 var name = r_replace.replace(r.name, r.name.length,0, "");
256 name = o_replace.replace(name, name.length,0, "");
257 name = name.replace("/", ".");
261 repo.branches = local;
268 public bool parseBranchListItem(string str)
270 if (str.length < 1) {
273 this.active = str[0] == '*';
275 var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip());
276 if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
279 this.is_remote = false;
280 this.lastrev = parts[1];
281 if (parts[0].has_prefix("remotes/")) {
282 this.is_remote = true;
283 this.remote = parts[0];
285 this.name = parts[0];
289 public string realName()
291 return this.is_remote ? this.remote : this.name;
294 // public void create() { }