1/*
2 * copy-cmd.c -- Subversion copy command
3 *
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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
20 *    under the License.
21 * ====================================================================
22 */
23
24/* ==================================================================== */
25
26
27
28/*** Includes. ***/
29
30#include "svn_client.h"
31#include "svn_path.h"
32#include "svn_error.h"
33#include "cl.h"
34
35#include "svn_private_config.h"
36
37
38/*** Code. ***/
39
40/* This implements the `svn_opt_subcommand_t' interface. */
41svn_error_t *
42svn_cl__copy(apr_getopt_t *os,
43             void *baton,
44             apr_pool_t *pool)
45{
46  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
47  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
48  apr_array_header_t *targets, *sources;
49  const char *src_path, *dst_path;
50  svn_boolean_t srcs_are_urls, dst_is_url;
51  svn_error_t *err;
52  int i;
53
54  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
55                                                      opt_state->targets,
56                                                      ctx, FALSE, pool));
57  if (targets->nelts < 2)
58    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
59
60  /* Get the src list and associated peg revs */
61  sources = apr_array_make(pool, targets->nelts - 1,
62                           sizeof(svn_client_copy_source_t *));
63  for (i = 0; i < (targets->nelts - 1); i++)
64    {
65      const char *target = APR_ARRAY_IDX(targets, i, const char *);
66      svn_client_copy_source_t *source = apr_palloc(pool, sizeof(*source));
67      const char *src;
68      svn_opt_revision_t *peg_revision = apr_palloc(pool,
69                                                    sizeof(*peg_revision));
70
71      err = svn_opt_parse_path(peg_revision, &src, target, pool);
72
73      if (err)
74        {
75          /* Issue #3606: 'svn cp .@HEAD target' gives
76             svn: '@HEAD' is just a peg revision. Maybe try '@HEAD@' instead?
77
78             This is caused by a first round of canonicalization in
79             svn_cl__args_to_target_array_print_reserved(). Undo that in an
80             attempt to fix this issue without revving many apis.
81           */
82          if (*target == '@' && err->apr_err == SVN_ERR_BAD_FILENAME)
83            {
84              svn_error_t *err2;
85
86              err2 = svn_opt_parse_path(peg_revision, &src,
87                                        apr_pstrcat(pool, ".", target,
88                                                    (const char *)NULL), pool);
89
90              if (err2)
91                {
92                  /* Fix attempt failed; return original error */
93                  svn_error_clear(err2);
94                }
95              else
96                {
97                  /* Error resolved. Use path */
98                  svn_error_clear(err);
99                  err = NULL;
100                }
101            }
102
103          if (err)
104              return svn_error_trace(err);
105        }
106
107      source->path = src;
108      source->revision = &(opt_state->start_revision);
109      source->peg_revision = peg_revision;
110
111      APR_ARRAY_PUSH(sources, svn_client_copy_source_t *) = source;
112    }
113
114  /* Get DST_PATH (the target path or URL) and check that no peg revision is
115   * specified for it. */
116  {
117    const char *tgt = APR_ARRAY_IDX(targets, targets->nelts - 1, const char *);
118    svn_opt_revision_t peg;
119
120    SVN_ERR(svn_opt_parse_path(&peg, &dst_path, tgt, pool));
121    if (peg.kind != svn_opt_revision_unspecified)
122      return svn_error_createf(SVN_ERR_ILLEGAL_TARGET, NULL,
123                               _("'%s': a peg revision is not allowed here"),
124                               tgt);
125  }
126
127  /* Figure out which type of notification to use.
128     (There is no need to check that the src paths are homogeneous;
129     svn_client_copy6() through its subroutine try_copy() will return an
130     error if they are not.) */
131  src_path = APR_ARRAY_IDX(targets, 0, const char *);
132  srcs_are_urls = svn_path_is_url(src_path);
133  dst_is_url = svn_path_is_url(dst_path);
134
135  if ((! srcs_are_urls) && (! dst_is_url))
136    {
137      /* WC->WC */
138    }
139  else if ((! srcs_are_urls) && (dst_is_url))
140    {
141      /* WC->URL : Use notification. */
142      if (! opt_state->quiet)
143        SVN_ERR(svn_cl__notifier_mark_wc_to_repos_copy(ctx->notify_baton2));
144    }
145  else if ((srcs_are_urls) && (! dst_is_url))
146    {
147     /* URL->WC : Use checkout-style notification. */
148     if (! opt_state->quiet)
149       SVN_ERR(svn_cl__notifier_mark_checkout(ctx->notify_baton2));
150    }
151  else
152    {
153      /* URL -> URL, meaning that no notification is needed. */
154      ctx->notify_func2 = NULL;
155    }
156
157  if (! dst_is_url)
158    {
159      ctx->log_msg_func3 = NULL;
160      if (opt_state->message || opt_state->filedata || opt_state->revprop_table)
161        return svn_error_create
162          (SVN_ERR_CL_UNNECESSARY_LOG_MESSAGE, NULL,
163           _("Local, non-commit operations do not take a log message "
164             "or revision properties"));
165    }
166
167  if (ctx->log_msg_func3)
168    SVN_ERR(svn_cl__make_log_msg_baton(&(ctx->log_msg_baton3), opt_state,
169                                       NULL, ctx->config, pool));
170
171  err = svn_client_copy6(sources, dst_path, TRUE,
172                         opt_state->parents, opt_state->ignore_externals,
173                         opt_state->revprop_table,
174                         (opt_state->quiet ? NULL : svn_cl__print_commit_info),
175                         NULL,
176                         ctx, pool);
177
178  if (ctx->log_msg_func3)
179    SVN_ERR(svn_cl__cleanup_log_msg(ctx->log_msg_baton3, err, pool));
180  else if (err)
181    return svn_error_trace(err);
182
183  return SVN_NO_ERROR;
184}
185