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")
27 // mmh.. no git add/commit in library...
28 Process.spawn_command_line_sync ("ls");
37 public class Repo : Object {
42 public Repo(string path)
44 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
45 this.callbacks = new Callbacks(this);
48 public void fetchAll()
50 // remotes probably will not work with http auth..
51 var ar = this.repo.list_remotes();
52 foreach(var n in ar) {
53 GLib.debug("got remote '%s'", n);
54 var r = this.repo.lookup_remote(n);
55 GLib.debug("connecting '%s'", r.get_url());
58 string[] h = { "a = 1" };
59 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
62 GLib.debug("Got Error Message: %s", e.message);
65 GLib.debug("getting specs '%s'", n);
68 var far = r.get_fetch_specs();
70 foreach(var rs in far) {
71 GLib.debug("got remote spec: %s", rs);
74 var options = new Ggit.FetchOptions();
75 options.set_remote_callbacks(this.callbacks);
76 //yield Async.thread(() => {
78 r.download(far, options);
91 // remotes probably will not work with http auth..
92 var ar = this.repo.list_remotes();
93 foreach(var n in ar) {
94 GLib.debug("got remote '%s'", n);
95 var r = this.repo.lookup_remote(n);
96 GLib.debug("connecting '%s'", r.get_url());
99 string[] h = { "a = 1" };
100 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
103 GLib.debug("Got Error Message: %s", e.message);
106 //GLib.debug("getting specs '%s'", n);
108 var head = this.repo.get_head();
109 this.repo.add_remote_push(
111 "+%s:%s".printf(head.get_shorthand(),head.get_name())
114 var far = r.get_push_specs();
116 foreach(var rs in far) {
117 GLib.debug("got remote spec: %s", rs);
121 var popts = new Ggit.PushOptions();
122 //popts.callbacks = this.callbacks;
124 r.upload(null,popts);
125 GLib.debug("Push done?");
139 private class Callbacks : Ggit.RemoteCallbacks
141 //private Remote d_remote;
142 private Ggit.RemoteCallbacks? d_proxy = null;
144 public delegate void TransferProgress(Ggit.TransferProgress stats);
145 //private TransferProgress? d_transfer_progress;
147 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
151 //d_transfer_progress = (owned)transfer_progress;
154 protected override void progress(string message)
156 GLib.debug("progress");
159 d_proxy.progress(message);
163 protected override void transfer_progress(Ggit.TransferProgress stats)
165 GLib.debug("transfer_progress");
167 if (d_transfer_progress != null)
169 d_transfer_progress(stats);
174 d_proxy.transfer_progress(stats);
179 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
181 GLib.debug("update_tips");
182 //d_remote.tip_updated(refname, a, b);
186 d_proxy.update_tips(refname, a, b);
190 protected override void completion(Ggit.RemoteCompletionType type)
192 GLib.debug("completion");
195 d_proxy.completion(type);
199 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
202 GLib.debug("get credentials %s UN=%s", url, username_from_url);
203 var uri = new Soup.URI(url);
206 var ret = this.netrc(uri.get_host());
211 //return new Ggit.CredPlaintext(username_from_url, "test");
214 /*var provider = d_remote.credentials_provider;
216 if (provider != null)
218 ret = provider.credentials(url, username_from_url, allowed_types);
221 if (ret == null && d_proxy != null)
223 ret = d_proxy.credentials(url, username_from_url, allowed_types);
229 public Ggit.Cred netrc(string domain)
232 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
233 var lines = str.split("\n");
234 for(var i=0; i< lines.length; i++) {
235 // assumes one line per entry.. if not we are buggered...
236 //GLib.debug("got %s" , lines[i]);
238 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
239 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
242 GLib.debug("found password?");
243 // we are gussing....
244 return new Ggit.CredPlaintext(bits[3], bits[5]);