1251881Speter/*
2251881Speter * relocate-cmd.c -- Update working tree administrative data to
3251881Speter *                   account for repository change-of-address.
4251881Speter *
5251881Speter * ====================================================================
6251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
7251881Speter *    or more contributor license agreements.  See the NOTICE file
8251881Speter *    distributed with this work for additional information
9251881Speter *    regarding copyright ownership.  The ASF licenses this file
10251881Speter *    to you under the Apache License, Version 2.0 (the
11251881Speter *    "License"); you may not use this file except in compliance
12251881Speter *    with the License.  You may obtain a copy of the License at
13251881Speter *
14251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
15251881Speter *
16251881Speter *    Unless required by applicable law or agreed to in writing,
17251881Speter *    software distributed under the License is distributed on an
18251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19251881Speter *    KIND, either express or implied.  See the License for the
20251881Speter *    specific language governing permissions and limitations
21251881Speter *    under the License.
22251881Speter * ====================================================================
23251881Speter */
24251881Speter
25251881Speter/* ==================================================================== */
26251881Speter
27251881Speter
28251881Speter
29251881Speter/*** Includes. ***/
30251881Speter
31251881Speter#include "svn_wc.h"
32251881Speter#include "svn_client.h"
33251881Speter#include "svn_dirent_uri.h"
34251881Speter#include "svn_path.h"
35251881Speter#include "svn_error.h"
36251881Speter#include "svn_pools.h"
37251881Speter#include "cl.h"
38251881Speter
39251881Speter#include "svn_private_config.h"
40251881Speter
41251881Speter/*** Code. ***/
42251881Speter
43251881Speter/* This implements the `svn_opt_subcommand_t' interface. */
44251881Spetersvn_error_t *
45251881Spetersvn_cl__relocate(apr_getopt_t *os,
46251881Speter                 void *baton,
47251881Speter                 apr_pool_t *scratch_pool)
48251881Speter{
49251881Speter  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
50251881Speter  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
51251881Speter  svn_boolean_t ignore_externals = opt_state->ignore_externals;
52251881Speter  apr_array_header_t *targets;
53251881Speter  const char *from, *to, *path;
54251881Speter
55251881Speter  /* We've got two different syntaxes to support:
56251881Speter
57251881Speter     1. relocate FROM-PREFIX TO-PREFIX [PATH ...]
58251881Speter     2. relocate TO-URL [PATH]
59251881Speter  */
60251881Speter  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
61251881Speter                                                      opt_state->targets,
62251881Speter                                                      ctx, FALSE,
63251881Speter                                                      scratch_pool));
64251881Speter  if (targets->nelts < 1)
65251881Speter    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
66251881Speter
67251881Speter  /* If we have a single target, we're in form #2.  If we have two
68251881Speter     targets and the first is a URL and the second is not, we're also
69251881Speter     in form #2.  */
70251881Speter  if ((targets->nelts == 1) ||
71251881Speter      ((targets->nelts == 2)
72251881Speter       && (svn_path_is_url(APR_ARRAY_IDX(targets, 0, const char *)))
73251881Speter       && (! svn_path_is_url(APR_ARRAY_IDX(targets, 1, const char *)))))
74251881Speter
75251881Speter    {
76251881Speter      to = APR_ARRAY_IDX(targets, 0, const char *);
77251881Speter      path = (targets->nelts == 2) ? APR_ARRAY_IDX(targets, 1, const char *)
78251881Speter                                   : "";
79251881Speter
80251881Speter      SVN_ERR(svn_client_url_from_path2(&from, path, ctx,
81251881Speter                                        scratch_pool, scratch_pool));
82251881Speter      SVN_ERR(svn_client_relocate2(path, from, to, ignore_externals,
83251881Speter                                   ctx, scratch_pool));
84251881Speter    }
85251881Speter  /* ... Everything else is form #1. */
86251881Speter  else
87251881Speter    {
88251881Speter      from = APR_ARRAY_IDX(targets, 0, const char *);
89251881Speter      to = APR_ARRAY_IDX(targets, 1, const char *);
90251881Speter
91251881Speter      if (targets->nelts == 2)
92251881Speter        {
93251881Speter          SVN_ERR(svn_client_relocate2("", from, to, ignore_externals,
94251881Speter                                       ctx, scratch_pool));
95251881Speter        }
96251881Speter      else
97251881Speter        {
98251881Speter          apr_pool_t *subpool = svn_pool_create(scratch_pool);
99251881Speter          int i;
100251881Speter
101251881Speter          /* Target working copy root dir must be local. */
102251881Speter          for (i = 2; i < targets->nelts; i++)
103251881Speter            {
104251881Speter              path = APR_ARRAY_IDX(targets, i, const char *);
105251881Speter              SVN_ERR(svn_cl__check_target_is_local_path(path));
106251881Speter            }
107251881Speter
108251881Speter          for (i = 2; i < targets->nelts; i++)
109251881Speter            {
110251881Speter              svn_pool_clear(subpool);
111251881Speter              path = APR_ARRAY_IDX(targets, i, const char *);
112251881Speter              SVN_ERR(svn_client_relocate2(path, from, to, ignore_externals,
113251881Speter                                           ctx, subpool));
114251881Speter            }
115251881Speter          svn_pool_destroy(subpool);
116251881Speter        }
117251881Speter    }
118251881Speter
119251881Speter  return SVN_NO_ERROR;
120251881Speter}
121