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/gitlive/gitlive");
24 var a = new GitLive.Repo("/home/alan/git/test1-clone");
26 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
27 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
29 var ix = a.repo.get_index();
32 var treeoid = ix.write_tree();
34 var head = a.repo.get_head();
35 var parent = head.lookup() as Ggit.Commit;
36 Ggit.Commit[] parents = (parent == null) ?
37 new Ggit.Commit[] {} :
38 new Ggit.Commit[] { parent };
40 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
42 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
43 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
45 // mmh.. no git add/commit in library...
46 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
47 //string[] spawn_env = Environ.get ();
48 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
56 public class Repo : Object {
58 public Ggit.Repository repo;
61 public Repo(string path)
63 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
64 this.callbacks = new Callbacks(this);
68 public Gee.ArrayList<Git.Branch> branches()
70 this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
75 public void fetchAll()
77 // remotes probably will not work with http auth..
78 var ar = this.repo.list_remotes();
79 foreach(var n in ar) {
80 GLib.debug("got remote '%s'", n);
81 var r = this.repo.lookup_remote(n);
82 GLib.debug("connecting '%s'", r.get_url());
85 string[] h = { "a = 1" };
86 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
89 GLib.debug("Got Error Message: %s", e.message);
92 GLib.debug("getting specs '%s'", n);
95 var far = r.get_fetch_specs();
97 foreach(var rs in far) {
98 GLib.debug("got remote spec: %s", rs);
101 var options = new Ggit.FetchOptions();
102 options.set_remote_callbacks(this.callbacks);
103 //yield Async.thread(() => {
105 r.download(far, options);
116 public void pushAll()
118 // remotes probably will not work with http auth..
119 var ar = this.repo.list_remotes();
120 foreach(var n in ar) {
121 GLib.debug("got remote '%s'", n);
122 var r = this.repo.lookup_remote(n);
123 GLib.debug("connecting '%s'", r.get_url());
126 string[] h = { "a = 1" };
127 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
130 GLib.debug("Got Error Message: %s", e.message);
133 //GLib.debug("getting specs '%s'", n);
136 this.repo.add_remote_push(
138 "+%s:%s".printf(head.get_shorthand(),head.get_name())
141 var head = this.repo.get_head();
143 far += "+%s:%s".printf(head.get_name(),head.get_name());
145 foreach(var rs in far) {
146 GLib.debug("got remote spec: %s", rs);
150 var popts = new Ggit.PushOptions();
151 popts.callbacks = this.callbacks;
154 GLib.debug("Push done?");
168 private class Callbacks : Ggit.RemoteCallbacks
170 //private Remote d_remote;
171 private Ggit.RemoteCallbacks? d_proxy = null;
173 public delegate void TransferProgress(Ggit.TransferProgress stats);
174 //private TransferProgress? d_transfer_progress;
176 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
180 //d_transfer_progress = (owned)transfer_progress;
183 protected override void progress(string message)
185 GLib.debug("progress");
188 d_proxy.progress(message);
192 protected override void transfer_progress(Ggit.TransferProgress stats)
194 GLib.debug("transfer_progress");
196 if (d_transfer_progress != null)
198 d_transfer_progress(stats);
203 d_proxy.transfer_progress(stats);
208 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
210 GLib.debug("update_tips");
211 //d_remote.tip_updated(refname, a, b);
215 d_proxy.update_tips(refname, a, b);
219 protected override void completion(Ggit.RemoteCompletionType type)
221 GLib.debug("completion");
224 d_proxy.completion(type);
228 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
231 GLib.debug("get credentials %s UN=%s", url, username_from_url);
232 var uri = new Soup.URI(url);
235 var ret = this.netrc(uri.get_host());
240 //return new Ggit.CredPlaintext(username_from_url, "test");
243 /*var provider = d_remote.credentials_provider;
245 if (provider != null)
247 ret = provider.credentials(url, username_from_url, allowed_types);
250 if (ret == null && d_proxy != null)
252 ret = d_proxy.credentials(url, username_from_url, allowed_types);
258 public Ggit.Cred netrc(string domain)
261 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
262 var lines = str.split("\n");
263 for(var i=0; i< lines.length; i++) {
264 // assumes one line per entry.. if not we are buggered...
265 //GLib.debug("got %s" , lines[i]);
267 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
268 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
271 GLib.debug("found password?");
272 // we are gussing....
273 return new Ggit.CredPlaintext(bits[3], bits[5]);