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                                 var head = this.repo.get_head();
126                                 this.repo.add_remote_push(
127                                         "origin",
128                                         "+%s:%s".printf(head.get_shorthand(),head.get_name())
129                                 );
130  */
131                                 string[] far = {};
132                                 far.push("+%s:%s".printf(head.get_shorthand(),head.get_name()));
133                                 
134                                 foreach(var rs in far) {
135                                         GLib.debug("got remote spec: %s", rs);
136                                         
137                                 }
138                                         
139                                 var popts = new Ggit.PushOptions();
140                                 //popts.callbacks = this.callbacks;
141                                 GLib.debug("Push?");
142                                 r.upload(far,popts);
143                                 GLib.debug("Push done?");
144                                 r.disconnect();
145                                 
146                                 
147                                 //r.download(
148                         }
149                 
150                 }
151                 
152                 
153                 
154
155         }
156         
157         private class Callbacks : Ggit.RemoteCallbacks
158         {
159                 //private Remote d_remote;
160                 private Ggit.RemoteCallbacks? d_proxy = null;
161
162                 public delegate void TransferProgress(Ggit.TransferProgress stats);
163                 //private TransferProgress? d_transfer_progress;
164
165                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
166                 {
167                         //d_remote = remote;
168                         //d_proxy = proxy;
169                         //d_transfer_progress = (owned)transfer_progress;
170                 }
171
172                 protected override void progress(string message)
173                 {
174                         GLib.debug("progress");
175                         if (d_proxy != null)
176                         {
177                                 d_proxy.progress(message);
178                         }
179                 }
180
181                 protected override void transfer_progress(Ggit.TransferProgress stats)
182                 {
183                         GLib.debug("transfer_progress");
184                         /*
185                         if (d_transfer_progress != null)
186                         {
187                                 d_transfer_progress(stats);
188                         }
189
190                         if (d_proxy != null)
191                         {
192                                 d_proxy.transfer_progress(stats);
193                         }
194                         */
195                 }
196
197                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
198                 {
199                         GLib.debug("update_tips");
200                         //d_remote.tip_updated(refname, a, b);
201
202                         if (d_proxy != null)
203                         {
204                                 d_proxy.update_tips(refname, a, b);
205                         }
206                 }
207
208                 protected override void completion(Ggit.RemoteCompletionType type)
209                 {
210                         GLib.debug("completion");
211                         if (d_proxy != null)
212                         {
213                                 d_proxy.completion(type);
214                         }
215                 }
216
217                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
218                 {
219  
220                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
221                         var uri = new Soup.URI(url);
222
223                         if (uri != null) {
224                                 var ret = this.netrc(uri.get_host());
225                                 if (ret != null) {
226                                         return ret;
227                                 }
228                                 
229                                 //return new Ggit.CredPlaintext(username_from_url, "test");
230                         }
231                         return null;
232                         /*var provider = d_remote.credentials_provider;
233
234                         if (provider != null)
235                         {
236                                 ret = provider.credentials(url, username_from_url, allowed_types);
237                         }
238
239                         if (ret == null && d_proxy != null)
240                         {
241                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
242                         }
243                         
244                         return ret;
245                         */
246                 }
247                 public  Ggit.Cred netrc(string domain) 
248                 {
249                         string str;
250                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
251                         var lines = str.split("\n");
252                         for(var i=0; i< lines.length; i++) {
253                                 // assumes one line per entry.. if not we are buggered...
254                                 //GLib.debug("got %s" , lines[i]);
255                         
256                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
257                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
258                                         continue;
259                                 }
260                                 GLib.debug("found password?");
261                                 // we are gussing.... 
262                                 return new Ggit.CredPlaintext(bits[3], bits[5]);
263
264                         }
265                         return null;
266
267
268                 
269                 
270                 }
271                 
272         }
273         
274         
275
276 }