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