Git.vala
[gitlive] / Git.vala
index f9d9bce..43872d4 100644 (file)
--- a/Git.vala
+++ b/Git.vala
@@ -1,5 +1,6 @@
 
-// valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libglib
+// valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 -pkg gee-0.8 -g
+
 
 
 void main()
@@ -17,16 +18,47 @@ void main()
          ); 
 
        Ggit.init();
-       var a = new Git.Repo("/home/alan/git/vala");
-       a.test();
+       
+       var a = new GitLive.Repo("/home/alan/gitlive/gitlive");
+       a.loadBranches();
+       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.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.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);
+       a.pushAll();
 }
  
 
-namespace  Git {
+namespace  GitLive {
 
        public class Repo : Object {
 
-               Ggit.Repository  repo;
+               public Ggit.Repository  repo;
                Callbacks callbacks;
 
                public Repo(string path)
@@ -35,7 +67,44 @@ namespace  Git {
                        this.callbacks = new Callbacks(this);
                
                }
-               public void test()
+               
+           Gee.ArrayList<Ggit.Branch>          branches = null;
+               public void loadBranches()
+               {
+                       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)
+               {
+                       foreach(var br in this.branches) {
+                               if (br.get_upstream().get_name() == remote_name) {
+                                       return br;
+                               }
+                       }
+                       return null;
+               
+               }
+               
+               
+               
+               public void fetchAll()
                {
                        // remotes probably will not work with http auth..
                        var ar = this.repo.list_remotes();
@@ -52,13 +121,85 @@ namespace  Git {
                                        GLib.debug("Got Error Message: %s", e.message);
                                        return;
                                }
-                               GLib.debug("getting specs '%s'", n);
                                
+                               var heads = r.list();
+                               foreach(var rh in heads) {
+                                       var br = this.getBranch(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()
+                                       );
+                               
+                               }
+                               
+                               
+                               
+                               GLib.debug("getting specs '%s'", n);
+
+
                                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(() => {
+       
+                                       r.download(far, options);
+                               //});
+                               r.disconnect();
                                
+                               //r.download(
+                       }
+               
+               }
+               
+               
+               
+               public void pushAll()
+               {
+                       // remotes probably will not work with http auth..
+                       var ar = this.repo.list_remotes();
+                       foreach(var n in ar) {
+                               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;
+                               }
+                               //GLib.debug("getting specs '%s'", n);
+                               /* 
+                               
+                               this.repo.add_remote_push(
+                                       "origin",
+                                       "+%s:%s".printf(head.get_shorthand(),head.get_name())
+                               );
+ */
+                               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;
+                               GLib.debug("Push?");
+                               r.upload(far,popts);
+                               GLib.debug("Push done?");
+                               r.disconnect();
                                
                                
                                //r.download(
@@ -74,15 +215,15 @@ namespace  Git {
        private class Callbacks : Ggit.RemoteCallbacks
        {
                //private Remote d_remote;
-               private Ggit.RemoteCallbacks? d_proxy;
+               private Ggit.RemoteCallbacks? d_proxy = null;
 
                public delegate void TransferProgress(Ggit.TransferProgress stats);
                //private TransferProgress? d_transfer_progress;
 
-               public Callbacks( Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
+               public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
                {
                        //d_remote = remote;
-                       d_proxy = proxy;
+                       //d_proxy = proxy;
                        //d_transfer_progress = (owned)transfer_progress;
                }
 
@@ -133,10 +274,19 @@ namespace  Git {
 
                protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
                {
-                       Ggit.Cred? ret = null;
                        GLib.debug("get credentials %s  UN=%s", url, username_from_url);
-                       
-                       return new Ggit.CredPlaintext(username_from_url, "test");
+                       var uri = new Soup.URI(url);
+
+                       if (uri != null) {
+                               var ret = this.netrc(uri.get_host());
+                               if (ret != null) {
+                                       return ret;
+                               }
+                               
+                               //return new Ggit.CredPlaintext(username_from_url, "test");
+                       }
+                       return null;
                        /*var provider = d_remote.credentials_provider;
 
                        if (provider != null)
@@ -152,6 +302,31 @@ namespace  Git {
                        return ret;
                        */
                }
+               public  Ggit.Cred netrc(string domain) 
+               {
+                       string str;
+                       GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
+                       var lines = str.split("\n");
+                       for(var i=0; i< lines.length; i++) {
+                               // assumes one line per entry.. if not we are buggered...
+                               //GLib.debug("got %s" , lines[i]);
+                       
+                               var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
+                               if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
+                                       continue;
+                               }
+                               GLib.debug("found password?");
+                               // we are gussing.... 
+                               return new Ggit.CredPlaintext(bits[3], bits[5]);
+
+                       }
+                       return null;
+
+
+               
+               
+               }
+               
        }