svnversion.c revision 262253
1/* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 */ 21 22#include "svn_cmdline.h" 23#include "svn_dirent_uri.h" 24#include "svn_pools.h" 25#include "svn_wc.h" 26#include "svn_utf.h" 27#include "svn_opt.h" 28#include "svn_version.h" 29 30#include "private/svn_opt_private.h" 31#include "private/svn_cmdline_private.h" 32#include "private/svn_subr_private.h" 33 34#include "svn_private_config.h" 35 36#define SVNVERSION_OPT_VERSION SVN_OPT_FIRST_LONGOPT_ID 37 38 39static svn_error_t * 40version(svn_boolean_t quiet, apr_pool_t *pool) 41{ 42 return svn_opt_print_help4(NULL, "svnversion", TRUE, quiet, FALSE, 43 NULL, NULL, NULL, NULL, NULL, NULL, pool); 44} 45 46static void 47usage(apr_pool_t *pool) 48{ 49 svn_error_clear(svn_cmdline_fprintf 50 (stderr, pool, _("Type 'svnversion --help' for usage.\n"))); 51 exit(1); 52} 53 54 55static void 56help(const apr_getopt_option_t *options, apr_pool_t *pool) 57{ 58 svn_error_clear 59 (svn_cmdline_fprintf 60 (stdout, pool, 61 _("usage: svnversion [OPTIONS] [WC_PATH [TRAIL_URL]]\n\n" 62 " Produce a compact version identifier for the working copy path\n" 63 " WC_PATH. TRAIL_URL is the trailing portion of the URL used to\n" 64 " determine if WC_PATH itself is switched (detection of switches\n" 65 " within WC_PATH does not rely on TRAIL_URL). The version identifier\n" 66 " is written to standard output. For example:\n" 67 "\n" 68 " $ svnversion . /repos/svn/trunk\n" 69 " 4168\n" 70 "\n" 71 " The version identifier will be a single number if the working\n" 72 " copy is single revision, unmodified, not switched and with\n" 73 " a URL that matches the TRAIL_URL argument. If the working\n" 74 " copy is unusual the version identifier will be more complex:\n" 75 "\n" 76 " 4123:4168 mixed revision working copy\n" 77 " 4168M modified working copy\n" 78 " 4123S switched working copy\n" 79 " 4123P partial working copy, from a sparse checkout\n" 80 " 4123:4168MS mixed revision, modified, switched working copy\n" 81 "\n" 82 " If WC_PATH is an unversioned path, the program will output\n" 83 " 'Unversioned directory' or 'Unversioned file'. If WC_PATH is\n" 84 " an added or copied or moved path, the program will output\n" 85 " 'Uncommitted local addition, copy or move'.\n" 86 "\n" 87 " If invoked without arguments WC_PATH will be the current directory.\n" 88 "\n" 89 "Valid options:\n"))); 90 while (options->description) 91 { 92 const char *optstr; 93 svn_opt_format_option(&optstr, options, TRUE, pool); 94 svn_error_clear(svn_cmdline_fprintf(stdout, pool, " %s\n", optstr)); 95 ++options; 96 } 97 svn_error_clear(svn_cmdline_fprintf(stdout, pool, "\n")); 98 exit(0); 99} 100 101 102/* Version compatibility check */ 103static svn_error_t * 104check_lib_versions(void) 105{ 106 static const svn_version_checklist_t checklist[] = 107 { 108 { "svn_subr", svn_subr_version }, 109 { "svn_wc", svn_wc_version }, 110 { NULL, NULL } 111 }; 112 SVN_VERSION_DEFINE(my_version); 113 114 return svn_ver_check_list2(&my_version, checklist, svn_ver_equal); 115} 116 117/* 118 * Why is this not an svn subcommand? I have this vague idea that it could 119 * be run as part of the build process, with the output embedded in the svn 120 * program. Obviously we don't want to have to run svn when building svn. 121 */ 122int 123main(int argc, const char *argv[]) 124{ 125 const char *wc_path, *trail_url; 126 const char *local_abspath; 127 apr_pool_t *pool; 128 svn_wc_revision_status_t *res; 129 svn_boolean_t no_newline = FALSE, committed = FALSE; 130 svn_error_t *err; 131 apr_getopt_t *os; 132 svn_wc_context_t *wc_ctx; 133 svn_boolean_t quiet = FALSE; 134 svn_boolean_t is_version = FALSE; 135 const apr_getopt_option_t options[] = 136 { 137 {"no-newline", 'n', 0, N_("do not output the trailing newline")}, 138 {"committed", 'c', 0, N_("last changed rather than current revisions")}, 139 {"help", 'h', 0, N_("display this help")}, 140 {"version", SVNVERSION_OPT_VERSION, 0, 141 N_("show program version information")}, 142 {"quiet", 'q', 0, 143 N_("no progress (only errors) to stderr")}, 144 {0, 0, 0, 0} 145 }; 146 147 /* Initialize the app. */ 148 if (svn_cmdline_init("svnversion", stderr) != EXIT_SUCCESS) 149 return EXIT_FAILURE; 150 151 /* Create our top-level pool. Use a separate mutexless allocator, 152 * given this application is single threaded. 153 */ 154 pool = apr_allocator_owner_get(svn_pool_create_allocator(FALSE)); 155 156 /* Check library versions */ 157 err = check_lib_versions(); 158 if (err) 159 return svn_cmdline_handle_exit_error(err, pool, "svnversion: "); 160 161#if defined(WIN32) || defined(__CYGWIN__) 162 /* Set the working copy administrative directory name. */ 163 if (getenv("SVN_ASP_DOT_NET_HACK")) 164 { 165 err = svn_wc_set_adm_dir("_svn", pool); 166 if (err) 167 return svn_cmdline_handle_exit_error(err, pool, "svnversion: "); 168 } 169#endif 170 171 err = svn_cmdline__getopt_init(&os, argc, argv, pool); 172 if (err) 173 return svn_cmdline_handle_exit_error(err, pool, "svnversion: "); 174 175 os->interleave = 1; 176 while (1) 177 { 178 int opt; 179 const char *arg; 180 apr_status_t status = apr_getopt_long(os, options, &opt, &arg); 181 if (APR_STATUS_IS_EOF(status)) 182 break; 183 if (status != APR_SUCCESS) 184 usage(pool); /* this will exit() */ 185 186 switch (opt) 187 { 188 case 'n': 189 no_newline = TRUE; 190 break; 191 case 'c': 192 committed = TRUE; 193 break; 194 case 'q': 195 quiet = TRUE; 196 break; 197 case 'h': 198 help(options, pool); 199 break; 200 case SVNVERSION_OPT_VERSION: 201 is_version = TRUE; 202 break; 203 default: 204 usage(pool); /* this will exit() */ 205 } 206 } 207 208 if (is_version) 209 { 210 SVN_INT_ERR(version(quiet, pool)); 211 exit(0); 212 } 213 if (os->ind > argc || os->ind < argc - 2) 214 usage(pool); /* this will exit() */ 215 216 SVN_INT_ERR(svn_utf_cstring_to_utf8(&wc_path, 217 (os->ind < argc) ? os->argv[os->ind] 218 : ".", 219 pool)); 220 221 SVN_INT_ERR(svn_opt__arg_canonicalize_path(&wc_path, wc_path, pool)); 222 SVN_INT_ERR(svn_dirent_get_absolute(&local_abspath, wc_path, pool)); 223 SVN_INT_ERR(svn_wc_context_create(&wc_ctx, NULL, pool, pool)); 224 225 if (os->ind+1 < argc) 226 SVN_INT_ERR(svn_utf_cstring_to_utf8(&trail_url, os->argv[os->ind+1], 227 pool)); 228 else 229 trail_url = NULL; 230 231 err = svn_wc_revision_status2(&res, wc_ctx, local_abspath, trail_url, 232 committed, NULL, NULL, pool, pool); 233 234 if (err && (err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND 235 || err->apr_err == SVN_ERR_WC_NOT_WORKING_COPY)) 236 { 237 svn_node_kind_t kind; 238 svn_boolean_t special; 239 240 svn_error_clear(err); 241 242 SVN_INT_ERR(svn_io_check_special_path(local_abspath, &kind, &special, 243 pool)); 244 245 if (special) 246 SVN_INT_ERR(svn_cmdline_printf(pool, _("Unversioned symlink%s"), 247 no_newline ? "" : "\n")); 248 else if (kind == svn_node_dir) 249 SVN_INT_ERR(svn_cmdline_printf(pool, _("Unversioned directory%s"), 250 no_newline ? "" : "\n")); 251 else if (kind == svn_node_file) 252 SVN_INT_ERR(svn_cmdline_printf(pool, _("Unversioned file%s"), 253 no_newline ? "" : "\n")); 254 else 255 { 256 SVN_INT_ERR(svn_cmdline_fprintf(stderr, pool, 257 kind == svn_node_none 258 ? _("'%s' doesn't exist\n") 259 : _("'%s' is of unknown type\n"), 260 svn_dirent_local_style(local_abspath, 261 pool))); 262 svn_pool_destroy(pool); 263 return EXIT_FAILURE; 264 } 265 svn_pool_destroy(pool); 266 return EXIT_SUCCESS; 267 } 268 269 SVN_INT_ERR(err); 270 271 if (! SVN_IS_VALID_REVNUM(res->min_rev)) 272 { 273 /* Local uncommitted modifications, no revision info was found. */ 274 SVN_INT_ERR(svn_cmdline_printf(pool, _("Uncommitted local addition, " 275 "copy or move%s"), 276 no_newline ? "" : "\n")); 277 svn_pool_destroy(pool); 278 return EXIT_SUCCESS; 279 } 280 281 /* Build compact '123[:456]M?S?' string. */ 282 SVN_INT_ERR(svn_cmdline_printf(pool, "%ld", res->min_rev)); 283 if (res->min_rev != res->max_rev) 284 SVN_INT_ERR(svn_cmdline_printf(pool, ":%ld", res->max_rev)); 285 if (res->modified) 286 SVN_INT_ERR(svn_cmdline_fputs("M", stdout, pool)); 287 if (res->switched) 288 SVN_INT_ERR(svn_cmdline_fputs("S", stdout, pool)); 289 if (res->sparse_checkout) 290 SVN_INT_ERR(svn_cmdline_fputs("P", stdout, pool)); 291 292 if (! no_newline) 293 SVN_INT_ERR(svn_cmdline_fputs("\n", stdout, pool)); 294 295 svn_pool_destroy(pool); 296 297 /* Flush stdout to make sure that the user will see any printing errors. */ 298 SVN_INT_ERR(svn_cmdline_fflush(stdout)); 299 300 return EXIT_SUCCESS; 301} 302