Git.vala
[gitlive] / Git.vala
1
2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libglib
3
4
5 void main()
6 {
7          GLib.Log.set_handler(null, 
8             GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, 
9             (dom, lvl, msg) => {
10                                         
11                                         
12                // should we debug..
13                                         
14                         
15                print("%s\n", msg);
16             }
17          ); 
18
19         Ggit.init();
20         var a = new Git.Repo("/home/alan/gitlive/gitlive");
21         a.test();
22 }
23  
24
25 namespace  Git {
26
27         public class Repo : Object {
28
29                 Ggit.Repository  repo;
30                 Callbacks callbacks;
31
32                 public Repo(string path)
33                 {
34                         this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
35                         this.callbacks = new Callbacks(this);
36                 
37                 }
38                 public void test()
39                 {
40                         // remotes probably will not work with http auth..
41                         var ar = this.repo.list_remotes();
42                         foreach(var n in ar) {
43                                 GLib.debug("got remote '%s'", n);
44                                 var r = this.repo.lookup_remote(n);
45                                 GLib.debug("connecting '%s'", r.get_url());
46                                  
47                                 try {
48                                         string[] h = { "a = 1" };
49                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
50                                         
51                                 } catch (Error e) {
52                                         GLib.debug("Got Error Message: %s", e.message);
53                                         return;
54                                 }
55                                 GLib.debug("getting specs '%s'", n);
56                                 
57                                 var far = r.get_fetch_specs();
58                                 foreach(var rs in far) {
59                                         GLib.debug("got remote spec: %s", rs);
60                                 }
61                                 
62                                 
63                                 
64                                 //r.download(
65                         }
66                 
67                 }
68                 
69                 
70                 
71
72         }
73         
74         private class Callbacks : Ggit.RemoteCallbacks
75         {
76                 //private Remote d_remote;
77                 private Ggit.RemoteCallbacks? d_proxy = null;
78
79                 public delegate void TransferProgress(Ggit.TransferProgress stats);
80                 //private TransferProgress? d_transfer_progress;
81
82                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
83                 {
84                         //d_remote = remote;
85                         //d_proxy = proxy;
86                         //d_transfer_progress = (owned)transfer_progress;
87                 }
88
89                 protected override void progress(string message)
90                 {
91                         GLib.debug("progress");
92                         if (d_proxy != null)
93                         {
94                                 d_proxy.progress(message);
95                         }
96                 }
97
98                 protected override void transfer_progress(Ggit.TransferProgress stats)
99                 {
100                         GLib.debug("transfer_progress");
101                         /*
102                         if (d_transfer_progress != null)
103                         {
104                                 d_transfer_progress(stats);
105                         }
106
107                         if (d_proxy != null)
108                         {
109                                 d_proxy.transfer_progress(stats);
110                         }
111                         */
112                 }
113
114                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
115                 {
116                         GLib.debug("update_tips");
117                         //d_remote.tip_updated(refname, a, b);
118
119                         if (d_proxy != null)
120                         {
121                                 d_proxy.update_tips(refname, a, b);
122                         }
123                 }
124
125                 protected override void completion(Ggit.RemoteCompletionType type)
126                 {
127                         GLib.debug("completion");
128                         if (d_proxy != null)
129                         {
130                                 d_proxy.completion(type);
131                         }
132                 }
133
134                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
135                 {
136                         Ggit.Cred? ret = null;
137                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
138                         var uri = new Soup.URI(url);
139
140                         if (uri != null) {
141                                 var ret = this.netrc(uri.get_host());
142                                 if (ret != null) {
143                                         return ret;
144                                 }
145                                 
146                                 //return new Ggit.CredPlaintext(username_from_url, "test");
147                         }
148                         return null;
149                         /*var provider = d_remote.credentials_provider;
150
151                         if (provider != null)
152                         {
153                                 ret = provider.credentials(url, username_from_url, allowed_types);
154                         }
155
156                         if (ret == null && d_proxy != null)
157                         {
158                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
159                         }
160                         
161                         return ret;
162                         */
163                 }
164         }
165         
166         public  Ggit.Cred netrc(string domain) 
167         {
168                 string str;
169                 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
170                 var lines = str.split("\n");
171                 for(var i=0; i< lines.length; i++) {
172                         // assumes one line per entry.. if not we are buggered...
173                         GLib.debug("got %s" , lines[i]);
174                 
175                         var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
176                         if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
177                                 continue;
178                         }
179                         GLib.debug("found password?");
180                         // we are gussing.... 
181                         return new Ggit.CredPlaintext(bits[3], bits[5]);
182
183                 }
184                 return null;
185
186
187         
188         
189         }
190
191 }