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