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,
22 GLib.Timeout.add (1, () => {
23 GLib.debug("Meanwhile");
25 }, GLib.Priority.HIGH);
27 var loop = new MainLoop();
29 var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
30 GLib.debug("Starting");
31 a.loadRemoteHeads.begin(true, (obj,res) => {
32 a.loadRemoteHeads.end(res);
33 GLib.debug("got results");
34 a.loadLocalBranches();
38 //a.mergeMasterIntoHead();
48 var a = new GitLive.Repo("/home/alan/git/test1-clone");
50 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
51 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
53 var ix = a.repo.get_index();
56 var treeoid = ix.write_tree();
58 var head = a.repo.get_head();
59 var parent = head.lookup() as Ggit.Commit;
60 Ggit.Commit[] parents = (parent == null) ?
61 new Ggit.Commit[] {} :
62 new Ggit.Commit[] { parent };
64 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
66 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
67 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
69 // mmh.. no git add/commit in library...
70 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
71 //string[] spawn_env = Environ.get ();
72 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
80 public class Repo : Object {
83 public Ggit.Repository repo;
86 public Repo(string path)
88 this.name = GLib.Path.get_basename(path);
89 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
90 this.callbacks = new Callbacks(this);
93 Ggit.Branch head = null;
94 Gee.ArrayList<Ggit.Branch> branches = null;
95 Ggit.RemoteHead[] remote_heads = null;
96 public void loadLocalBranches(bool force = false)
98 if (!force && this.branches != null) {
102 this.branches = new Gee.ArrayList<Ggit.Branch>();
103 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
105 var br = r.get() as Ggit.Branch;
110 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
111 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
113 GLib.debug("got branch: N=%s", br.get_name() );
114 GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
116 GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
117 GLib.debug("oid = %s",br.get_target().to_string());
118 this.branches.add(br);
120 GLib.debug("HEAD= %s", br.get_name());
124 GLib.debug("Skip branch");
131 public bool is_managed()
133 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
134 return this.repo.get_config().get_int32("gitlive.managed") == 1;
138 public bool is_autocommit ()
140 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
141 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
143 public void set_autocommit(bool val)
145 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
149 public void walkDiff()
151 this.loadLocalBranches();
155 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
156 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
158 var a = new Ggit.RevisionWalker(this.repo);
159 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
163 for (var noid = a.next(); noid != null; noid= a.next()) {
164 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
165 GLib.debug("rev: %s",
170 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
171 var parent = commit.get_parents();
172 GLib.debug("parent = %s", parent.get_id(0).to_string());
173 var master_rev = parent.get_id(0);
174 var master_commit = this.repo.lookup_commit(master_rev);;
176 var head_commit = this.repo.lookup_commit(oid);
178 var master_tree = master_commit.get_tree();
179 var head_tree = head_commit.get_tree();
181 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
183 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
184 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
187 // let's try a merge..
188 var mo = new Ggit.MergeOptions();
189 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
190 var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
192 if (ix.has_conflicts()) {
193 GLib.debug("merge has conflicts");
196 var treeoid = ix.write_tree();
200 var parents = new Ggit.Commit[] { master_commit };
203 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
205 var sig = new Ggit.Signature.now(
206 this.repo.get_config().get_string("user.name"),
207 this.repo.get_config().get_string("user.email")
212 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
217 public void mergeMasterIntoHead()
219 // assumes head is not master...
220 this.loadLocalBranches();
221 GLib.debug("head rev = %s", this.head.get_name());
222 var head_oid = this.repo.revparse(this.head.get_name() ).get_id() ;
223 //var master_oid = this.repo.revparse("refs/heads/master" ).get_id() ;
224 var master_oid = this.repo.revparse("HEAD" ).get_id() ;
226 var master_commit = this.repo.lookup_commit(master_oid);;
227 var head_commit = this.repo.lookup_commit(head_oid);
230 var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
232 var anc_commit = this.repo.lookup_commit(anc_oid);
233 var anc_tree = anc_commit.get_tree();
235 var mo = new Ggit.MergeOptions();
236 var co = new Ggit.CheckoutOptions();
237 var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
239 var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
241 var commits = new Ggit.AnnotatedCommit[] { ac };
243 this.repo.merge(commits, mo, co);
245 var cfg = this.repo.get_config().snapshot();
247 var sig = new Ggit.Signature.now(
248 cfg.get_string("user.name"),
249 cfg.get_string("user.email")
251 var new_head = this.repo.get_head();
252 var oid = new_head.get_target();
254 var ix = this.repo.get_index();
257 var treeoid = ix.write_tree();
259 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
261 var parent = new_head.lookup() as Ggit.Commit;
262 Ggit.Commit[] parents = new Ggit.Commit[] { parent };
264 this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
270 public bool doMergeClose(string commit_message)
272 this.loadLocalBranches(true);
273 var oldbranch = this.head.get_name();
274 // going to asume this is merge trees..
275 string [] cmd = { "merge", "--squash", oldbranch };
277 cmd = { "commit", "-a" , "-m", commit_message };
280 this.loadBranches(); // updates lastrev..
282 var notification = new Notify.Notification(
283 "Merged branch %s to %s".printf(oldbranch, master),
289 notification.set_timeout(5);
297 public bool is_auto_branch ()
299 if (this.name == "gitlog") {
303 if (this.is_managed()) {
311 public async void loadRemoteHeads(bool force = false)
313 SourceFunc callback = loadRemoteHeads.callback;
315 ThreadFunc<bool> run = () => {
317 if (!force && this.remote_heads != null) {
320 var r = this.repo.lookup_remote("origin");
321 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
323 this.remote_heads = r.list();
325 foreach(var br in this.remote_heads) {
326 if (!br.get_name().has_prefix("refs/heads/")) {
330 GLib.debug("Remote: name=%s oid=%s local_oid=%s is_local=%s",
331 br.get_name().substring(11),
332 br.get_oid().to_string(),
333 br.get_local_oid().to_string(),
334 br.is_local() ? "Y" : "n"
337 Idle.add((owned) callback);
340 new Thread<bool>("thread-example", run);
347 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
349 //GLib.debug("bn=%s",remote_branch_name);
350 if (remote_branch_name.has_prefix("refs/remotes/")) {
354 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
355 if (target == "HEAD") {
356 target = remote_name +"/master";
359 foreach(var br in this.branches) {
360 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
361 if ( br.get_upstream().get_shorthand() == target) {
366 //GLib.debug("missing %s", remote_branch_name);
372 public void fetchAll()
374 this.loadLocalBranches();
375 this.loadRemoteHeads();
377 // remotes probably will not work with http auth..
378 //var ar = this.repo.list_remotes();
379 //foreach(var n in ar) {
382 GLib.debug("got remote '%s'", n);
383 var r = this.repo.lookup_remote(n);
384 GLib.debug("connecting '%s'", r.get_url());
387 string[] h = { "a = 1" };
388 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
391 GLib.debug("Got Error Message: %s", e.message);
396 foreach(var rh in this.remote_heads) {
397 if (rh.get_name().has_prefix("refs/remotes/")) {
401 var br = this.getBranch(n, rh.get_name());
403 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
405 rh.get_oid().to_string(),
406 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
409 //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
411 var loc_oid = br.get_target();
413 size_t ahead, behind;
414 this.repo.get_ahead_behind(
421 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
427 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
430 if (far.length < 1) {
431 GLib.debug("no fetch required.. it's uptodate");
436 GLib.debug("getting remote specs '%s', %d", n, far.length);
439 var far = r.get_fetch_specs();
441 foreach(var rs in far) {
442 GLib.debug("got remote spec: %s", rs);
446 var options = new Ggit.FetchOptions();
447 options.set_remote_callbacks(this.callbacks);
448 //yield Async.thread(() => {
450 r.download(far, options);
461 public void pushAll()
464 this.loadLocalBranches();
465 this.loadRemoteHeads();
466 // remotes probably will not work with http auth..
467 //var ar = this.repo.list_remotes();
470 GLib.debug("got remote '%s'", n);
471 var r = this.repo.lookup_remote(n);
472 GLib.debug("connecting '%s'", r.get_url());
474 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
476 //GLib.debug("getting specs '%s'", n);
479 this.repo.add_remote_push(
481 "+%s:%s".printf(head.get_shorthand(),head.get_name())
486 var heads = r.list();
487 foreach(var rh in heads) {
488 if (rh.get_name().has_prefix("refs/remotes/")) {
492 var br = this.getBranch(n, rh.get_name());
494 GLib.debug("got heads: name=%s rev=%s localrev=%s",
496 rh.get_oid().to_string(),
497 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
499 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
500 size_t ahead, behind;
501 this.repo.get_ahead_behind(
508 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
514 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
517 if (far.length < 1) {
518 GLib.debug("no push required.. it's uptodate");
523 /*var head = this.repo.get_head();
525 far += "+%s:%s".printf(head.get_name(),head.get_name());
527 foreach(var rs in far) {
528 GLib.debug("got remote spec: %s", rs);
532 var popts = new Ggit.PushOptions();
533 popts.callbacks = this.callbacks;
536 GLib.debug("Push done?");
549 private class Callbacks : Ggit.RemoteCallbacks
551 //private Remote d_remote;
552 private Ggit.RemoteCallbacks? d_proxy = null;
554 public delegate void TransferProgress(Ggit.TransferProgress stats);
555 //private TransferProgress? d_transfer_progress;
557 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
561 //d_transfer_progress = (owned)transfer_progress;
564 protected override void progress(string message)
566 GLib.debug("progress: %s", message);
569 d_proxy.progress(message);
573 protected override void transfer_progress(Ggit.TransferProgress stats)
575 GLib.debug("transfer_progress");
577 if (d_transfer_progress != null)
579 d_transfer_progress(stats);
584 d_proxy.transfer_progress(stats);
589 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
591 GLib.debug("update_tips");
592 //d_remote.tip_updated(refname, a, b);
596 d_proxy.update_tips(refname, a, b);
600 protected override void completion(Ggit.RemoteCompletionType type)
602 GLib.debug("completion");
605 d_proxy.completion(type);
609 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
612 GLib.debug("get credentials %s UN=%s", url, username_from_url);
613 var uri = new Soup.URI(url);
616 var ret = this.netrc(uri.get_host());
621 //return new Ggit.CredPlaintext(username_from_url, "test");
624 /*var provider = d_remote.credentials_provider;
626 if (provider != null)
628 ret = provider.credentials(url, username_from_url, allowed_types);
631 if (ret == null && d_proxy != null)
633 ret = d_proxy.credentials(url, username_from_url, allowed_types);
639 public Ggit.Cred netrc(string domain)
642 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
643 var lines = str.split("\n");
644 for(var i=0; i< lines.length; i++) {
645 // assumes one line per entry.. if not we are buggered...
646 //GLib.debug("got %s" , lines[i]);
648 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
649 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
652 GLib.debug("found password?");
653 // we are gussing....
654 return new Ggit.CredPlaintext(bits[3], bits[5]);