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