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