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");
23 a.mergeMasterIntoHead();
28 a.loadLocalBranches();
33 var a = new GitLive.Repo("/home/alan/git/test1-clone");
35 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
36 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
38 var ix = a.repo.get_index();
41 var treeoid = ix.write_tree();
43 var head = a.repo.get_head();
44 var parent = head.lookup() as Ggit.Commit;
45 Ggit.Commit[] parents = (parent == null) ?
46 new Ggit.Commit[] {} :
47 new Ggit.Commit[] { parent };
49 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
51 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
52 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
54 // mmh.. no git add/commit in library...
55 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
56 //string[] spawn_env = Environ.get ();
57 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
65 public class Repo : Object {
68 public Ggit.Repository repo;
71 public Repo(string path)
73 this.name = GLib.Path.get_basename(path);
74 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
75 this.callbacks = new Callbacks(this);
78 Ggit.Branch head = null;
79 Gee.ArrayList<Ggit.Branch> branches = null;
80 Ggit.RemoteHead[] remote_heads = null;
81 public void loadLocalBranches(bool force = false)
83 if (!force && this.branches != null) {
87 this.branches = new Gee.ArrayList<Ggit.Branch>();
88 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
90 var br = r.get() as Ggit.Branch;
91 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
92 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
93 //GLib.debug("got branch: name = %s upstream = %s oid = %s ",
94 // br.get_name(), br.get_upstream().get_name(),
96 this.branches.add(br);
98 GLib.debug("HEAD= %s", br.get_name());
107 public bool is_managed()
109 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
110 return this.repo.get_config().get_int32("gitlive.managed") == 1;
114 public bool is_autocommit ()
116 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
117 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
119 public void set_autocommit(bool val)
121 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
125 public void walkDiff()
127 this.loadLocalBranches();
129 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
130 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
132 var a = new Ggit.RevisionWalker(this.repo);
133 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
137 for (var noid = a.next(); noid != null; noid= a.next()) {
138 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
139 GLib.debug("rev: %s",
144 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
145 var parent = commit.get_parents();
146 GLib.debug("parent = %s", parent.get_id(0).to_string());
147 var master_rev = parent.get_id(0);
148 var master_commit = this.repo.lookup_commit(master_rev);;
150 var head_commit = this.repo.lookup_commit(oid);
152 var master_tree = master_commit.get_tree();
153 var head_tree = head_commit.get_tree();
155 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
157 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
158 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
161 // let's try a merge..
162 var mo = new Ggit.MergeOptions();
163 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
164 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
166 if (ix.has_conflicts()) {
167 GLib.debug("merge has conflicts");
170 var treeoid = ix.write_tree();
174 var parents = new Ggit.Commit[] { master_commit };
177 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
179 var sig = new Ggit.Signature.now(
180 this.repo.get_config().get_string("user.name"),
181 this.repo.get_config().get_string("user.email")
183 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
190 public void mergeMasterIntoHead()
192 // assumes head is not master...
193 GLib.debug("head rev = %s", this.head.get_name());
194 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
195 var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
197 var master_commit = this.repo.lookup_commit(master_oid);;
198 var head_commit = this.repo.lookup_commit(head_oid);
201 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
203 var anc_commit = this.repo.lookup_commit(anc_oid);
204 var anc_tree = anc_commit.get_tree();
206 var master_tree = master_commit.get_tree();
207 var head_tree = head_commit.get_tree();
208 var mo = new Ggit.MergeOptions();
209 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
210 var ix = this.repo.merge_trees(anc_tree, master_tree, head_tree, mo);
211 var treeoid = ix.write_tree();
215 var parents = new Ggit.Commit[] { master_commit };
218 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
220 var sig = new Ggit.Signature.now(
221 this.repo.get_config().get_string("user.name"),
222 this.repo.get_config().get_string("user.email")
224 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
231 public bool doMergeClose(string commit_message)
233 this.loadLocalBranches(true);
234 var oldbranch = this.head.get_name();
235 // going to asume this is merge trees..
236 string [] cmd = { "merge", "--squash", oldbranch };
238 cmd = { "commit", "-a" , "-m", commit_message };
241 this.loadBranches(); // updates lastrev..
243 var notification = new Notify.Notification(
244 "Merged branch %s to %s".printf(oldbranch, master),
250 notification.set_timeout(5);
258 public bool is_auto_branch ()
260 if (this.name == "gitlog") {
264 if (this.is_managed()) {
272 public void loadRemoteHeads(bool force = false)
275 if (!force && this.remote_heads != null) {
278 var r = this.repo.lookup_remote("origin");
279 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
280 this.remote_heads = r.list();
286 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
288 //GLib.debug("bn=%s",remote_branch_name);
289 if (remote_branch_name.has_prefix("refs/remotes/")) {
293 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
294 if (target == "HEAD") {
295 target = remote_name +"/master";
298 foreach(var br in this.branches) {
299 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
300 if ( br.get_upstream().get_shorthand() == target) {
305 //GLib.debug("missing %s", remote_branch_name);
311 public void fetchAll()
313 this.loadLocalBranches();
314 this.loadRemoteHeads();
316 // remotes probably will not work with http auth..
317 //var ar = this.repo.list_remotes();
318 //foreach(var n in ar) {
321 GLib.debug("got remote '%s'", n);
322 var r = this.repo.lookup_remote(n);
323 GLib.debug("connecting '%s'", r.get_url());
326 string[] h = { "a = 1" };
327 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
330 GLib.debug("Got Error Message: %s", e.message);
335 foreach(var rh in this.remote_heads) {
336 if (rh.get_name().has_prefix("refs/remotes/")) {
340 var br = this.getBranch(n, rh.get_name());
342 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
344 rh.get_oid().to_string(),
345 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
348 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
349 size_t ahead, behind;
350 this.repo.get_ahead_behind(
357 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
363 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
366 if (far.length < 1) {
367 GLib.debug("no fetch required.. it's uptodate");
372 GLib.debug("getting remote specs '%s', %d", n, far.length);
375 var far = r.get_fetch_specs();
377 foreach(var rs in far) {
378 GLib.debug("got remote spec: %s", rs);
382 var options = new Ggit.FetchOptions();
383 options.set_remote_callbacks(this.callbacks);
384 //yield Async.thread(() => {
386 r.download(far, options);
397 public void pushAll()
400 this.loadLocalBranches();
401 this.loadRemoteHeads();
402 // remotes probably will not work with http auth..
403 //var ar = this.repo.list_remotes();
406 GLib.debug("got remote '%s'", n);
407 var r = this.repo.lookup_remote(n);
408 GLib.debug("connecting '%s'", r.get_url());
410 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
412 //GLib.debug("getting specs '%s'", n);
415 this.repo.add_remote_push(
417 "+%s:%s".printf(head.get_shorthand(),head.get_name())
422 var heads = r.list();
423 foreach(var rh in heads) {
424 if (rh.get_name().has_prefix("refs/remotes/")) {
428 var br = this.getBranch(n, rh.get_name());
430 GLib.debug("got heads: name=%s rev=%s localrev=%s",
432 rh.get_oid().to_string(),
433 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
435 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
436 size_t ahead, behind;
437 this.repo.get_ahead_behind(
444 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
450 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
453 if (far.length < 1) {
454 GLib.debug("no push required.. it's uptodate");
459 /*var head = this.repo.get_head();
461 far += "+%s:%s".printf(head.get_name(),head.get_name());
463 foreach(var rs in far) {
464 GLib.debug("got remote spec: %s", rs);
468 var popts = new Ggit.PushOptions();
469 popts.callbacks = this.callbacks;
472 GLib.debug("Push done?");
485 private class Callbacks : Ggit.RemoteCallbacks
487 //private Remote d_remote;
488 private Ggit.RemoteCallbacks? d_proxy = null;
490 public delegate void TransferProgress(Ggit.TransferProgress stats);
491 //private TransferProgress? d_transfer_progress;
493 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
497 //d_transfer_progress = (owned)transfer_progress;
500 protected override void progress(string message)
502 GLib.debug("progress: %s", message);
505 d_proxy.progress(message);
509 protected override void transfer_progress(Ggit.TransferProgress stats)
511 GLib.debug("transfer_progress");
513 if (d_transfer_progress != null)
515 d_transfer_progress(stats);
520 d_proxy.transfer_progress(stats);
525 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
527 GLib.debug("update_tips");
528 //d_remote.tip_updated(refname, a, b);
532 d_proxy.update_tips(refname, a, b);
536 protected override void completion(Ggit.RemoteCompletionType type)
538 GLib.debug("completion");
541 d_proxy.completion(type);
545 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
548 GLib.debug("get credentials %s UN=%s", url, username_from_url);
549 var uri = new Soup.URI(url);
552 var ret = this.netrc(uri.get_host());
557 //return new Ggit.CredPlaintext(username_from_url, "test");
560 /*var provider = d_remote.credentials_provider;
562 if (provider != null)
564 ret = provider.credentials(url, username_from_url, allowed_types);
567 if (ret == null && d_proxy != null)
569 ret = d_proxy.credentials(url, username_from_url, allowed_types);
575 public Ggit.Cred netrc(string domain)
578 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
579 var lines = str.split("\n");
580 for(var i=0; i< lines.length; i++) {
581 // assumes one line per entry.. if not we are buggered...
582 //GLib.debug("got %s" , lines[i]);
584 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
585 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
588 GLib.debug("found password?");
589 // we are gussing....
590 return new Ggit.CredPlaintext(bits[3], bits[5]);