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