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