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