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