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();
30 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
31 a.repo.repo.create_commit("HEAD", sig, sig, null, "test commit " + str,
32 // mmh.. no git add/commit in library...
33 string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
34 string[] spawn_env = Environ.get ();
35 Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
43 public class Repo : Object {
45 public Ggit.Repository repo;
48 public Repo(string path)
50 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
51 this.callbacks = new Callbacks(this);
54 public void fetchAll()
56 // remotes probably will not work with http auth..
57 var ar = this.repo.list_remotes();
58 foreach(var n in ar) {
59 GLib.debug("got remote '%s'", n);
60 var r = this.repo.lookup_remote(n);
61 GLib.debug("connecting '%s'", r.get_url());
64 string[] h = { "a = 1" };
65 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
68 GLib.debug("Got Error Message: %s", e.message);
71 GLib.debug("getting specs '%s'", n);
74 var far = r.get_fetch_specs();
76 foreach(var rs in far) {
77 GLib.debug("got remote spec: %s", rs);
80 var options = new Ggit.FetchOptions();
81 options.set_remote_callbacks(this.callbacks);
82 //yield Async.thread(() => {
84 r.download(far, options);
97 // remotes probably will not work with http auth..
98 var ar = this.repo.list_remotes();
99 foreach(var n in ar) {
100 GLib.debug("got remote '%s'", n);
101 var r = this.repo.lookup_remote(n);
102 GLib.debug("connecting '%s'", r.get_url());
105 string[] h = { "a = 1" };
106 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
109 GLib.debug("Got Error Message: %s", e.message);
112 //GLib.debug("getting specs '%s'", n);
114 var head = this.repo.get_head();
115 this.repo.add_remote_push(
117 "+%s:%s".printf(head.get_shorthand(),head.get_name())
120 var far = r.get_push_specs();
122 foreach(var rs in far) {
123 GLib.debug("got remote spec: %s", rs);
127 var popts = new Ggit.PushOptions();
128 //popts.callbacks = this.callbacks;
131 GLib.debug("Push done?");
145 private class Callbacks : Ggit.RemoteCallbacks
147 //private Remote d_remote;
148 private Ggit.RemoteCallbacks? d_proxy = null;
150 public delegate void TransferProgress(Ggit.TransferProgress stats);
151 //private TransferProgress? d_transfer_progress;
153 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
157 //d_transfer_progress = (owned)transfer_progress;
160 protected override void progress(string message)
162 GLib.debug("progress");
165 d_proxy.progress(message);
169 protected override void transfer_progress(Ggit.TransferProgress stats)
171 GLib.debug("transfer_progress");
173 if (d_transfer_progress != null)
175 d_transfer_progress(stats);
180 d_proxy.transfer_progress(stats);
185 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
187 GLib.debug("update_tips");
188 //d_remote.tip_updated(refname, a, b);
192 d_proxy.update_tips(refname, a, b);
196 protected override void completion(Ggit.RemoteCompletionType type)
198 GLib.debug("completion");
201 d_proxy.completion(type);
205 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
208 GLib.debug("get credentials %s UN=%s", url, username_from_url);
209 var uri = new Soup.URI(url);
212 var ret = this.netrc(uri.get_host());
217 //return new Ggit.CredPlaintext(username_from_url, "test");
220 /*var provider = d_remote.credentials_provider;
222 if (provider != null)
224 ret = provider.credentials(url, username_from_url, allowed_types);
227 if (ret == null && d_proxy != null)
229 ret = d_proxy.credentials(url, username_from_url, allowed_types);
235 public Ggit.Cred netrc(string domain)
238 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
239 var lines = str.split("\n");
240 for(var i=0; i< lines.length; i++) {
241 // assumes one line per entry.. if not we are buggered...
242 //GLib.debug("got %s" , lines[i]);
244 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
245 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
248 GLib.debug("found password?");
249 // we are gussing....
250 return new Ggit.CredPlaintext(bits[3], bits[5]);