2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4
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/test2");
24 GLib.FileUtils.set_contents("/home/alan/git/test2/test1",
25 GLib.DateTime.now_utc().format("%Y-%m-%d %H:%M:%S")
33 public class Repo : Object {
38 public Repo(string path)
40 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
41 this.callbacks = new Callbacks(this);
44 public void fetchAll()
46 // remotes probably will not work with http auth..
47 var ar = this.repo.list_remotes();
48 foreach(var n in ar) {
49 GLib.debug("got remote '%s'", n);
50 var r = this.repo.lookup_remote(n);
51 GLib.debug("connecting '%s'", r.get_url());
54 string[] h = { "a = 1" };
55 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
58 GLib.debug("Got Error Message: %s", e.message);
61 GLib.debug("getting specs '%s'", n);
64 var far = r.get_fetch_specs();
66 foreach(var rs in far) {
67 GLib.debug("got remote spec: %s", rs);
70 var options = new Ggit.FetchOptions();
71 options.set_remote_callbacks(this.callbacks);
72 //yield Async.thread(() => {
74 r.download(far, options);
84 // remotes probably will not work with http auth..
85 var ar = this.repo.list_remotes();
86 foreach(var n in ar) {
87 GLib.debug("got remote '%s'", n);
88 var r = this.repo.lookup_remote(n);
89 GLib.debug("connecting '%s'", r.get_url());
92 string[] h = { "a = 1" };
93 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
96 GLib.debug("Got Error Message: %s", e.message);
99 //GLib.debug("getting specs '%s'", n);
101 var head = this.repo.get_head();
102 this.repo.add_remote_push(
104 "+%s:%s".printf(head.get_shorthand(),head.get_name())
107 var far = r.get_push_specs();
109 foreach(var rs in far) {
110 GLib.debug("got remote spec: %s", rs);
114 var popts = new Ggit.PushOptions();
115 //popts.callbacks = this.callbacks;
117 r.upload(null,popts);
118 GLib.debug("Push done?");
132 private class Callbacks : Ggit.RemoteCallbacks
134 //private Remote d_remote;
135 private Ggit.RemoteCallbacks? d_proxy = null;
137 public delegate void TransferProgress(Ggit.TransferProgress stats);
138 //private TransferProgress? d_transfer_progress;
140 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
144 //d_transfer_progress = (owned)transfer_progress;
147 protected override void progress(string message)
149 GLib.debug("progress");
152 d_proxy.progress(message);
156 protected override void transfer_progress(Ggit.TransferProgress stats)
158 GLib.debug("transfer_progress");
160 if (d_transfer_progress != null)
162 d_transfer_progress(stats);
167 d_proxy.transfer_progress(stats);
172 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
174 GLib.debug("update_tips");
175 //d_remote.tip_updated(refname, a, b);
179 d_proxy.update_tips(refname, a, b);
183 protected override void completion(Ggit.RemoteCompletionType type)
185 GLib.debug("completion");
188 d_proxy.completion(type);
192 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
195 GLib.debug("get credentials %s UN=%s", url, username_from_url);
196 var uri = new Soup.URI(url);
199 var ret = this.netrc(uri.get_host());
204 //return new Ggit.CredPlaintext(username_from_url, "test");
207 /*var provider = d_remote.credentials_provider;
209 if (provider != null)
211 ret = provider.credentials(url, username_from_url, allowed_types);
214 if (ret == null && d_proxy != null)
216 ret = d_proxy.credentials(url, username_from_url, allowed_types);
222 public Ggit.Cred netrc(string domain)
225 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
226 var lines = str.split("\n");
227 for(var i=0; i< lines.length; i++) {
228 // assumes one line per entry.. if not we are buggered...
229 //GLib.debug("got %s" , lines[i]);
231 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
232 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
235 GLib.debug("found password?");
236 // we are gussing....
237 return new Ggit.CredPlaintext(bits[3], bits[5]);