Git.vala
[gitlive] / Git.vala
1
2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libglib
3
4
5 void main()
6 {
7          GLib.Log.set_handler(null, 
8             GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, 
9             (dom, lvl, msg) => {
10                                         
11                                         
12                // should we debug..
13                                         
14                         
15                print("%s\n", msg);
16             }
17          ); 
18
19         Ggit.init();
20         var a = new Git.Repo("/home/alan/git/vala");
21         a.test();
22 }
23  
24
25 namespace  Git {
26
27         public class Repo : Object {
28
29                 Ggit.Repository  repo;
30                 Callbacks callbacks;
31
32                 public Repo(string path)
33                 {
34                         this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
35                         this.callbacks = new Callbacks(this);
36                 
37                 }
38                 public void test()
39                 {
40                         // remotes probably will not work with http auth..
41                         var ar = this.repo.list_remotes();
42                         foreach(var n in ar) {
43                                 GLib.debug("got remote '%s'", n);
44                                 var r = this.repo.lookup_remote(n);
45                                 GLib.debug("connecting '%s'", r.get_url());
46                                  
47                                 try {
48                                         string[] h = { "a = 1" };
49                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
50                                         
51                                 } catch (Error e) {
52                                         GLib.debug("Got Error Message: %s", e.message);
53                                         return;
54                                 }
55                                 GLib.debug("getting specs '%s'", n);
56                                 
57                                 var far = r.get_fetch_specs();
58                                 foreach(var rs in far) {
59                                         GLib.debug("got remote spec: %s", rs);
60                                 }
61                                 
62                                 
63                                 
64                                 //r.download(
65                         }
66                 
67                 }
68                 
69                 
70                 
71
72         }
73         
74         private class Callbacks : Ggit.RemoteCallbacks
75         {
76                 private Remote d_remote;
77                 private Ggit.RemoteCallbacks? d_proxy;
78
79                 public delegate void TransferProgress(Ggit.TransferProgress stats);
80                 private TransferProgress? d_transfer_progress;
81
82                 public Callbacks(Remote remote, Ggit.RemoteCallbacks? proxy, owned TransferProgress? transfer_progress)
83                 {
84                         d_remote = remote;
85                         d_proxy = proxy;
86                         d_transfer_progress = (owned)transfer_progress;
87                 }
88
89                 protected override void progress(string message)
90                 {
91                         if (d_proxy != null)
92                         {
93                                 d_proxy.progress(message);
94                         }
95                 }
96
97                 protected override void transfer_progress(Ggit.TransferProgress stats)
98                 {
99                         if (d_transfer_progress != null)
100                         {
101                                 d_transfer_progress(stats);
102                         }
103
104                         if (d_proxy != null)
105                         {
106                                 d_proxy.transfer_progress(stats);
107                         }
108                 }
109
110                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
111                 {
112                         d_remote.tip_updated(refname, a, b);
113
114                         if (d_proxy != null)
115                         {
116                                 d_proxy.update_tips(refname, a, b);
117                         }
118                 }
119
120                 protected override void completion(Ggit.RemoteCompletionType type)
121                 {
122                         if (d_proxy != null)
123                         {
124                                 d_proxy.completion(type);
125                         }
126                 }
127
128                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
129                 {
130                         Ggit.Cred? ret = null;
131                         GLib.log("get credentials %s  UN=%s", url, username_from_url);
132                         
133                         new Ggit.CredPlaintext(username_from_url, "test");
134                         /*var provider = d_remote.credentials_provider;
135
136                         if (provider != null)
137                         {
138                                 ret = provider.credentials(url, username_from_url, allowed_types);
139                         }
140
141                         if (ret == null && d_proxy != null)
142                         {
143                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
144                         }
145                         
146                         return ret;
147                 }
148         }
149         
150         
151
152 }