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,
20 var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
24 GLib.Timeout.add (1, () => {
25 GLib.debug("Meanwhile");
27 }, GLib.Priority.HIGH);
29 var loop = new MainLoop();
31 var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
32 GLib.debug("Starting");
33 a.loadRemoteHeads.begin(true, (obj,res) => {
34 a.loadRemoteHeads.end(res);
35 GLib.debug("got results");
36 a.loadLocalBranches();
40 //a.mergeMasterIntoHead();
50 var a = new GitLive.Repo("/home/alan/git/test1-clone");
52 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
53 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
55 var ix = a.repo.get_index();
58 var treeoid = ix.write_tree();
60 var head = a.repo.get_head();
61 var parent = head.lookup() as Ggit.Commit;
62 Ggit.Commit[] parents = (parent == null) ?
63 new Ggit.Commit[] {} :
64 new Ggit.Commit[] { parent };
66 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
68 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
69 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
71 // mmh.. no git add/commit in library...
72 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
73 //string[] spawn_env = Environ.get ();
74 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
82 public class Repo : Object {
85 public Ggit.Repository repo;
88 public Repo(string path)
90 this.name = GLib.Path.get_basename(path);
91 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
92 this.callbacks = new Callbacks(this);
95 Ggit.Branch head = null;
96 Gee.ArrayList<Ggit.Branch> branches = null;
97 Ggit.RemoteHead[] remote_heads = null;
98 public void loadLocalBranches(bool force = false)
100 if (!force && this.branches != null) {
104 this.branches = new Gee.ArrayList<Ggit.Branch>();
105 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
107 var br = r.get() as Ggit.Branch;
115 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
116 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
118 GLib.debug("got branch: N=%s", br.get_name() );
119 GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
121 GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
122 GLib.debug("oid = %s",br.get_target().to_string());
123 this.branches.add(br);
125 GLib.debug("HEAD= %s", br.get_name());
129 GLib.debug("Skip branch");
136 public bool is_managed()
138 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
139 return this.repo.get_config().get_int32("gitlive.managed") == 1;
143 public bool is_autocommit ()
145 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
146 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
148 public void set_autocommit(bool val)
150 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
154 public void walkDiff()
156 this.loadLocalBranches();
160 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
161 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
163 var a = new Ggit.RevisionWalker(this.repo);
164 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
168 for (var noid = a.next(); noid != null; noid= a.next()) {
169 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
170 GLib.debug("rev: %s",
175 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
176 var parent = commit.get_parents();
177 GLib.debug("parent = %s", parent.get_id(0).to_string());
178 var master_rev = parent.get_id(0);
179 var master_commit = this.repo.lookup_commit(master_rev);;
181 var head_commit = this.repo.lookup_commit(oid);
183 var master_tree = master_commit.get_tree();
184 var head_tree = head_commit.get_tree();
186 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
188 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
189 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
192 // let's try a merge..
193 var mo = new Ggit.MergeOptions();
194 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
195 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
197 if (ix.has_conflicts()) {
198 GLib.debug("merge has conflicts");
201 var treeoid = ix.write_tree();
205 var parents = new Ggit.Commit[] { master_commit };
208 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
210 var sig = new Ggit.Signature.now(
211 this.repo.get_config().get_string("user.name"),
212 this.repo.get_config().get_string("user.email")
217 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
222 public void mergeMasterIntoHead()
224 // assumes head is not master...
225 this.loadLocalBranches();
226 GLib.debug("head rev = %s", this.head.get_name());
227 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
228 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
229 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
231 var master_commit = this.repo.lookup_commit(master_oid);;
232 var head_commit = this.repo.lookup_commit(head_oid);
235 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
237 var anc_commit = this.repo.lookup_commit(anc_oid);
238 var anc_tree = anc_commit.get_tree();
240 var mo = new Ggit.MergeOptions();
241 var co = new Ggit.CheckoutOptions();
242 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
244 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
246 var commits = new Ggit.AnnotatedCommit[] { ac };
248 this.repo.merge(commits, mo, co);
250 var cfg = this.repo.get_config().snapshot();
252 var sig = new Ggit.Signature.now(
253 cfg.get_string("user.name"),
254 cfg.get_string("user.email")
256 var new_head = this.repo.get_head();
257 var oid = new_head.get_target();
259 var ix = this.repo.get_index();
262 var treeoid = ix.write_tree();
264 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
266 var parent = new_head.lookup() as Ggit.Commit;
267 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
269 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
275 public bool doMergeClose(string commit_message)
277 this.loadLocalBranches(true);
278 var oldbranch = this.head.get_name();
279 // going to asume this is merge trees..
280 string [] cmd = { "merge", "--squash", oldbranch };
282 cmd = { "commit", "-a" , "-m", commit_message };
285 this.loadBranches(); // updates lastrev..
287 var notification = new Notify.Notification(
288 "Merged branch %s to %s".printf(oldbranch, master),
294 notification.set_timeout(5);
302 public bool is_auto_branch ()
304 if (this.name == "gitlog") {
308 if (this.is_managed()) {
316 public async void loadRemoteHeads(bool force = false)
318 SourceFunc callback = loadRemoteHeads.callback;
320 ThreadFunc<bool> run = () => {
322 if (!force && this.remote_heads != null) {
325 var r = this.repo.lookup_remote("origin");
326 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
328 this.remote_heads = r.list();
330 foreach(var br in this.remote_heads) {
331 if (!br.get_name().has_prefix("refs/heads/")) {
335 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
337 br.get_oid().to_string(),
338 br.get_local_oid().to_string(),
339 br.is_local() ? "Y" : "n"
342 Idle.add((owned) callback);
345 new Thread<bool>("loadRemoteHeads-" , run);
352 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
354 //GLib.debug("bn=%s",remote_branch_name);
355 if (remote_branch_name.has_prefix("refs/remotes/")) {
359 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
360 if (target == "HEAD") {
361 target = remote_name +"/master";
364 foreach(var br in this.branches) {
365 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
366 if ( br.get_upstream().get_shorthand() == target) {
371 //GLib.debug("missing %s", remote_branch_name);
377 public void fetchAll()
379 this.loadLocalBranches();
380 this.loadRemoteHeads();
382 // remotes probably will not work with http auth..
383 //var ar = this.repo.list_remotes();
384 //foreach(var n in ar) {
387 GLib.debug("got remote '%s'", n);
388 var r = this.repo.lookup_remote(n);
389 GLib.debug("connecting '%s'", r.get_url());
392 string[] h = { "a = 1" };
393 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
396 GLib.debug("Got Error Message: %s", e.message);
401 foreach(var rh in this.remote_heads) {
402 if (rh.get_name().has_prefix("refs/remotes/")) {
406 var br = this.getBranch(n, rh.get_name());
408 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
410 rh.get_oid().to_string(),
411 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
414 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
416 var loc_oid = br.get_target();
418 size_t ahead, behind;
419 this.repo.get_ahead_behind(
426 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
432 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
435 if (far.length < 1) {
436 GLib.debug("no fetch required.. it's uptodate");
441 GLib.debug("getting remote specs '%s', %d", n, far.length);
444 var far = r.get_fetch_specs();
446 foreach(var rs in far) {
447 GLib.debug("got remote spec: %s", rs);
451 var options = new Ggit.FetchOptions();
452 options.set_remote_callbacks(this.callbacks);
453 //yield Async.thread(() => {
455 r.download(far, options);
466 public void pushAll()
469 this.loadLocalBranches();
470 this.loadRemoteHeads();
471 // remotes probably will not work with http auth..
472 //var ar = this.repo.list_remotes();
475 GLib.debug("got remote '%s'", n);
476 var r = this.repo.lookup_remote(n);
477 GLib.debug("connecting '%s'", r.get_url());
479 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
481 //GLib.debug("getting specs '%s'", n);
484 this.repo.add_remote_push(
486 "+%s:%s".printf(head.get_shorthand(),head.get_name())
491 var heads = r.list();
492 foreach(var rh in heads) {
493 if (rh.get_name().has_prefix("refs/remotes/")) {
497 var br = this.getBranch(n, rh.get_name());
499 GLib.debug("got heads: name=%s rev=%s localrev=%s",
501 rh.get_oid().to_string(),
502 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
504 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
505 size_t ahead, behind;
506 this.repo.get_ahead_behind(
513 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
519 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
522 if (far.length < 1) {
523 GLib.debug("no push required.. it's uptodate");
528 /*var head = this.repo.get_head();
530 far += "+%s:%s".printf(head.get_name(),head.get_name());
532 foreach(var rs in far) {
533 GLib.debug("got remote spec: %s", rs);
537 var popts = new Ggit.PushOptions();
538 popts.callbacks = this.callbacks;
541 GLib.debug("Push done?");
554 private class Callbacks : Ggit.RemoteCallbacks
556 //private Remote d_remote;
557 private Ggit.RemoteCallbacks? d_proxy = null;
559 public delegate void TransferProgress(Ggit.TransferProgress stats);
560 //private TransferProgress? d_transfer_progress;
562 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
566 //d_transfer_progress = (owned)transfer_progress;
569 protected override void progress(string message)
571 GLib.debug("progress: %s", message);
574 d_proxy.progress(message);
578 protected override void transfer_progress(Ggit.TransferProgress stats)
580 GLib.debug("transfer_progress");
582 if (d_transfer_progress != null)
584 d_transfer_progress(stats);
589 d_proxy.transfer_progress(stats);
594 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
596 GLib.debug("update_tips");
597 //d_remote.tip_updated(refname, a, b);
601 d_proxy.update_tips(refname, a, b);
605 protected override void completion(Ggit.RemoteCompletionType type)
607 GLib.debug("completion");
610 d_proxy.completion(type);
614 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
617 GLib.debug("get credentials %s UN=%s", url, username_from_url);
618 var uri = new Soup.URI(url);
621 var ret = this.netrc(uri.get_host());
626 //return new Ggit.CredPlaintext(username_from_url, "test");
629 /*var provider = d_remote.credentials_provider;
631 if (provider != null)
633 ret = provider.credentials(url, username_from_url, allowed_types);
636 if (ret == null && d_proxy != null)
638 ret = d_proxy.credentials(url, username_from_url, allowed_types);
644 public Ggit.Cred netrc(string domain)
647 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
648 var lines = str.split("\n");
649 for(var i=0; i< lines.length; i++) {
650 // assumes one line per entry.. if not we are buggered...
651 //GLib.debug("got %s" , lines[i]);
653 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
654 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
657 GLib.debug("found password?");
658 // we are gussing....
659 return new Ggit.CredPlaintext(bits[3], bits[5]);