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