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