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