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");
27 var a = new GitLive.Repo("/home/alan/git/test1-clone");
29 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
30 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
32 var ix = a.repo.get_index();
35 var treeoid = ix.write_tree();
37 var head = a.repo.get_head();
38 var parent = head.lookup() as Ggit.Commit;
39 Ggit.Commit[] parents = (parent == null) ?
40 new Ggit.Commit[] {} :
41 new Ggit.Commit[] { parent };
43 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
45 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
46 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
48 // mmh.. no git add/commit in library...
49 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
50 //string[] spawn_env = Environ.get ();
51 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
59 public class Repo : Object {
61 public Ggit.Repository repo;
64 public Repo(string path)
66 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
67 this.callbacks = new Callbacks(this);
71 Gee.ArrayList<Ggit.Branch> branches = null;
72 public void loadBranches()
74 this.branches = new Gee.ArrayList<Ggit.Branch>();
75 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
81 var br = r.get() as Ggit.Branch;
82 var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
83 var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
84 GLib.debug("got branch: name = %s upstream = %s oid = %s ",
85 br.get_name(), br.get_upstream().get_name(),
87 this.branches.add(br);
94 Ggit.Branch? getBranch(string remote_name)
96 foreach(var br in this.branches) {
97 if (br.get_upstream().get_name() == remote_name ||
98 "refs/heads/" + br.get_upstream().get_name() == remote_name
110 public void fetchAll()
112 // remotes probably will not work with http auth..
113 var ar = this.repo.list_remotes();
114 foreach(var n in ar) {
115 GLib.debug("got remote '%s'", n);
116 var r = this.repo.lookup_remote(n);
117 GLib.debug("connecting '%s'", r.get_url());
120 string[] h = { "a = 1" };
121 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
124 GLib.debug("Got Error Message: %s", e.message);
128 var heads = r.list();
129 foreach(var rh in heads) {
130 var br = this.getBranch(rh.get_name());
132 GLib.debug("got heads: name=%s rev=%s localrev=%s",
134 rh.get_oid().to_string(),
135 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
142 GLib.debug("getting specs '%s'", n);
145 var far = r.get_fetch_specs();
147 foreach(var rs in far) {
148 GLib.debug("got remote spec: %s", rs);
151 var options = new Ggit.FetchOptions();
152 options.set_remote_callbacks(this.callbacks);
153 //yield Async.thread(() => {
155 r.download(far, options);
166 public void pushAll()
168 // remotes probably will not work with http auth..
169 var ar = this.repo.list_remotes();
170 foreach(var n in ar) {
171 GLib.debug("got remote '%s'", n);
172 var r = this.repo.lookup_remote(n);
173 GLib.debug("connecting '%s'", r.get_url());
176 string[] h = { "a = 1" };
177 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
180 GLib.debug("Got Error Message: %s", e.message);
183 //GLib.debug("getting specs '%s'", n);
186 this.repo.add_remote_push(
188 "+%s:%s".printf(head.get_shorthand(),head.get_name())
191 var head = this.repo.get_head();
193 far += "+%s:%s".printf(head.get_name(),head.get_name());
195 foreach(var rs in far) {
196 GLib.debug("got remote spec: %s", rs);
200 var popts = new Ggit.PushOptions();
201 popts.callbacks = this.callbacks;
204 GLib.debug("Push done?");
218 private class Callbacks : Ggit.RemoteCallbacks
220 //private Remote d_remote;
221 private Ggit.RemoteCallbacks? d_proxy = null;
223 public delegate void TransferProgress(Ggit.TransferProgress stats);
224 //private TransferProgress? d_transfer_progress;
226 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
230 //d_transfer_progress = (owned)transfer_progress;
233 protected override void progress(string message)
235 GLib.debug("progress");
238 d_proxy.progress(message);
242 protected override void transfer_progress(Ggit.TransferProgress stats)
244 GLib.debug("transfer_progress");
246 if (d_transfer_progress != null)
248 d_transfer_progress(stats);
253 d_proxy.transfer_progress(stats);
258 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
260 GLib.debug("update_tips");
261 //d_remote.tip_updated(refname, a, b);
265 d_proxy.update_tips(refname, a, b);
269 protected override void completion(Ggit.RemoteCompletionType type)
271 GLib.debug("completion");
274 d_proxy.completion(type);
278 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
281 GLib.debug("get credentials %s UN=%s", url, username_from_url);
282 var uri = new Soup.URI(url);
285 var ret = this.netrc(uri.get_host());
290 //return new Ggit.CredPlaintext(username_from_url, "test");
293 /*var provider = d_remote.credentials_provider;
295 if (provider != null)
297 ret = provider.credentials(url, username_from_url, allowed_types);
300 if (ret == null && d_proxy != null)
302 ret = d_proxy.credentials(url, username_from_url, allowed_types);
308 public Ggit.Cred netrc(string domain)
311 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
312 var lines = str.split("\n");
313 for(var i=0; i< lines.length; i++) {
314 // assumes one line per entry.. if not we are buggered...
315 //GLib.debug("got %s" , lines[i]);
317 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
318 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
321 GLib.debug("found password?");
322 // we are gussing....
323 return new Ggit.CredPlaintext(bits[3], bits[5]);