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 = null;
78
79                 public delegate void TransferProgress(Ggit.TransferProgress stats);
80                 //private TransferProgress? d_transfer_progress;
81
82                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, 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                         GLib.debug("progress");
92                         if (d_proxy != null)
93                         {
94                                 d_proxy.progress(message);
95                         }
96                 }
97
98                 protected override void transfer_progress(Ggit.TransferProgress stats)
99                 {
100                         GLib.debug("transfer_progress");
101                         /*
102                         if (d_transfer_progress != null)
103                         {
104                                 d_transfer_progress(stats);
105                         }
106
107                         if (d_proxy != null)
108                         {
109                                 d_proxy.transfer_progress(stats);
110                         }
111                         */
112                 }
113
114                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
115                 {
116                         GLib.debug("update_tips");
117                         //d_remote.tip_updated(refname, a, b);
118
119                         if (d_proxy != null)
120                         {
121                                 d_proxy.update_tips(refname, a, b);
122                         }
123                 }
124
125                 protected override void completion(Ggit.RemoteCompletionType type)
126                 {
127                         GLib.debug("completion");
128                         if (d_proxy != null)
129                         {
130                                 d_proxy.completion(type);
131                         }
132                 }
133
134                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
135                 {
136                         Ggit.Cred? ret = null;
137                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
138                         
139                         return new Ggit.CredPlaintext(username_from_url, "test");
140                         /*var provider = d_remote.credentials_provider;
141
142                         if (provider != null)
143                         {
144                                 ret = provider.credentials(url, username_from_url, allowed_types);
145                         }
146
147                         if (ret == null && d_proxy != null)
148                         {
149                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
150                         }
151                         
152                         return ret;
153                         */
154                 }
155         }
156         
157         
158
159 }