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