* subversion/mod_dav_svn/reports/replay.c
[svn.git] / subversion / libsvn_client / switch.c
blobdef42157b147b05c8be223736486cb080a775159
1 /*
2 * switch.c: implement 'switch' feature via WC & RA interfaces.
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 <assert.h>
27 #include "svn_client.h"
28 #include "svn_error.h"
29 #include "svn_time.h"
30 #include "svn_path.h"
31 #include "svn_config.h"
32 #include "svn_pools.h"
33 #include "client.h"
35 #include "svn_private_config.h"
36 #include "private/svn_wc_private.h"
39 /*** Code. ***/
41 /* This feature is essentially identical to 'svn update' (see
42 ./update.c), but with two differences:
44 - the reporter->finish_report() routine needs to make the server
45 run delta_dirs() on two *different* paths, rather than on two
46 identical paths.
48 - after the update runs, we need to more than just
49 ensure_uniform_revision; we need to rewrite all the entries'
50 URL attributes.
54 svn_error_t *
55 svn_client__switch_internal(svn_revnum_t *result_rev,
56 const char *path,
57 const char *switch_url,
58 const svn_opt_revision_t *peg_revision,
59 const svn_opt_revision_t *revision,
60 svn_depth_t depth,
61 svn_boolean_t *timestamp_sleep,
62 svn_boolean_t ignore_externals,
63 svn_boolean_t allow_unver_obstructions,
64 svn_client_ctx_t *ctx,
65 apr_pool_t *pool)
67 const svn_ra_reporter3_t *reporter;
68 void *report_baton;
69 const svn_wc_entry_t *entry;
70 const char *URL, *anchor, *target, *source_root, *switch_rev_url;
71 svn_ra_session_t *ra_session;
72 svn_revnum_t revnum;
73 svn_error_t *err = SVN_NO_ERROR;
74 svn_wc_adm_access_t *adm_access, *dir_access;
75 const char *diff3_cmd;
76 svn_boolean_t use_commit_times;
77 svn_boolean_t sleep_here;
78 svn_boolean_t *use_sleep = timestamp_sleep ? timestamp_sleep : &sleep_here;
79 const svn_delta_editor_t *switch_editor;
80 void *switch_edit_baton;
81 svn_wc_traversal_info_t *traversal_info = svn_wc_init_traversal_info(pool);
82 const char *preserved_exts_str;
83 apr_array_header_t *preserved_exts;
84 svn_boolean_t server_supports_depth;
85 svn_config_t *cfg = ctx->config ? apr_hash_get(ctx->config,
86 SVN_CONFIG_CATEGORY_CONFIG,
87 APR_HASH_KEY_STRING)
88 : NULL;
90 /* Get the external diff3, if any. */
91 svn_config_get(cfg, &diff3_cmd, SVN_CONFIG_SECTION_HELPERS,
92 SVN_CONFIG_OPTION_DIFF3_CMD, NULL);
94 /* See if the user wants last-commit timestamps instead of current ones. */
95 SVN_ERR(svn_config_get_bool(cfg, &use_commit_times,
96 SVN_CONFIG_SECTION_MISCELLANY,
97 SVN_CONFIG_OPTION_USE_COMMIT_TIMES, FALSE));
99 /* See which files the user wants to preserve the extension of when
100 conflict files are made. */
101 svn_config_get(cfg, &preserved_exts_str, SVN_CONFIG_SECTION_MISCELLANY,
102 SVN_CONFIG_OPTION_PRESERVED_CF_EXTS, "");
103 preserved_exts = *preserved_exts_str
104 ? svn_cstring_split(preserved_exts_str, "\n\r\t\v ", FALSE, pool)
105 : NULL;
107 /* Sanity check. Without these, the switch is meaningless. */
108 assert(path);
109 assert(switch_url && (switch_url[0] != '\0'));
111 /* ### Need to lock the whole target tree to invalidate wcprops. Does
112 non-recursive switch really need to invalidate the whole tree? */
113 SVN_ERR(svn_wc_adm_open_anchor(&adm_access, &dir_access, &target, path,
114 TRUE, -1, ctx->cancel_func,
115 ctx->cancel_baton, pool));
116 anchor = svn_wc_adm_access_path(adm_access);
118 SVN_ERR(svn_wc__entry_versioned(&entry, anchor, adm_access, FALSE, pool));
119 if (! entry->url)
120 return svn_error_createf(SVN_ERR_ENTRY_MISSING_URL, NULL,
121 _("Directory '%s' has no URL"),
122 svn_path_local_style(anchor, pool));
124 URL = apr_pstrdup(pool, entry->url);
126 /* Open an RA session to 'source' URL */
127 SVN_ERR(svn_client__ra_session_from_path(&ra_session, &revnum,
128 &switch_rev_url,
129 switch_url, adm_access,
130 peg_revision, revision,
131 ctx, pool));
132 SVN_ERR(svn_ra_get_repos_root(ra_session, &source_root, pool));
134 /* Disallow a switch operation to change the repository root of the
135 target. */
136 if (! svn_path_is_ancestor(source_root, URL))
137 return svn_error_createf
138 (SVN_ERR_WC_INVALID_SWITCH, NULL,
139 _("'%s'\n"
140 "is not the same repository as\n"
141 "'%s'"), URL, source_root);
143 SVN_ERR(svn_ra_reparent(ra_session, URL, pool));
145 /* Fetch the switch (update) editor. If REVISION is invalid, that's
146 okay; the RA driver will call editor->set_target_revision() later on. */
147 SVN_ERR(svn_wc_get_switch_editor3(&revnum, adm_access, target,
148 switch_rev_url, use_commit_times, depth,
149 allow_unver_obstructions,
150 ctx->notify_func2, ctx->notify_baton2,
151 ctx->cancel_func, ctx->cancel_baton,
152 ctx->conflict_func, ctx->conflict_baton,
153 diff3_cmd, preserved_exts,
154 &switch_editor, &switch_edit_baton,
155 traversal_info, pool));
157 /* Tell RA to do an update of URL+TARGET to REVISION; if we pass an
158 invalid revnum, that means RA will use the latest revision. */
159 SVN_ERR(svn_ra_do_switch2(ra_session, &reporter, &report_baton, revnum,
160 target, depth, switch_rev_url,
161 switch_editor, switch_edit_baton, pool));
163 SVN_ERR(svn_ra_has_capability(ra_session, &server_supports_depth,
164 SVN_RA_CAPABILITY_DEPTH, pool));
166 /* Drive the reporter structure, describing the revisions within
167 PATH. When we call reporter->finish_report, the update_editor
168 will be driven by svn_repos_dir_delta2.
170 We pass NULL for traversal_info because this is a switch, not an
171 update, and therefore we don't want to handle any externals
172 except the ones directly affected by the switch. */
173 err = svn_wc_crawl_revisions3(path, dir_access, reporter, report_baton,
174 TRUE, depth, (! server_supports_depth),
175 use_commit_times,
176 ctx->notify_func2, ctx->notify_baton2,
177 NULL, /* no traversal info */
178 pool);
180 if (err)
182 /* Don't rely on the error handling to handle the sleep later, do
183 it now */
184 svn_sleep_for_timestamps();
185 return err;
187 *use_sleep = TRUE;
189 /* We handle externals after the switch is complete, so that
190 handling external items (and any errors therefrom) doesn't delay
191 the primary operation. */
192 if (SVN_DEPTH_IS_RECURSIVE(depth) && (! ignore_externals))
193 err = svn_client__handle_externals(traversal_info, path, switch_url,
194 source_root, depth, FALSE, use_sleep,
195 ctx, pool);
197 /* Sleep to ensure timestamp integrity (we do this regardless of
198 errors in the actual switch operation(s)). */
199 if (sleep_here)
200 svn_sleep_for_timestamps();
202 /* Return errors we might have sustained. */
203 if (err)
204 return err;
206 SVN_ERR(svn_wc_adm_close(adm_access));
208 /* Let everyone know we're finished here. */
209 if (ctx->notify_func2)
211 svn_wc_notify_t *notify
212 = svn_wc_create_notify(anchor, svn_wc_notify_update_completed, pool);
213 notify->kind = svn_node_none;
214 notify->content_state = notify->prop_state
215 = svn_wc_notify_state_inapplicable;
216 notify->lock_state = svn_wc_notify_lock_state_inapplicable;
217 notify->revision = revnum;
218 (*ctx->notify_func2)(ctx->notify_baton2, notify, pool);
221 /* If the caller wants the result revision, give it to them. */
222 if (result_rev)
223 *result_rev = revnum;
225 return SVN_NO_ERROR;
228 svn_error_t *
229 svn_client_switch2(svn_revnum_t *result_rev,
230 const char *path,
231 const char *switch_url,
232 const svn_opt_revision_t *peg_revision,
233 const svn_opt_revision_t *revision,
234 svn_depth_t depth,
235 svn_boolean_t ignore_externals,
236 svn_boolean_t allow_unver_obstructions,
237 svn_client_ctx_t *ctx,
238 apr_pool_t *pool)
240 return svn_client__switch_internal(result_rev, path, switch_url,
241 peg_revision, revision,
242 depth, NULL, ignore_externals,
243 allow_unver_obstructions, ctx, pool);
246 svn_error_t *
247 svn_client_switch(svn_revnum_t *result_rev,
248 const char *path,
249 const char *switch_url,
250 const svn_opt_revision_t *revision,
251 svn_boolean_t recurse,
252 svn_client_ctx_t *ctx,
253 apr_pool_t *pool)
255 svn_opt_revision_t peg_revision;
256 peg_revision.kind = svn_opt_revision_unspecified;
257 return svn_client__switch_internal(result_rev, path, switch_url,
258 &peg_revision, revision,
259 SVN_DEPTH_INFINITY_OR_FILES(recurse),
260 NULL, FALSE, FALSE, ctx, pool);