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