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