2 * export-cmd.c -- Subversion export command
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * ====================================================================
24 /* ==================================================================== */
30 #include "svn_client.h"
31 #include "svn_error.h"
32 #include "svn_dirent_uri.h"
34 #include "svn_cmdline.h"
37 #include "svn_private_config.h"
38 #include "private/svn_string_private.h"
39 #include "private/svn_client_private.h"
41 /*** The export editor code. ***/
43 /* ---------------------------------------------------------------------- */
45 /*** A dedicated 'export' editor, which does no .svn/ accounting. ***/
47 typedef struct edit_baton_t
49 apr_int64_t file_count;
50 apr_int64_t dir_count;
51 apr_int64_t byte_count;
52 apr_int64_t prop_count;
53 apr_int64_t prop_byte_count;
57 set_target_revision(void *edit_baton,
58 svn_revnum_t target_revision,
65 /* Just ensure that the main export directory exists. */
67 open_root(void *edit_baton,
68 svn_revnum_t base_revision,
72 *root_baton = edit_baton;
77 /* Ensure the directory exists, and send feedback. */
79 add_directory(const char *path,
81 const char *copyfrom_path,
82 svn_revnum_t copyfrom_revision,
86 edit_baton_t *eb = parent_baton;
89 *baton = parent_baton;
94 /* Build a file baton. */
96 add_file(const char *path,
98 const char *copyfrom_path,
99 svn_revnum_t copyfrom_revision,
103 edit_baton_t *eb = parent_baton;
106 *baton = parent_baton;
111 window_handler(svn_txdelta_window_t *window, void *baton)
113 edit_baton_t *eb = baton;
115 eb->byte_count += window->tview_len;
120 /* Write incoming data into the tmpfile stream */
123 apply_textdelta(void *file_baton,
124 const char *base_checksum,
126 svn_txdelta_window_handler_t *handler,
127 void **handler_baton)
129 *handler_baton = file_baton;
130 *handler = window_handler;
136 change_file_prop(void *file_baton,
138 const svn_string_t *value,
141 edit_baton_t *eb = file_baton;
143 eb->prop_byte_count += value->len;
149 change_dir_prop(void *dir_baton,
151 const svn_string_t *value,
154 edit_baton_t *eb = dir_baton;
161 close_file(void *file_baton,
162 const char *text_checksum,
169 /*** Public Interfaces ***/
172 bench_null_export(svn_revnum_t *result_rev,
173 const char *from_path_or_url,
174 svn_opt_revision_t *peg_revision,
175 svn_opt_revision_t *revision,
178 svn_client_ctx_t *ctx,
182 svn_revnum_t edit_revision = SVN_INVALID_REVNUM;
183 svn_boolean_t from_is_url = svn_path_is_url(from_path_or_url);
185 SVN_ERR_ASSERT(peg_revision != NULL);
186 SVN_ERR_ASSERT(revision != NULL);
188 if (peg_revision->kind == svn_opt_revision_unspecified)
189 peg_revision->kind = svn_path_is_url(from_path_or_url)
190 ? svn_opt_revision_head
191 : svn_opt_revision_working;
193 if (revision->kind == svn_opt_revision_unspecified)
194 revision = peg_revision;
196 if (from_is_url || ! SVN_CLIENT__REVKIND_IS_LOCAL_TO_WC(revision->kind))
198 svn_client__pathrev_t *loc;
199 svn_ra_session_t *ra_session;
200 svn_node_kind_t kind;
202 /* Get the RA connection. */
203 SVN_ERR(svn_client__ra_session_from_path2(&ra_session, &loc,
204 from_path_or_url, NULL,
206 revision, ctx, pool));
208 SVN_ERR(svn_ra_check_path(ra_session, "", loc->rev, &kind, pool));
210 if (kind == svn_node_file)
214 /* Since you cannot actually root an editor at a file, we
215 * manually drive a few functions of our editor. */
217 /* Step outside the editor-likeness for a moment, to actually talk
218 * to the repository. */
219 /* ### note: the stream will not be closed */
220 SVN_ERR(svn_ra_get_file(ra_session, "", loc->rev,
221 svn_stream_empty(pool),
222 NULL, &props, pool));
224 else if (kind == svn_node_dir)
226 void *edit_baton = NULL;
227 const svn_delta_editor_t *export_editor = NULL;
228 const svn_ra_reporter3_t *reporter;
231 svn_delta_editor_t *editor = svn_delta_default_editor(pool);
233 editor->set_target_revision = set_target_revision;
234 editor->open_root = open_root;
235 editor->add_directory = add_directory;
236 editor->add_file = add_file;
237 editor->apply_textdelta = apply_textdelta;
238 editor->close_file = close_file;
239 editor->change_file_prop = change_file_prop;
240 editor->change_dir_prop = change_dir_prop;
242 /* for ra_svn, we don't need an editior in quiet mode */
243 if (!quiet || strncmp(loc->repos_root_url, "svn:", 4))
244 SVN_ERR(svn_delta_get_cancellation_editor(ctx->cancel_func,
252 /* Manufacture a basic 'report' to the update reporter. */
253 SVN_ERR(svn_ra_do_update3(ra_session,
254 &reporter, &report_baton,
256 "", /* no sub-target */
258 FALSE, /* don't want copyfrom-args */
259 FALSE, /* don't want ignore_ancestry */
260 export_editor, edit_baton,
263 SVN_ERR(reporter->set_path(report_baton, "", loc->rev,
264 /* Depth is irrelevant, as we're
265 passing start_empty=TRUE anyway. */
267 TRUE, /* "help, my dir is empty!" */
270 SVN_ERR(reporter->finish_report(report_baton, pool));
272 else if (kind == svn_node_none)
274 return svn_error_createf(SVN_ERR_RA_ILLEGAL_URL, NULL,
275 _("URL '%s' doesn't exist"),
278 /* kind == svn_node_unknown not handled */
283 *result_rev = edit_revision;
291 /* This implements the `svn_opt_subcommand_t' interface. */
293 svn_cl__null_export(apr_getopt_t *os,
297 svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
298 svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
300 apr_array_header_t *targets;
302 svn_opt_revision_t peg_revision;
303 const char *truefrom;
304 edit_baton_t eb = { 0 };
306 SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
310 /* We want exactly 1 or 2 targets for this subcommand. */
311 if (targets->nelts < 1)
312 return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
313 if (targets->nelts > 2)
314 return svn_error_create(SVN_ERR_CL_ARG_PARSING_ERROR, 0, NULL);
316 /* The first target is the `from' path. */
317 from = APR_ARRAY_IDX(targets, 0, const char *);
319 /* Get the peg revision if present. */
320 SVN_ERR(svn_opt_parse_path(&peg_revision, &truefrom, from, pool));
322 if (opt_state->depth == svn_depth_unknown)
323 opt_state->depth = svn_depth_infinity;
326 err = bench_null_export(NULL, truefrom, &peg_revision,
327 &(opt_state->start_revision),
330 ctx, opt_state->quiet, pool);
332 if (!opt_state->quiet)
333 SVN_ERR(svn_cmdline_printf(pool,
334 _("%15s directories\n"
336 "%15s bytes in files\n"
338 "%15s bytes in properties\n"),
339 svn__ui64toa_sep(eb.dir_count, ',', pool),
340 svn__ui64toa_sep(eb.file_count, ',', pool),
341 svn__ui64toa_sep(eb.byte_count, ',', pool),
342 svn__ui64toa_sep(eb.prop_count, ',', pool),
343 svn__ui64toa_sep(eb.prop_byte_count, ',', pool)));
345 return svn_error_trace(err);