Uncommited changes synced
[gitlive] / GitBranch.vala
1 /**
2 represent a git branch..
3 Branching / Gitlive:
4
5 Does repo require branching? - flag in config?
6 ** list of repo's ?? with ability to turn on/off
7
8 Start editing without branch?
9 -> show prompt to start branch
10 -> flag a ticket? optional ??
11
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..
16
17 ?? closing ticket in system ??
18 -> done by the ui?
19
20 need to push all? / fetch all?
21
22
23 list of repo's
24
25
26 */
27
28 public class GitBranch : Object
29 {
30         public GitRepo repo;
31         
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 = "";
39     public int ahead = 0;
40     public int behind = 0;    
41         
42      
43         
44         public string toString()
45         {
46                 return 
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" ;
54         }
55         
56         
57         public GitBranch(GitRepo repo)
58         {
59                 this.repo = repo;
60         }
61         
62         
63         
64
65         public static  void loadBranches(GitRepo repo)
66         {
67                 repo.branches = new Gee.HashMap<string,GitBranch>();
68         
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>();
73          
74   
75                 
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/")) {
83                                 continue;
84                         }
85                         remotes.set(rn.substring(11), rh.get_oid());
86                         remotes_used.set(rn.substring(11), false);
87
88           } 
89                 
90                  
91                 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
92                 while (r.next()) {
93                 
94
95                 
96                         var br = new GitBranch(repo);
97                         var gbr = r.get() as Ggit.Branch;
98                         if (!gbr.is_branch()) {
99                                 continue;
100                         }
101                         
102                         br.active = gbr.is_head();
103                         br.name = gbr.get_name();
104                         br.lastrev = gbr.get_target().to_string();
105                         string rname ;
106                         try {
107                                 rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
108                         } catch(Error e) {
109                                 GLib.debug("Skip branch = got error");
110                                 continue;
111                         }
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)) {
116                                         br.remote = rname;
117                                         br.remoterev = remotes.get(rname).to_string();
118                                         remotes_used.set(rname,true);
119                                         size_t ahead, behind;
120                                         br.behind = 1;
121                                         
122                                         try {
123                                                 repo.repo.get_ahead_behind(
124                                                         gbr.get_target(),
125                                                         remotes.get(rname),
126                                                         out ahead,
127                                                         out behind
128                                                 );
129                                                 br.ahead = (int)ahead;
130                                                 br.behind = (int) behind;
131
132                                         } catch(Error e) {
133                                                 GLib.debug("we probably need to fetch... %s", repo.name);
134                                         }
135                                         
136                                 }
137                                  
138                                 
139                                 // age?
140                                 // behind or infront..
141                         }
142                         if (br.active) {
143                                 GLib.debug("repo: %s currentBranch = %s", repo.name, br.name);
144                                 repo._currentBranch = br;
145                         }
146                         
147                 }
148                 
149                 // find unused remotes.. and track them...
150                 foreach(var rn in remotes_used.keys) {
151                         if (remotes_used.get(rn)) {
152                                 continue;
153                         }
154                         if (repo.branches.has_key(rn)) {
155                                 GLib.debug("skip tracking branch - same name exists?");
156                                 continue;
157                         }
158                         // not clear how to do this yet...
159                         try {
160                                 repo.git(  { "branch" ,"--track" , rn,  "origin/" + rn} ); 
161                         } catch (Error e) {
162                                 continue; // allow failure?
163                         }
164                         var br = new GitBranch(repo);
165                         br.name = rn;
166                         br.lastrev = ""; // it's behind
167                         br.remoterev  =  remotes.get(rn).to_string();
168                         br.remote = rn;
169                         br.ahead = 0;
170                         br.behind = 1;
171                         // behind/ahead == 0...
172                         repo.branches.set(rn, br);
173                         
174                 }
175
176                 if (repo._currentBranch == null) {
177                         GLib.error("could not find active Branch for %s", repo.name);
178                 }
179                 
180                          
181                 
182         /*
183       //         repo.git( { "fetch",  "-a" } ); == done async before...
184         
185         string[] cmd = { "branch",   "--no-color", "--verbose", "--no-abbrev" , "-a"  };
186         var res = repo.git( cmd );
187         var lines = res.split("\n");
188         for (var i = 0; i < lines.length ; i++) {
189                 var br = new GitBranch(repo);
190                 if (!br.parseBranchListItem(lines[i])) {
191                         continue;
192                 }
193                 GLib.debug("add branch %s", br.realName());
194                 if (!br.is_remote) {
195                         local.set(br.name, br);
196                 } else {
197                         remotes.set(br.remote, br);
198                         }
199                  
200                 branches.set(br.realName(), br);
201                 if (br.active) {
202                         repo.currentBranch = br;
203                 }
204         }
205         
206          var bl = repo.git({
207                 "for-each-ref",
208                 "--format",   "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
209                 "refs/heads"
210                 }).split("\n");
211         
212         foreach(var line in bl) {
213                 if (line.length < 1) continue;
214                 
215                 var ar = line.split(":remotes/");
216                 var lname= ar[0];
217             var rname = "remotes/" + ar[1];
218             
219             
220                 //print(rname);
221                 // we should always have a local version of it.
222             if (branches.has_key(lname)) {      
223                     branches.get(lname).remote = rname;
224                     branches.get(lname).age = ar[2];
225             }
226             
227             if (!branches.has_key(rname) || !branches.has_key(lname)  ) {
228                     continue;
229             }
230             branches.get(lname).remoterev =  branches.get(rname).lastrev;
231                 // flag it for not adding..
232                 
233             branches.get(rname).name = lname;
234         }
235         foreach(var br in branches.values) {
236                 GLib.debug("BRANCH:\n%s\n" , br.toString());
237                 if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) {
238                         GLib.debug("SKIP - track exists");
239                         continue;
240                 }
241                 var newname = br.remote.replace("remotes/origin/","");
242                 if (branches.has_key(newname)) {
243                         GLib.debug("SKIP - have branch already");
244                         continue;
245                         }
246                     
247                  
248                 repo.git(  { "branch" ,"--track" , newname,  "origin/" + newname} ); 
249                 //
250                 br.name  = newname;
251                 local.set(br.name, br);
252         }
253         */
254         /*
255         this bit of the code tries to turn a local branch into a track of a remote one.
256         -- that's probably not a good idea.
257         var r_replace = new Regex("^remotes/");
258         var o_replace = new Regex("^origin/");
259         foreach(var r in remotes.values) {
260                 if (r.name.length >0) {
261                         return;
262                 }
263                 var name = r_replace.replace(r.name, r.name.length,0, "");
264                 name = o_replace.replace(name, name.length,0, "");
265                 name = name.replace("/", ".");
266                 
267         }
268        
269         repo.branches = local;
270          */
271     
272         
273         }
274
275
276         public bool parseBranchListItem(string str)
277         {
278                 if (str.length  < 1) {
279                         return false;
280                 }
281                 this.active = str[0] == '*';
282                 
283                 var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip());
284                 if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
285                         return false;
286                 }
287                 this.is_remote = false;
288                 this.lastrev = parts[1];
289                 if (parts[0].has_prefix("remotes/")) {
290                         this.is_remote  = true;
291                         this.remote = parts[0];
292                 } else {
293                         this.name = parts[0];
294                 }
295             return true;
296         }
297         public string  realName()
298         {
299                 return this.is_remote ? this.remote : this.name;
300         }
301         
302 //      public void create() { }
303         
304 }
305         
306