2 * cmdline.c: command-line processing
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 /* ==================================================================== */
28 #include "svn_client.h"
29 #include "svn_error.h"
30 #include "svn_dirent_uri.h"
37 #include "private/svn_opt_private.h"
39 #include "svn_private_config.h"
44 #define DEFAULT_ARRAY_SIZE 5
47 /* Attempt to find the repository root url for TARGET, possibly using CTX for
48 * authentication. If one is found and *ROOT_URL is not NULL, then just check
49 * that the root url for TARGET matches the value given in *ROOT_URL and
50 * return an error if it does not. If one is found and *ROOT_URL is NULL then
51 * set *ROOT_URL to the root url for TARGET, allocated from POOL.
52 * If a root url is not found for TARGET because it does not exist in the
53 * repository, then return with no error.
55 * TARGET is a UTF-8 encoded string that is fully canonicalized and escaped.
58 check_root_url_of_target(const char **root_url,
60 svn_client_ctx_t *ctx,
64 const char *tmp_root_url;
66 svn_opt_revision_t opt_rev;
68 SVN_ERR(svn_opt_parse_path(&opt_rev, &truepath, target, pool));
69 if (!svn_path_is_url(truepath))
70 SVN_ERR(svn_dirent_get_absolute(&truepath, truepath, pool));
72 err = svn_client_get_repos_root(&tmp_root_url, NULL, truepath,
77 /* It is OK if the given target does not exist, it just means
78 * we will not be able to determine the root url from this particular
81 * If the target itself is a URL to a repository that does not exist,
82 * that's fine, too. The callers will deal with this argument in an
83 * appropriate manner if it does not make any sense.
85 * Also tolerate locally added targets ("bad revision" error).
87 if ((err->apr_err == SVN_ERR_ENTRY_NOT_FOUND)
88 || (err->apr_err == SVN_ERR_WC_PATH_NOT_FOUND)
89 || (err->apr_err == SVN_ERR_WC_NOT_WORKING_COPY)
90 || (err->apr_err == SVN_ERR_RA_CANNOT_CREATE_SESSION)
91 || (err->apr_err == SVN_ERR_CLIENT_BAD_REVISION))
97 return svn_error_trace(err);
100 if (*root_url && tmp_root_url)
102 if (strcmp(*root_url, tmp_root_url) != 0)
103 return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
104 _("All non-relative targets must have "
105 "the same root URL"));
108 *root_url = tmp_root_url;
113 /* Note: This is substantially copied from svn_opt__args_to_target_array() in
114 * order to move to libsvn_client while maintaining backward compatibility. */
116 svn_client_args_to_target_array2(apr_array_header_t **targets_p,
118 const apr_array_header_t *known_targets,
119 svn_client_ctx_t *ctx,
120 svn_boolean_t keep_last_origpath_on_truepath_collision,
124 svn_boolean_t rel_url_found = FALSE;
125 const char *root_url = NULL;
126 apr_array_header_t *input_targets =
127 apr_array_make(pool, DEFAULT_ARRAY_SIZE, sizeof(const char *));
128 apr_array_header_t *output_targets =
129 apr_array_make(pool, DEFAULT_ARRAY_SIZE, sizeof(const char *));
130 apr_array_header_t *reserved_names = NULL;
132 /* Step 1: create a master array of targets that are in UTF-8
133 encoding, and come from concatenating the targets left by apr_getopt,
134 plus any extra targets (e.g., from the --targets switch.)
135 If any of the targets are relative urls, then set the rel_url_found
138 for (; os->ind < os->argc; os->ind++)
140 /* The apr_getopt targets are still in native encoding. */
141 const char *raw_target = os->argv[os->ind];
142 const char *utf8_target;
144 SVN_ERR(svn_utf_cstring_to_utf8(&utf8_target,
147 if (svn_path_is_repos_relative_url(utf8_target))
148 rel_url_found = TRUE;
150 APR_ARRAY_PUSH(input_targets, const char *) = utf8_target;
155 for (i = 0; i < known_targets->nelts; i++)
157 /* The --targets array have already been converted to UTF-8,
158 because we needed to split up the list with svn_cstring_split. */
159 const char *utf8_target = APR_ARRAY_IDX(known_targets,
162 if (svn_path_is_repos_relative_url(utf8_target))
163 rel_url_found = TRUE;
165 APR_ARRAY_PUSH(input_targets, const char *) = utf8_target;
169 /* Step 2: process each target. */
171 for (i = 0; i < input_targets->nelts; i++)
173 const char *utf8_target = APR_ARRAY_IDX(input_targets, i, const char *);
175 /* Relative urls will be canonicalized when they are resolved later in
178 if (svn_path_is_repos_relative_url(utf8_target))
180 APR_ARRAY_PUSH(output_targets, const char *) = utf8_target;
184 const char *true_target;
189 * This is needed so that the target can be properly canonicalized,
190 * otherwise the canonicalization does not treat a ".@BASE" as a "."
191 * with a BASE peg revision, and it is not canonicalized to "@BASE".
192 * If any peg revision exists, it is appended to the final
193 * canonicalized path or URL. Do not use svn_opt_parse_path()
194 * because the resulting peg revision is a structure that would have
195 * to be converted back into a string. Converting from a string date
196 * to the apr_time_t field in the svn_opt_revision_value_t and back to
197 * a string would not necessarily preserve the exact bytes of the
198 * input date, so its easier just to keep it in string form.
200 SVN_ERR(svn_opt__split_arg_at_peg_revision(&true_target, &peg_rev,
203 /* Reject the form "@abc", a peg specifier with no path. */
204 if (true_target[0] == '\0' && peg_rev[0] != '\0')
206 return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
207 _("'%s' is just a peg revision. "
208 "Maybe try '%s@' instead?"),
209 utf8_target, utf8_target);
212 /* URLs and wc-paths get treated differently. */
213 if (svn_path_is_url(true_target))
215 SVN_ERR(svn_opt__arg_canonicalize_url(&true_target,
218 else /* not a url, so treat as a path */
220 const char *base_name;
221 const char *original_target;
223 original_target = svn_dirent_internal_style(true_target, pool);
224 SVN_ERR(svn_opt__arg_canonicalize_path(&true_target,
227 /* There are two situations in which a 'truepath-conversion'
228 (case-canonicalization to on-disk path on case-insensitive
229 filesystem) needs to be undone:
231 1. If KEEP_LAST_ORIGPATH_ON_TRUEPATH_COLLISION is TRUE, and
232 this is the last target of a 2-element target list, and
233 both targets have the same truepath. */
234 if (keep_last_origpath_on_truepath_collision
235 && input_targets->nelts == 2 && i == 1
236 && strcmp(original_target, true_target) != 0)
238 const char *src_truepath = APR_ARRAY_IDX(output_targets,
241 if (strcmp(src_truepath, true_target) == 0)
242 true_target = original_target;
245 /* 2. If there is an exact match in the wc-db without a
246 corresponding on-disk path (e.g. a scheduled-for-delete
247 file only differing in case from an on-disk file). */
248 if (strcmp(original_target, true_target) != 0)
250 const char *target_abspath;
251 svn_node_kind_t kind;
254 SVN_ERR(svn_dirent_get_absolute(&target_abspath,
255 original_target, pool));
256 err2 = svn_wc_read_kind2(&kind, ctx->wc_ctx, target_abspath,
259 && (err2->apr_err == SVN_ERR_WC_NOT_WORKING_COPY
260 || err2->apr_err == SVN_ERR_WC_UPGRADE_REQUIRED))
262 svn_error_clear(err2);
267 /* We successfully did a lookup in the wc-db. Now see
268 if it's something interesting. */
269 if (kind == svn_node_file || kind == svn_node_dir)
270 true_target = original_target;
274 /* If the target has the same name as a Subversion
275 working copy administrative dir, skip it. */
276 base_name = svn_dirent_basename(true_target, pool);
278 if (svn_wc_is_adm_dir(base_name, pool))
281 reserved_names = apr_array_make(pool, DEFAULT_ARRAY_SIZE,
282 sizeof(const char *));
284 APR_ARRAY_PUSH(reserved_names, const char *) = utf8_target;
290 target = apr_pstrcat(pool, true_target, peg_rev, SVN_VA_NULL);
294 /* Later targets have priority over earlier target, I
295 don't know why, see basic_relative_url_multi_repo. */
296 SVN_ERR(check_root_url_of_target(&root_url, target,
300 APR_ARRAY_PUSH(output_targets, const char *) = target;
304 /* Only resolve relative urls if there were some actually found earlier. */
308 * Use the current directory's root url if one wasn't found using the
311 if (root_url == NULL)
313 const char *current_abspath;
316 SVN_ERR(svn_dirent_get_absolute(¤t_abspath, "", pool));
317 err = svn_client_get_repos_root(&root_url, NULL /* uuid */,
318 current_abspath, ctx, pool, pool);
319 if (err || root_url == NULL)
320 return svn_error_create(SVN_ERR_WC_NOT_WORKING_COPY, err,
321 _("Resolving '^/': no repository root "
322 "found in the target arguments or "
323 "in the current directory"));
326 *targets_p = apr_array_make(pool, output_targets->nelts,
327 sizeof(const char *));
329 for (i = 0; i < output_targets->nelts; i++)
331 const char *target = APR_ARRAY_IDX(output_targets, i,
334 if (svn_path_is_repos_relative_url(target))
336 const char *abs_target;
337 const char *true_target;
340 SVN_ERR(svn_opt__split_arg_at_peg_revision(&true_target, &peg_rev,
343 SVN_ERR(svn_path_resolve_repos_relative_url(&abs_target,
347 SVN_ERR(svn_opt__arg_canonicalize_url(&true_target, abs_target,
350 target = apr_pstrcat(pool, true_target, peg_rev, SVN_VA_NULL);
353 APR_ARRAY_PUSH(*targets_p, const char *) = target;
357 *targets_p = output_targets;
361 svn_error_t *err = SVN_NO_ERROR;
363 for (i = 0; i < reserved_names->nelts; ++i)
364 err = svn_error_createf(SVN_ERR_RESERVED_FILENAME_SPECIFIED, err,
365 _("'%s' ends in a reserved name"),
366 APR_ARRAY_IDX(reserved_names, i,
368 return svn_error_trace(err);