Git.vala
[gitlive] / Git.vala
index 7bc0d55..07c6e74 100644 (file)
--- a/Git.vala
+++ b/Git.vala
@@ -20,7 +20,12 @@ void main()
        Ggit.init();
        
        var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
-       a.branches();
+       a.is_managed();
+       a.is_autocommit();
+       a.loadLocalBranches();
+       a.loadRemoteHeads();
+       a.fetchAll();
+       //
        /*
        var a = new GitLive.Repo("/home/alan/git/test1-clone");
        //a.fetchAll();
@@ -61,35 +66,146 @@ namespace  GitLive {
 
                public Repo(string path)
                {
+                       this.name = GLib.Path.get_basename(path);
                        this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
                        this.callbacks = new Callbacks(this);
                
                }
-               
-               public Gee.ArrayList<Ggit.Branch> branches()
+               Ggit.Branch                                             head = null;
+           Gee.ArrayList<Ggit.Branch>          branches = null;
+           Ggit.RemoteHead[]                           remote_heads = null;
+               public void loadLocalBranches(bool force = false)
                {
-                       var ret = new Gee.ArrayList<Ggit.Branch>();
-                       var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
+                       if (!force && this.branches != null) {
+                               return;
+                       }
                        
+                       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(), rhead.to_string());
+                               //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);
+                               if (br.is_head()) {
+                                       GLib.debug("HEAD= %s", br.get_name());
+                                       this.head = br;
+                               }
+                               
+                       }
+                       
+                       
+               }
+                
+               public bool is_managed()
+               {
+                       GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
+                       return this.repo.get_config().get_int32("gitlive.managed") == 1;
+               }
+               
+               
+               public bool is_autocommit ()
+               {       
+                       GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
+                       return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
+               }
+               public void set_autocommit(bool val)
+               {
+                       this.repo.get_config().set_int32("gitlive.autocommit", val ? "1" : "0");
+               
+               }
+                
+               public bool doMergeClose(string commit_message)
+               {
+                       this.loadLocalBranches(true);
+                       var oldbranch = this.head.get_name();
+            
+                          string [] cmd = { "merge",   "--squash",  oldbranch };
+                          this.git( cmd );
+                          cmd = { "commit",   "-a" , "-m",  commit_message };
+                          this.git( cmd );
+                          this.push();
+                          this.loadBranches(); // updates lastrev..
+               
+                      var notification = new Notify.Notification(
+                               "Merged branch %s to %s".printf(oldbranch, master),
+                               "",
+                                "dialog-information"
+                               
+                       );
 
+                       notification.set_timeout(5);
+                       notification.show();   
+               
+               
+               
+               
+               public bool is_auto_branch ()
+               {
+                       if (this.name == "gitlog") {
+                               return false;
+                       }
+                       // check remote...
+                       if (this.is_managed()) {
+                               return true;
+                       }
+                       return false;
+                       
+        
+               }
+               
+               public void loadRemoteHeads(bool force = false)
+               {
+                       
+                       if (!force && this.remote_heads != null) {
+                               return;
+                       }
+                       var r = this.repo.lookup_remote("origin");
+                       r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
+                       this.remote_heads = r.list();
+               
+               }
+               
+               
+               
+               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;
+                               }
                                
                        }
-                       return ret;
+                       //GLib.debug("missing %s", remote_branch_name);                 
+                       return null;
                
                }
+                
                
                public void fetchAll()
                {
+                       this.loadLocalBranches();
+                       this.loadRemoteHeads();
+                       
                        // remotes probably will not work with http auth..
-                       var ar = this.repo.list_remotes();
-                       foreach(var n in ar) {
+                       //var ar = this.repo.list_remotes();
+                       //foreach(var n in ar) {
+                       var n = "origin"; 
+                       
                                GLib.debug("got remote '%s'", n);
                                var r = this.repo.lookup_remote(n);
                                GLib.debug("connecting '%s'", r.get_url());
@@ -102,15 +218,55 @@ namespace  GitLive {
                                        GLib.debug("Got Error Message: %s", e.message);
                                        return;
                                }
-                               GLib.debug("getting specs '%s'", n);
-
+                               string[] far = {};
 
-                               var far = r.get_fetch_specs();
+                               foreach(var rh in this.remote_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");
+                                       r.disconnect();
+                                       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(() => {
@@ -120,7 +276,7 @@ namespace  GitLive {
                                r.disconnect();
                                
                                //r.download(
-                       }
+                        
                
                }
                
@@ -128,21 +284,19 @@ namespace  GitLive {
                
                public void pushAll()
                {
+                       
+                       this.loadLocalBranches();
+                       this.loadRemoteHeads();
                        // remotes probably will not work with http auth..
-                       var ar = this.repo.list_remotes();
-                       foreach(var n in ar) {
+                       //var ar = this.repo.list_remotes();
+                       var n = "origin";
+                       
                                GLib.debug("got remote '%s'", n);
                                var r = this.repo.lookup_remote(n);
                                GLib.debug("connecting '%s'", r.get_url());
                                 
-                               try {
-                                       string[] h = { "a = 1" };
-                                        r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
-                                       
-                               } catch (Error e) {
-                                       GLib.debug("Got Error Message: %s", e.message);
-                                       return;
-                               }
+                               r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
+                               
                                //GLib.debug("getting specs '%s'", n);
                                /* 
                                
@@ -151,10 +305,49 @@ namespace  GitLive {
                                        "+%s:%s".printf(head.get_shorthand(),head.get_name())
                                );
  */
-                               var head = this.repo.get_head();
+                               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");
+                                       r.disconnect();
+                                       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);
                                        
@@ -169,12 +362,11 @@ namespace  GitLive {
                                
                                
                                //r.download(
-                       }
+                        
                
                }
                
-               
-               
+                
 
        }
        
@@ -195,7 +387,7 @@ namespace  GitLive {
 
                protected override void progress(string message)
                {
-                       GLib.debug("progress");
+                       GLib.debug("progress: %s", message);
                        if (d_proxy != null)
                        {
                                d_proxy.progress(message);