Partial Fix #5782 - messing around with libgit2-glib
[gitlive] / GitCallbacks.vala
diff --git a/GitCallbacks.vala b/GitCallbacks.vala
new file mode 100644 (file)
index 0000000..55de1b2
--- /dev/null
@@ -0,0 +1,116 @@
+public class GitCallbacks : Ggit.RemoteCallbacks
+{
+       //private Remote d_remote;
+       private Ggit.RemoteCallbacks? d_proxy = null;
+
+       public delegate void TransferProgress(Ggit.TransferProgress stats);
+       //private TransferProgress? d_transfer_progress;
+
+       public GitCallbacks( GitRepo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
+       {
+               //d_remote = remote;
+               //d_proxy = proxy;
+               //d_transfer_progress = (owned)transfer_progress;
+       }
+
+       protected override void progress(string message)
+       {
+               GLib.debug("progress: %s", message);
+               if (d_proxy != null)
+               {
+                       d_proxy.progress(message);
+               }
+       }
+
+       protected override void transfer_progress(Ggit.TransferProgress stats)
+       {
+               GLib.debug("transfer_progress");
+               /*
+               if (d_transfer_progress != null)
+               {
+                       d_transfer_progress(stats);
+               }
+
+               if (d_proxy != null)
+               {
+                       d_proxy.transfer_progress(stats);
+               }
+               */
+       }
+
+       protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
+       {
+               GLib.debug("update_tips");
+               //d_remote.tip_updated(refname, a, b);
+
+               if (d_proxy != null)
+               {
+                       d_proxy.update_tips(refname, a, b);
+               }
+       }
+
+       protected override void completion(Ggit.RemoteCompletionType type)
+       {
+               GLib.debug("completion");
+               if (d_proxy != null)
+               {
+                       d_proxy.completion(type);
+               }
+       }
+
+       protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
+       {
+
+               GLib.debug("get credentials %s  UN=%s", url, username_from_url);
+               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)
+               {
+                       ret = provider.credentials(url, username_from_url, allowed_types);
+               }
+
+               if (ret == null && d_proxy != null)
+               {
+                       ret = d_proxy.credentials(url, username_from_url, allowed_types);
+               }
+               
+               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;
+
+
+       
+       
+       }
+       
+}
\ No newline at end of file