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  
40         
41      
42         
43         public string toString()
44         {
45                 return 
46                         "active: " + (active ? "true" : "false") + "\n" +
47                         "is_remote: " + (is_remote ? "true" : "false") + "\n" +                 
48                         "lastrev: " + lastrev + "\n" +
49                         "name: " + name + "\n" +
50                         "remote: " + remote + "\n" +
51                         "remoterev: " + remoterev + "\n" +
52                         "age: " + age + "\n" ;
53         }
54         
55         
56         public GitBranch(GitRepo repo)
57         {
58                 this.repo = repo;
59         }
60         
61         
62         
63
64         public static  void loadBranches(GitRepo repo)
65         {
66                 repo.branches = new Gee.HashMap<string,GitBranch>();
67         
68         //var branches =  new Gee.HashMap<string,GitBranch>();
69         //var local =  new Gee.HashMap<string,GitBranch>();
70         var remotes  =  new Gee.HashMap<string,string>();
71         var remotes_used  =  new Gee.HashMap<string,bool>();
72   
73                 
74                 var rem = repo.repo.lookup_remote("origin");
75                 var cb = new GitCallbacks(repo);
76                 rem.connect(Ggit.Direction.FETCH, cb, null, null);
77                 var remote_heads = rem.list();
78                 foreach(var rh in remote_heads) {
79                         var rn = rh.get_name();
80                         if (!rn.has_prefix("refs/heads/")) {
81                                 continue;
82                         }
83                         remotes.set(rn.substring(11), rh.get_oid().to_string());
84                         remotes_used.set(rn.substring(11), false);
85           }
86                 
87                  
88                 var r = repo.repo.enumerate_branches(Ggit.BranchType.LOCAL);
89                 while (r.next()) {
90                         var br = new GitBranch(repo);
91                         var gbr = r.get() as Ggit.Branch;
92                         repo.branches.set(gbr.get_name(), br);
93                         br.active = gbr.is_head();
94                         br.name = gbr.get_name();
95                         br.lastrev = gbr.get_target().to_string();
96                         var rname = gbr.get_upstream() != null ? gbr.get_upstream().get_name() : "";
97                         if (rname.has_prefix("refs/remotes/origin/")) {
98                                 rname = rname.substring(20);
99                                 if (remotes.has_key(rname)) {
100                                         br.remote = rname;
101                                         br.remoterev = remotes.get(rname);
102                                         remotes_used.set(rname,true);
103                                 }
104                                 // age?
105                                 // behind or infront..
106                         }
107                         
108                 }
109                 
110                 
111         
112       //         repo.git( { "fetch",  "-a" } ); == done async before...
113         
114         string[] cmd = { "branch",   "--no-color", "--verbose", "--no-abbrev" , "-a"  };
115         var res = repo.git( cmd );
116         var lines = res.split("\n");
117         for (var i = 0; i < lines.length ; i++) {
118                 var br = new GitBranch(repo);
119                 if (!br.parseBranchListItem(lines[i])) {
120                         continue;
121                 }
122                 GLib.debug("add branch %s", br.realName());
123                 if (!br.is_remote) {
124                         local.set(br.name, br);
125                 } else {
126                         remotes.set(br.remote, br);
127                         }
128                  
129                 branches.set(br.realName(), br);
130                 if (br.active) {
131                         repo.currentBranch = br;
132                 }
133         }
134         
135          var bl = repo.git({
136                 "for-each-ref",
137                 "--format",   "%(refname:short):remotes/%(upstream:short):remotes/%(authordate:relative)",
138                 "refs/heads"
139                 }).split("\n");
140         
141         foreach(var line in bl) {
142                 if (line.length < 1) continue;
143                 
144                 var ar = line.split(":remotes/");
145                 var lname= ar[0];
146             var rname = "remotes/" + ar[1];
147             
148             
149                 //print(rname);
150                 // we should always have a local version of it.
151             if (branches.has_key(lname)) {      
152                     branches.get(lname).remote = rname;
153                     branches.get(lname).age = ar[2];
154             }
155             
156             if (!branches.has_key(rname) || !branches.has_key(lname)  ) {
157                     continue;
158             }
159             branches.get(lname).remoterev =  branches.get(rname).lastrev;
160                 // flag it for not adding..
161                 
162             branches.get(rname).name = lname;
163         }
164         foreach(var br in branches.values) {
165                 GLib.debug("BRANCH:\n%s\n" , br.toString());
166                 if (br.name.length > 0 || ! /^remotes\/origin\//.match(br.remote)) {
167                         GLib.debug("SKIP - track exists");
168                         continue;
169                 }
170                 var newname = br.remote.replace("remotes/origin/","");
171                 if (branches.has_key(newname)) {
172                         GLib.debug("SKIP - have branch already");
173                         continue;
174                         }
175                     
176                  
177                 repo.git(  { "branch" ,"--track" , newname,  "origin/" + newname} ); 
178                 //
179                 br.name  = newname;
180                 local.set(br.name, br);
181         }
182         
183         /*
184         this bit of the code tries to turn a local branch into a track of a remote one.
185         -- that's probably not a good idea.
186         var r_replace = new Regex("^remotes/");
187         var o_replace = new Regex("^origin/");
188         foreach(var r in remotes.values) {
189                 if (r.name.length >0) {
190                         return;
191                 }
192                 var name = r_replace.replace(r.name, r.name.length,0, "");
193                 name = o_replace.replace(name, name.length,0, "");
194                 name = name.replace("/", ".");
195                 
196         }
197         */
198         repo.branches = local;
199         
200     
201         
202         }
203
204
205         public bool parseBranchListItem(string str)
206         {
207                 if (str.length  < 1) {
208                         return false;
209                 }
210                 this.active = str[0] == '*';
211                 
212                 var parts = Regex.split_simple ("[ \t]+", str.substring(2).strip());
213                 if (parts[1] == "->") { // it's an alias.. eg. remotes/origin/HEAD -> origin/master..
214                         return false;
215                 }
216                 this.is_remote = false;
217                 this.lastrev = parts[1];
218                 if (parts[0].has_prefix("remotes/")) {
219                         this.is_remote  = true;
220                         this.remote = parts[0];
221                 } else {
222                         this.name = parts[0];
223                 }
224             return true;
225         }
226         public string  realName()
227         {
228                 return this.is_remote ? this.remote : this.name;
229         }
230         
231         public void create()
232         {
233         
234         }
235         
236 }
237         
238