Followup to r29625: fix getopt tests.
[svn.git] / subversion / svn / switch-cmd.c
blob76a3aae4b347770ef97997bbbebd01c289ba0a61
1 /*
2 * switch-cmd.c -- Bring work tree in sync with a different URL
4 * ====================================================================
5 * Copyright (c) 2000-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 * ====================================================================
19 /* ==================================================================== */
23 /*** Includes. ***/
25 #include "svn_wc.h"
26 #include "svn_client.h"
27 #include "svn_path.h"
28 #include "svn_error.h"
29 #include "svn_pools.h"
30 #include "cl.h"
32 #include "svn_private_config.h"
34 /*** Code. ***/
36 static svn_error_t *
37 rewrite_urls(apr_array_header_t *targets,
38 svn_boolean_t recurse,
39 svn_client_ctx_t *ctx,
40 apr_pool_t *pool)
42 apr_pool_t *subpool;
43 const char *from;
44 const char *to;
45 int i;
47 if (targets->nelts < 2)
48 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
50 from = APR_ARRAY_IDX(targets, 0, const char *);
51 to = APR_ARRAY_IDX(targets, 1, const char *);
53 /* "--relocate http https" and "--relocate http://foo svn://bar" are OK,
54 but things like "--relocate http://foo svn" are not */
55 if (svn_path_is_url(from) != svn_path_is_url(to))
56 return svn_error_createf
57 (SVN_ERR_INCORRECT_PARAMS, NULL,
58 _("'%s' to '%s' is not a valid relocation"), from, to);
60 subpool = svn_pool_create(pool);
62 if (targets->nelts == 2)
64 SVN_ERR(svn_client_relocate("", from, to, recurse, ctx, pool));
66 else
68 for (i = 2; i < targets->nelts; i++)
70 const char *target = APR_ARRAY_IDX(targets, i, const char *);
71 svn_pool_clear(subpool);
72 SVN_ERR(svn_client_relocate(target, from, to, recurse,
73 ctx, subpool));
77 svn_pool_destroy(subpool);
78 return SVN_NO_ERROR;
82 /* This implements the `svn_opt_subcommand_t' interface. */
83 svn_error_t *
84 svn_cl__switch(apr_getopt_t *os,
85 void *baton,
86 apr_pool_t *pool)
88 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
89 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
90 apr_array_header_t *targets;
91 const char *target = NULL, *switch_url = NULL;
92 svn_wc_adm_access_t *adm_access;
93 const svn_wc_entry_t *entry;
94 const char *parent_dir, *base_tgt, *true_path;
95 svn_opt_revision_t peg_revision;
96 svn_depth_t depth;
97 svn_boolean_t depth_is_sticky;
99 /* This command should discover (or derive) exactly two cmdline
100 arguments: a local path to update ("target"), and a new url to
101 switch to ("switch_url"). */
102 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
103 opt_state->targets,
104 pool));
106 /* handle only-rewrite case specially */
107 if (opt_state->relocate)
108 return rewrite_urls(targets,
109 SVN_DEPTH_IS_RECURSIVE(opt_state->depth),
110 ctx, pool);
112 if (targets->nelts < 1)
113 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
114 if (targets->nelts > 2)
115 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, 0, NULL);
117 /* Get the required SWITCH_URL and the optional TARGET arguments. */
118 if (targets->nelts == 1)
120 switch_url = APR_ARRAY_IDX(targets, 0, const char *);
121 target = "";
123 else
125 switch_url = APR_ARRAY_IDX(targets, 0, const char *);
126 target = APR_ARRAY_IDX(targets, 1, const char *);
129 /* Strip peg revision if targets contains an URI. */
130 SVN_ERR(svn_opt_parse_path(&peg_revision, &true_path, switch_url, pool));
131 APR_ARRAY_IDX(targets, 0, const char *) = true_path;
132 switch_url = true_path;
134 /* Validate the switch_url */
135 if (! svn_path_is_url(switch_url))
136 return svn_error_createf
137 (SVN_ERR_BAD_URL, NULL,
138 _("'%s' does not appear to be a URL"), switch_url);
140 /* Canonicalize the URL. */
141 switch_url = svn_path_canonicalize(switch_url, pool);
143 /* Validate the target */
144 SVN_ERR(svn_wc_adm_probe_open3(&adm_access, NULL, target, FALSE, 0,
145 ctx->cancel_func, ctx->cancel_baton,
146 pool));
147 SVN_ERR(svn_wc_entry(&entry, target, adm_access, FALSE, pool));
148 if (! entry)
149 return svn_error_createf
150 (SVN_ERR_ENTRY_NOT_FOUND, NULL,
151 _("'%s' does not appear to be a working copy path"), target);
153 /* We want the switch to print the same letters as a regular update. */
154 if (entry->kind == svn_node_file)
155 SVN_ERR(svn_wc_get_actual_target(target, &parent_dir, &base_tgt, pool));
156 else if (entry->kind == svn_node_dir)
157 parent_dir = target;
159 if (! opt_state->quiet)
160 svn_cl__get_notifier(&ctx->notify_func2, &ctx->notify_baton2, FALSE,
161 FALSE, FALSE, pool);
163 /* Deal with depthstuffs. */
164 if (opt_state->set_depth != svn_depth_unknown)
166 depth = opt_state->set_depth;
167 depth_is_sticky = TRUE;
169 else
171 depth = opt_state->depth;
172 depth_is_sticky = FALSE;
175 /* Do the 'switch' update. */
176 SVN_ERR(svn_client_switch2(NULL, target, switch_url, &peg_revision,
177 &(opt_state->start_revision), depth,
178 depth_is_sticky, opt_state->ignore_externals,
179 opt_state->force, ctx, pool));
181 return SVN_NO_ERROR;