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