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 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);
27 var ix = a.repo.repo.get_index();
29 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
30 a.repo.repo.create_commit("HEAD", sig, sig, null, "test commit " + str,
31 // mmh.. no git add/commit in library...
32 string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
33 string[] spawn_env = Environ.get ();
34 Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
42 public class Repo : Object {
44 public Ggit.Repository repo;
47 public Repo(string path)
49 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
50 this.callbacks = new Callbacks(this);
53 public void fetchAll()
55 // remotes probably will not work with http auth..
56 var ar = this.repo.list_remotes();
57 foreach(var n in ar) {
58 GLib.debug("got remote '%s'", n);
59 var r = this.repo.lookup_remote(n);
60 GLib.debug("connecting '%s'", r.get_url());
63 string[] h = { "a = 1" };
64 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
67 GLib.debug("Got Error Message: %s", e.message);
70 GLib.debug("getting specs '%s'", n);
73 var far = r.get_fetch_specs();
75 foreach(var rs in far) {
76 GLib.debug("got remote spec: %s", rs);
79 var options = new Ggit.FetchOptions();
80 options.set_remote_callbacks(this.callbacks);
81 //yield Async.thread(() => {
83 r.download(far, options);
96 // remotes probably will not work with http auth..
97 var ar = this.repo.list_remotes();
98 foreach(var n in ar) {
99 GLib.debug("got remote '%s'", n);
100 var r = this.repo.lookup_remote(n);
101 GLib.debug("connecting '%s'", r.get_url());
104 string[] h = { "a = 1" };
105 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
108 GLib.debug("Got Error Message: %s", e.message);
111 //GLib.debug("getting specs '%s'", n);
113 var head = this.repo.get_head();
114 this.repo.add_remote_push(
116 "+%s:%s".printf(head.get_shorthand(),head.get_name())
119 var far = r.get_push_specs();
121 foreach(var rs in far) {
122 GLib.debug("got remote spec: %s", rs);
126 var popts = new Ggit.PushOptions();
127 //popts.callbacks = this.callbacks;
130 GLib.debug("Push done?");
144 private class Callbacks : Ggit.RemoteCallbacks
146 //private Remote d_remote;
147 private Ggit.RemoteCallbacks? d_proxy = null;
149 public delegate void TransferProgress(Ggit.TransferProgress stats);
150 //private TransferProgress? d_transfer_progress;
152 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
156 //d_transfer_progress = (owned)transfer_progress;
159 protected override void progress(string message)
161 GLib.debug("progress");
164 d_proxy.progress(message);
168 protected override void transfer_progress(Ggit.TransferProgress stats)
170 GLib.debug("transfer_progress");
172 if (d_transfer_progress != null)
174 d_transfer_progress(stats);
179 d_proxy.transfer_progress(stats);
184 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
186 GLib.debug("update_tips");
187 //d_remote.tip_updated(refname, a, b);
191 d_proxy.update_tips(refname, a, b);
195 protected override void completion(Ggit.RemoteCompletionType type)
197 GLib.debug("completion");
200 d_proxy.completion(type);
204 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
207 GLib.debug("get credentials %s UN=%s", url, username_from_url);
208 var uri = new Soup.URI(url);
211 var ret = this.netrc(uri.get_host());
216 //return new Ggit.CredPlaintext(username_from_url, "test");
219 /*var provider = d_remote.credentials_provider;
221 if (provider != null)
223 ret = provider.credentials(url, username_from_url, allowed_types);
226 if (ret == null && d_proxy != null)
228 ret = d_proxy.credentials(url, username_from_url, allowed_types);
234 public Ggit.Cred netrc(string domain)
237 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
238 var lines = str.split("\n");
239 for(var i=0; i< lines.length; i++) {
240 // assumes one line per entry.. if not we are buggered...
241 //GLib.debug("got %s" , lines[i]);
243 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
244 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
247 GLib.debug("found password?");
248 // we are gussing....
249 return new Ggit.CredPlaintext(bits[3], bits[5]);