1 | /* |
---|
2 | * headrev.c : print out the HEAD revision of a repository. |
---|
3 | * |
---|
4 | * ==================================================================== |
---|
5 | * Copyright (c) 2004 CollabNet. All rights reserved. |
---|
6 | * |
---|
7 | * This software is licensed as described in the file COPYING, which |
---|
8 | * you should have received as part of this distribution. The terms |
---|
9 | * are also available at http://subversion.tigris.org/license-1.html. |
---|
10 | * If newer versions of this license are posted there, you may use a |
---|
11 | * newer version instead, at your option. |
---|
12 | * |
---|
13 | * This software consists of voluntary contributions made by many |
---|
14 | * individuals. For exact contribution history, see the revision |
---|
15 | * history and logs, available at http://subversion.tigris.org/. |
---|
16 | * ==================================================================== |
---|
17 | * |
---|
18 | * To compile on unix against Subversion and APR libraries, try |
---|
19 | * something like: |
---|
20 | * |
---|
21 | * cc headrev.c -o headrev \ |
---|
22 | * -I/usr/local/include/subversion-1 -I/usr/local/apache2/include \ |
---|
23 | * -L/usr/local/apache2/lib -L/usr/local/lib \ |
---|
24 | * -lsvn_client-1 -lsvn_ra-1 -lsvn_subr-1 -lapr-0 -laprutil-0 |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "svn_client.h" |
---|
29 | #include "svn_pools.h" |
---|
30 | #include "svn_config.h" |
---|
31 | #include "svn_fs.h" |
---|
32 | #include "svn_path.h" |
---|
33 | #include "svn_cmdline.h" |
---|
34 | |
---|
35 | |
---|
36 | /* Display a prompt and read a one-line response into the provided buffer, |
---|
37 | removing a trailing newline if present. */ |
---|
38 | static svn_error_t * |
---|
39 | prompt_and_read_line(const char *prompt, |
---|
40 | char *buffer, |
---|
41 | size_t max) |
---|
42 | { |
---|
43 | int len; |
---|
44 | printf("%s: ", prompt); |
---|
45 | if (fgets(buffer, max, stdin) == NULL) |
---|
46 | return svn_error_create(0, NULL, "error reading stdin"); |
---|
47 | len = strlen(buffer); |
---|
48 | if (len > 0 && buffer[len-1] == '\n') |
---|
49 | buffer[len-1] = 0; |
---|
50 | return SVN_NO_ERROR; |
---|
51 | } |
---|
52 | |
---|
53 | /* A tiny callback function of type 'svn_auth_simple_prompt_func_t'. For |
---|
54 | a much better example, see svn_cl__auth_simple_prompt in the official |
---|
55 | svn cmdline client. */ |
---|
56 | static svn_error_t * |
---|
57 | my_simple_prompt_callback (svn_auth_cred_simple_t **cred, |
---|
58 | void *baton, |
---|
59 | const char *realm, |
---|
60 | const char *username, |
---|
61 | svn_boolean_t may_save, |
---|
62 | apr_pool_t *pool) |
---|
63 | { |
---|
64 | svn_auth_cred_simple_t *ret = apr_pcalloc (pool, sizeof (*ret)); |
---|
65 | char answerbuf[100]; |
---|
66 | |
---|
67 | if (realm) |
---|
68 | { |
---|
69 | printf ("Authentication realm: %s\n", realm); |
---|
70 | } |
---|
71 | |
---|
72 | if (username) |
---|
73 | ret->username = apr_pstrdup (pool, username); |
---|
74 | else |
---|
75 | { |
---|
76 | SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf))); |
---|
77 | ret->username = apr_pstrdup (pool, answerbuf); |
---|
78 | } |
---|
79 | |
---|
80 | SVN_ERR (prompt_and_read_line("Password", answerbuf, sizeof(answerbuf))); |
---|
81 | ret->password = apr_pstrdup (pool, answerbuf); |
---|
82 | |
---|
83 | *cred = ret; |
---|
84 | return SVN_NO_ERROR; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | /* A tiny callback function of type 'svn_auth_username_prompt_func_t'. For |
---|
89 | a much better example, see svn_cl__auth_username_prompt in the official |
---|
90 | svn cmdline client. */ |
---|
91 | static svn_error_t * |
---|
92 | my_username_prompt_callback (svn_auth_cred_username_t **cred, |
---|
93 | void *baton, |
---|
94 | const char *realm, |
---|
95 | svn_boolean_t may_save, |
---|
96 | apr_pool_t *pool) |
---|
97 | { |
---|
98 | svn_auth_cred_username_t *ret = apr_pcalloc (pool, sizeof (*ret)); |
---|
99 | char answerbuf[100]; |
---|
100 | |
---|
101 | if (realm) |
---|
102 | { |
---|
103 | printf ("Authentication realm: %s\n", realm); |
---|
104 | } |
---|
105 | |
---|
106 | SVN_ERR (prompt_and_read_line("Username", answerbuf, sizeof(answerbuf))); |
---|
107 | ret->username = apr_pstrdup (pool, answerbuf); |
---|
108 | |
---|
109 | *cred = ret; |
---|
110 | return SVN_NO_ERROR; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /* A callback function used when the RA layer needs a handle to a |
---|
115 | temporary file. This is a reduced version of the callback used in |
---|
116 | the official svn cmdline client. */ |
---|
117 | static svn_error_t * |
---|
118 | open_tmp_file (apr_file_t **fp, |
---|
119 | void *callback_baton, |
---|
120 | apr_pool_t *pool) |
---|
121 | { |
---|
122 | const char *path; |
---|
123 | const char *ignored_filename; |
---|
124 | |
---|
125 | SVN_ERR (svn_io_temp_dir (&path, pool)); |
---|
126 | path = svn_path_join (path, "tempfile", pool); |
---|
127 | |
---|
128 | /* Open a unique file, with delete-on-close set. */ |
---|
129 | SVN_ERR (svn_io_open_unique_file2 (fp, &ignored_filename, |
---|
130 | path, ".tmp", |
---|
131 | svn_io_file_del_on_close, pool)); |
---|
132 | |
---|
133 | return SVN_NO_ERROR; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | int |
---|
138 | main (int argc, const char **argv) |
---|
139 | { |
---|
140 | apr_pool_t *pool; |
---|
141 | svn_error_t *err; |
---|
142 | const char *URL; |
---|
143 | svn_ra_session_t *session; |
---|
144 | svn_ra_callbacks2_t *cbtable; |
---|
145 | svn_revnum_t rev; |
---|
146 | apr_hash_t *cfg_hash; |
---|
147 | svn_auth_baton_t *auth_baton; |
---|
148 | |
---|
149 | if (argc <= 1) |
---|
150 | { |
---|
151 | printf ("Usage: %s URL\n", argv[0]); |
---|
152 | printf (" Print HEAD revision of URL's repository.\n"); |
---|
153 | return EXIT_FAILURE; |
---|
154 | } |
---|
155 | else |
---|
156 | URL = argv[1]; |
---|
157 | |
---|
158 | /* Initialize the app. Send all error messages to 'stderr'. */ |
---|
159 | if (svn_cmdline_init ("headrev", stderr) != EXIT_SUCCESS) |
---|
160 | return EXIT_FAILURE; |
---|
161 | |
---|
162 | /* Create top-level memory pool. Be sure to read the HACKING file to |
---|
163 | understand how to properly use/free subpools. */ |
---|
164 | pool = svn_pool_create (NULL); |
---|
165 | |
---|
166 | /* Initialize the FS library. */ |
---|
167 | err = svn_fs_initialize (pool); |
---|
168 | if (err) goto hit_error; |
---|
169 | |
---|
170 | /* Make sure the ~/.subversion run-time config files exist, and load. */ |
---|
171 | err = svn_config_ensure (NULL, pool); |
---|
172 | if (err) goto hit_error; |
---|
173 | |
---|
174 | err = svn_config_get_config (&cfg_hash, NULL, pool); |
---|
175 | if (err) goto hit_error; |
---|
176 | |
---|
177 | /* Build an authentication baton. */ |
---|
178 | { |
---|
179 | /* There are many different kinds of authentication back-end |
---|
180 | "providers". See svn_auth.h for a full overview. */ |
---|
181 | svn_auth_provider_object_t *provider; |
---|
182 | apr_array_header_t *providers |
---|
183 | = apr_array_make (pool, 4, sizeof (svn_auth_provider_object_t *)); |
---|
184 | |
---|
185 | svn_client_get_simple_prompt_provider (&provider, |
---|
186 | my_simple_prompt_callback, |
---|
187 | NULL, /* baton */ |
---|
188 | 2, /* retry limit */ pool); |
---|
189 | APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider; |
---|
190 | |
---|
191 | svn_client_get_username_prompt_provider (&provider, |
---|
192 | my_username_prompt_callback, |
---|
193 | NULL, /* baton */ |
---|
194 | 2, /* retry limit */ pool); |
---|
195 | APR_ARRAY_PUSH (providers, svn_auth_provider_object_t *) = provider; |
---|
196 | |
---|
197 | /* Register the auth-providers into the context's auth_baton. */ |
---|
198 | svn_auth_open (&auth_baton, providers, pool); |
---|
199 | } |
---|
200 | |
---|
201 | /* Create a table of callbacks for the RA session, mostly nonexistent. */ |
---|
202 | cbtable = apr_pcalloc (pool, sizeof(*cbtable)); |
---|
203 | cbtable->auth_baton = auth_baton; |
---|
204 | cbtable->open_tmp_file = open_tmp_file; |
---|
205 | |
---|
206 | /* Now do the real work. */ |
---|
207 | |
---|
208 | err = svn_ra_open2(&session, URL, cbtable, NULL, cfg_hash, pool); |
---|
209 | if (err) goto hit_error; |
---|
210 | |
---|
211 | err = svn_ra_get_latest_revnum(session, &rev, pool); |
---|
212 | if (err) goto hit_error; |
---|
213 | |
---|
214 | printf ("The latest revision is %ld.\n", rev); |
---|
215 | |
---|
216 | return EXIT_SUCCESS; |
---|
217 | |
---|
218 | hit_error: |
---|
219 | svn_handle_error2 (err, stderr, FALSE, "headrev: "); |
---|
220 | return EXIT_FAILURE; |
---|
221 | } |
---|