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