2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 --pkg gee-0.8 -g
7 GLib.Log.set_handler(null,
8 GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO,
21 var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
26 GLib.Timeout.add (1, () => {
27 GLib.debug("Meanwhile");
29 }, GLib.Priority.HIGH);
31 var loop = new MainLoop();
33 var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
34 GLib.debug("Starting");
35 a.loadRemoteHeads.begin(true, (obj,res) => {
36 a.loadRemoteHeads.end(res);
37 GLib.debug("got results");
38 a.loadLocalBranches();
42 //a.mergeMasterIntoHead();
52 var a = new GitLive.Repo("/home/alan/git/test1-clone");
54 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
55 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
57 var ix = a.repo.get_index();
60 var treeoid = ix.write_tree();
62 var head = a.repo.get_head();
63 var parent = head.lookup() as Ggit.Commit;
64 Ggit.Commit[] parents = (parent == null) ?
65 new Ggit.Commit[] {} :
66 new Ggit.Commit[] { parent };
68 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
70 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
71 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
73 // mmh.. no git add/commit in library...
74 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
75 //string[] spawn_env = Environ.get ();
76 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
84 public class Repo : Object {
87 public Ggit.Repository repo;
90 public Repo(string path)
92 this.name = GLib.Path.get_basename(path);
93 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
94 this.callbacks = new Callbacks(this);
97 Ggit.Branch head = null;
98 Gee.ArrayList<Ggit.Branch> branches = null;
99 Ggit.RemoteHead[] remote_heads = null;
100 public void loadLocalBranches(bool force = false)
102 if (!force && this.branches != null) {
106 this.branches = new Gee.ArrayList<Ggit.Branch>();
107 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
109 var br = r.get() as Ggit.Branch;
117 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
118 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
120 GLib.debug("got branch: N=%s", br.get_name() );
121 GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
123 GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
124 GLib.debug("oid = %s",br.get_target().to_string());
125 this.branches.add(br);
127 GLib.debug("HEAD= %s", br.get_name());
131 GLib.debug("Skip branch");
138 public bool is_managed()
140 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
141 return this.repo.get_config().get_int32("gitlive.managed") == 1;
145 public bool is_autocommit ()
147 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
148 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
150 public void set_autocommit(bool val)
152 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
156 public void walkDiff()
158 this.loadLocalBranches();
162 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
163 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
165 var a = new Ggit.RevisionWalker(this.repo);
166 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
170 for (var noid = a.next(); noid != null; noid= a.next()) {
171 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
172 GLib.debug("rev: %s",
177 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
178 var parent = commit.get_parents();
179 GLib.debug("parent = %s", parent.get_id(0).to_string());
180 var master_rev = parent.get_id(0);
181 var master_commit = this.repo.lookup_commit(master_rev);;
183 var head_commit = this.repo.lookup_commit(oid);
185 var master_tree = master_commit.get_tree();
186 var head_tree = head_commit.get_tree();
188 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
190 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
191 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
194 // let's try a merge..
195 var mo = new Ggit.MergeOptions();
196 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
197 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
199 if (ix.has_conflicts()) {
200 GLib.debug("merge has conflicts");
203 var treeoid = ix.write_tree();
207 var parents = new Ggit.Commit[] { master_commit };
210 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
212 var sig = new Ggit.Signature.now(
213 this.repo.get_config().get_string("user.name"),
214 this.repo.get_config().get_string("user.email")
219 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
225 public void diffhead()
227 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
230 var gbr = r.get() as Ggit.Branch;
235 GLib.debug("checking head=%s", head.get_name());
236 var br = this.repo.lookup_branch(head.get_name(),Ggit.BranchType.LOCAL);
237 var tree = this.repo.lookup_tree(br.get_target());
243 public void mergeMasterIntoHead()
245 // assumes head is not master...
246 this.loadLocalBranches();
247 GLib.debug("head rev = %s", this.head.get_name());
248 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
249 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
250 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
252 var master_commit = this.repo.lookup_commit(master_oid);;
253 var head_commit = this.repo.lookup_commit(head_oid);
256 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
258 var anc_commit = this.repo.lookup_commit(anc_oid);
259 var anc_tree = anc_commit.get_tree();
261 var mo = new Ggit.MergeOptions();
262 var co = new Ggit.CheckoutOptions();
263 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
265 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
267 var commits = new Ggit.AnnotatedCommit[] { ac };
269 this.repo.merge(commits, mo, co);
271 var cfg = this.repo.get_config().snapshot();
273 var sig = new Ggit.Signature.now(
274 cfg.get_string("user.name"),
275 cfg.get_string("user.email")
277 var new_head = this.repo.get_head();
278 var oid = new_head.get_target();
280 var ix = this.repo.get_index();
283 var treeoid = ix.write_tree();
285 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
287 var parent = new_head.lookup() as Ggit.Commit;
288 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
290 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
299 public bool doMergeClose(string commit_message)
301 this.loadLocalBranches(true);
302 var oldbranch = this.head.get_name();
303 // going to asume this is merge trees..
304 string [] cmd = { "merge", "--squash", oldbranch };
306 cmd = { "commit", "-a" , "-m", commit_message };
309 this.loadBranches(); // updates lastrev..
311 var notification = new Notify.Notification(
312 "Merged branch %s to %s".printf(oldbranch, master),
318 notification.set_timeout(5);
326 public bool is_auto_branch ()
328 if (this.name == "gitlog") {
332 if (this.is_managed()) {
340 public async void loadRemoteHeads(bool force = false)
342 SourceFunc callback = loadRemoteHeads.callback;
344 ThreadFunc<bool> run = () => {
346 if (!force && this.remote_heads != null) {
349 var r = this.repo.lookup_remote("origin");
350 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
352 this.remote_heads = r.list();
354 foreach(var br in this.remote_heads) {
355 if (!br.get_name().has_prefix("refs/heads/")) {
359 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
361 br.get_oid().to_string(),
362 br.get_local_oid().to_string(),
363 br.is_local() ? "Y" : "n"
366 Idle.add((owned) callback);
369 new Thread<bool>("loadRemoteHeads-" , run);
376 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
378 //GLib.debug("bn=%s",remote_branch_name);
379 if (remote_branch_name.has_prefix("refs/remotes/")) {
383 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
384 if (target == "HEAD") {
385 target = remote_name +"/master";
388 foreach(var br in this.branches) {
389 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
390 if ( br.get_upstream().get_shorthand() == target) {
395 //GLib.debug("missing %s", remote_branch_name);
401 public void fetchAll()
403 this.loadLocalBranches();
404 this.loadRemoteHeads();
406 // remotes probably will not work with http auth..
407 //var ar = this.repo.list_remotes();
408 //foreach(var n in ar) {
411 GLib.debug("got remote '%s'", n);
412 var r = this.repo.lookup_remote(n);
413 GLib.debug("connecting '%s'", r.get_url());
416 string[] h = { "a = 1" };
417 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
420 GLib.debug("Got Error Message: %s", e.message);
425 foreach(var rh in this.remote_heads) {
426 if (rh.get_name().has_prefix("refs/remotes/")) {
430 var br = this.getBranch(n, rh.get_name());
432 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
434 rh.get_oid().to_string(),
435 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
438 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
440 var loc_oid = br.get_target();
442 size_t ahead, behind;
443 this.repo.get_ahead_behind(
450 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
456 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
459 if (far.length < 1) {
460 GLib.debug("no fetch required.. it's uptodate");
465 GLib.debug("getting remote specs '%s', %d", n, far.length);
468 var far = r.get_fetch_specs();
470 foreach(var rs in far) {
471 GLib.debug("got remote spec: %s", rs);
475 var options = new Ggit.FetchOptions();
476 options.set_remote_callbacks(this.callbacks);
477 //yield Async.thread(() => {
479 r.download(far, options);
490 public void pushAll()
493 this.loadLocalBranches();
494 this.loadRemoteHeads();
495 // remotes probably will not work with http auth..
496 //var ar = this.repo.list_remotes();
499 GLib.debug("got remote '%s'", n);
500 var r = this.repo.lookup_remote(n);
501 GLib.debug("connecting '%s'", r.get_url());
503 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
505 //GLib.debug("getting specs '%s'", n);
508 this.repo.add_remote_push(
510 "+%s:%s".printf(head.get_shorthand(),head.get_name())
515 var heads = r.list();
516 foreach(var rh in heads) {
517 if (rh.get_name().has_prefix("refs/remotes/")) {
521 var br = this.getBranch(n, rh.get_name());
523 GLib.debug("got heads: name=%s rev=%s localrev=%s",
525 rh.get_oid().to_string(),
526 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
528 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
529 size_t ahead, behind;
530 this.repo.get_ahead_behind(
537 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
543 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
546 if (far.length < 1) {
547 GLib.debug("no push required.. it's uptodate");
552 /*var head = this.repo.get_head();
554 far += "+%s:%s".printf(head.get_name(),head.get_name());
556 foreach(var rs in far) {
557 GLib.debug("got remote spec: %s", rs);
561 var popts = new Ggit.PushOptions();
562 popts.callbacks = this.callbacks;
565 GLib.debug("Push done?");
578 private class Callbacks : Ggit.RemoteCallbacks
580 //private Remote d_remote;
581 private Ggit.RemoteCallbacks? d_proxy = null;
583 public delegate void TransferProgress(Ggit.TransferProgress stats);
584 //private TransferProgress? d_transfer_progress;
586 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
590 //d_transfer_progress = (owned)transfer_progress;
593 protected override void progress(string message)
595 GLib.debug("progress: %s", message);
598 d_proxy.progress(message);
602 protected override void transfer_progress(Ggit.TransferProgress stats)
604 GLib.debug("transfer_progress");
606 if (d_transfer_progress != null)
608 d_transfer_progress(stats);
613 d_proxy.transfer_progress(stats);
618 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
620 GLib.debug("update_tips");
621 //d_remote.tip_updated(refname, a, b);
625 d_proxy.update_tips(refname, a, b);
629 protected override void completion(Ggit.RemoteCompletionType type)
631 GLib.debug("completion");
634 d_proxy.completion(type);
638 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
641 GLib.debug("get credentials %s UN=%s", url, username_from_url);
642 var uri = new Soup.URI(url);
645 var ret = this.netrc(uri.get_host());
650 //return new Ggit.CredPlaintext(username_from_url, "test");
653 /*var provider = d_remote.credentials_provider;
655 if (provider != null)
657 ret = provider.credentials(url, username_from_url, allowed_types);
660 if (ret == null && d_proxy != null)
662 ret = d_proxy.credentials(url, username_from_url, allowed_types);
668 public Ggit.Cred netrc(string domain)
671 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
672 var lines = str.split("\n");
673 for(var i=0; i< lines.length; i++) {
674 // assumes one line per entry.. if not we are buggered...
675 //GLib.debug("got %s" , lines[i]);
677 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
678 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
681 GLib.debug("found password?");
682 // we are gussing....
683 return new Ggit.CredPlaintext(bits[3], bits[5]);