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 loop = new MainLoop();
24 var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
26 a.loadRemoteHeads.begin((obj,res) => {
27 a.loadRemoteHeads.end(res);
29 a.loadLocalBranches();
33 //a.mergeMasterIntoHead();
43 var a = new GitLive.Repo("/home/alan/git/test1-clone");
45 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
46 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
48 var ix = a.repo.get_index();
51 var treeoid = ix.write_tree();
53 var head = a.repo.get_head();
54 var parent = head.lookup() as Ggit.Commit;
55 Ggit.Commit[] parents = (parent == null) ?
56 new Ggit.Commit[] {} :
57 new Ggit.Commit[] { parent };
59 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
61 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
62 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
64 // mmh.. no git add/commit in library...
65 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
66 //string[] spawn_env = Environ.get ();
67 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
75 public class Repo : Object {
78 public Ggit.Repository repo;
81 public Repo(string path)
83 this.name = GLib.Path.get_basename(path);
84 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
85 this.callbacks = new Callbacks(this);
88 Ggit.Branch head = null;
89 Gee.ArrayList<Ggit.Branch> branches = null;
90 Ggit.RemoteHead[] remote_heads = null;
91 public void loadLocalBranches(bool force = false)
93 if (!force && this.branches != null) {
97 this.branches = new Gee.ArrayList<Ggit.Branch>();
98 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
100 var br = r.get() as Ggit.Branch;
101 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
102 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
103 GLib.debug("got branch: H=%s name = %s upstream = %s oid = %s ",
104 br.is_head() ? "Y" : "n",
106 br.get_upstream().get_name().substring(20),
107 br.get_target().to_string());
108 this.branches.add(br);
110 GLib.debug("HEAD= %s", br.get_name());
119 public bool is_managed()
121 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
122 return this.repo.get_config().get_int32("gitlive.managed") == 1;
126 public bool is_autocommit ()
128 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
129 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
131 public void set_autocommit(bool val)
133 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
137 public void walkDiff()
139 this.loadLocalBranches();
143 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
144 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
146 var a = new Ggit.RevisionWalker(this.repo);
147 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
151 for (var noid = a.next(); noid != null; noid= a.next()) {
152 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
153 GLib.debug("rev: %s",
158 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
159 var parent = commit.get_parents();
160 GLib.debug("parent = %s", parent.get_id(0).to_string());
161 var master_rev = parent.get_id(0);
162 var master_commit = this.repo.lookup_commit(master_rev);;
164 var head_commit = this.repo.lookup_commit(oid);
166 var master_tree = master_commit.get_tree();
167 var head_tree = head_commit.get_tree();
169 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
171 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
172 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
175 // let's try a merge..
176 var mo = new Ggit.MergeOptions();
177 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
178 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
180 if (ix.has_conflicts()) {
181 GLib.debug("merge has conflicts");
184 var treeoid = ix.write_tree();
188 var parents = new Ggit.Commit[] { master_commit };
191 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
193 var sig = new Ggit.Signature.now(
194 this.repo.get_config().get_string("user.name"),
195 this.repo.get_config().get_string("user.email")
200 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
205 public void mergeMasterIntoHead()
207 // assumes head is not master...
208 this.loadLocalBranches();
209 GLib.debug("head rev = %s", this.head.get_name());
210 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
211 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
212 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
214 var master_commit = this.repo.lookup_commit(master_oid);;
215 var head_commit = this.repo.lookup_commit(head_oid);
218 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
220 var anc_commit = this.repo.lookup_commit(anc_oid);
221 var anc_tree = anc_commit.get_tree();
223 var mo = new Ggit.MergeOptions();
224 var co = new Ggit.CheckoutOptions();
225 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
227 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
229 var commits = new Ggit.AnnotatedCommit[] { ac };
231 this.repo.merge(commits, mo, co);
233 var cfg = this.repo.get_config().snapshot();
235 var sig = new Ggit.Signature.now(
236 cfg.get_string("user.name"),
237 cfg.get_string("user.email")
239 var new_head = this.repo.get_head();
240 var oid = new_head.get_target();
242 var ix = this.repo.get_index();
245 var treeoid = ix.write_tree();
247 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
249 var parent = new_head.lookup() as Ggit.Commit;
250 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
252 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
258 public bool doMergeClose(string commit_message)
260 this.loadLocalBranches(true);
261 var oldbranch = this.head.get_name();
262 // going to asume this is merge trees..
263 string [] cmd = { "merge", "--squash", oldbranch };
265 cmd = { "commit", "-a" , "-m", commit_message };
268 this.loadBranches(); // updates lastrev..
270 var notification = new Notify.Notification(
271 "Merged branch %s to %s".printf(oldbranch, master),
277 notification.set_timeout(5);
285 public bool is_auto_branch ()
287 if (this.name == "gitlog") {
291 if (this.is_managed()) {
299 public async void loadRemoteHeads(bool force = false)
302 if (!force && this.remote_heads != null) {
305 var r = this.repo.lookup_remote("origin");
306 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
307 this.remote_heads = r.list();
309 foreach(var br in this.remote_heads) {
310 if (!br.get_name().has_prefix("refs/heads/")) {
314 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
315 br.get_name().substring(11),
316 br.get_oid().to_string(),
317 br.get_local_oid().to_string(),
318 br.is_local() ? "Y" : "n"
325 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
327 //GLib.debug("bn=%s",remote_branch_name);
328 if (remote_branch_name.has_prefix("refs/remotes/")) {
332 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
333 if (target == "HEAD") {
334 target = remote_name +"/master";
337 foreach(var br in this.branches) {
338 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
339 if ( br.get_upstream().get_shorthand() == target) {
344 //GLib.debug("missing %s", remote_branch_name);
350 public void fetchAll()
352 this.loadLocalBranches();
353 this.loadRemoteHeads();
355 // remotes probably will not work with http auth..
356 //var ar = this.repo.list_remotes();
357 //foreach(var n in ar) {
360 GLib.debug("got remote '%s'", n);
361 var r = this.repo.lookup_remote(n);
362 GLib.debug("connecting '%s'", r.get_url());
365 string[] h = { "a = 1" };
366 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
369 GLib.debug("Got Error Message: %s", e.message);
374 foreach(var rh in this.remote_heads) {
375 if (rh.get_name().has_prefix("refs/remotes/")) {
379 var br = this.getBranch(n, rh.get_name());
381 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
383 rh.get_oid().to_string(),
384 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
387 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
389 var loc_oid = br.get_target();
391 size_t ahead, behind;
392 this.repo.get_ahead_behind(
399 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
405 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
408 if (far.length < 1) {
409 GLib.debug("no fetch required.. it's uptodate");
414 GLib.debug("getting remote specs '%s', %d", n, far.length);
417 var far = r.get_fetch_specs();
419 foreach(var rs in far) {
420 GLib.debug("got remote spec: %s", rs);
424 var options = new Ggit.FetchOptions();
425 options.set_remote_callbacks(this.callbacks);
426 //yield Async.thread(() => {
428 r.download(far, options);
439 public void pushAll()
442 this.loadLocalBranches();
443 this.loadRemoteHeads();
444 // remotes probably will not work with http auth..
445 //var ar = this.repo.list_remotes();
448 GLib.debug("got remote '%s'", n);
449 var r = this.repo.lookup_remote(n);
450 GLib.debug("connecting '%s'", r.get_url());
452 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
454 //GLib.debug("getting specs '%s'", n);
457 this.repo.add_remote_push(
459 "+%s:%s".printf(head.get_shorthand(),head.get_name())
464 var heads = r.list();
465 foreach(var rh in heads) {
466 if (rh.get_name().has_prefix("refs/remotes/")) {
470 var br = this.getBranch(n, rh.get_name());
472 GLib.debug("got heads: name=%s rev=%s localrev=%s",
474 rh.get_oid().to_string(),
475 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
477 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
478 size_t ahead, behind;
479 this.repo.get_ahead_behind(
486 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
492 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
495 if (far.length < 1) {
496 GLib.debug("no push required.. it's uptodate");
501 /*var head = this.repo.get_head();
503 far += "+%s:%s".printf(head.get_name(),head.get_name());
505 foreach(var rs in far) {
506 GLib.debug("got remote spec: %s", rs);
510 var popts = new Ggit.PushOptions();
511 popts.callbacks = this.callbacks;
514 GLib.debug("Push done?");
527 private class Callbacks : Ggit.RemoteCallbacks
529 //private Remote d_remote;
530 private Ggit.RemoteCallbacks? d_proxy = null;
532 public delegate void TransferProgress(Ggit.TransferProgress stats);
533 //private TransferProgress? d_transfer_progress;
535 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
539 //d_transfer_progress = (owned)transfer_progress;
542 protected override void progress(string message)
544 GLib.debug("progress: %s", message);
547 d_proxy.progress(message);
551 protected override void transfer_progress(Ggit.TransferProgress stats)
553 GLib.debug("transfer_progress");
555 if (d_transfer_progress != null)
557 d_transfer_progress(stats);
562 d_proxy.transfer_progress(stats);
567 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
569 GLib.debug("update_tips");
570 //d_remote.tip_updated(refname, a, b);
574 d_proxy.update_tips(refname, a, b);
578 protected override void completion(Ggit.RemoteCompletionType type)
580 GLib.debug("completion");
583 d_proxy.completion(type);
587 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
590 GLib.debug("get credentials %s UN=%s", url, username_from_url);
591 var uri = new Soup.URI(url);
594 var ret = this.netrc(uri.get_host());
599 //return new Ggit.CredPlaintext(username_from_url, "test");
602 /*var provider = d_remote.credentials_provider;
604 if (provider != null)
606 ret = provider.credentials(url, username_from_url, allowed_types);
609 if (ret == null && d_proxy != null)
611 ret = d_proxy.credentials(url, username_from_url, allowed_types);
617 public Ggit.Cred netrc(string domain)
620 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
621 var lines = str.split("\n");
622 for(var i=0; i< lines.length; i++) {
623 // assumes one line per entry.. if not we are buggered...
624 //GLib.debug("got %s" , lines[i]);
626 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
627 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
630 GLib.debug("found password?");
631 // we are gussing....
632 return new Ggit.CredPlaintext(bits[3], bits[5]);