2 * headrev.c : print out the HEAD revision of a repository.
4 * ====================================================================
5 * Copyright (c) 2004 CollabNet. All rights reserved.
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.
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 * ====================================================================
18 * To compile on unix against Subversion and APR libraries, try
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
28 #include "svn_client.h"
29 #include "svn_pools.h"
30 #include "svn_config.h"
33 #include "svn_cmdline.h"
36 /* Display a prompt and read a one-line response into the provided buffer,
37 removing a trailing newline if present. */
39 prompt_and_read_line(const char *prompt
,
44 printf("%s: ", prompt
);
45 if (fgets(buffer
, max
, stdin
) == NULL
)
46 return svn_error_create(0, NULL
, "error reading stdin");
48 if (len
> 0 && buffer
[len
-1] == '\n')
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. */
57 my_simple_prompt_callback (svn_auth_cred_simple_t
**cred
,
61 svn_boolean_t may_save
,
64 svn_auth_cred_simple_t
*ret
= apr_pcalloc (pool
, sizeof (*ret
));
69 printf ("Authentication realm: %s\n", realm
);
73 ret
->username
= apr_pstrdup (pool
, username
);
76 SVN_ERR (prompt_and_read_line("Username", answerbuf
, sizeof(answerbuf
)));
77 ret
->username
= apr_pstrdup (pool
, answerbuf
);
80 SVN_ERR (prompt_and_read_line("Password", answerbuf
, sizeof(answerbuf
)));
81 ret
->password
= apr_pstrdup (pool
, answerbuf
);
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. */
92 my_username_prompt_callback (svn_auth_cred_username_t
**cred
,
95 svn_boolean_t may_save
,
98 svn_auth_cred_username_t
*ret
= apr_pcalloc (pool
, sizeof (*ret
));
103 printf ("Authentication realm: %s\n", realm
);
106 SVN_ERR (prompt_and_read_line("Username", answerbuf
, sizeof(answerbuf
)));
107 ret
->username
= apr_pstrdup (pool
, answerbuf
);
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. */
118 open_tmp_file (apr_file_t
**fp
,
119 void *callback_baton
,
123 const char *ignored_filename
;
125 SVN_ERR (svn_io_temp_dir (&path
, pool
));
126 path
= svn_path_join (path
, "tempfile", pool
);
128 /* Open a unique file, with delete-on-close set. */
129 SVN_ERR (svn_io_open_unique_file2 (fp
, &ignored_filename
,
131 svn_io_file_del_on_close
, pool
));
138 main (int argc
, const char **argv
)
143 svn_ra_session_t
*session
;
144 svn_ra_callbacks2_t
*cbtable
;
146 apr_hash_t
*cfg_hash
;
147 svn_auth_baton_t
*auth_baton
;
151 printf ("Usage: %s URL\n", argv
[0]);
152 printf (" Print HEAD revision of URL's repository.\n");
158 /* Initialize the app. Send all error messages to 'stderr'. */
159 if (svn_cmdline_init ("headrev", stderr
) != EXIT_SUCCESS
)
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
);
166 /* Initialize the FS library. */
167 err
= svn_fs_initialize (pool
);
168 if (err
) goto hit_error
;
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
;
174 err
= svn_config_get_config (&cfg_hash
, NULL
, pool
);
175 if (err
) goto hit_error
;
177 /* Build an authentication baton. */
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
*));
185 svn_client_get_simple_prompt_provider (&provider
,
186 my_simple_prompt_callback
,
188 2, /* retry limit */ pool
);
189 APR_ARRAY_PUSH (providers
, svn_auth_provider_object_t
*) = provider
;
191 svn_client_get_username_prompt_provider (&provider
,
192 my_username_prompt_callback
,
194 2, /* retry limit */ pool
);
195 APR_ARRAY_PUSH (providers
, svn_auth_provider_object_t
*) = provider
;
197 /* Register the auth-providers into the context's auth_baton. */
198 svn_auth_open (&auth_baton
, providers
, pool
);
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
;
206 /* Now do the real work. */
208 err
= svn_ra_open2(&session
, URL
, cbtable
, NULL
, cfg_hash
, pool
);
209 if (err
) goto hit_error
;
211 err
= svn_ra_get_latest_revnum(session
, &rev
, pool
);
212 if (err
) goto hit_error
;
214 printf ("The latest revision is %ld.\n", rev
);
219 svn_handle_error2 (err
, stderr
, FALSE
, "headrev: ");