2 * switch-cmd.c -- Bring work tree in sync with a different URL
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 /* ==================================================================== */
31 #include "svn_client.h"
32 #include "svn_dirent_uri.h"
34 #include "svn_error.h"
35 #include "svn_pools.h"
38 #include "svn_private_config.h"
43 rewrite_urls(const apr_array_header_t *targets,
44 svn_boolean_t ignore_externals,
45 svn_client_ctx_t *ctx,
52 if (targets->nelts < 2)
53 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
55 from = APR_ARRAY_IDX(targets, 0, const char *);
56 to = APR_ARRAY_IDX(targets, 1, const char *);
58 /* "--relocate http https" and "--relocate http://foo svn://bar" are OK,
59 but things like "--relocate http://foo svn" are not */
60 if (svn_path_is_url(from) != svn_path_is_url(to))
61 return svn_error_createf
62 (SVN_ERR_INCORRECT_PARAMS, NULL,
63 _("'%s' to '%s' is not a valid relocation"), from, to);
65 subpool = svn_pool_create(pool);
67 if (targets->nelts == 2)
69 SVN_ERR(svn_client_relocate2("", from, to, ignore_externals,
76 for (i = 2; i < targets->nelts; i++)
78 const char *target = APR_ARRAY_IDX(targets, i, const char *);
79 svn_pool_clear(subpool);
80 SVN_ERR(svn_client_relocate2(target, from, to,
81 ignore_externals, ctx, subpool));
85 svn_pool_destroy(subpool);
90 /* This implements the `svn_opt_subcommand_t' interface. */
92 svn_cl__switch(apr_getopt_t *os,
94 apr_pool_t *scratch_pool)
96 svn_error_t *err = SVN_NO_ERROR;
97 svn_error_t *externals_err = SVN_NO_ERROR;
98 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
99 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
100 apr_array_header_t *targets;
101 const char *target, *switch_url;
102 svn_opt_revision_t peg_revision;
104 svn_boolean_t depth_is_sticky;
105 struct svn_cl__check_externals_failed_notify_baton nwb;
107 /* This command should discover (or derive) exactly two cmdline
108 arguments: a local path to update ("target"), and a new url to
109 switch to ("switch_url"). */
110 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
115 /* handle only-rewrite case specially */
116 if (opt_state->relocate)
117 return rewrite_urls(targets, opt_state->ignore_externals,
120 if (targets->nelts < 1)
121 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
122 if (targets->nelts > 2)
123 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, 0, NULL);
125 /* Get the required SWITCH_URL and its optional PEG_REVISION, and the
126 * optional TARGET argument. */
127 SVN_ERR(svn_opt_parse_path(&peg_revision, &switch_url,
128 APR_ARRAY_IDX(targets, 0, const char *),
130 if (targets->nelts == 1)
133 target = APR_ARRAY_IDX(targets, 1, const char *);
135 /* Validate the switch_url */
136 if (! svn_path_is_url(switch_url))
137 return svn_error_createf(SVN_ERR_BAD_URL, NULL,
138 _("'%s' does not appear to be a URL"), switch_url);
140 SVN_ERR(svn_cl__check_target_is_local_path(target));
142 /* Deal with depthstuffs. */
143 if (opt_state->set_depth != svn_depth_unknown)
145 depth = opt_state->set_depth;
146 depth_is_sticky = TRUE;
150 depth = opt_state->depth;
151 depth_is_sticky = FALSE;
154 nwb.wrapped_func = ctx->notify_func2;
155 nwb.wrapped_baton = ctx->notify_baton2;
156 nwb.had_externals_error = FALSE;
157 ctx->notify_func2 = svn_cl__check_externals_failed_notify_wrapper;
158 ctx->notify_baton2 = &nwb;
160 /* Do the 'switch' update. */
161 err = svn_client_switch3(NULL, target, switch_url, &peg_revision,
162 &(opt_state->start_revision), depth,
163 depth_is_sticky, opt_state->ignore_externals,
164 opt_state->force, opt_state->ignore_ancestry,
168 if (err->apr_err == SVN_ERR_CLIENT_UNRELATED_RESOURCES)
169 return svn_error_createf(SVN_ERR_CLIENT_UNRELATED_RESOURCES, err,
170 _("Path '%s' does not share common version "
171 "control ancestry with the requested switch "
172 "location. Use --ignore-ancestry to "
173 "disable this check."),
174 svn_dirent_local_style(target,
176 if (err->apr_err == SVN_ERR_RA_UUID_MISMATCH
177 || err->apr_err == SVN_ERR_WC_INVALID_SWITCH)
178 return svn_error_quick_wrap(
180 _("'svn switch' does not support switching a working copy to "
181 "a different repository"));
185 if (nwb.had_externals_error)
186 externals_err = svn_error_create(SVN_ERR_CL_ERROR_PROCESSING_EXTERNALS,
188 _("Failure occurred processing one or "
189 "more externals definitions"));
191 if (! opt_state->quiet)
193 err = svn_cl__notifier_print_conflict_stats(nwb.wrapped_baton, scratch_pool);
195 return svn_error_compose_create(externals_err, err);
198 return svn_error_compose_create(externals_err, err);