1/*
2 * relocate-cmd.c -- Update working tree administrative data to
3 *                   account for repository change-of-address.
4 *
5 * ====================================================================
6 *    Licensed to the Apache Software Foundation (ASF) under one
7 *    or more contributor license agreements.  See the NOTICE file
8 *    distributed with this work for additional information
9 *    regarding copyright ownership.  The ASF licenses this file
10 *    to you under the Apache License, Version 2.0 (the
11 *    "License"); you may not use this file except in compliance
12 *    with the License.  You may obtain a copy of the License at
13 *
14 *      http://www.apache.org/licenses/LICENSE-2.0
15 *
16 *    Unless required by applicable law or agreed to in writing,
17 *    software distributed under the License is distributed on an
18 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 *    KIND, either express or implied.  See the License for the
20 *    specific language governing permissions and limitations
21 *    under the License.
22 * ====================================================================
23 */
24
25/* ==================================================================== */
26
27
28
29/*** Includes. ***/
30
31#include "svn_wc.h"
32#include "svn_client.h"
33#include "svn_dirent_uri.h"
34#include "svn_path.h"
35#include "svn_error.h"
36#include "svn_pools.h"
37#include "cl.h"
38
39#include "svn_private_config.h"
40
41/*** Code. ***/
42
43/* This implements the `svn_opt_subcommand_t' interface. */
44svn_error_t *
45svn_cl__relocate(apr_getopt_t *os,
46                 void *baton,
47                 apr_pool_t *scratch_pool)
48{
49  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
50  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
51  svn_boolean_t ignore_externals = opt_state->ignore_externals;
52  apr_array_header_t *targets;
53  const char *from, *to, *path;
54
55  /* We've got two different syntaxes to support:
56
57     1. relocate FROM-PREFIX TO-PREFIX [PATH ...]
58     2. relocate TO-URL [PATH]
59  */
60  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
61                                                      opt_state->targets,
62                                                      ctx, FALSE,
63                                                      scratch_pool));
64  if (targets->nelts < 1)
65    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
66
67  /* If we have a single target, we're in form #2.  If we have two
68     targets and the first is a URL and the second is not, we're also
69     in form #2.  */
70  if ((targets->nelts == 1) ||
71      ((targets->nelts == 2)
72       && (svn_path_is_url(APR_ARRAY_IDX(targets, 0, const char *)))
73       && (! svn_path_is_url(APR_ARRAY_IDX(targets, 1, const char *)))))
74
75    {
76      to = APR_ARRAY_IDX(targets, 0, const char *);
77      path = (targets->nelts == 2) ? APR_ARRAY_IDX(targets, 1, const char *)
78                                   : "";
79
80      SVN_ERR(svn_client_url_from_path2(&from, path, ctx,
81                                        scratch_pool, scratch_pool));
82      SVN_ERR(svn_client_relocate2(path, from, to, ignore_externals,
83                                   ctx, scratch_pool));
84    }
85  /* ... Everything else is form #1. */
86  else
87    {
88      from = APR_ARRAY_IDX(targets, 0, const char *);
89      to = APR_ARRAY_IDX(targets, 1, const char *);
90
91      if (targets->nelts == 2)
92        {
93          SVN_ERR(svn_client_relocate2("", from, to, ignore_externals,
94                                       ctx, scratch_pool));
95        }
96      else
97        {
98          apr_pool_t *subpool = svn_pool_create(scratch_pool);
99          int i;
100
101          /* Target working copy root dir must be local. */
102          for (i = 2; i < targets->nelts; i++)
103            {
104              path = APR_ARRAY_IDX(targets, i, const char *);
105              SVN_ERR(svn_cl__check_target_is_local_path(path));
106            }
107
108          for (i = 2; i < targets->nelts; i++)
109            {
110              svn_pool_clear(subpool);
111              path = APR_ARRAY_IDX(targets, i, const char *);
112              SVN_ERR(svn_client_relocate2(path, from, to, ignore_externals,
113                                           ctx, subpool));
114            }
115          svn_pool_destroy(subpool);
116        }
117    }
118
119  return SVN_NO_ERROR;
120}
121