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 /* ==================================================================== */
27 #include "svn_client.h"
28 #include "svn_error.h"
31 #include "svn_config.h"
32 #include "svn_pools.h"
35 #include "svn_private_config.h"
36 #include "private/svn_wc_private.h"
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
48 - after the update runs, we need to more than just
49 ensure_uniform_revision; we need to rewrite all the entries'
55 svn_client__switch_internal(svn_revnum_t
*result_rev
,
57 const char *switch_url
,
58 const svn_opt_revision_t
*peg_revision
,
59 const svn_opt_revision_t
*revision
,
61 svn_boolean_t depth_is_sticky
,
62 svn_boolean_t
*timestamp_sleep
,
63 svn_boolean_t ignore_externals
,
64 svn_boolean_t allow_unver_obstructions
,
65 svn_client_ctx_t
*ctx
,
68 const svn_ra_reporter3_t
*reporter
;
70 const svn_wc_entry_t
*entry
;
71 const char *URL
, *anchor
, *target
, *source_root
, *switch_rev_url
;
72 svn_ra_session_t
*ra_session
;
74 svn_error_t
*err
= SVN_NO_ERROR
;
75 svn_wc_adm_access_t
*adm_access
, *dir_access
;
76 const char *diff3_cmd
;
77 svn_boolean_t use_commit_times
;
78 svn_boolean_t sleep_here
;
79 svn_boolean_t
*use_sleep
= timestamp_sleep
? timestamp_sleep
: &sleep_here
;
80 const svn_delta_editor_t
*switch_editor
;
81 void *switch_edit_baton
;
82 svn_wc_traversal_info_t
*traversal_info
= svn_wc_init_traversal_info(pool
);
83 const char *preserved_exts_str
;
84 apr_array_header_t
*preserved_exts
;
85 svn_boolean_t server_supports_depth
;
86 svn_config_t
*cfg
= ctx
->config
? apr_hash_get(ctx
->config
,
87 SVN_CONFIG_CATEGORY_CONFIG
,
91 /* An unknown depth can't be sticky. */
92 if (depth
== svn_depth_unknown
)
93 depth_is_sticky
= FALSE
;
95 /* Get the external diff3, if any. */
96 svn_config_get(cfg
, &diff3_cmd
, SVN_CONFIG_SECTION_HELPERS
,
97 SVN_CONFIG_OPTION_DIFF3_CMD
, NULL
);
99 /* See if the user wants last-commit timestamps instead of current ones. */
100 SVN_ERR(svn_config_get_bool(cfg
, &use_commit_times
,
101 SVN_CONFIG_SECTION_MISCELLANY
,
102 SVN_CONFIG_OPTION_USE_COMMIT_TIMES
, FALSE
));
104 /* See which files the user wants to preserve the extension of when
105 conflict files are made. */
106 svn_config_get(cfg
, &preserved_exts_str
, SVN_CONFIG_SECTION_MISCELLANY
,
107 SVN_CONFIG_OPTION_PRESERVED_CF_EXTS
, "");
108 preserved_exts
= *preserved_exts_str
109 ? svn_cstring_split(preserved_exts_str
, "\n\r\t\v ", FALSE
, pool
)
112 /* Sanity check. Without these, the switch is meaningless. */
114 assert(switch_url
&& (switch_url
[0] != '\0'));
116 /* ### Need to lock the whole target tree to invalidate wcprops. Does
117 non-recursive switch really need to invalidate the whole tree? */
118 SVN_ERR(svn_wc_adm_open_anchor(&adm_access
, &dir_access
, &target
, path
,
119 TRUE
, -1, ctx
->cancel_func
,
120 ctx
->cancel_baton
, pool
));
121 anchor
= svn_wc_adm_access_path(adm_access
);
123 SVN_ERR(svn_wc__entry_versioned(&entry
, anchor
, adm_access
, FALSE
, pool
));
125 return svn_error_createf(SVN_ERR_ENTRY_MISSING_URL
, NULL
,
126 _("Directory '%s' has no URL"),
127 svn_path_local_style(anchor
, pool
));
129 URL
= apr_pstrdup(pool
, entry
->url
);
131 /* Open an RA session to 'source' URL */
132 SVN_ERR(svn_client__ra_session_from_path(&ra_session
, &revnum
,
134 switch_url
, adm_access
,
135 peg_revision
, revision
,
137 SVN_ERR(svn_ra_get_repos_root2(ra_session
, &source_root
, pool
));
139 /* Disallow a switch operation to change the repository root of the
141 if (! svn_path_is_ancestor(source_root
, URL
))
142 return svn_error_createf
143 (SVN_ERR_WC_INVALID_SWITCH
, NULL
,
145 "is not the same repository as\n"
146 "'%s'"), URL
, source_root
);
148 SVN_ERR(svn_ra_reparent(ra_session
, URL
, pool
));
150 /* Fetch the switch (update) editor. If REVISION is invalid, that's
151 okay; the RA driver will call editor->set_target_revision() later on. */
152 SVN_ERR(svn_wc_get_switch_editor3(&revnum
, adm_access
, target
,
153 switch_rev_url
, use_commit_times
, depth
,
154 depth_is_sticky
, allow_unver_obstructions
,
155 ctx
->notify_func2
, ctx
->notify_baton2
,
156 ctx
->cancel_func
, ctx
->cancel_baton
,
157 ctx
->conflict_func
, ctx
->conflict_baton
,
158 diff3_cmd
, preserved_exts
,
159 &switch_editor
, &switch_edit_baton
,
160 traversal_info
, pool
));
162 /* Tell RA to do an update of URL+TARGET to REVISION; if we pass an
163 invalid revnum, that means RA will use the latest revision. */
164 SVN_ERR(svn_ra_do_switch2(ra_session
, &reporter
, &report_baton
, revnum
,
165 target
, depth
, switch_rev_url
,
166 switch_editor
, switch_edit_baton
, pool
));
168 SVN_ERR(svn_ra_has_capability(ra_session
, &server_supports_depth
,
169 SVN_RA_CAPABILITY_DEPTH
, pool
));
171 /* Drive the reporter structure, describing the revisions within
172 PATH. When we call reporter->finish_report, the update_editor
173 will be driven by svn_repos_dir_delta2.
175 We pass NULL for traversal_info because this is a switch, not an
176 update, and therefore we don't want to handle any externals
177 except the ones directly affected by the switch. */
178 err
= svn_wc_crawl_revisions3(path
, dir_access
, reporter
, report_baton
,
179 TRUE
, depth
, (! server_supports_depth
),
181 ctx
->notify_func2
, ctx
->notify_baton2
,
182 NULL
, /* no traversal info */
187 /* Don't rely on the error handling to handle the sleep later, do
189 svn_sleep_for_timestamps();
194 /* We handle externals after the switch is complete, so that
195 handling external items (and any errors therefrom) doesn't delay
196 the primary operation. */
197 if (SVN_DEPTH_IS_RECURSIVE(depth
) && (! ignore_externals
))
198 err
= svn_client__handle_externals(traversal_info
, path
, switch_url
,
199 source_root
, depth
, FALSE
, use_sleep
,
202 /* Sleep to ensure timestamp integrity (we do this regardless of
203 errors in the actual switch operation(s)). */
205 svn_sleep_for_timestamps();
207 /* Return errors we might have sustained. */
211 SVN_ERR(svn_wc_adm_close(adm_access
));
213 /* Let everyone know we're finished here. */
214 if (ctx
->notify_func2
)
216 svn_wc_notify_t
*notify
217 = svn_wc_create_notify(anchor
, svn_wc_notify_update_completed
, pool
);
218 notify
->kind
= svn_node_none
;
219 notify
->content_state
= notify
->prop_state
220 = svn_wc_notify_state_inapplicable
;
221 notify
->lock_state
= svn_wc_notify_lock_state_inapplicable
;
222 notify
->revision
= revnum
;
223 (*ctx
->notify_func2
)(ctx
->notify_baton2
, notify
, pool
);
226 /* If the caller wants the result revision, give it to them. */
228 *result_rev
= revnum
;
234 svn_client_switch2(svn_revnum_t
*result_rev
,
236 const char *switch_url
,
237 const svn_opt_revision_t
*peg_revision
,
238 const svn_opt_revision_t
*revision
,
240 svn_boolean_t depth_is_sticky
,
241 svn_boolean_t ignore_externals
,
242 svn_boolean_t allow_unver_obstructions
,
243 svn_client_ctx_t
*ctx
,
246 return svn_client__switch_internal(result_rev
, path
, switch_url
,
247 peg_revision
, revision
, depth
,
248 depth_is_sticky
, NULL
, ignore_externals
,
249 allow_unver_obstructions
, ctx
, pool
);
253 svn_client_switch(svn_revnum_t
*result_rev
,
255 const char *switch_url
,
256 const svn_opt_revision_t
*revision
,
257 svn_boolean_t recurse
,
258 svn_client_ctx_t
*ctx
,
261 svn_opt_revision_t peg_revision
;
262 peg_revision
.kind
= svn_opt_revision_unspecified
;
263 return svn_client__switch_internal(result_rev
, path
, switch_url
,
264 &peg_revision
, revision
,
265 SVN_DEPTH_INFINITY_OR_FILES(recurse
),
266 FALSE
, NULL
, FALSE
, FALSE
, ctx
, pool
);