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