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/web.Texon");
25 GLib.Timeout.add (1, () => {
26 GLib.debug("Meanwhile");
28 }, GLib.Priority.HIGH);
30 var loop = new MainLoop();
32 var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
33 GLib.debug("Starting");
34 a.loadRemoteHeads.begin(true, (obj,res) => {
35 a.loadRemoteHeads.end(res);
36 GLib.debug("got results");
37 a.loadLocalBranches();
41 //a.mergeMasterIntoHead();
51 var a = new GitLive.Repo("/home/alan/git/test1-clone");
53 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
54 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
56 var ix = a.repo.get_index();
59 var treeoid = ix.write_tree();
61 var head = a.repo.get_head();
62 var parent = head.lookup() as Ggit.Commit;
63 Ggit.Commit[] parents = (parent == null) ?
64 new Ggit.Commit[] {} :
65 new Ggit.Commit[] { parent };
67 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
69 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
70 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
72 // mmh.. no git add/commit in library...
73 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
74 //string[] spawn_env = Environ.get ();
75 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
83 public class Repo : Object {
86 public Ggit.Repository repo;
89 public Repo(string path)
91 this.name = GLib.Path.get_basename(path);
92 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
93 this.callbacks = new Callbacks(this);
96 Ggit.Branch head = null;
97 Gee.ArrayList<Ggit.Branch> branches = null;
98 Ggit.RemoteHead[] remote_heads = null;
99 public void loadLocalBranches(bool force = false)
101 if (!force && this.branches != null) {
105 this.branches = new Gee.ArrayList<Ggit.Branch>();
106 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
108 var br = r.get() as Ggit.Branch;
116 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
117 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
119 GLib.debug("got branch: N=%s", br.get_name() );
120 GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
122 GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
123 GLib.debug("oid = %s",br.get_target().to_string());
124 this.branches.add(br);
126 GLib.debug("HEAD= %s", br.get_name());
130 GLib.debug("Skip branch");
137 public bool is_managed()
139 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
140 return this.repo.get_config().get_int32("gitlive.managed") == 1;
144 public bool is_autocommit ()
146 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
147 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
149 public void set_autocommit(bool val)
151 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
155 public void walkDiff()
157 this.loadLocalBranches();
161 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
162 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
164 var a = new Ggit.RevisionWalker(this.repo);
165 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
169 for (var noid = a.next(); noid != null; noid= a.next()) {
170 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
171 GLib.debug("rev: %s",
176 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
177 var parent = commit.get_parents();
178 GLib.debug("parent = %s", parent.get_id(0).to_string());
179 var master_rev = parent.get_id(0);
180 var master_commit = this.repo.lookup_commit(master_rev);;
182 var head_commit = this.repo.lookup_commit(oid);
184 var master_tree = master_commit.get_tree();
185 var head_tree = head_commit.get_tree();
187 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
189 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
190 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
193 // let's try a merge..
194 var mo = new Ggit.MergeOptions();
195 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
196 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
198 if (ix.has_conflicts()) {
199 GLib.debug("merge has conflicts");
202 var treeoid = ix.write_tree();
206 var parents = new Ggit.Commit[] { master_commit };
209 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
211 var sig = new Ggit.Signature.now(
212 this.repo.get_config().get_string("user.name"),
213 this.repo.get_config().get_string("user.email")
218 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
224 public void diffhead()
226 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
229 var gbr = r.get() as Ggit.Branch;
238 public void mergeMasterIntoHead()
240 // assumes head is not master...
241 this.loadLocalBranches();
242 GLib.debug("head rev = %s", this.head.get_name());
243 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
244 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
245 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
247 var master_commit = this.repo.lookup_commit(master_oid);;
248 var head_commit = this.repo.lookup_commit(head_oid);
251 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
253 var anc_commit = this.repo.lookup_commit(anc_oid);
254 var anc_tree = anc_commit.get_tree();
256 var mo = new Ggit.MergeOptions();
257 var co = new Ggit.CheckoutOptions();
258 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
260 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
262 var commits = new Ggit.AnnotatedCommit[] { ac };
264 this.repo.merge(commits, mo, co);
266 var cfg = this.repo.get_config().snapshot();
268 var sig = new Ggit.Signature.now(
269 cfg.get_string("user.name"),
270 cfg.get_string("user.email")
272 var new_head = this.repo.get_head();
273 var oid = new_head.get_target();
275 var ix = this.repo.get_index();
278 var treeoid = ix.write_tree();
280 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
282 var parent = new_head.lookup() as Ggit.Commit;
283 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
285 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
294 public bool doMergeClose(string commit_message)
296 this.loadLocalBranches(true);
297 var oldbranch = this.head.get_name();
298 // going to asume this is merge trees..
299 string [] cmd = { "merge", "--squash", oldbranch };
301 cmd = { "commit", "-a" , "-m", commit_message };
304 this.loadBranches(); // updates lastrev..
306 var notification = new Notify.Notification(
307 "Merged branch %s to %s".printf(oldbranch, master),
313 notification.set_timeout(5);
321 public bool is_auto_branch ()
323 if (this.name == "gitlog") {
327 if (this.is_managed()) {
335 public async void loadRemoteHeads(bool force = false)
337 SourceFunc callback = loadRemoteHeads.callback;
339 ThreadFunc<bool> run = () => {
341 if (!force && this.remote_heads != null) {
344 var r = this.repo.lookup_remote("origin");
345 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
347 this.remote_heads = r.list();
349 foreach(var br in this.remote_heads) {
350 if (!br.get_name().has_prefix("refs/heads/")) {
354 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
356 br.get_oid().to_string(),
357 br.get_local_oid().to_string(),
358 br.is_local() ? "Y" : "n"
361 Idle.add((owned) callback);
364 new Thread<bool>("loadRemoteHeads-" , run);
371 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
373 //GLib.debug("bn=%s",remote_branch_name);
374 if (remote_branch_name.has_prefix("refs/remotes/")) {
378 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
379 if (target == "HEAD") {
380 target = remote_name +"/master";
383 foreach(var br in this.branches) {
384 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
385 if ( br.get_upstream().get_shorthand() == target) {
390 //GLib.debug("missing %s", remote_branch_name);
396 public void fetchAll()
398 this.loadLocalBranches();
399 this.loadRemoteHeads();
401 // remotes probably will not work with http auth..
402 //var ar = this.repo.list_remotes();
403 //foreach(var n in ar) {
406 GLib.debug("got remote '%s'", n);
407 var r = this.repo.lookup_remote(n);
408 GLib.debug("connecting '%s'", r.get_url());
411 string[] h = { "a = 1" };
412 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
415 GLib.debug("Got Error Message: %s", e.message);
420 foreach(var rh in this.remote_heads) {
421 if (rh.get_name().has_prefix("refs/remotes/")) {
425 var br = this.getBranch(n, rh.get_name());
427 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
429 rh.get_oid().to_string(),
430 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
433 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
435 var loc_oid = br.get_target();
437 size_t ahead, behind;
438 this.repo.get_ahead_behind(
445 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
451 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
454 if (far.length < 1) {
455 GLib.debug("no fetch required.. it's uptodate");
460 GLib.debug("getting remote specs '%s', %d", n, far.length);
463 var far = r.get_fetch_specs();
465 foreach(var rs in far) {
466 GLib.debug("got remote spec: %s", rs);
470 var options = new Ggit.FetchOptions();
471 options.set_remote_callbacks(this.callbacks);
472 //yield Async.thread(() => {
474 r.download(far, options);
485 public void pushAll()
488 this.loadLocalBranches();
489 this.loadRemoteHeads();
490 // remotes probably will not work with http auth..
491 //var ar = this.repo.list_remotes();
494 GLib.debug("got remote '%s'", n);
495 var r = this.repo.lookup_remote(n);
496 GLib.debug("connecting '%s'", r.get_url());
498 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
500 //GLib.debug("getting specs '%s'", n);
503 this.repo.add_remote_push(
505 "+%s:%s".printf(head.get_shorthand(),head.get_name())
510 var heads = r.list();
511 foreach(var rh in heads) {
512 if (rh.get_name().has_prefix("refs/remotes/")) {
516 var br = this.getBranch(n, rh.get_name());
518 GLib.debug("got heads: name=%s rev=%s localrev=%s",
520 rh.get_oid().to_string(),
521 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
523 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
524 size_t ahead, behind;
525 this.repo.get_ahead_behind(
532 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
538 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
541 if (far.length < 1) {
542 GLib.debug("no push required.. it's uptodate");
547 /*var head = this.repo.get_head();
549 far += "+%s:%s".printf(head.get_name(),head.get_name());
551 foreach(var rs in far) {
552 GLib.debug("got remote spec: %s", rs);
556 var popts = new Ggit.PushOptions();
557 popts.callbacks = this.callbacks;
560 GLib.debug("Push done?");
573 private class Callbacks : Ggit.RemoteCallbacks
575 //private Remote d_remote;
576 private Ggit.RemoteCallbacks? d_proxy = null;
578 public delegate void TransferProgress(Ggit.TransferProgress stats);
579 //private TransferProgress? d_transfer_progress;
581 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
585 //d_transfer_progress = (owned)transfer_progress;
588 protected override void progress(string message)
590 GLib.debug("progress: %s", message);
593 d_proxy.progress(message);
597 protected override void transfer_progress(Ggit.TransferProgress stats)
599 GLib.debug("transfer_progress");
601 if (d_transfer_progress != null)
603 d_transfer_progress(stats);
608 d_proxy.transfer_progress(stats);
613 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
615 GLib.debug("update_tips");
616 //d_remote.tip_updated(refname, a, b);
620 d_proxy.update_tips(refname, a, b);
624 protected override void completion(Ggit.RemoteCompletionType type)
626 GLib.debug("completion");
629 d_proxy.completion(type);
633 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
636 GLib.debug("get credentials %s UN=%s", url, username_from_url);
637 var uri = new Soup.URI(url);
640 var ret = this.netrc(uri.get_host());
645 //return new Ggit.CredPlaintext(username_from_url, "test");
648 /*var provider = d_remote.credentials_provider;
650 if (provider != null)
652 ret = provider.credentials(url, username_from_url, allowed_types);
655 if (ret == null && d_proxy != null)
657 ret = d_proxy.credentials(url, username_from_url, allowed_types);
663 public Ggit.Cred netrc(string domain)
666 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
667 var lines = str.split("\n");
668 for(var i=0; i< lines.length; i++) {
669 // assumes one line per entry.. if not we are buggered...
670 //GLib.debug("got %s" , lines[i]);
672 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
673 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
676 GLib.debug("found password?");
677 // we are gussing....
678 return new Ggit.CredPlaintext(bits[3], bits[5]);