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