Followup to r29625: fix getopt tests.
[svn.git] / subversion / svn / checkout-cmd.c
blob0daa3fb119de0686a70ad0b7756666769d62a4d3
1 /*
2 * checkout-cmd.c -- Subversion checkout command
4 * ====================================================================
5 * Copyright (c) 2000-2007 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_client.h"
26 #include "svn_path.h"
27 #include "svn_error.h"
28 #include "svn_pools.h"
29 #include "cl.h"
31 #include "svn_private_config.h"
34 /*** Code. ***/
37 This is what it does
39 - case 1: one URL
40 $ svn co http://host/repos/module
41 checkout into ./module/
43 - case 2: one URL and explicit path
44 $ svn co http://host/repos/module path
45 checkout into ./path/
47 - case 3: multiple URLs
48 $ svn co http://host1/repos1/module1 http://host2/repos2/module2
49 checkout into ./module1/ and ./module2/
51 - case 4: multiple URLs and explicit path
52 $ svn co http://host1/repos1/module1 http://host2/repos2/module2 path
53 checkout into ./path/module1/ and ./path/module2/
55 Is this the same as CVS? Does it matter if it is not?
59 /* This implements the `svn_opt_subcommand_t' interface. */
60 svn_error_t *
61 svn_cl__checkout(apr_getopt_t *os,
62 void *baton,
63 apr_pool_t *pool)
65 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
66 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
67 apr_pool_t *subpool;
68 apr_array_header_t *targets;
69 const char *local_dir;
70 const char *repos_url;
71 int i;
73 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
74 opt_state->targets,
75 pool));
77 if (! targets->nelts)
78 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
80 /* Add a path if the user only specified URLs */
81 local_dir = APR_ARRAY_IDX(targets, targets->nelts - 1, const char *);
82 if (svn_path_is_url(local_dir))
84 if (targets->nelts == 1)
86 svn_opt_revision_t pegrev;
88 /* Discard the peg-revision, if one was provided. */
89 SVN_ERR(svn_opt_parse_path(&pegrev, &local_dir, local_dir, pool));
90 if (pegrev.kind != svn_opt_revision_unspecified)
91 local_dir = svn_path_canonicalize(local_dir, pool);
93 local_dir = svn_path_basename(local_dir, pool);
94 local_dir = svn_path_uri_decode(local_dir, pool);
96 else
98 local_dir = "";
100 APR_ARRAY_PUSH(targets, const char *) = local_dir;
102 else
104 /* What? They gave us one target, and it wasn't a URL. */
105 if (targets->nelts == 1)
106 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, 0, NULL);
109 if (! opt_state->quiet)
110 svn_cl__get_notifier(&ctx->notify_func2, &ctx->notify_baton2, TRUE, FALSE,
111 FALSE, pool);
113 subpool = svn_pool_create(pool);
114 for (i = 0; i < targets->nelts - 1; ++i)
116 const char *target_dir;
117 const char *true_url;
118 svn_opt_revision_t revision = opt_state->start_revision;
119 svn_opt_revision_t peg_revision;
121 svn_pool_clear(subpool);
123 SVN_ERR(svn_cl__check_cancel(ctx->cancel_baton));
125 /* Validate the REPOS_URL */
126 repos_url = APR_ARRAY_IDX(targets, i, const char *);
127 if (! svn_path_is_url(repos_url))
128 return svn_error_createf
129 (SVN_ERR_BAD_URL, NULL,
130 _("'%s' does not appear to be a URL"), repos_url);
132 /* Get a possible peg revision. */
133 SVN_ERR(svn_opt_parse_path(&peg_revision, &true_url, repos_url,
134 subpool));
136 true_url = svn_path_canonicalize(true_url, subpool);
138 /* Use sub-directory of destination if checking-out multiple URLs */
139 if (targets->nelts == 2)
141 target_dir = local_dir;
143 else
145 target_dir = svn_path_basename(true_url, subpool);
146 target_dir = svn_path_uri_decode(target_dir, subpool);
147 target_dir = svn_path_join(local_dir, target_dir, subpool);
150 /* Checkout doesn't accept an unspecified revision, so default to
151 the peg revision, or to HEAD if there wasn't a peg. */
152 if (revision.kind == svn_opt_revision_unspecified)
154 if (peg_revision.kind != svn_opt_revision_unspecified)
155 revision = peg_revision;
156 else
157 revision.kind = svn_opt_revision_head;
160 SVN_ERR(svn_client_checkout3
161 (NULL, true_url, target_dir,
162 &peg_revision,
163 &revision,
164 opt_state->depth,
165 opt_state->ignore_externals,
166 opt_state->force,
167 ctx, subpool));
169 svn_pool_destroy(subpool);
171 return SVN_NO_ERROR;