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.get_index();
30 var treeoid = index.write_tree();
32 var head = d_repository.get_head();
33 var parent = head.lookup() as Ggit.Commit;
34 Ggit.Commit[] parents = (parent == null) ?
35 new Ggit.Commit[] {} :
36 new Ggit.Commit[] { parent };
38 var tree = a.repo.lookup<Ggit.Tree>(treeoid); // odd format..
40 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
41 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
42 // mmh.. no git add/commit in library...
43 string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
44 string[] spawn_env = Environ.get ();
45 Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
53 public class Repo : Object {
55 public Ggit.Repository repo;
58 public Repo(string path)
60 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
61 this.callbacks = new Callbacks(this);
64 public void fetchAll()
66 // remotes probably will not work with http auth..
67 var ar = this.repo.list_remotes();
68 foreach(var n in ar) {
69 GLib.debug("got remote '%s'", n);
70 var r = this.repo.lookup_remote(n);
71 GLib.debug("connecting '%s'", r.get_url());
74 string[] h = { "a = 1" };
75 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
78 GLib.debug("Got Error Message: %s", e.message);
81 GLib.debug("getting specs '%s'", n);
84 var far = r.get_fetch_specs();
86 foreach(var rs in far) {
87 GLib.debug("got remote spec: %s", rs);
90 var options = new Ggit.FetchOptions();
91 options.set_remote_callbacks(this.callbacks);
92 //yield Async.thread(() => {
94 r.download(far, options);
105 public void pushAll()
107 // remotes probably will not work with http auth..
108 var ar = this.repo.list_remotes();
109 foreach(var n in ar) {
110 GLib.debug("got remote '%s'", n);
111 var r = this.repo.lookup_remote(n);
112 GLib.debug("connecting '%s'", r.get_url());
115 string[] h = { "a = 1" };
116 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
119 GLib.debug("Got Error Message: %s", e.message);
122 //GLib.debug("getting specs '%s'", n);
124 var head = this.repo.get_head();
125 this.repo.add_remote_push(
127 "+%s:%s".printf(head.get_shorthand(),head.get_name())
130 var far = r.get_push_specs();
132 foreach(var rs in far) {
133 GLib.debug("got remote spec: %s", rs);
137 var popts = new Ggit.PushOptions();
138 //popts.callbacks = this.callbacks;
141 GLib.debug("Push done?");
155 private class Callbacks : Ggit.RemoteCallbacks
157 //private Remote d_remote;
158 private Ggit.RemoteCallbacks? d_proxy = null;
160 public delegate void TransferProgress(Ggit.TransferProgress stats);
161 //private TransferProgress? d_transfer_progress;
163 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
167 //d_transfer_progress = (owned)transfer_progress;
170 protected override void progress(string message)
172 GLib.debug("progress");
175 d_proxy.progress(message);
179 protected override void transfer_progress(Ggit.TransferProgress stats)
181 GLib.debug("transfer_progress");
183 if (d_transfer_progress != null)
185 d_transfer_progress(stats);
190 d_proxy.transfer_progress(stats);
195 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
197 GLib.debug("update_tips");
198 //d_remote.tip_updated(refname, a, b);
202 d_proxy.update_tips(refname, a, b);
206 protected override void completion(Ggit.RemoteCompletionType type)
208 GLib.debug("completion");
211 d_proxy.completion(type);
215 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
218 GLib.debug("get credentials %s UN=%s", url, username_from_url);
219 var uri = new Soup.URI(url);
222 var ret = this.netrc(uri.get_host());
227 //return new Ggit.CredPlaintext(username_from_url, "test");
230 /*var provider = d_remote.credentials_provider;
232 if (provider != null)
234 ret = provider.credentials(url, username_from_url, allowed_types);
237 if (ret == null && d_proxy != null)
239 ret = d_proxy.credentials(url, username_from_url, allowed_types);
245 public Ggit.Cred netrc(string domain)
248 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
249 var lines = str.split("\n");
250 for(var i=0; i< lines.length; i++) {
251 // assumes one line per entry.. if not we are buggered...
252 //GLib.debug("got %s" , lines[i]);
254 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
255 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
258 GLib.debug("found password?");
259 // we are gussing....
260 return new Ggit.CredPlaintext(bits[3], bits[5]);