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);
31 GLib.log("done commit?");
39 public class Repo : Object {
44 public Repo(string path)
46 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
47 this.callbacks = new Callbacks(this);
50 public void fetchAll()
52 // remotes probably will not work with http auth..
53 var ar = this.repo.list_remotes();
54 foreach(var n in ar) {
55 GLib.debug("got remote '%s'", n);
56 var r = this.repo.lookup_remote(n);
57 GLib.debug("connecting '%s'", r.get_url());
60 string[] h = { "a = 1" };
61 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
64 GLib.debug("Got Error Message: %s", e.message);
67 GLib.debug("getting specs '%s'", n);
70 var far = r.get_fetch_specs();
72 foreach(var rs in far) {
73 GLib.debug("got remote spec: %s", rs);
76 var options = new Ggit.FetchOptions();
77 options.set_remote_callbacks(this.callbacks);
78 //yield Async.thread(() => {
80 r.download(far, options);
93 // remotes probably will not work with http auth..
94 var ar = this.repo.list_remotes();
95 foreach(var n in ar) {
96 GLib.debug("got remote '%s'", n);
97 var r = this.repo.lookup_remote(n);
98 GLib.debug("connecting '%s'", r.get_url());
101 string[] h = { "a = 1" };
102 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
105 GLib.debug("Got Error Message: %s", e.message);
108 //GLib.debug("getting specs '%s'", n);
110 var head = this.repo.get_head();
111 this.repo.add_remote_push(
113 "+%s:%s".printf(head.get_shorthand(),head.get_name())
116 var far = r.get_push_specs();
118 foreach(var rs in far) {
119 GLib.debug("got remote spec: %s", rs);
123 var popts = new Ggit.PushOptions();
124 //popts.callbacks = this.callbacks;
127 GLib.debug("Push done?");
141 private class Callbacks : Ggit.RemoteCallbacks
143 //private Remote d_remote;
144 private Ggit.RemoteCallbacks? d_proxy = null;
146 public delegate void TransferProgress(Ggit.TransferProgress stats);
147 //private TransferProgress? d_transfer_progress;
149 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
153 //d_transfer_progress = (owned)transfer_progress;
156 protected override void progress(string message)
158 GLib.debug("progress");
161 d_proxy.progress(message);
165 protected override void transfer_progress(Ggit.TransferProgress stats)
167 GLib.debug("transfer_progress");
169 if (d_transfer_progress != null)
171 d_transfer_progress(stats);
176 d_proxy.transfer_progress(stats);
181 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
183 GLib.debug("update_tips");
184 //d_remote.tip_updated(refname, a, b);
188 d_proxy.update_tips(refname, a, b);
192 protected override void completion(Ggit.RemoteCompletionType type)
194 GLib.debug("completion");
197 d_proxy.completion(type);
201 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
204 GLib.debug("get credentials %s UN=%s", url, username_from_url);
205 var uri = new Soup.URI(url);
208 var ret = this.netrc(uri.get_host());
213 //return new Ggit.CredPlaintext(username_from_url, "test");
216 /*var provider = d_remote.credentials_provider;
218 if (provider != null)
220 ret = provider.credentials(url, username_from_url, allowed_types);
223 if (ret == null && d_proxy != null)
225 ret = d_proxy.credentials(url, username_from_url, allowed_types);
231 public Ggit.Cred netrc(string domain)
234 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
235 var lines = str.split("\n");
236 for(var i=0; i< lines.length; i++) {
237 // assumes one line per entry.. if not we are buggered...
238 //GLib.debug("got %s" , lines[i]);
240 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
241 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
244 GLib.debug("found password?");
245 // we are gussing....
246 return new Ggit.CredPlaintext(bits[3], bits[5]);