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