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 a.loadLocalBranches();
30 var a = new GitLive.Repo("/home/alan/git/test1-clone");
32 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
33 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
35 var ix = a.repo.get_index();
38 var treeoid = ix.write_tree();
40 var head = a.repo.get_head();
41 var parent = head.lookup() as Ggit.Commit;
42 Ggit.Commit[] parents = (parent == null) ?
43 new Ggit.Commit[] {} :
44 new Ggit.Commit[] { parent };
46 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
48 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
49 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
51 // mmh.. no git add/commit in library...
52 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
53 //string[] spawn_env = Environ.get ();
54 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
62 public class Repo : Object {
64 public Ggit.Repository repo;
67 public Repo(string path)
69 this.name = GLib.Path.get_basename(path);
70 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
71 this.callbacks = new Callbacks(this);
74 Ggit.Branch head = null;
75 Gee.ArrayList<Ggit.Branch> branches = null;
76 Ggit.RemoteHead[] remote_heads = null;
77 public void loadLocalBranches(bool force = false)
79 if (!force && this.branches != null) {
83 this.branches = new Gee.ArrayList<Ggit.Branch>();
84 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
86 var br = r.get() as Ggit.Branch;
87 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
88 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
89 //GLib.debug("got branch: name = %s upstream = %s oid = %s ",
90 // br.get_name(), br.get_upstream().get_name(),
92 this.branches.add(br);
94 GLib.debug("HEAD= %s", br.get_name());
103 public bool is_managed()
105 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
106 return this.repo.get_config().get_int32("gitlive.managed") == 1;
110 public bool is_autocommit ()
112 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
113 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
115 public void set_autocommit(bool val)
117 this.repo.get_config().set_int32("gitlive.autocommit", val ? "1" : "0");
121 public bool doMergeClose(string commit_message)
123 this.loadLocalBranches(true);
124 var oldbranch = this.head.get_name();
126 string [] cmd = { "merge", "--squash", oldbranch };
128 cmd = { "commit", "-a" , "-m", commit_message };
131 this.loadBranches(); // updates lastrev..
133 var notification = new Notify.Notification(
134 "Merged branch %s to %s".printf(oldbranch, master),
140 notification.set_timeout(5);
146 public bool is_auto_branch ()
148 if (this.name == "gitlog") {
152 if (this.is_managed()) {
160 public void loadRemoteHeads(bool force = false)
163 if (!force && this.remote_heads != null) {
166 var r = this.repo.lookup_remote("origin");
167 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
168 this.remote_heads = r.list();
174 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
176 //GLib.debug("bn=%s",remote_branch_name);
177 if (remote_branch_name.has_prefix("refs/remotes/")) {
181 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
182 if (target == "HEAD") {
183 target = remote_name +"/master";
186 foreach(var br in this.branches) {
187 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
188 if ( br.get_upstream().get_shorthand() == target) {
193 //GLib.debug("missing %s", remote_branch_name);
199 public void fetchAll()
201 this.loadLocalBranches();
202 this.loadRemoteHeads();
204 // remotes probably will not work with http auth..
205 //var ar = this.repo.list_remotes();
206 //foreach(var n in ar) {
209 GLib.debug("got remote '%s'", n);
210 var r = this.repo.lookup_remote(n);
211 GLib.debug("connecting '%s'", r.get_url());
214 string[] h = { "a = 1" };
215 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
218 GLib.debug("Got Error Message: %s", e.message);
223 foreach(var rh in this.remote_heads) {
224 if (rh.get_name().has_prefix("refs/remotes/")) {
228 var br = this.getBranch(n, rh.get_name());
230 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
232 rh.get_oid().to_string(),
233 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
236 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
237 size_t ahead, behind;
238 this.repo.get_ahead_behind(
245 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
251 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
254 if (far.length < 1) {
255 GLib.debug("no fetch required.. it's uptodate");
260 GLib.debug("getting remote specs '%s', %d", n, far.length);
263 var far = r.get_fetch_specs();
265 foreach(var rs in far) {
266 GLib.debug("got remote spec: %s", rs);
270 var options = new Ggit.FetchOptions();
271 options.set_remote_callbacks(this.callbacks);
272 //yield Async.thread(() => {
274 r.download(far, options);
285 public void pushAll()
288 this.loadLocalBranches();
289 this.loadRemoteHeads();
290 // remotes probably will not work with http auth..
291 //var ar = this.repo.list_remotes();
294 GLib.debug("got remote '%s'", n);
295 var r = this.repo.lookup_remote(n);
296 GLib.debug("connecting '%s'", r.get_url());
298 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
300 //GLib.debug("getting specs '%s'", n);
303 this.repo.add_remote_push(
305 "+%s:%s".printf(head.get_shorthand(),head.get_name())
310 var heads = r.list();
311 foreach(var rh in heads) {
312 if (rh.get_name().has_prefix("refs/remotes/")) {
316 var br = this.getBranch(n, rh.get_name());
318 GLib.debug("got heads: name=%s rev=%s localrev=%s",
320 rh.get_oid().to_string(),
321 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
323 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
324 size_t ahead, behind;
325 this.repo.get_ahead_behind(
332 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
338 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
341 if (far.length < 1) {
342 GLib.debug("no push required.. it's uptodate");
347 /*var head = this.repo.get_head();
349 far += "+%s:%s".printf(head.get_name(),head.get_name());
351 foreach(var rs in far) {
352 GLib.debug("got remote spec: %s", rs);
356 var popts = new Ggit.PushOptions();
357 popts.callbacks = this.callbacks;
360 GLib.debug("Push done?");
373 private class Callbacks : Ggit.RemoteCallbacks
375 //private Remote d_remote;
376 private Ggit.RemoteCallbacks? d_proxy = null;
378 public delegate void TransferProgress(Ggit.TransferProgress stats);
379 //private TransferProgress? d_transfer_progress;
381 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
385 //d_transfer_progress = (owned)transfer_progress;
388 protected override void progress(string message)
390 GLib.debug("progress: %s", message);
393 d_proxy.progress(message);
397 protected override void transfer_progress(Ggit.TransferProgress stats)
399 GLib.debug("transfer_progress");
401 if (d_transfer_progress != null)
403 d_transfer_progress(stats);
408 d_proxy.transfer_progress(stats);
413 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
415 GLib.debug("update_tips");
416 //d_remote.tip_updated(refname, a, b);
420 d_proxy.update_tips(refname, a, b);
424 protected override void completion(Ggit.RemoteCompletionType type)
426 GLib.debug("completion");
429 d_proxy.completion(type);
433 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
436 GLib.debug("get credentials %s UN=%s", url, username_from_url);
437 var uri = new Soup.URI(url);
440 var ret = this.netrc(uri.get_host());
445 //return new Ggit.CredPlaintext(username_from_url, "test");
448 /*var provider = d_remote.credentials_provider;
450 if (provider != null)
452 ret = provider.credentials(url, username_from_url, allowed_types);
455 if (ret == null && d_proxy != null)
457 ret = d_proxy.credentials(url, username_from_url, allowed_types);
463 public Ggit.Cred netrc(string domain)
466 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
467 var lines = str.split("\n");
468 for(var i=0; i< lines.length; i++) {
469 // assumes one line per entry.. if not we are buggered...
470 //GLib.debug("got %s" , lines[i]);
472 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
473 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
476 GLib.debug("found password?");
477 // we are gussing....
478 return new Ggit.CredPlaintext(bits[3], bits[5]);