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