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