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             Gee.ArrayList<Ggit.Branch>          branches = null;
71                 public Gee.ArrayList<Ggit.Branch> loadBranches()
72                 {
73                         this.branches new Gee.ArrayList<Ggit.Branch>();
74                         var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
75                         
76                         
77                         
78                         
79                         while (r.next()) {
80                                 var br = r.get() as Ggit.Branch;
81                                 var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
82                                 var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
83                                 GLib.debug("got branch: name = %s upstream = %s oid = %s ", 
84                                                 br.get_name(), br.get_upstream().get_name(), 
85                                                 head.to_string());
86                                 this.branches.add(br);
87                                 
88                         }
89                         
90                         return ret;
91                 
92                 }
93                 Ggit.Branch? getBranch(string remote_name)
94                 {
95                         foreach(var br in this.branches) {
96                                 if (br.get_upstream().get_name() == remote_name) {
97                                         return br;
98                                 }
99                         }
100                         return null;
101                 
102                 }
103                 
104                 
105                 public void fetchAll()
106                 {
107                         // remotes probably will not work with http auth..
108                         var ar = this.repo.list_remotes();
109                         foreach(var n in ar) {
110                                 GLib.debug("got remote '%s'", n);
111                                 var r = this.repo.lookup_remote(n);
112                                 GLib.debug("connecting '%s'", r.get_url());
113                                  
114                                 try {
115                                         string[] h = { "a = 1" };
116                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
117                                         
118                                 } catch (Error e) {
119                                         GLib.debug("Got Error Message: %s", e.message);
120                                         return;
121                                 }
122                                 
123                                 var heads = r.list();
124                                 foreach(var rh in heads) {
125                                         rh.get_shotor_name(), 
126                                 
127                                         GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
128                                                 rh.get_name(), 
129                                                 rh.get_oid().to_string(),
130                                                 rh.get_local_oid().to_string()
131                                         );
132                                 
133                                 }
134                                 
135                                 
136                                 
137                                 GLib.debug("getting specs '%s'", n);
138
139
140                                 var far = r.get_fetch_specs();
141                                 
142                                 foreach(var rs in far) {
143                                         GLib.debug("got remote spec: %s", rs);
144                                         
145                                 }
146                                 var options = new Ggit.FetchOptions();
147                                 options.set_remote_callbacks(this.callbacks);
148                                 //yield Async.thread(() => {
149         
150                                         r.download(far, options);
151                                 //});
152                                 r.disconnect();
153                                 
154                                 //r.download(
155                         }
156                 
157                 }
158                 
159                 
160                 
161                 public void pushAll()
162                 {
163                         // remotes probably will not work with http auth..
164                         var ar = this.repo.list_remotes();
165                         foreach(var n in ar) {
166                                 GLib.debug("got remote '%s'", n);
167                                 var r = this.repo.lookup_remote(n);
168                                 GLib.debug("connecting '%s'", r.get_url());
169                                  
170                                 try {
171                                         string[] h = { "a = 1" };
172                                          r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
173                                         
174                                 } catch (Error e) {
175                                         GLib.debug("Got Error Message: %s", e.message);
176                                         return;
177                                 }
178                                 //GLib.debug("getting specs '%s'", n);
179                                 /* 
180                                 
181                                 this.repo.add_remote_push(
182                                         "origin",
183                                         "+%s:%s".printf(head.get_shorthand(),head.get_name())
184                                 );
185  */
186                                 var head = this.repo.get_head();
187                                 string[] far = {};
188                                 far += "+%s:%s".printf(head.get_name(),head.get_name());
189                                 
190                                 foreach(var rs in far) {
191                                         GLib.debug("got remote spec: %s", rs);
192                                         
193                                 }
194                                         
195                                 var popts = new Ggit.PushOptions();
196                                  popts.callbacks = this.callbacks;
197                                 GLib.debug("Push?");
198                                 r.upload(far,popts);
199                                 GLib.debug("Push done?");
200                                 r.disconnect();
201                                 
202                                 
203                                 //r.download(
204                         }
205                 
206                 }
207                 
208                 
209                 
210
211         }
212         
213         private class Callbacks : Ggit.RemoteCallbacks
214         {
215                 //private Remote d_remote;
216                 private Ggit.RemoteCallbacks? d_proxy = null;
217
218                 public delegate void TransferProgress(Ggit.TransferProgress stats);
219                 //private TransferProgress? d_transfer_progress;
220
221                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
222                 {
223                         //d_remote = remote;
224                         //d_proxy = proxy;
225                         //d_transfer_progress = (owned)transfer_progress;
226                 }
227
228                 protected override void progress(string message)
229                 {
230                         GLib.debug("progress");
231                         if (d_proxy != null)
232                         {
233                                 d_proxy.progress(message);
234                         }
235                 }
236
237                 protected override void transfer_progress(Ggit.TransferProgress stats)
238                 {
239                         GLib.debug("transfer_progress");
240                         /*
241                         if (d_transfer_progress != null)
242                         {
243                                 d_transfer_progress(stats);
244                         }
245
246                         if (d_proxy != null)
247                         {
248                                 d_proxy.transfer_progress(stats);
249                         }
250                         */
251                 }
252
253                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
254                 {
255                         GLib.debug("update_tips");
256                         //d_remote.tip_updated(refname, a, b);
257
258                         if (d_proxy != null)
259                         {
260                                 d_proxy.update_tips(refname, a, b);
261                         }
262                 }
263
264                 protected override void completion(Ggit.RemoteCompletionType type)
265                 {
266                         GLib.debug("completion");
267                         if (d_proxy != null)
268                         {
269                                 d_proxy.completion(type);
270                         }
271                 }
272
273                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
274                 {
275  
276                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
277                         var uri = new Soup.URI(url);
278
279                         if (uri != null) {
280                                 var ret = this.netrc(uri.get_host());
281                                 if (ret != null) {
282                                         return ret;
283                                 }
284                                 
285                                 //return new Ggit.CredPlaintext(username_from_url, "test");
286                         }
287                         return null;
288                         /*var provider = d_remote.credentials_provider;
289
290                         if (provider != null)
291                         {
292                                 ret = provider.credentials(url, username_from_url, allowed_types);
293                         }
294
295                         if (ret == null && d_proxy != null)
296                         {
297                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
298                         }
299                         
300                         return ret;
301                         */
302                 }
303                 public  Ggit.Cred netrc(string domain) 
304                 {
305                         string str;
306                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
307                         var lines = str.split("\n");
308                         for(var i=0; i< lines.length; i++) {
309                                 // assumes one line per entry.. if not we are buggered...
310                                 //GLib.debug("got %s" , lines[i]);
311                         
312                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
313                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
314                                         continue;
315                                 }
316                                 GLib.debug("found password?");
317                                 // we are gussing.... 
318                                 return new Ggit.CredPlaintext(bits[3], bits[5]);
319
320                         }
321                         return null;
322
323
324                 
325                 
326                 }
327                 
328         }
329         
330         
331
332 }