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