Git.vala
[gitlive] / Git.vala
index 07c6e74..0755899 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 -pkg gee-0.8 -g
+// valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 --pkg gee-0.8 -g
 
 
 
@@ -20,6 +20,9 @@ void main()
        Ggit.init();
        
        var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
+       a.mergeMasterIntoHead();
+       //a.walkDiff();
+       return;
        a.is_managed();
        a.is_autocommit();
        a.loadLocalBranches();
@@ -61,6 +64,7 @@ namespace  GitLive {
 
        public class Repo : Object {
 
+               string name = "";
                public Ggit.Repository  repo;
                Callbacks callbacks;
 
@@ -114,15 +118,125 @@ namespace  GitLive {
                }
                public void set_autocommit(bool val)
                {
-                       this.repo.get_config().set_int32("gitlive.autocommit", val ? "1" : "0");
+                       this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
                
                }
                 
+               public void walkDiff()
+               {
+                       this.loadLocalBranches();
+                       
+                       
+                       
+                       var oid =  this.repo.revparse(this.head.get_name() ).get_id()  ;
+                       var moid =  this.repo.revparse("refs/heads/master" ).get_id()  ;
+                       
+                       var a = new Ggit.RevisionWalker(this.repo);
+                       a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
+                       a.push(oid);
+                       a.hide(moid);
+                       var last = oid;
+                       for (var noid = a.next(); noid != null; noid= a.next()) {
+                               //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
+                               GLib.debug("rev: %s",
+                                       noid.to_string()
+                               );
+                               last = noid;
+                       }
+                       var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
+                       var parent = commit.get_parents();
+                       GLib.debug("parent = %s", parent.get_id(0).to_string());
+                       var master_rev =  parent.get_id(0);
+                       var master_commit  = this.repo.lookup_commit(master_rev);;
+                       
+                       var head_commit = this.repo.lookup_commit(oid);
+                       
+                       var master_tree = master_commit.get_tree();
+                       var head_tree = head_commit.get_tree();
+                       
+                       var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
+                       
+                       diff.print(Ggit.DiffFormatType.PATCH, ( delta,  hunk,  line) => {
+                               GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
+                               return 0;
+                       });
+                       // let's try a merge..
+                       var mo = new Ggit.MergeOptions();
+                       mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
+                       var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
+                       
+                       if (ix.has_conflicts()) {
+                               GLib.debug("merge has conflicts");
+                               return;
+                       }
+                        var treeoid = ix.write_tree();
+                        
+                        
+                         
+                       var parents =   new Ggit.Commit[] { master_commit };
+
+
+                       var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;
+                       
+                       var sig = new Ggit.Signature.now(
+                                       this.repo.get_config().get_string("user.name"),
+                                       this.repo.get_config().get_string("user.email")
+                       );
+                       this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
+       
+                       
+                       
+                       
+                       
+               }
+               public void mergeMasterIntoHead()
+               {
+                       // assumes head is not master...
+                       this.loadLocalBranches();
+                       GLib.debug("head rev = %s", this.head.get_name());
+                       var head_oid =  this.repo.revparse(this.head.get_name() ).get_id()  ;
+                       var master_oid =  this.repo.revparse("refs/heads/master" ).get_id()  ;
+
+                       var master_commit  = this.repo.lookup_commit(master_oid);;
+                       var head_commit = this.repo.lookup_commit(head_oid);
+                        
+               
+                       var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
+                       
+                       var anc_commit = this.repo.lookup_commit(anc_oid);
+                       var anc_tree = anc_commit.get_tree();
+                       
+                       var master_tree = master_commit.get_tree();
+                       var head_tree = head_commit.get_tree();
+                       var mo = new Ggit.MergeOptions();
+                       mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
+                       var ix  = this.repo.merge_trees(anc_tree, master_tree, head_tree, mo);
+                       ix.write();
+                        var treeoid = ix.write_tree();
+                        
+                        
+                         
+                       var parents =   new Ggit.Commit[] { master_commit };
+
+
+                       var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;
+                       
+                       var sig = new Ggit.Signature.now(
+                                       this.repo.get_config().get_string("user.name"),
+                                       this.repo.get_config().get_string("user.email")
+                       );
+                       this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
+       
+               
+               }
+               
+               
+               /*
                public bool doMergeClose(string commit_message)
                {
                        this.loadLocalBranches(true);
                        var oldbranch = this.head.get_name();
-            
+            // going to asume this is merge trees..
                           string [] cmd = { "merge",   "--squash",  oldbranch };
                           this.git( cmd );
                           cmd = { "commit",   "-a" , "-m",  commit_message };
@@ -141,6 +255,8 @@ namespace  GitLive {
                        notification.show();   
                
                
+               }
+               */
                
                
                public bool is_auto_branch ()