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