2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -g
8 GLib.Log.set_handler(null,
9 GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO,
22 var a = new GitLive.Repo("/home/alan/git/test1-clone");
24 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1",
25 (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S")
27 // mmh.. no git add/commit in library...
28 string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
29 string[] spawn_env = Environ.get ();
30 Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
38 public class Repo : Object {
43 public Repo(string path)
45 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
46 this.callbacks = new Callbacks(this);
49 public void fetchAll()
51 // remotes probably will not work with http auth..
52 var ar = this.repo.list_remotes();
53 foreach(var n in ar) {
54 GLib.debug("got remote '%s'", n);
55 var r = this.repo.lookup_remote(n);
56 GLib.debug("connecting '%s'", r.get_url());
59 string[] h = { "a = 1" };
60 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
63 GLib.debug("Got Error Message: %s", e.message);
66 GLib.debug("getting specs '%s'", n);
69 var far = r.get_fetch_specs();
71 foreach(var rs in far) {
72 GLib.debug("got remote spec: %s", rs);
75 var options = new Ggit.FetchOptions();
76 options.set_remote_callbacks(this.callbacks);
77 //yield Async.thread(() => {
79 r.download(far, options);
92 // remotes probably will not work with http auth..
93 var ar = this.repo.list_remotes();
94 foreach(var n in ar) {
95 GLib.debug("got remote '%s'", n);
96 var r = this.repo.lookup_remote(n);
97 GLib.debug("connecting '%s'", r.get_url());
100 string[] h = { "a = 1" };
101 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
104 GLib.debug("Got Error Message: %s", e.message);
107 //GLib.debug("getting specs '%s'", n);
109 var head = this.repo.get_head();
110 this.repo.add_remote_push(
112 "+%s:%s".printf(head.get_shorthand(),head.get_name())
115 var far = r.get_push_specs();
117 foreach(var rs in far) {
118 GLib.debug("got remote spec: %s", rs);
122 var popts = new Ggit.PushOptions();
123 //popts.callbacks = this.callbacks;
126 GLib.debug("Push done?");
140 private class Callbacks : Ggit.RemoteCallbacks
142 //private Remote d_remote;
143 private Ggit.RemoteCallbacks? d_proxy = null;
145 public delegate void TransferProgress(Ggit.TransferProgress stats);
146 //private TransferProgress? d_transfer_progress;
148 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
152 //d_transfer_progress = (owned)transfer_progress;
155 protected override void progress(string message)
157 GLib.debug("progress");
160 d_proxy.progress(message);
164 protected override void transfer_progress(Ggit.TransferProgress stats)
166 GLib.debug("transfer_progress");
168 if (d_transfer_progress != null)
170 d_transfer_progress(stats);
175 d_proxy.transfer_progress(stats);
180 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
182 GLib.debug("update_tips");
183 //d_remote.tip_updated(refname, a, b);
187 d_proxy.update_tips(refname, a, b);
191 protected override void completion(Ggit.RemoteCompletionType type)
193 GLib.debug("completion");
196 d_proxy.completion(type);
200 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
203 GLib.debug("get credentials %s UN=%s", url, username_from_url);
204 var uri = new Soup.URI(url);
207 var ret = this.netrc(uri.get_host());
212 //return new Ggit.CredPlaintext(username_from_url, "test");
215 /*var provider = d_remote.credentials_provider;
217 if (provider != null)
219 ret = provider.credentials(url, username_from_url, allowed_types);
222 if (ret == null && d_proxy != null)
224 ret = d_proxy.credentials(url, username_from_url, allowed_types);
230 public Ggit.Cred netrc(string domain)
233 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
234 var lines = str.split("\n");
235 for(var i=0; i< lines.length; i++) {
236 // assumes one line per entry.. if not we are buggered...
237 //GLib.debug("got %s" , lines[i]);
239 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
240 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
243 GLib.debug("found password?");
244 // we are gussing....
245 return new Ggit.CredPlaintext(bits[3], bits[5]);