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