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 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
73 GLib.debug("got branch: name = %s ", ref.get_name());
79 public void fetchAll()
81 // remotes probably will not work with http auth..
82 var ar = this.repo.list_remotes();
83 foreach(var n in ar) {
84 GLib.debug("got remote '%s'", n);
85 var r = this.repo.lookup_remote(n);
86 GLib.debug("connecting '%s'", r.get_url());
89 string[] h = { "a = 1" };
90 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
93 GLib.debug("Got Error Message: %s", e.message);
96 GLib.debug("getting specs '%s'", n);
99 var far = r.get_fetch_specs();
101 foreach(var rs in far) {
102 GLib.debug("got remote spec: %s", rs);
105 var options = new Ggit.FetchOptions();
106 options.set_remote_callbacks(this.callbacks);
107 //yield Async.thread(() => {
109 r.download(far, options);
120 public void pushAll()
122 // remotes probably will not work with http auth..
123 var ar = this.repo.list_remotes();
124 foreach(var n in ar) {
125 GLib.debug("got remote '%s'", n);
126 var r = this.repo.lookup_remote(n);
127 GLib.debug("connecting '%s'", r.get_url());
130 string[] h = { "a = 1" };
131 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
134 GLib.debug("Got Error Message: %s", e.message);
137 //GLib.debug("getting specs '%s'", n);
140 this.repo.add_remote_push(
142 "+%s:%s".printf(head.get_shorthand(),head.get_name())
145 var head = this.repo.get_head();
147 far += "+%s:%s".printf(head.get_name(),head.get_name());
149 foreach(var rs in far) {
150 GLib.debug("got remote spec: %s", rs);
154 var popts = new Ggit.PushOptions();
155 popts.callbacks = this.callbacks;
158 GLib.debug("Push done?");
172 private class Callbacks : Ggit.RemoteCallbacks
174 //private Remote d_remote;
175 private Ggit.RemoteCallbacks? d_proxy = null;
177 public delegate void TransferProgress(Ggit.TransferProgress stats);
178 //private TransferProgress? d_transfer_progress;
180 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
184 //d_transfer_progress = (owned)transfer_progress;
187 protected override void progress(string message)
189 GLib.debug("progress");
192 d_proxy.progress(message);
196 protected override void transfer_progress(Ggit.TransferProgress stats)
198 GLib.debug("transfer_progress");
200 if (d_transfer_progress != null)
202 d_transfer_progress(stats);
207 d_proxy.transfer_progress(stats);
212 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
214 GLib.debug("update_tips");
215 //d_remote.tip_updated(refname, a, b);
219 d_proxy.update_tips(refname, a, b);
223 protected override void completion(Ggit.RemoteCompletionType type)
225 GLib.debug("completion");
228 d_proxy.completion(type);
232 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
235 GLib.debug("get credentials %s UN=%s", url, username_from_url);
236 var uri = new Soup.URI(url);
239 var ret = this.netrc(uri.get_host());
244 //return new Ggit.CredPlaintext(username_from_url, "test");
247 /*var provider = d_remote.credentials_provider;
249 if (provider != null)
251 ret = provider.credentials(url, username_from_url, allowed_types);
254 if (ret == null && d_proxy != null)
256 ret = d_proxy.credentials(url, username_from_url, allowed_types);
262 public Ggit.Cred netrc(string domain)
265 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
266 var lines = str.split("\n");
267 for(var i=0; i< lines.length; i++) {
268 // assumes one line per entry.. if not we are buggered...
269 //GLib.debug("got %s" , lines[i]);
271 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
272 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
275 GLib.debug("found password?");
276 // we are gussing....
277 return new Ggit.CredPlaintext(bits[3], bits[5]);