Partial Fix #5782 - messing around with libgit2-glib
[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         public Ggit.Tree getTree()
63         {
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();
66         }
67         
68
69         public static  void loadBranches(GitRepo repo)
70         {
71                 repo.branches = new Gee.HashMap<string,GitBranch>();
72         
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>();
77          
78   
79                 
80                 var rem = repo.repo.lookup_remote("origin");
81                 var cb = new GitCallbacks(repo);
82                 rem.connect(Ggit.Direction.FETCH, cb, null, null);
83                 var remote_heads = rem.list();
84                 foreach(var rh in remote_heads) {
85                         var rn = rh.get_name();
86                         if (!rn.has_prefix("refs/heads/")) {
87                                 continue;
88                         }
89                         remotes.set(rn.substring(11), rh.get_oid());
90                         remotes_used.set(rn.substring(11), false);
91
92           } 
93                 
94                  
95                 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
96                 while (r.next()) {
97                 
98
99                 
100                         var br = new GitBranch(repo);
101                         var gbr = r.get() as Ggit.Branch;
102                         if (!gbr.is_branch()) {
103                                 continue;
104                         }
105                         
106                         br.active = gbr.is_head();
107                         br.name = gbr.get_name();
108                         br.lastrev = gbr.get_target().to_string();
109                         string rname ;
110                         try {
111                                 rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
112                         } catch(Error e) {
113                                 GLib.debug("Skip branch = got error");
114                                 continue;
115                         }
116                         repo.branches.set(gbr.get_name(), br);                  
117                         if (rname.has_prefix("refs/remotes/origin/")) {
118                                 rname = rname.substring(20);
119                                 if (remotes.has_key(rname)) {
120                                         br.remote = rname;
121                                         br.remoterev = remotes.get(rname).to_string();
122                                         remotes_used.set(rname,true);
123                                         size_t ahead, behind;
124                                         br.behind = 1;
125                                         
126                                         try {
127                                                 repo.repo.get_ahead_behind(
128                                                         gbr.get_target(),
129                                                         remotes.get(rname),
130                                                         out ahead,
131                                                         out behind
132                                                 );
133                                                 br.ahead = (int)ahead;
134                                                 br.behind = (int) behind;
135
136                                         } catch(Error e) {
137                                                 GLib.debug("we probably need to fetch... %s", repo.name);
138                                         }
139                                         
140                                 }
141                                  
142                                 
143                                 // age?
144                                 // behind or infront..
145                         }
146                         if (br.active) {
147                                 GLib.debug("repo: %s currentBranch = %s", repo.name, br.name);
148                                 repo._currentBranch = br;
149                         }
150                         
151                 }
152                 
153                 // find unused remotes.. and track them...
154                 foreach(var rn in remotes_used.keys) {
155                         if (remotes_used.get(rn)) {
156                                 continue;
157                         }
158                         if (repo.branches.has_key(rn)) {
159                                 GLib.debug("skip tracking branch - same name exists?");
160                                 continue;
161                         }
162                         // not clear how to do this yet...
163                         try {
164                                 repo.git(  { "branch" ,"--track" , rn,  "origin/" + rn} ); 
165                         } catch (Error e) {
166                                 continue; // allow failure?
167                         }
168                         var br = new GitBranch(repo);
169                         br.name = rn;
170                         br.lastrev = ""; // it's behind
171                         br.remoterev  =  remotes.get(rn).to_string();
172                         br.remote = rn;
173                         br.ahead = 0;
174                         br.behind = 1;
175                         // behind/ahead == 0...
176                         repo.branches.set(rn, br);
177                         
178                 }
179
180                 if (repo._currentBranch == null) {
181                         GLib.error("could not find active Branch for %s", repo.name);
182                 }
183                 
184                          
185                 
186         /*
187       //         repo.git( { "fetch",  "-a" } ); == done async before...
188         
189         string[] cmd = { "branch",   "--no-color", "--verbose", "--no-abbrev" , "-a"  };
190         var res = repo.git( cmd );
191         var lines = res.split("\n");
192         for (var i = 0; i < lines.length ; i++) {
193                 var br = new GitBranch(repo);
194                 if (!br.parseBranchListItem(lines[i])) {
195                         continue;
196                 }
197                 GLib.debug("add branch %s", br.realName());
198                 if (!br.is_remote) {
199                         local.set(br.name, br);
200                 } else {
201                         remotes.set(br.remote, br);
202                         }
203                  
204                 branches.set(br.realName(), br);
205                 if (br.active) {
206                         repo.currentBranch = br;
207                 }
208         }
209         
210          var bl = repo.git({
211                 "for-each-ref",
212                 "--format",   "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
213                 "refs/heads"
214                 }).split("\n");
215         
216         foreach(var line in bl) {
217                 if (line.length < 1) continue;
218                 
219                 var ar = line.split(":remotes/");
220                 var lname= ar[0];
221             var rname = "remotes/" + ar[1];
222             
223             
224                 //print(rname);
225                 // we should always have a local version of it.
226             if (branches.has_key(lname)) {      
227                     branches.get(lname).remote = rname;
228                     branches.get(lname).age = ar[2];
229             }
230             
231             if (!branches.has_key(rname) || !branches.has_key(lname)  ) {
232                     continue;
233             }
234             branches.get(lname).remoterev =  branches.get(rname).lastrev;
235                 // flag it for not adding..
236                 
237             branches.get(rname).name = lname;
238         }
239         foreach(var br in branches.values) {
240                 GLib.debug("BRANCH:\n%s\n" , br.toString());
241                 if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) {
242                         GLib.debug("SKIP - track exists");
243                         continue;
244                 }
245                 var newname = br.remote.replace("remotes/origin/","");
246                 if (branches.has_key(newname)) {
247                         GLib.debug("SKIP - have branch already");
248                         continue;
249                         }
250                     
251                  
252                 repo.git(  { "branch" ,"--track" , newname,  "origin/" + newname} ); 
253                 //
254                 br.name  = newname;
255                 local.set(br.name, br);
256         }
257         */
258         /*
259         this bit of the code tries to turn a local branch into a track of a remote one.
260         -- that's probably not a good idea.
261         var r_replace = new Regex("^remotes/");
262         var o_replace = new Regex("^origin/");
263         foreach(var r in remotes.values) {
264                 if (r.name.length >0) {
265                         return;
266                 }
267                 var name = r_replace.replace(r.name, r.name.length,0, "");
268                 name = o_replace.replace(name, name.length,0, "");
269                 name = name.replace("/", ".");
270                 
271         }
272        
273         repo.branches = local;
274          */
275     
276         
277         }
278
279
280         public bool parseBranchListItem(string str)
281         {
282                 if (str.length  < 1) {
283                         return false;
284                 }
285                 this.active = str[0] == '*';
286                 
287                 var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip());
288                 if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
289                         return false;
290                 }
291                 this.is_remote = false;
292                 this.lastrev = parts[1];
293                 if (parts[0].has_prefix("remotes/")) {
294                         this.is_remote  = true;
295                         this.remote = parts[0];
296                 } else {
297                         this.name = parts[0];
298                 }
299             return true;
300         }
301         public string  realName()
302         {
303                 return this.is_remote ? this.remote : this.name;
304         }
305         
306 //      public void create() { }
307         
308 }
309         
310