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