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,
22 var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
27 a.loadLocalBranches();
32 var a = new GitLive.Repo("/home/alan/git/test1-clone");
34 var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
35 GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1", str);
37 var ix = a.repo.get_index();
40 var treeoid = ix.write_tree();
42 var head = a.repo.get_head();
43 var parent = head.lookup() as Ggit.Commit;
44 Ggit.Commit[] parents = (parent == null) ?
45 new Ggit.Commit[] {} :
46 new Ggit.Commit[] { parent };
48 var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;// odd format..
50 var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
51 a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
53 // mmh.. no git add/commit in library...
54 // string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
55 //string[] spawn_env = Environ.get ();
56 // Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
64 public class Repo : Object {
67 public Ggit.Repository repo;
70 public Repo(string path)
72 this.name = GLib.Path.get_basename(path);
73 this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
74 this.callbacks = new Callbacks(this);
77 Ggit.Branch head = null;
78 Gee.ArrayList<Ggit.Branch> branches = null;
79 Ggit.RemoteHead[] remote_heads = null;
80 public void loadLocalBranches(bool force = false)
82 if (!force && this.branches != null) {
86 this.branches = new Gee.ArrayList<Ggit.Branch>();
87 var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
89 var br = r.get() as Ggit.Branch;
90 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
91 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
92 //GLib.debug("got branch: name = %s upstream = %s oid = %s ",
93 // br.get_name(), br.get_upstream().get_name(),
95 this.branches.add(br);
97 GLib.debug("HEAD= %s", br.get_name());
106 public bool is_managed()
108 GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
109 return this.repo.get_config().get_int32("gitlive.managed") == 1;
113 public bool is_autocommit ()
115 GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
116 return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
118 public void set_autocommit(bool val)
120 this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
124 public void walkDiff()
126 this.loadLocalBranches();
128 var oid = this.repo.revparse(this.head.get_name() ).get_id() ;
129 var moid = this.repo.revparse("refs/heads/master" ).get_id() ;
131 var a = new Ggit.RevisionWalker(this.repo);
132 a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
136 for (var noid = a.next(); noid != null; noid= a.next()) {
137 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
138 GLib.debug("rev: %s",
143 var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
144 var parent = commit.get_parents();
145 GLib.debug("parent = %s", parent.get_id(0).to_string());
146 var master_rev = parent.get_id(0);
147 var master_commit = this.repo.lookup_commit(master_rev);;
149 var head_commit = this.repo.lookup_commit(oid);
151 var master_tree = master_commit.get_tree();
152 var head_tree = head_commit.get_tree();
154 var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
156 diff.print(Ggit.DiffFormatType.PATCH, ( delta, hunk, line) => {
157 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
160 // let's try a merge..
161 var mo = new Ggit.MergeOptions();
162 mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
163 var ix = this.repo.merge_trees(null, master_tree, head_tree, mo);
165 if (ix.has_conflicts()) {
166 GLib.debug("merge has conflicts");
169 var treeoid = ix.write_tree();
173 var parents = new Ggit.Commit[] { master_commit };
176 var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree)) as Ggit.Tree;
178 var sig = new Ggit.Signature.now(
179 this.repo.get_config().get_string("user.name"),
180 this.repo.get_config().get_string("user.email")
182 this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
191 public bool doMergeClose(string commit_message)
193 this.loadLocalBranches(true);
194 var oldbranch = this.head.get_name();
195 // going to asume this is merge trees..
196 string [] cmd = { "merge", "--squash", oldbranch };
198 cmd = { "commit", "-a" , "-m", commit_message };
201 this.loadBranches(); // updates lastrev..
203 var notification = new Notify.Notification(
204 "Merged branch %s to %s".printf(oldbranch, master),
210 notification.set_timeout(5);
218 public bool is_auto_branch ()
220 if (this.name == "gitlog") {
224 if (this.is_managed()) {
232 public void loadRemoteHeads(bool force = false)
235 if (!force && this.remote_heads != null) {
238 var r = this.repo.lookup_remote("origin");
239 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
240 this.remote_heads = r.list();
246 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
248 //GLib.debug("bn=%s",remote_branch_name);
249 if (remote_branch_name.has_prefix("refs/remotes/")) {
253 var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
254 if (target == "HEAD") {
255 target = remote_name +"/master";
258 foreach(var br in this.branches) {
259 // GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
260 if ( br.get_upstream().get_shorthand() == target) {
265 //GLib.debug("missing %s", remote_branch_name);
271 public void fetchAll()
273 this.loadLocalBranches();
274 this.loadRemoteHeads();
276 // remotes probably will not work with http auth..
277 //var ar = this.repo.list_remotes();
278 //foreach(var n in ar) {
281 GLib.debug("got remote '%s'", n);
282 var r = this.repo.lookup_remote(n);
283 GLib.debug("connecting '%s'", r.get_url());
286 string[] h = { "a = 1" };
287 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
290 GLib.debug("Got Error Message: %s", e.message);
295 foreach(var rh in this.remote_heads) {
296 if (rh.get_name().has_prefix("refs/remotes/")) {
300 var br = this.getBranch(n, rh.get_name());
302 /*GLib.debug("got heads: name=%s rev=%s localrev=%s",
304 rh.get_oid().to_string(),
305 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
308 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
309 size_t ahead, behind;
310 this.repo.get_ahead_behind(
317 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
323 far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
326 if (far.length < 1) {
327 GLib.debug("no fetch required.. it's uptodate");
332 GLib.debug("getting remote specs '%s', %d", n, far.length);
335 var far = r.get_fetch_specs();
337 foreach(var rs in far) {
338 GLib.debug("got remote spec: %s", rs);
342 var options = new Ggit.FetchOptions();
343 options.set_remote_callbacks(this.callbacks);
344 //yield Async.thread(() => {
346 r.download(far, options);
357 public void pushAll()
360 this.loadLocalBranches();
361 this.loadRemoteHeads();
362 // remotes probably will not work with http auth..
363 //var ar = this.repo.list_remotes();
366 GLib.debug("got remote '%s'", n);
367 var r = this.repo.lookup_remote(n);
368 GLib.debug("connecting '%s'", r.get_url());
370 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
372 //GLib.debug("getting specs '%s'", n);
375 this.repo.add_remote_push(
377 "+%s:%s".printf(head.get_shorthand(),head.get_name())
382 var heads = r.list();
383 foreach(var rh in heads) {
384 if (rh.get_name().has_prefix("refs/remotes/")) {
388 var br = this.getBranch(n, rh.get_name());
390 GLib.debug("got heads: name=%s rev=%s localrev=%s",
392 rh.get_oid().to_string(),
393 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
395 var loc_oid = this.repo.revparse(br.get_name() ).get_id();
396 size_t ahead, behind;
397 this.repo.get_ahead_behind(
404 if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
410 far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
413 if (far.length < 1) {
414 GLib.debug("no push required.. it's uptodate");
419 /*var head = this.repo.get_head();
421 far += "+%s:%s".printf(head.get_name(),head.get_name());
423 foreach(var rs in far) {
424 GLib.debug("got remote spec: %s", rs);
428 var popts = new Ggit.PushOptions();
429 popts.callbacks = this.callbacks;
432 GLib.debug("Push done?");
445 private class Callbacks : Ggit.RemoteCallbacks
447 //private Remote d_remote;
448 private Ggit.RemoteCallbacks? d_proxy = null;
450 public delegate void TransferProgress(Ggit.TransferProgress stats);
451 //private TransferProgress? d_transfer_progress;
453 public Callbacks( Repo repo) //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
457 //d_transfer_progress = (owned)transfer_progress;
460 protected override void progress(string message)
462 GLib.debug("progress: %s", message);
465 d_proxy.progress(message);
469 protected override void transfer_progress(Ggit.TransferProgress stats)
471 GLib.debug("transfer_progress");
473 if (d_transfer_progress != null)
475 d_transfer_progress(stats);
480 d_proxy.transfer_progress(stats);
485 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
487 GLib.debug("update_tips");
488 //d_remote.tip_updated(refname, a, b);
492 d_proxy.update_tips(refname, a, b);
496 protected override void completion(Ggit.RemoteCompletionType type)
498 GLib.debug("completion");
501 d_proxy.completion(type);
505 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
508 GLib.debug("get credentials %s UN=%s", url, username_from_url);
509 var uri = new Soup.URI(url);
512 var ret = this.netrc(uri.get_host());
517 //return new Ggit.CredPlaintext(username_from_url, "test");
520 /*var provider = d_remote.credentials_provider;
522 if (provider != null)
524 ret = provider.credentials(url, username_from_url, allowed_types);
527 if (ret == null && d_proxy != null)
529 ret = d_proxy.credentials(url, username_from_url, allowed_types);
535 public Ggit.Cred netrc(string domain)
538 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
539 var lines = str.split("\n");
540 for(var i=0; i< lines.length; i++) {
541 // assumes one line per entry.. if not we are buggered...
542 //GLib.debug("got %s" , lines[i]);
544 var bits = Regex.split_simple ("[ \t]+", lines[i].strip());
545 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
548 GLib.debug("found password?");
549 // we are gussing....
550 return new Ggit.CredPlaintext(bits[3], bits[5]);