revert code that did not affect memory
[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                 try {
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/")) {
88                                         continue;
89                                 }
90                                 remotes.set(rn.substring(11), rh.get_oid());
91                                 remotes_used.set(rn.substring(11), false);
92
93                     } 
94                    
95                     
96                 } catch (Error e) {
97                         GLib.debug("Failed to fetch remotes continuing on ");
98                 }
99            rem.disconnect();
100                 
101                 
102                 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
103                 while (r.next()) {
104                 
105
106                 
107                         var br = new GitBranch(repo);
108                         var gbr = r.get() as Ggit.Branch;
109                         if (!gbr.is_branch()) {
110                                 continue;
111                         }
112                         
113                         br.active = gbr.is_head();
114                         br.name = gbr.get_name();
115                         br.lastrev = gbr.get_target().to_string();
116                         string rname ="";
117                         /*
118                         
119                         THIS IS DISABLED, and get_upstream has a major memor leak.
120                         try {
121                                 rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
122                         } catch(Error e) {
123                                 GLib.debug("Skip branch = got error");
124                                 continue;
125                         }
126                         */
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)) {
131                                         br.remote = rname;
132                                         br.remoterev = remotes.get(rname).to_string();
133                                         remotes_used.set(rname,true);
134                                         size_t ahead, behind;
135                                         br.behind = 1;
136                                         
137                                         try {
138                                                 repo.repo.get_ahead_behind(
139                                                         gbr.get_target(),
140                                                         remotes.get(rname),
141                                                         out ahead,
142                                                         out behind
143                                                 );
144                                                 br.ahead = (int)ahead;
145                                                 br.behind = (int) behind;
146
147                                         } catch(Error e) {
148                                                 GLib.debug("we probably need to fetch... %s", repo.name);
149                                         }
150                                         
151                                 }
152                                  
153                                 
154                                 // age?
155                                 // behind or infront..
156                         }
157                         if (br.active) {
158                                 GLib.debug("repo: %s currentBranch = %s", repo.name, br.name);
159                                 repo._currentBranch = br;
160                         }
161                         
162                 }
163                 
164                 // find unused remotes.. and track them...
165                 foreach(var rn in remotes_used.keys) {
166                         if (remotes_used.get(rn)) {
167                                 continue;
168                         }
169                         if (repo.branches.has_key(rn)) {
170                                 GLib.debug("skip tracking branch - same name exists?");
171                                 continue;
172                         }
173                         // not clear how to do this yet...
174                         try {
175                                 repo.git(  { "branch" ,"--track" , rn,  "origin/" + rn} ); 
176                         } catch (Error e) {
177                                 continue; // allow failure?
178                         }
179                         var br = new GitBranch(repo);
180                         br.name = rn;
181                         br.lastrev = ""; // it's behind
182                         br.remoterev  =  remotes.get(rn).to_string();
183                         br.remote = rn;
184                         br.ahead = 0;
185                         br.behind = 1;
186                         // behind/ahead == 0...
187                         repo.branches.set(rn, br);
188                         
189                 }
190
191                 if (repo._currentBranch == null) {
192                         GLib.error("could not find active Branch for %s", repo.name);
193                 }
194                 
195                          
196                 
197         /*
198       //         repo.git( { "fetch",  "-a" } ); == done async before...
199         
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])) {
206                         continue;
207                 }
208                 GLib.debug("add branch %s", br.realName());
209                 if (!br.is_remote) {
210                         local.set(br.name, br);
211                 } else {
212                         remotes.set(br.remote, br);
213                         }
214                  
215                 branches.set(br.realName(), br);
216                 if (br.active) {
217                         repo.currentBranch = br;
218                 }
219         }
220         
221          var bl = repo.git({
222                 "for-each-ref",
223                 "--format",   "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
224                 "refs/heads"
225                 }).split("\n");
226         
227         foreach(var line in bl) {
228                 if (line.length < 1) continue;
229                 
230                 var ar = line.split(":remotes/");
231                 var lname= ar[0];
232             var rname = "remotes/" + ar[1];
233             
234             
235                 //print(rname);
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];
240             }
241             
242             if (!branches.has_key(rname) || !branches.has_key(lname)  ) {
243                     continue;
244             }
245             branches.get(lname).remoterev =  branches.get(rname).lastrev;
246                 // flag it for not adding..
247                 
248             branches.get(rname).name = lname;
249         }
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");
254                         continue;
255                 }
256                 var newname = br.remote.replace("remotes/origin/","");
257                 if (branches.has_key(newname)) {
258                         GLib.debug("SKIP - have branch already");
259                         continue;
260                         }
261                     
262                  
263                 repo.git(  { "branch" ,"--track" , newname,  "origin/" + newname} ); 
264                 //
265                 br.name  = newname;
266                 local.set(br.name, br);
267         }
268         */
269         /*
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) {
276                         return;
277                 }
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("/", ".");
281                 
282         }
283        
284         repo.branches = local;
285          */
286     
287         
288         }
289
290
291         public bool parseBranchListItem(string str)
292         {
293                 if (str.length  < 1) {
294                         return false;
295                 }
296                 this.active = str[0] == '*';
297                 
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..
300                         return false;
301                 }
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];
307                 } else {
308                         this.name = parts[0];
309                 }
310             return true;
311         }
312         public string  realName()
313         {
314                 return this.is_remote ? this.remote : this.name;
315         }
316         
317 //      public void create() { }
318         
319 }
320         
321