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