sync
[gitlive] / Git.vala
index c50834d..8b5d7d3 100644 (file)
--- a/Git.vala
+++ b/Git.vala
@@ -1,5 +1,5 @@
 
-// valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -g
+// valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -pkg gee-0.8 -g
 
 
 
@@ -19,19 +19,36 @@ void main()
 
        Ggit.init();
        
+       var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
+       a.loadLocalBranches();
+       a.fetchAll();
+       //
+       /*
        var a = new GitLive.Repo("/home/alan/git/test1-clone");
        //a.fetchAll();
        var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
        GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1",  str); 
        
-       var ix = a.repo.repo.get_index();
+       var ix = a.repo.get_index();
+       ix.add_path("test1");
+       ix.write();
+       var treeoid = ix.write_tree();
+
+       var head = a.repo.get_head();
+       var parent = head.lookup() as Ggit.Commit;
+       Ggit.Commit[] parents = (parent == null) ? 
+               new Ggit.Commit[] {} :
+               new Ggit.Commit[] { parent };
+
+       var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;// odd format.. 
        
        var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
-       a.repo.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, 
+       a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
+       */
        // mmh.. no git add/commit in library...
-       string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
-       string[] spawn_env = Environ.get ();
-       Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
+//     string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
+       //string[] spawn_env = Environ.get ();
+//     Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
  
        a.pushAll();
 }
@@ -50,8 +67,53 @@ namespace  GitLive {
                        this.callbacks = new Callbacks(this);
                
                }
+               
+           Gee.ArrayList<Ggit.Branch>          branches = null;
+               public void loadLocalBranches()
+               {
+                       this.branches =  new Gee.ArrayList<Ggit.Branch>();
+                       var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
+                       while (r.next()) {
+                               var br = r.get() as Ggit.Branch;
+                               //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
+                               //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
+                               //GLib.debug("got branch: name = %s upstream = %s oid = %s ", 
+                               //              br.get_name(), br.get_upstream().get_name(), 
+                               //              head.to_string());
+                               this.branches.add(br);
+                               
+                       }
+               }
+               
+               Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
+               {
+                        //GLib.debug("bn=%s",remote_branch_name);
+                        if (remote_branch_name.has_prefix("refs/remotes/")) {
+                                return null;
+                        }
+
+                        var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
+                       if (target == "HEAD") {
+                               target = remote_name +"/master";
+                       }
+                        
+                       foreach(var br in this.branches) {
+                       //      GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
+                               if ( br.get_upstream().get_shorthand() == target) {
+                                       return br;
+                               }
+                               
+                       }
+                       //GLib.debug("missing %s", remote_branch_name);                 
+                       return null;
+               
+               }
+               
+               
+               
                public void fetchAll()
                {
+                       this.loadLocalBranches();
                        // remotes probably will not work with http auth..
                        var ar = this.repo.list_remotes();
                        foreach(var n in ar) {
@@ -67,15 +129,53 @@ namespace  GitLive {
                                        GLib.debug("Got Error Message: %s", e.message);
                                        return;
                                }
-                               GLib.debug("getting specs '%s'", n);
-
-
-                               var far = r.get_fetch_specs();
+                               string[] far = {};
+                               var heads = r.list();
+                               foreach(var rh in heads) {
+                                       if (rh.get_name().has_prefix("refs/remotes/")) {
+                                                  continue;
+                                        }
+                               
+                                       var br = this.getBranch(n, rh.get_name());
+                                
+                                       GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
+                                               rh.get_name(), 
+                                               rh.get_oid().to_string(),
+                                               br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
+                                       );
+                                       var loc_oid = this.repo.revparse(br.get_name() ).get_id();
+                                       size_t ahead, behind;
+                                       this.repo.get_ahead_behind(
+                                               loc_oid,
+                                               rh.get_oid(),
+                                               out ahead,
+                                               out behind
+                                       );
+                                       
+                                       if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
+                                               continue;
+                                       }
+                                       if (ahead > 0) {
+                                               continue;
+                                       }
+                                       far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
+                               } 
+                               
+                               if (far.length < 1) {
+                                       GLib.debug("no fetch required.. it's uptodate");
+                                       return;
+                               }
+                               
+                               GLib.debug("getting remote specs '%s', %d", n, far.length);
                                
+/*
+                               var far = r.get_fetch_specs();
+                               */
                                foreach(var rs in far) {
                                        GLib.debug("got remote spec: %s", rs);
                                        
                                }
+
                                var options = new Ggit.FetchOptions();
                                options.set_remote_callbacks(this.callbacks);
                                //yield Async.thread(() => {
@@ -102,7 +202,7 @@ namespace  GitLive {
                                 
                                try {
                                        string[] h = { "a = 1" };
-                                       r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
+                                        r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
                                        
                                } catch (Error e) {
                                        GLib.debug("Got Error Message: %s", e.message);
@@ -110,21 +210,61 @@ namespace  GitLive {
                                }
                                //GLib.debug("getting specs '%s'", n);
                                /* 
-                               var head = this.repo.get_head();
+                               
                                this.repo.add_remote_push(
                                        "origin",
                                        "+%s:%s".printf(head.get_shorthand(),head.get_name())
                                );
  */
-                               var far = r.get_push_specs();
+                               string[] far = {};
+                               var heads = r.list();
+                               foreach(var rh in heads) {
+                                       if (rh.get_name().has_prefix("refs/remotes/")) {
+                                                  continue;
+                                        }
+                               
+                                       var br = this.getBranch(n, rh.get_name());
+                                
+                                       GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
+                                               rh.get_name(), 
+                                               rh.get_oid().to_string(),
+                                               br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
+                                       );
+                                       var loc_oid = this.repo.revparse(br.get_name() ).get_id();
+                                       size_t ahead, behind;
+                                       this.repo.get_ahead_behind(
+                                               loc_oid,
+                                               rh.get_oid(),
+                                               out ahead,
+                                               out behind
+                                       );
+                                       
+                                       if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
+                                               continue;
+                                       }
+                                       if (behind > 0) {
+                                               continue;
+                                       }
+                                       far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
+                               }  
                                
+                               if (far.length < 1) {
+                                       GLib.debug("no push required.. it's uptodate");
+                                       return;
+                               }
+                               /*var head = this.repo.get_head();
+                               string[] far = {};
+                               far += "+%s:%s".printf(head.get_name(),head.get_name());
+                               */
                                foreach(var rs in far) {
                                        GLib.debug("got remote spec: %s", rs);
                                        
                                }
                                        
                                var popts = new Ggit.PushOptions();
-                               //popts.callbacks = this.callbacks;
+                                popts.callbacks = this.callbacks;
                                GLib.debug("Push?");
                                r.upload(far,popts);
                                GLib.debug("Push done?");