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