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