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