Uncommited changes synced
[gitlive] / Git.vala
index 0d3ff2e..795eabb 100644 (file)
--- a/Git.vala
+++ b/Git.vala
@@ -1,7 +1,6 @@
 
 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 --pkg gee-0.8 -g
-
-
 
 void main()
 {
@@ -17,16 +16,36 @@ void main()
             }
          ); 
 
-       Ggit.init();
        
+       Ggit.init();
        var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
+       a.diffhead();
+       //a.fetchAll();
+       return;
+       /*
+       GLib.Timeout.add (1, () => {
+      GLib.debug("Meanwhile");
+      return true;
+    }, GLib.Priority.HIGH);
+       
+       var loop = new MainLoop();
+        
+       var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
+      GLib.debug("Starting");  
+       a.loadRemoteHeads.begin(true, (obj,res) => {
+               a.loadRemoteHeads.end(res);
+                GLib.debug("got results");
+               a.loadLocalBranches();
+               loop.quit();
+       });
+       loop.run();
        //a.mergeMasterIntoHead();
        //a.walkDiff();
        //return;
        //a.is_managed();
        //a.is_autocommit();
-       a.loadLocalBranches();
-       a.loadRemoteHeads();
+       
+
        //a.fetchAll();
        //
        /*
@@ -88,18 +107,29 @@ namespace  GitLive {
                        var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
                        while (r.next()) {
                                var br = r.get() as Ggit.Branch;
+                               if (br == null) {
+                                       continue;
+                               }
+                               if (br.is_tag()) {
+                                       continue;
+                               }
+                               
                                //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: H=%s name = %s upstream = %s oid = %s ", 
-                                               br.is_head() ? "Y" : "n",
-                                               br.get_name(), br.get_upstream().get_name(), 
-                                           br.get_target().to_string());
-                               this.branches.add(br);
-                               if (br.is_head()) {
-                                       GLib.debug("HEAD= %s", br.get_name());
-                                       this.head = br;
+                               try {
+                                       GLib.debug("got branch: N=%s", br.get_name() );
+                                       GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
+
+                                       GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
+                                       GLib.debug("oid = %s",br.get_target().to_string());
+                                       this.branches.add(br);
+                                       if (br.is_head()) {
+                                               GLib.debug("HEAD= %s", br.get_name());
+                                               this.head = br;
+                                       }
+                               } catch (Error e) {
+                                       GLib.debug("Skip branch");
                                }
-                               
                        }
                        
                        
@@ -191,6 +221,40 @@ namespace  GitLive {
                        
                        
                }
+               
+               public void diffhead()
+               {
+                       var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
+                       Ggit.Branch? head = null;
+                       while (r.next()) {
+                               var gbr = r.get() as Ggit.Branch;
+                               if (gbr.is_head()) {
+                                       head = gbr;
+                               }
+                       }
+                       GLib.debug("checking head=%s",head == null ? "EMPTY" :  head.get_name());
+                       var br = this.repo.lookup_branch(head.get_name(),Ggit.BranchType.LOCAL);
+                       var commit  = this.repo.lookup_commit(br.get_target());
+
+                       
+                       var diff = new Ggit.Diff.tree_to_workdir(this.repo, commit.get_tree(), new Ggit.DiffOptions());
+                       var ret = "";
+                       diff.print(Ggit.DiffFormatType.PATCH, (delta, hunk, line) => {
+                               switch(line.get_origin()) {
+                                       case Ggit.DiffLineType.ADDITION: ret+="+"; break;
+                                       case Ggit.DiffLineType.DELETION: ret+="-";break;
+                                       case Ggit.DiffLineType.CONTEXT:  ret+=" ";break;
+                                       case Ggit.DiffLineType.HUNK_HDR: break;                                 
+                                       case Ggit.DiffLineType.FILE_HDR: break;
+                                       default: ret+=" ";break;
+                               }
+                               ret += " " + line.get_text();
+                               return 0;
+                       });
+                       GLib.debug("%s", ret);
+               }
+               
+               
                public void mergeMasterIntoHead()
                {
                        // assumes head is not master...
@@ -243,6 +307,9 @@ namespace  GitLive {
                }
                
                
+
+               
+               
                /*
                public bool doMergeClose(string commit_message)
                {
@@ -285,19 +352,41 @@ namespace  GitLive {
         
                }
                
-               public void loadRemoteHeads(bool force = false)
+               public async void loadRemoteHeads(bool force = false)
                {
+                   SourceFunc callback = loadRemoteHeads.callback;
                        
-                       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();
-               
+                       ThreadFunc<bool> run = () => {
+                               
+                               if (!force && this.remote_heads != null) {
+                                       return true;;
+                               }
+                               var r = this.repo.lookup_remote("origin");
+                               r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
+                               yield;
+                               this.remote_heads = r.list();
+                               
+                               foreach(var br in this.remote_heads) {
+                                       if (!br.get_name().has_prefix("refs/heads/")) {
+                                               continue;
+                                       }
+                                       
+                                       GLib.debug("Remote: name=%s  oid=%s local_oid=%s is_local=%s",
+                                               br.get_name(),
+                                               br.get_oid().to_string(),
+                                               br.get_local_oid().to_string(),
+                                               br.is_local() ? "Y" : "n"
+                                               );
+                               }
+                               Idle.add((owned) callback);
+                               return true;;
+                       };
+                       new Thread<bool>("loadRemoteHeads-" , run);
+                       yield;
+                       
+
                }
-               
-               
+                
                
                Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
                {
@@ -361,7 +450,10 @@ namespace  GitLive {
                                                br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
                                        );
                                        */
-                                       var loc_oid = this.repo.revparse(br.get_name() ).get_id();
+                                       //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
+                                       
+                                       var loc_oid = br.get_target();
+                                       
                                        size_t ahead, behind;
                                        this.repo.get_ahead_behind(
                                                loc_oid,