Git.vala
[gitlive] / Git.vala
1
2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup
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         var a = new Git.Repo("/home/alan/gitlive/gitlive");
22         a.test();
23 }
24  
25
26 namespace  Git {
27
28         public class Repo : Object {
29
30                 Ggit.Repository  repo;
31                 Callbacks callbacks;
32
33                 public Repo(string path)
34                 {
35                         this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
36                         this.callbacks = new Callbacks(this);
37                 
38                 }
39                 public void test()
40                 {
41                         // remotes probably will not work with http auth..
42                         var ar = this.repo.list_remotes();
43                         foreach(var n in ar) {
44                                 GLib.debug("got remote '%s'", n);
45                                 var r = this.repo.lookup_remote(n);
46                                 GLib.debug("connecting '%s'", r.get_url());
47                                  
48                                 try {
49                                         string[] h = { "a = 1" };
50                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
51                                         
52                                 } catch (Error e) {
53                                         GLib.debug("Got Error Message: %s", e.message);
54                                         return;
55                                 }
56                                 GLib.debug("getting specs '%s'", n);
57                                 
58                                 var far = r.get_fetch_specs();
59                                 foreach(var rs in far) {
60                                         GLib.debug("got remote spec: %s", rs);
61                                 }
62                                 
63                                 
64                                 
65                                 //r.download(
66                         }
67                 
68                 }
69                 
70                 
71                 
72
73         }
74         
75         private class Callbacks : Ggit.RemoteCallbacks
76         {
77                 //private Remote d_remote;
78                 private Ggit.RemoteCallbacks? d_proxy = null;
79
80                 public delegate void TransferProgress(Ggit.TransferProgress stats);
81                 //private TransferProgress? d_transfer_progress;
82
83                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
84                 {
85                         //d_remote = remote;
86                         //d_proxy = proxy;
87                         //d_transfer_progress = (owned)transfer_progress;
88                 }
89
90                 protected override void progress(string message)
91                 {
92                         GLib.debug("progress");
93                         if (d_proxy != null)
94                         {
95                                 d_proxy.progress(message);
96                         }
97                 }
98
99                 protected override void transfer_progress(Ggit.TransferProgress stats)
100                 {
101                         GLib.debug("transfer_progress");
102                         /*
103                         if (d_transfer_progress != null)
104                         {
105                                 d_transfer_progress(stats);
106                         }
107
108                         if (d_proxy != null)
109                         {
110                                 d_proxy.transfer_progress(stats);
111                         }
112                         */
113                 }
114
115                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
116                 {
117                         GLib.debug("update_tips");
118                         //d_remote.tip_updated(refname, a, b);
119
120                         if (d_proxy != null)
121                         {
122                                 d_proxy.update_tips(refname, a, b);
123                         }
124                 }
125
126                 protected override void completion(Ggit.RemoteCompletionType type)
127                 {
128                         GLib.debug("completion");
129                         if (d_proxy != null)
130                         {
131                                 d_proxy.completion(type);
132                         }
133                 }
134
135                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
136                 {
137                         Ggit.Cred? ret = null;
138                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
139                         var uri = new Soup.URI(url);
140
141                         if (uri != null) {
142                                 var ret = this.netrc(uri.get_host());
143                                 if (ret != null) {
144                                         return ret;
145                                 }
146                                 
147                                 //return new Ggit.CredPlaintext(username_from_url, "test");
148                         }
149                         return null;
150                         /*var provider = d_remote.credentials_provider;
151
152                         if (provider != null)
153                         {
154                                 ret = provider.credentials(url, username_from_url, allowed_types);
155                         }
156
157                         if (ret == null && d_proxy != null)
158                         {
159                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
160                         }
161                         
162                         return ret;
163                         */
164                 }
165         }
166         
167         public  Ggit.Cred netrc(string domain) 
168         {
169                 string str;
170                 GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
171                 var lines = str.split("\n");
172                 for(var i=0; i< lines.length; i++) {
173                         // assumes one line per entry.. if not we are buggered...
174                         GLib.debug("got %s" , lines[i]);
175                 
176                         var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
177                         if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
178                                 continue;
179                         }
180                         GLib.debug("found password?");
181                         // we are gussing.... 
182                         return new Ggit.CredPlaintext(bits[3], bits[5]);
183
184                 }
185                 return null;
186
187
188         
189         
190         }
191
192 }