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