1251881Speter/*
2251881Speter * import-cmd.c -- Import a file or tree into the repository.
3251881Speter *
4251881Speter * ====================================================================
5251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
6251881Speter *    or more contributor license agreements.  See the NOTICE file
7251881Speter *    distributed with this work for additional information
8251881Speter *    regarding copyright ownership.  The ASF licenses this file
9251881Speter *    to you under the Apache License, Version 2.0 (the
10251881Speter *    "License"); you may not use this file except in compliance
11251881Speter *    with the License.  You may obtain a copy of the License at
12251881Speter *
13251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
14251881Speter *
15251881Speter *    Unless required by applicable law or agreed to in writing,
16251881Speter *    software distributed under the License is distributed on an
17251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18251881Speter *    KIND, either express or implied.  See the License for the
19251881Speter *    specific language governing permissions and limitations
20251881Speter *    under the License.
21251881Speter * ====================================================================
22251881Speter */
23251881Speter
24251881Speter/* ==================================================================== */
25251881Speter
26251881Speter
27251881Speter
28251881Speter/*** Includes. ***/
29251881Speter
30251881Speter#include "svn_client.h"
31251881Speter#include "svn_path.h"
32251881Speter#include "svn_error.h"
33251881Speter#include "cl.h"
34251881Speter
35251881Speter#include "svn_private_config.h"
36251881Speter
37251881Speter
38251881Speter/*** Code. ***/
39251881Speter
40251881Speter/* This implements the `svn_opt_subcommand_t' interface. */
41251881Spetersvn_error_t *
42251881Spetersvn_cl__import(apr_getopt_t *os,
43251881Speter               void *baton,
44251881Speter               apr_pool_t *pool)
45251881Speter{
46251881Speter  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
47251881Speter  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
48251881Speter  apr_array_header_t *targets;
49251881Speter  const char *path;
50251881Speter  const char *url;
51251881Speter
52251881Speter  /* Import takes two arguments, for example
53251881Speter   *
54251881Speter   *   $ svn import projects/test file:///home/jrandom/repos/trunk
55251881Speter   *                ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56251881Speter   *                 (source)       (repository)
57251881Speter   *
58251881Speter   * or
59251881Speter   *
60251881Speter   *   $ svn import file:///home/jrandom/repos/some/subdir
61251881Speter   *
62251881Speter   * What is the nicest behavior for import, from the user's point of
63251881Speter   * view?  This is a subtle question.  Seemingly intuitive answers
64251881Speter   * can lead to weird situations, such never being able to create
65251881Speter   * non-directories in the top-level of the repository.
66251881Speter   *
67251881Speter   * If 'source' is a file then the basename of 'url' is used as the
68251881Speter   * filename in the repository.  If 'source' is a directory then the
69251881Speter   * import happens directly in the repository target dir, creating
70251881Speter   * however many new entries are necessary.  If some part of 'url'
71251881Speter   * does not exist in the repository then parent directories are created
72251881Speter   * as necessary.
73251881Speter   *
74251881Speter   * In the case where no 'source' is given '.' (the current directory)
75251881Speter   * is implied.
76251881Speter   *
77251881Speter   * ### kff todo: review above behaviors.
78251881Speter   */
79251881Speter
80251881Speter  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
81251881Speter                                                      opt_state->targets,
82251881Speter                                                      ctx, FALSE, pool));
83251881Speter
84251881Speter  if (targets->nelts < 1)
85251881Speter    return svn_error_create
86251881Speter      (SVN_ERR_CL_INSUFFICIENT_ARGS, NULL,
87251881Speter       _("Repository URL required when importing"));
88251881Speter  else if (targets->nelts > 2)
89251881Speter    return svn_error_create
90251881Speter      (SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
91251881Speter       _("Too many arguments to import command"));
92251881Speter  else if (targets->nelts == 1)
93251881Speter    {
94251881Speter      url = APR_ARRAY_IDX(targets, 0, const char *);
95251881Speter      path = "";
96251881Speter    }
97251881Speter  else
98251881Speter    {
99251881Speter      path = APR_ARRAY_IDX(targets, 0, const char *);
100251881Speter      url = APR_ARRAY_IDX(targets, 1, const char *);
101251881Speter    }
102251881Speter
103251881Speter  SVN_ERR(svn_cl__check_target_is_local_path(path));
104251881Speter
105251881Speter  if (! svn_path_is_url(url))
106251881Speter    return svn_error_createf(SVN_ERR_CL_ARG_PARSING_ERROR, NULL,
107251881Speter                             _("Invalid URL '%s'"), url);
108251881Speter
109251881Speter  if (opt_state->depth == svn_depth_unknown)
110251881Speter    opt_state->depth = svn_depth_infinity;
111251881Speter
112251881Speter  SVN_ERR(svn_cl__make_log_msg_baton(&(ctx->log_msg_baton3), opt_state,
113251881Speter                                     NULL, ctx->config, pool));
114251881Speter
115251881Speter  SVN_ERR(svn_cl__cleanup_log_msg
116251881Speter          (ctx->log_msg_baton3,
117251881Speter           svn_client_import5(path,
118251881Speter                              url,
119251881Speter                              opt_state->depth,
120251881Speter                              opt_state->no_ignore,
121251881Speter                              opt_state->no_autoprops,
122251881Speter                              opt_state->force,
123251881Speter                              opt_state->revprop_table,
124251881Speter                              NULL, NULL,  /* filter callback / baton */
125251881Speter                              (opt_state->quiet
126251881Speter                               ? NULL : svn_cl__print_commit_info),
127251881Speter                              NULL,
128251881Speter                              ctx,
129251881Speter                              pool), pool));
130251881Speter
131251881Speter  return SVN_NO_ERROR;
132251881Speter}
133