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 Git.Repo("/home/alan/gitlive/web.Texon");
22         a.fetchAll();
23 }
24  
25
26 namespace  Git {
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                                 r.download(far, options);
68                                 
69                                 
70                                 
71                                 //r.download(
72                         }
73                 
74                 }
75                 public void pushAll()
76                 {
77                         // remotes probably will not work with http auth..
78                         var ar = this.repo.list_remotes();
79                         foreach(var n in ar) {
80                                 GLib.debug("got remote '%s'", n);
81                                 var r = this.repo.lookup_remote(n);
82                                 GLib.debug("connecting '%s'", r.get_url());
83                                  
84                                 try {
85                                         string[] h = { "a = 1" };
86                                         r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
87                                         
88                                 } catch (Error e) {
89                                         GLib.debug("Got Error Message: %s", e.message);
90                                         return;
91                                 }
92                                 GLib.debug("getting specs '%s'", n);
93
94
95                                 var far = r.get_fetch_specs();
96                                 
97                                 foreach(var rs in far) {
98                                         GLib.debug("got remote spec: %s", rs);
99                                         
100                                 }
101                                 var options = new Ggit.FetchOptions();
102                                 options.set_remote_callbacks(this.callbacks);
103                                 r.download(far, options);
104                                 
105                                 
106                                 
107                                 //r.download(
108                         }
109                 
110                 }
111                 
112                 
113                 
114
115         }
116         
117         private class Callbacks : Ggit.RemoteCallbacks
118         {
119                 //private Remote d_remote;
120                 private Ggit.RemoteCallbacks? d_proxy = null;
121
122                 public delegate void TransferProgress(Ggit.TransferProgress stats);
123                 //private TransferProgress? d_transfer_progress;
124
125                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
126                 {
127                         //d_remote = remote;
128                         //d_proxy = proxy;
129                         //d_transfer_progress = (owned)transfer_progress;
130                 }
131
132                 protected override void progress(string message)
133                 {
134                         GLib.debug("progress");
135                         if (d_proxy != null)
136                         {
137                                 d_proxy.progress(message);
138                         }
139                 }
140
141                 protected override void transfer_progress(Ggit.TransferProgress stats)
142                 {
143                         GLib.debug("transfer_progress");
144                         /*
145                         if (d_transfer_progress != null)
146                         {
147                                 d_transfer_progress(stats);
148                         }
149
150                         if (d_proxy != null)
151                         {
152                                 d_proxy.transfer_progress(stats);
153                         }
154                         */
155                 }
156
157                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
158                 {
159                         GLib.debug("update_tips");
160                         //d_remote.tip_updated(refname, a, b);
161
162                         if (d_proxy != null)
163                         {
164                                 d_proxy.update_tips(refname, a, b);
165                         }
166                 }
167
168                 protected override void completion(Ggit.RemoteCompletionType type)
169                 {
170                         GLib.debug("completion");
171                         if (d_proxy != null)
172                         {
173                                 d_proxy.completion(type);
174                         }
175                 }
176
177                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
178                 {
179  
180                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
181                         var uri = new Soup.URI(url);
182
183                         if (uri != null) {
184                                 var ret = this.netrc(uri.get_host());
185                                 if (ret != null) {
186                                         return ret;
187                                 }
188                                 
189                                 //return new Ggit.CredPlaintext(username_from_url, "test");
190                         }
191                         return null;
192                         /*var provider = d_remote.credentials_provider;
193
194                         if (provider != null)
195                         {
196                                 ret = provider.credentials(url, username_from_url, allowed_types);
197                         }
198
199                         if (ret == null && d_proxy != null)
200                         {
201                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
202                         }
203                         
204                         return ret;
205                         */
206                 }
207                 public  Ggit.Cred netrc(string domain) 
208                 {
209                         string str;
210                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
211                         var lines = str.split("\n");
212                         for(var i=0; i< lines.length; i++) {
213                                 // assumes one line per entry.. if not we are buggered...
214                                 GLib.debug("got %s" , lines[i]);
215                         
216                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
217                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
218                                         continue;
219                                 }
220                                 GLib.debug("found password?");
221                                 // we are gussing.... 
222                                 return new Ggit.CredPlaintext(bits[3], bits[5]);
223
224                         }
225                         return null;
226
227
228                 
229                 
230                 }
231                 
232         }
233         
234         
235
236 }