2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -pkg gee-0.8 -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/gitlive/gitlive");
25 var a = new GitLive.Repo("/home/alan/git/test1-clone");
27 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
28 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
30 var ix = a.repo.get_index();
33 var treeoid = ix.write_tree();
35 var head = a.repo.get_head();
36 var parent = head.lookup() as Ggit.Commit;
37 Ggit.Commit[] parents = (parent == null) ?
38 new Ggit.Commit[] {} :
39 new Ggit.Commit[] { parent };
41 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
43 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
44 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
46 // mmh.. no git add/commit in library...
47 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
48 //string[] spawn_env = Environ.get ();
49 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
57 public class Repo : Object {
59 public Ggit.Repository repo;
62 public Repo(string path)
64 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
65 this.callbacks = new Callbacks(this);
69 public Gee.ArrayList<Ggit.Branch> branches()
71 var ret = new Gee.ArrayList<Ggit.Branch>();
72 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
75 var br = r.get() as Ggit.Branch;
76 var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
77 var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
78 GLib.debug("got branch: name = %s upstream = %s oid = %s", br.get_name(), br.get_upstream().get_name(), head.to_string());
86 public void fetchAll()
88 // remotes probably will not work with http auth..
89 var ar = this.repo.list_remotes();
90 foreach(var n in ar) {
91 GLib.debug("got remote '%s'", n);
92 var r = this.repo.lookup_remote(n);
93 GLib.debug("connecting '%s'", r.get_url());
96 string[] h = { "a = 1" };
97 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
100 GLib.debug("Got Error Message: %s", e.message);
103 GLib.debug("getting specs '%s'", n);
106 var far = r.get_fetch_specs();
108 foreach(var rs in far) {
109 GLib.debug("got remote spec: %s", rs);
112 var options = new Ggit.FetchOptions();
113 options.set_remote_callbacks(this.callbacks);
114 //yield Async.thread(() => {
116 r.download(far, options);
127 public void pushAll()
129 // remotes probably will not work with http auth..
130 var ar = this.repo.list_remotes();
131 foreach(var n in ar) {
132 GLib.debug("got remote '%s'", n);
133 var r = this.repo.lookup_remote(n);
134 GLib.debug("connecting '%s'", r.get_url());
137 string[] h = { "a = 1" };
138 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
141 GLib.debug("Got Error Message: %s", e.message);
144 //GLib.debug("getting specs '%s'", n);
147 this.repo.add_remote_push(
149 "+%s:%s".printf(head.get_shorthand(),head.get_name())
152 var head = this.repo.get_head();
154 far += "+%s:%s".printf(head.get_name(),head.get_name());
156 foreach(var rs in far) {
157 GLib.debug("got remote spec: %s", rs);
161 var popts = new Ggit.PushOptions();
162 popts.callbacks = this.callbacks;
165 GLib.debug("Push done?");
179 private class Callbacks : Ggit.RemoteCallbacks
181 //private Remote d_remote;
182 private Ggit.RemoteCallbacks? d_proxy = null;
184 public delegate void TransferProgress(Ggit.TransferProgress stats);
185 //private TransferProgress? d_transfer_progress;
187 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
191 //d_transfer_progress = (owned)transfer_progress;
194 protected override void progress(string message)
196 GLib.debug("progress");
199 d_proxy.progress(message);
203 protected override void transfer_progress(Ggit.TransferProgress stats)
205 GLib.debug("transfer_progress");
207 if (d_transfer_progress != null)
209 d_transfer_progress(stats);
214 d_proxy.transfer_progress(stats);
219 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
221 GLib.debug("update_tips");
222 //d_remote.tip_updated(refname, a, b);
226 d_proxy.update_tips(refname, a, b);
230 protected override void completion(Ggit.RemoteCompletionType type)
232 GLib.debug("completion");
235 d_proxy.completion(type);
239 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
242 GLib.debug("get credentials %s UN=%s", url, username_from_url);
243 var uri = new Soup.URI(url);
246 var ret = this.netrc(uri.get_host());
251 //return new Ggit.CredPlaintext(username_from_url, "test");
254 /*var provider = d_remote.credentials_provider;
256 if (provider != null)
258 ret = provider.credentials(url, username_from_url, allowed_types);
261 if (ret == null && d_proxy != null)
263 ret = d_proxy.credentials(url, username_from_url, allowed_types);
269 public Ggit.Cred netrc(string domain)
272 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
273 var lines = str.split("\n");
274 for(var i=0; i< lines.length; i++) {
275 // assumes one line per entry.. if not we are buggered...
276 //GLib.debug("got %s" , lines[i]);
278 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
279 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
282 GLib.debug("found password?");
283 // we are gussing....
284 return new Ggit.CredPlaintext(bits[3], bits[5]);