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