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