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,
23 GLib.Timeout.add (5, () => {
24 GLib.debug("Meanwhile");
26 }, GLib.Priority.DEFAULT);
28 var loop = new MainLoop();
30 var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
32 a.loadRemoteHeads.begin(true, (obj,res) => {
33 a.loadRemoteHeads.end(res);
35 a.loadLocalBranches();
39 //a.mergeMasterIntoHead();
49 var a = new GitLive.Repo("/home/alan/git/test1-clone");
51 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
52 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
54 var ix = a.repo.get_index();
57 var treeoid = ix.write_tree();
59 var head = a.repo.get_head();
60 var parent = head.lookup() as Ggit.Commit;
61 Ggit.Commit[] parents = (parent == null) ?
62 new Ggit.Commit[] {} :
63 new Ggit.Commit[] { parent };
65 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
67 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
68 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
70 // mmh.. no git add/commit in library...
71 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
72 //string[] spawn_env = Environ.get ();
73 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
81 public class Repo : Object {
84 public Ggit.Repository repo;
87 public Repo(string path)
89 this.name = GLib.Path.get_basename(path);
90 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
91 this.callbacks = new Callbacks(this);
94 Ggit.Branch head = null;
95 Gee.ArrayList<Ggit.Branch> branches = null;
96 Ggit.RemoteHead[] remote_heads = null;
97 public void loadLocalBranches(bool force = false)
99 if (!force && this.branches != null) {
103 this.branches = new Gee.ArrayList<Ggit.Branch>();
104 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
106 var br = r.get() as Ggit.Branch;
107 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
108 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
109 GLib.debug("got branch: H=%s name = %s upstream = %s oid = %s ",
110 br.is_head() ? "Y" : "n",
112 br.get_upstream().get_name().substring(20),
113 br.get_target().to_string());
114 this.branches.add(br);
116 GLib.debug("HEAD= %s", br.get_name());
125 public bool is_managed()
127 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
128 return this.repo.get_config().get_int32("gitlive.managed") == 1;
132 public bool is_autocommit ()
134 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
135 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
137 public void set_autocommit(bool val)
139 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
143 public void walkDiff()
145 this.loadLocalBranches();
149 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
150 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
152 var a = new Ggit.RevisionWalker(this.repo);
153 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
157 for (var noid = a.next(); noid != null; noid= a.next()) {
158 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
159 GLib.debug("rev: %s",
164 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
165 var parent = commit.get_parents();
166 GLib.debug("parent = %s", parent.get_id(0).to_string());
167 var master_rev = parent.get_id(0);
168 var master_commit = this.repo.lookup_commit(master_rev);;
170 var head_commit = this.repo.lookup_commit(oid);
172 var master_tree = master_commit.get_tree();
173 var head_tree = head_commit.get_tree();
175 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
177 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
178 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
181 // let's try a merge..
182 var mo = new Ggit.MergeOptions();
183 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
184 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
186 if (ix.has_conflicts()) {
187 GLib.debug("merge has conflicts");
190 var treeoid = ix.write_tree();
194 var parents = new Ggit.Commit[] { master_commit };
197 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
199 var sig = new Ggit.Signature.now(
200 this.repo.get_config().get_string("user.name"),
201 this.repo.get_config().get_string("user.email")
206 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
211 public void mergeMasterIntoHead()
213 // assumes head is not master...
214 this.loadLocalBranches();
215 GLib.debug("head rev = %s", this.head.get_name());
216 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
217 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
218 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
220 var master_commit = this.repo.lookup_commit(master_oid);;
221 var head_commit = this.repo.lookup_commit(head_oid);
224 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
226 var anc_commit = this.repo.lookup_commit(anc_oid);
227 var anc_tree = anc_commit.get_tree();
229 var mo = new Ggit.MergeOptions();
230 var co = new Ggit.CheckoutOptions();
231 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
233 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
235 var commits = new Ggit.AnnotatedCommit[] { ac };
237 this.repo.merge(commits, mo, co);
239 var cfg = this.repo.get_config().snapshot();
241 var sig = new Ggit.Signature.now(
242 cfg.get_string("user.name"),
243 cfg.get_string("user.email")
245 var new_head = this.repo.get_head();
246 var oid = new_head.get_target();
248 var ix = this.repo.get_index();
251 var treeoid = ix.write_tree();
253 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
255 var parent = new_head.lookup() as Ggit.Commit;
256 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
258 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
264 public bool doMergeClose(string commit_message)
266 this.loadLocalBranches(true);
267 var oldbranch = this.head.get_name();
268 // going to asume this is merge trees..
269 string [] cmd = { "merge", "--squash", oldbranch };
271 cmd = { "commit", "-a" , "-m", commit_message };
274 this.loadBranches(); // updates lastrev..
276 var notification = new Notify.Notification(
277 "Merged branch %s to %s".printf(oldbranch, master),
283 notification.set_timeout(5);
291 public bool is_auto_branch ()
293 if (this.name == "gitlog") {
297 if (this.is_managed()) {
305 public async void loadRemoteHeads(bool force = false)
307 SourceFunc callback = loadRemoteHeads.callback;
309 ThreadFunc<bool> run = () => {
311 if (!force && this.remote_heads != null) {
314 var r = this.repo.lookup_remote("origin");
315 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
316 this.remote_heads = r.list();
318 foreach(var br in this.remote_heads) {
319 if (!br.get_name().has_prefix("refs/heads/")) {
323 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
324 br.get_name().substring(11),
325 br.get_oid().to_string(),
326 br.get_local_oid().to_string(),
327 br.is_local() ? "Y" : "n"
330 Idle.add((owned) callback);
333 new Thread<bool>("thread-example", run);
340 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
342 //GLib.debug("bn=%s",remote_branch_name);
343 if (remote_branch_name.has_prefix("refs/remotes/")) {
347 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
348 if (target == "HEAD") {
349 target = remote_name +"/master";
352 foreach(var br in this.branches) {
353 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
354 if ( br.get_upstream().get_shorthand() == target) {
359 //GLib.debug("missing %s", remote_branch_name);
365 public void fetchAll()
367 this.loadLocalBranches();
368 this.loadRemoteHeads();
370 // remotes probably will not work with http auth..
371 //var ar = this.repo.list_remotes();
372 //foreach(var n in ar) {
375 GLib.debug("got remote '%s'", n);
376 var r = this.repo.lookup_remote(n);
377 GLib.debug("connecting '%s'", r.get_url());
380 string[] h = { "a = 1" };
381 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
384 GLib.debug("Got Error Message: %s", e.message);
389 foreach(var rh in this.remote_heads) {
390 if (rh.get_name().has_prefix("refs/remotes/")) {
394 var br = this.getBranch(n, rh.get_name());
396 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
398 rh.get_oid().to_string(),
399 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
402 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
404 var loc_oid = br.get_target();
406 size_t ahead, behind;
407 this.repo.get_ahead_behind(
414 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
420 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
423 if (far.length < 1) {
424 GLib.debug("no fetch required.. it's uptodate");
429 GLib.debug("getting remote specs '%s', %d", n, far.length);
432 var far = r.get_fetch_specs();
434 foreach(var rs in far) {
435 GLib.debug("got remote spec: %s", rs);
439 var options = new Ggit.FetchOptions();
440 options.set_remote_callbacks(this.callbacks);
441 //yield Async.thread(() => {
443 r.download(far, options);
454 public void pushAll()
457 this.loadLocalBranches();
458 this.loadRemoteHeads();
459 // remotes probably will not work with http auth..
460 //var ar = this.repo.list_remotes();
463 GLib.debug("got remote '%s'", n);
464 var r = this.repo.lookup_remote(n);
465 GLib.debug("connecting '%s'", r.get_url());
467 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
469 //GLib.debug("getting specs '%s'", n);
472 this.repo.add_remote_push(
474 "+%s:%s".printf(head.get_shorthand(),head.get_name())
479 var heads = r.list();
480 foreach(var rh in heads) {
481 if (rh.get_name().has_prefix("refs/remotes/")) {
485 var br = this.getBranch(n, rh.get_name());
487 GLib.debug("got heads: name=%s rev=%s localrev=%s",
489 rh.get_oid().to_string(),
490 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
492 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
493 size_t ahead, behind;
494 this.repo.get_ahead_behind(
501 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
507 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
510 if (far.length < 1) {
511 GLib.debug("no push required.. it's uptodate");
516 /*var head = this.repo.get_head();
518 far += "+%s:%s".printf(head.get_name(),head.get_name());
520 foreach(var rs in far) {
521 GLib.debug("got remote spec: %s", rs);
525 var popts = new Ggit.PushOptions();
526 popts.callbacks = this.callbacks;
529 GLib.debug("Push done?");
542 private class Callbacks : Ggit.RemoteCallbacks
544 //private Remote d_remote;
545 private Ggit.RemoteCallbacks? d_proxy = null;
547 public delegate void TransferProgress(Ggit.TransferProgress stats);
548 //private TransferProgress? d_transfer_progress;
550 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
554 //d_transfer_progress = (owned)transfer_progress;
557 protected override void progress(string message)
559 GLib.debug("progress: %s", message);
562 d_proxy.progress(message);
566 protected override void transfer_progress(Ggit.TransferProgress stats)
568 GLib.debug("transfer_progress");
570 if (d_transfer_progress != null)
572 d_transfer_progress(stats);
577 d_proxy.transfer_progress(stats);
582 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
584 GLib.debug("update_tips");
585 //d_remote.tip_updated(refname, a, b);
589 d_proxy.update_tips(refname, a, b);
593 protected override void completion(Ggit.RemoteCompletionType type)
595 GLib.debug("completion");
598 d_proxy.completion(type);
602 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
605 GLib.debug("get credentials %s UN=%s", url, username_from_url);
606 var uri = new Soup.URI(url);
609 var ret = this.netrc(uri.get_host());
614 //return new Ggit.CredPlaintext(username_from_url, "test");
617 /*var provider = d_remote.credentials_provider;
619 if (provider != null)
621 ret = provider.credentials(url, username_from_url, allowed_types);
624 if (ret == null && d_proxy != null)
626 ret = d_proxy.credentials(url, username_from_url, allowed_types);
632 public Ggit.Cred netrc(string domain)
635 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
636 var lines = str.split("\n");
637 for(var i=0; i< lines.length; i++) {
638 // assumes one line per entry.. if not we are buggered...
639 //GLib.debug("got %s" , lines[i]);
641 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
642 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
645 GLib.debug("found password?");
646 // we are gussing....
647 return new Ggit.CredPlaintext(bits[3], bits[5]);