Git.vala
[gitlive] / Git.vala
1
2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -pkg gee-0.8 -g
3
4
5
6 void main()
7 {
8          GLib.Log.set_handler(null, 
9             GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, 
10             (dom, lvl, msg) => {
11                                         
12                                         
13                // should we debug..
14                                         
15                         
16                print("%s\n", msg);
17             }
18          ); 
19
20         Ggit.init();
21         
22         var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
23         a.loadBranches();
24         a.fetchAll();
25         //
26         /*
27         var a = new GitLive.Repo("/home/alan/git/test1-clone");
28         //a.fetchAll();
29         var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
30         GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1",  str); 
31         
32         var ix = a.repo.get_index();
33         ix.add_path("test1");
34         ix.write();
35         var treeoid = ix.write_tree();
36
37         var head = a.repo.get_head();
38         var parent = head.lookup() as Ggit.Commit;
39         Ggit.Commit[] parents = (parent == null) ? 
40                 new Ggit.Commit[] {} :
41                 new Ggit.Commit[] { parent };
42
43         var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;// odd format.. 
44         
45         var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
46         a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
47         */
48         // mmh.. no git add/commit in library...
49 //      string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
50         //string[] spawn_env = Environ.get ();
51 //      Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
52  
53         a.pushAll();
54 }
55  
56
57 namespace  GitLive {
58
59         public class Repo : Object {
60
61                 public Ggit.Repository  repo;
62                 Callbacks callbacks;
63
64                 public Repo(string path)
65                 {
66                         this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
67                         this.callbacks = new Callbacks(this);
68                 
69                 }
70                 
71             Gee.ArrayList<Ggit.Branch>          branches = null;
72                 public void loadBranches()
73                 {
74                         this.branches =  new Gee.ArrayList<Ggit.Branch>();
75                         var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
76                         
77                         
78                         
79                         
80                         while (r.next()) {
81                                 var br = r.get() as Ggit.Branch;
82                                 var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
83                                 var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
84                                 GLib.debug("got branch: name = %s upstream = %s oid = %s ", 
85                                                 br.get_name(), br.get_upstream().get_name(), 
86                                                 head.to_string());
87                                 this.branches.add(br);
88                                 
89                         }
90                         
91                         
92                 
93                 }
94                 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
95                 {
96                         //GLib.debug("lookup %s:%s", remote_name, remote_branch_name);
97                         foreach(var br in this.branches) {
98                                 GLib.debug("test:%s=%s", "ref/remotes/" +  br.get_upstream().get_shorthand() , remote_branch_name);
99                                 if ("ref/remotes/" +  br.get_upstream().get_shorthand() == remote_branch_name) {
100                                         return br;
101                                 }
102                                 
103                         }
104                         //GLib.debug("missing %s", remote_branch_name);                 
105                         return null;
106                 
107                 }
108                 
109                 
110                 
111                 public void fetchAll()
112                 {
113                         // remotes probably will not work with http auth..
114                         var ar = this.repo.list_remotes();
115                         foreach(var n in ar) {
116                                 GLib.debug("got remote '%s'", n);
117                                 var r = this.repo.lookup_remote(n);
118                                 GLib.debug("connecting '%s'", r.get_url());
119                                  
120                                 try {
121                                         string[] h = { "a = 1" };
122                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
123                                         
124                                 } catch (Error e) {
125                                         GLib.debug("Got Error Message: %s", e.message);
126                                         return;
127                                 }
128                                 
129                                 var heads = r.list();
130                                 foreach(var rh in heads) {
131                                         var br = this.getBranch(n, rh.get_name());
132                                 
133                                         GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
134                                                 rh.get_name(), 
135                                                 rh.get_oid().to_string(),
136                                                 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
137                                         );
138                                 
139                                 }
140                                 
141                                 
142                                 
143                                 GLib.debug("getting specs '%s'", n);
144
145
146                                 var far = r.get_fetch_specs();
147                                 
148                                 foreach(var rs in far) {
149                                         GLib.debug("got remote spec: %s", rs);
150                                         
151                                 }
152                                 var options = new Ggit.FetchOptions();
153                                 options.set_remote_callbacks(this.callbacks);
154                                 //yield Async.thread(() => {
155         
156                                         r.download(far, options);
157                                 //});
158                                 r.disconnect();
159                                 
160                                 //r.download(
161                         }
162                 
163                 }
164                 
165                 
166                 
167                 public void pushAll()
168                 {
169                         // remotes probably will not work with http auth..
170                         var ar = this.repo.list_remotes();
171                         foreach(var n in ar) {
172                                 GLib.debug("got remote '%s'", n);
173                                 var r = this.repo.lookup_remote(n);
174                                 GLib.debug("connecting '%s'", r.get_url());
175                                  
176                                 try {
177                                         string[] h = { "a = 1" };
178                                          r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
179                                         
180                                 } catch (Error e) {
181                                         GLib.debug("Got Error Message: %s", e.message);
182                                         return;
183                                 }
184                                 //GLib.debug("getting specs '%s'", n);
185                                 /* 
186                                 
187                                 this.repo.add_remote_push(
188                                         "origin",
189                                         "+%s:%s".printf(head.get_shorthand(),head.get_name())
190                                 );
191  */
192                                 var head = this.repo.get_head();
193                                 string[] far = {};
194                                 far += "+%s:%s".printf(head.get_name(),head.get_name());
195                                 
196                                 foreach(var rs in far) {
197                                         GLib.debug("got remote spec: %s", rs);
198                                         
199                                 }
200                                         
201                                 var popts = new Ggit.PushOptions();
202                                  popts.callbacks = this.callbacks;
203                                 GLib.debug("Push?");
204                                 r.upload(far,popts);
205                                 GLib.debug("Push done?");
206                                 r.disconnect();
207                                 
208                                 
209                                 //r.download(
210                         }
211                 
212                 }
213                 
214                 
215                 
216
217         }
218         
219         private class Callbacks : Ggit.RemoteCallbacks
220         {
221                 //private Remote d_remote;
222                 private Ggit.RemoteCallbacks? d_proxy = null;
223
224                 public delegate void TransferProgress(Ggit.TransferProgress stats);
225                 //private TransferProgress? d_transfer_progress;
226
227                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
228                 {
229                         //d_remote = remote;
230                         //d_proxy = proxy;
231                         //d_transfer_progress = (owned)transfer_progress;
232                 }
233
234                 protected override void progress(string message)
235                 {
236                         GLib.debug("progress");
237                         if (d_proxy != null)
238                         {
239                                 d_proxy.progress(message);
240                         }
241                 }
242
243                 protected override void transfer_progress(Ggit.TransferProgress stats)
244                 {
245                         GLib.debug("transfer_progress");
246                         /*
247                         if (d_transfer_progress != null)
248                         {
249                                 d_transfer_progress(stats);
250                         }
251
252                         if (d_proxy != null)
253                         {
254                                 d_proxy.transfer_progress(stats);
255                         }
256                         */
257                 }
258
259                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
260                 {
261                         GLib.debug("update_tips");
262                         //d_remote.tip_updated(refname, a, b);
263
264                         if (d_proxy != null)
265                         {
266                                 d_proxy.update_tips(refname, a, b);
267                         }
268                 }
269
270                 protected override void completion(Ggit.RemoteCompletionType type)
271                 {
272                         GLib.debug("completion");
273                         if (d_proxy != null)
274                         {
275                                 d_proxy.completion(type);
276                         }
277                 }
278
279                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
280                 {
281  
282                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
283                         var uri = new Soup.URI(url);
284
285                         if (uri != null) {
286                                 var ret = this.netrc(uri.get_host());
287                                 if (ret != null) {
288                                         return ret;
289                                 }
290                                 
291                                 //return new Ggit.CredPlaintext(username_from_url, "test");
292                         }
293                         return null;
294                         /*var provider = d_remote.credentials_provider;
295
296                         if (provider != null)
297                         {
298                                 ret = provider.credentials(url, username_from_url, allowed_types);
299                         }
300
301                         if (ret == null && d_proxy != null)
302                         {
303                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
304                         }
305                         
306                         return ret;
307                         */
308                 }
309                 public  Ggit.Cred netrc(string domain) 
310                 {
311                         string str;
312                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
313                         var lines = str.split("\n");
314                         for(var i=0; i< lines.length; i++) {
315                                 // assumes one line per entry.. if not we are buggered...
316                                 //GLib.debug("got %s" , lines[i]);
317                         
318                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
319                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
320                                         continue;
321                                 }
322                                 GLib.debug("found password?");
323                                 // we are gussing.... 
324                                 return new Ggit.CredPlaintext(bits[3], bits[5]);
325
326                         }
327                         return null;
328
329
330                 
331                 
332                 }
333                 
334         }
335         
336         
337
338 }