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");
26 var a = new GitLive.Repo("/home/alan/git/test1-clone");
28 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
29 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
31 var ix = a.repo.get_index();
34 var treeoid = ix.write_tree();
36 var head = a.repo.get_head();
37 var parent = head.lookup() as Ggit.Commit;
38 Ggit.Commit[] parents = (parent == null) ?
39 new Ggit.Commit[] {} :
40 new Ggit.Commit[] { parent };
42 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
44 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
45 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
47 // mmh.. no git add/commit in library...
48 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
49 //string[] spawn_env = Environ.get ();
50 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
58 public class Repo : Object {
60 public Ggit.Repository repo;
63 public Repo(string path)
65 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
66 this.callbacks = new Callbacks(this);
70 Gee.ArrayList<Ggit.Branch> branches = null;
71 public Gee.ArrayList<Ggit.Branch> loadBranches()
73 var ret = new Gee.ArrayList<Ggit.Branch>();
74 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
80 var br = r.get() as Ggit.Branch;
81 var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
82 var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
83 GLib.debug("got branch: name = %s upstream = %s oid = %s roid= %s",
84 br.get_name(), br.get_upstream().get_name(),
85 head.to_string(), rhead.to_string());
96 public void fetchAll()
98 // remotes probably will not work with http auth..
99 var ar = this.repo.list_remotes();
100 foreach(var n in ar) {
101 GLib.debug("got remote '%s'", n);
102 var r = this.repo.lookup_remote(n);
103 GLib.debug("connecting '%s'", r.get_url());
106 string[] h = { "a = 1" };
107 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
110 GLib.debug("Got Error Message: %s", e.message);
114 var heads = r.list();
115 foreach(var rh in heads) {
116 rh.get_shotor_name(),
118 GLib.debug("got heads: name=%s rev=%s localrev=%s",
120 rh.get_oid().to_string(),
121 rh.get_local_oid().to_string()
128 GLib.debug("getting specs '%s'", n);
131 var far = r.get_fetch_specs();
133 foreach(var rs in far) {
134 GLib.debug("got remote spec: %s", rs);
137 var options = new Ggit.FetchOptions();
138 options.set_remote_callbacks(this.callbacks);
139 //yield Async.thread(() => {
141 r.download(far, options);
152 public void pushAll()
154 // remotes probably will not work with http auth..
155 var ar = this.repo.list_remotes();
156 foreach(var n in ar) {
157 GLib.debug("got remote '%s'", n);
158 var r = this.repo.lookup_remote(n);
159 GLib.debug("connecting '%s'", r.get_url());
162 string[] h = { "a = 1" };
163 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
166 GLib.debug("Got Error Message: %s", e.message);
169 //GLib.debug("getting specs '%s'", n);
172 this.repo.add_remote_push(
174 "+%s:%s".printf(head.get_shorthand(),head.get_name())
177 var head = this.repo.get_head();
179 far += "+%s:%s".printf(head.get_name(),head.get_name());
181 foreach(var rs in far) {
182 GLib.debug("got remote spec: %s", rs);
186 var popts = new Ggit.PushOptions();
187 popts.callbacks = this.callbacks;
190 GLib.debug("Push done?");
204 private class Callbacks : Ggit.RemoteCallbacks
206 //private Remote d_remote;
207 private Ggit.RemoteCallbacks? d_proxy = null;
209 public delegate void TransferProgress(Ggit.TransferProgress stats);
210 //private TransferProgress? d_transfer_progress;
212 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
216 //d_transfer_progress = (owned)transfer_progress;
219 protected override void progress(string message)
221 GLib.debug("progress");
224 d_proxy.progress(message);
228 protected override void transfer_progress(Ggit.TransferProgress stats)
230 GLib.debug("transfer_progress");
232 if (d_transfer_progress != null)
234 d_transfer_progress(stats);
239 d_proxy.transfer_progress(stats);
244 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
246 GLib.debug("update_tips");
247 //d_remote.tip_updated(refname, a, b);
251 d_proxy.update_tips(refname, a, b);
255 protected override void completion(Ggit.RemoteCompletionType type)
257 GLib.debug("completion");
260 d_proxy.completion(type);
264 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
267 GLib.debug("get credentials %s UN=%s", url, username_from_url);
268 var uri = new Soup.URI(url);
271 var ret = this.netrc(uri.get_host());
276 //return new Ggit.CredPlaintext(username_from_url, "test");
279 /*var provider = d_remote.credentials_provider;
281 if (provider != null)
283 ret = provider.credentials(url, username_from_url, allowed_types);
286 if (ret == null && d_proxy != null)
288 ret = d_proxy.credentials(url, username_from_url, allowed_types);
294 public Ggit.Cred netrc(string domain)
297 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
298 var lines = str.split("\n");
299 for(var i=0; i< lines.length; i++) {
300 // assumes one line per entry.. if not we are buggered...
301 //GLib.debug("got %s" , lines[i]);
303 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
304 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
307 GLib.debug("found password?");
308 // we are gussing....
309 return new Ggit.CredPlaintext(bits[3], bits[5]);