Git.vala
authorAlan Knowles <alan@roojs.com>
Wed, 13 Mar 2019 09:22:39 +0000 (17:22 +0800)
committerAlan Knowles <alan@roojs.com>
Wed, 13 Mar 2019 09:22:39 +0000 (17:22 +0800)
Git.vala

index 8b5d7d3..07c6e74 100644 (file)
--- a/Git.vala
+++ b/Git.vala
@@ -20,7 +20,10 @@ void main()
        Ggit.init();
        
        var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
+       a.is_managed();
+       a.is_autocommit();
        a.loadLocalBranches();
+       a.loadRemoteHeads();
        a.fetchAll();
        //
        /*
@@ -63,14 +66,20 @@ 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);
                
                }
-               
+               Ggit.Branch                                             head = null;
            Gee.ArrayList<Ggit.Branch>          branches = null;
-               public void loadLocalBranches()
+           Ggit.RemoteHead[]                           remote_heads = null;
+               public void loadLocalBranches(bool force = false)
                {
+                       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()) {
@@ -81,10 +90,87 @@ namespace  GitLive {
                                //              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);
@@ -108,15 +194,18 @@ namespace  GitLive {
                        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());
@@ -130,19 +219,20 @@ namespace  GitLive {
                                        return;
                                }
                                string[] far = {};
-                               var heads = r.list();
-                               foreach(var rh in heads) {
+
+                               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",
+                                       /*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(
@@ -163,6 +253,7 @@ namespace  GitLive {
                                
                                if (far.length < 1) {
                                        GLib.debug("no fetch required.. it's uptodate");
+                                       r.disconnect();
                                        return;
                                }
                                
@@ -185,7 +276,7 @@ namespace  GitLive {
                                r.disconnect();
                                
                                //r.download(
-                       }
+                        
                
                }
                
@@ -193,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);
                                /* 
                                
@@ -225,12 +314,12 @@ namespace  GitLive {
                                         }
                                
                                        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(
@@ -251,6 +340,7 @@ namespace  GitLive {
                                
                                if (far.length < 1) {
                                        GLib.debug("no push required.. it's uptodate");
+                                       r.disconnect();
                                        return;
                                }
  
@@ -272,12 +362,11 @@ namespace  GitLive {
                                
                                
                                //r.download(
-                       }
+                        
                
                }
                
-               
-               
+                
 
        }
        
@@ -298,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);