2 * diff_pristine.c -- A simple diff walker which compares local files against
3 * their pristine versions.
5 * ====================================================================
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
22 * ====================================================================
24 * This is the simple working copy diff algorithm which is used when you
25 * just use 'svn diff PATH'. It shows what is modified in your working copy
26 * since a node was checked out or copied but doesn't show most kinds of
27 * restructuring operations.
29 * You can look at this as another form of the status walker.
34 #include "svn_error.h"
35 #include "svn_pools.h"
36 #include "svn_dirent_uri.h"
40 #include "private/svn_wc_private.h"
41 #include "private/svn_diff_tree.h"
45 #include "translate.h"
48 #include "svn_private_config.h"
50 /*-------------------------------------------------------------------------*/
52 /* Baton containing the state of a directory
53 reported open via a diff processor */
56 struct node_state_t *parent;
60 const char *local_abspath;
64 svn_diff_source_t *left_src;
65 svn_diff_source_t *right_src;
66 svn_diff_source_t *copy_src;
69 svn_boolean_t skip_children;
71 apr_hash_t *left_props;
72 apr_hash_t *right_props;
73 const apr_array_header_t *propchanges;
82 /* Report editor paths relative from this directory */
83 const char *anchor_abspath;
85 struct node_state_t *cur;
87 const svn_diff_tree_processor_t *processor;
89 /* Should this diff ignore node ancestry? */
90 svn_boolean_t ignore_ancestry;
92 /* Should this diff not compare copied files with their source? */
93 svn_boolean_t show_copies_as_adds;
95 /* Hash whose keys are const char * changelist names. */
96 apr_hash_t *changelist_hash;
98 /* Cancel function/baton */
99 svn_cancel_func_t cancel_func;
105 /* Recursively opens directories on the stack in EB, until LOCAL_ABSPATH
106 is reached. If RECURSIVE_SKIP is TRUE, don't open LOCAL_ABSPATH itself,
107 but create it marked with skip+skip_children.
110 ensure_state(struct diff_baton *eb,
111 const char *local_abspath,
112 svn_boolean_t recursive_skip,
113 apr_pool_t *scratch_pool)
115 struct node_state_t *ns;
121 relpath = svn_dirent_skip_ancestor(eb->anchor_abspath, local_abspath);
125 /* Don't recurse on the anchor, as that might loop infinately because
126 svn_dirent_dirname("/",...) -> "/"
127 svn_dirent_dirname("C:/",...) -> "C:/" (Windows) */
129 SVN_ERR(ensure_state(eb,
130 svn_dirent_dirname(local_abspath,scratch_pool),
134 else if (svn_dirent_is_child(eb->cur->local_abspath, local_abspath, NULL))
135 SVN_ERR(ensure_state(eb, svn_dirent_dirname(local_abspath,scratch_pool),
141 if (eb->cur && eb->cur->skip_children)
144 ns_pool = svn_pool_create(eb->cur ? eb->cur->pool : eb->pool);
145 ns = apr_pcalloc(ns_pool, sizeof(*ns));
148 ns->local_abspath = apr_pstrdup(ns_pool, local_abspath);
149 ns->relpath = svn_dirent_skip_ancestor(eb->anchor_abspath, ns->local_abspath);
150 ns->parent = eb->cur;
156 ns->skip_children = TRUE;
161 svn_revnum_t revision;
164 err = svn_wc__db_base_get_info(NULL, NULL, &revision, NULL, NULL, NULL,
165 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
167 eb->db, local_abspath,
168 scratch_pool, scratch_pool);
172 if (err->apr_err != SVN_ERR_WC_PATH_NOT_FOUND)
173 return svn_error_trace(err);
174 svn_error_clear(err);
176 revision = 0; /* Use original revision? */
178 ns->left_src = svn_diff__source_create(revision, ns->pool);
179 ns->right_src = svn_diff__source_create(SVN_INVALID_REVNUM, ns->pool);
181 SVN_ERR(eb->processor->dir_opened(&ns->baton, &ns->skip,
186 NULL /* copyfrom_source */,
187 ns->parent ? ns->parent->baton : NULL,
189 ns->pool, scratch_pool));
195 /* Implements svn_wc_status_func3_t */
197 diff_status_callback(void *baton,
198 const char *local_abspath,
199 const svn_wc_status3_t *status,
200 apr_pool_t *scratch_pool)
202 struct diff_baton *eb = baton;
203 svn_wc__db_t *db = eb->db;
205 if (! status->versioned)
206 return SVN_NO_ERROR; /* unversioned (includes dir externals) */
208 if (status->node_status == svn_wc_status_conflicted
209 && status->text_status == svn_wc_status_none
210 && status->prop_status == svn_wc_status_none)
212 /* Node is an actual only node describing a tree conflict */
216 /* Not text/prop modified, not copied. Easy out */
217 if (status->node_status == svn_wc_status_normal && !status->copied)
220 /* Mark all directories where we are no longer inside as closed */
222 && !svn_dirent_is_ancestor(eb->cur->local_abspath, local_abspath))
224 struct node_state_t *ns = eb->cur;
229 SVN_ERR(eb->processor->dir_changed(ns->relpath,
239 SVN_ERR(eb->processor->dir_closed(ns->relpath,
246 eb->cur = ns->parent;
247 svn_pool_clear(ns->pool);
249 SVN_ERR(ensure_state(eb, svn_dirent_dirname(local_abspath, scratch_pool),
250 FALSE, scratch_pool));
252 if (eb->cur && eb->cur->skip_children)
255 if (eb->changelist_hash != NULL
256 && (!status->changelist
257 || ! svn_hash_gets(eb->changelist_hash, status->changelist)))
258 return SVN_NO_ERROR; /* Filtered via changelist */
260 /* This code does about the same thing as the inner body of
261 walk_local_nodes_diff() in diff_editor.c, except that
262 it is already filtered by the status walker, doesn't have to
263 account for remote changes (and many tiny other details) */
266 svn_boolean_t repos_only;
267 svn_boolean_t local_only;
268 svn_wc__db_status_t db_status;
269 svn_boolean_t have_base;
270 svn_node_kind_t base_kind;
271 svn_node_kind_t db_kind = status->kind;
272 svn_depth_t depth_below_here = svn_depth_unknown;
274 const char *child_abspath = local_abspath;
275 const char *child_relpath = svn_dirent_skip_ancestor(eb->anchor_abspath,
282 /* ### optimize away this call using status info. Should
283 be possible in almost every case (except conflict, missing, obst.)*/
284 SVN_ERR(svn_wc__db_read_info(&db_status, NULL, NULL, NULL, NULL, NULL,
285 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
286 NULL, NULL, NULL, NULL, NULL, NULL, NULL,
287 NULL, NULL, NULL, NULL,
288 &have_base, NULL, NULL,
289 eb->db, local_abspath,
290 scratch_pool, scratch_pool));
293 local_only = TRUE; /* Only report additions */
295 else if (db_status == svn_wc__db_status_normal)
300 else if (db_status == svn_wc__db_status_deleted)
302 svn_wc__db_status_t base_status;
304 SVN_ERR(svn_wc__db_base_get_info(&base_status, &base_kind, NULL,
305 NULL, NULL, NULL, NULL, NULL,
306 NULL, NULL, NULL, NULL, NULL,
308 eb->db, local_abspath,
309 scratch_pool, scratch_pool));
311 if (base_status != svn_wc__db_status_normal)
316 /* working status is either added or deleted */
317 svn_wc__db_status_t base_status;
319 SVN_ERR(svn_wc__db_base_get_info(&base_status, &base_kind, NULL,
320 NULL, NULL, NULL, NULL, NULL,
321 NULL, NULL, NULL, NULL, NULL,
323 eb->db, local_abspath,
324 scratch_pool, scratch_pool));
326 if (base_status != svn_wc__db_status_normal)
328 else if (base_kind != db_kind || !eb->ignore_ancestry)
337 /* Report repository form deleted */
338 if (base_kind == svn_node_file)
339 SVN_ERR(svn_wc__diff_base_only_file(db, child_abspath,
343 eb->cur ? eb->cur->baton : NULL,
345 else if (base_kind == svn_node_dir)
346 SVN_ERR(svn_wc__diff_base_only_dir(db, child_abspath,
351 eb->cur ? eb->cur->baton : NULL,
356 else if (!local_only)
358 /* Diff base against actual */
359 if (db_kind == svn_node_file)
361 SVN_ERR(svn_wc__diff_base_working_diff(db, child_abspath,
374 else if (db_kind == svn_node_dir)
376 SVN_ERR(ensure_state(eb, local_abspath, FALSE, scratch_pool));
378 if (status->prop_status != svn_wc_status_none
379 && status->prop_status != svn_wc_status_normal)
381 apr_array_header_t *propchanges;
382 SVN_ERR(svn_wc__db_base_get_props(&eb->cur->left_props,
383 eb->db, local_abspath,
386 SVN_ERR(svn_wc__db_read_props(&eb->cur->right_props,
387 eb->db, local_abspath,
391 SVN_ERR(svn_prop_diffs(&propchanges,
392 eb->cur->right_props,
396 eb->cur->propchanges = propchanges;
401 if (local_only && (db_status != svn_wc__db_status_deleted))
403 if (db_kind == svn_node_file)
404 SVN_ERR(svn_wc__diff_local_only_file(db, child_abspath,
407 eb->cur ? eb->cur->baton : NULL,
413 else if (db_kind == svn_node_dir)
414 SVN_ERR(svn_wc__diff_local_only_dir(db, child_abspath,
415 child_relpath, depth_below_here,
417 eb->cur ? eb->cur->baton : NULL,
425 if (db_kind == svn_node_dir && (local_only || repos_only))
426 SVN_ERR(ensure_state(eb, local_abspath, TRUE /* skip */, scratch_pool));
433 /* Public Interface */
435 svn_wc_diff6(svn_wc_context_t *wc_ctx,
436 const char *local_abspath,
437 const svn_wc_diff_callbacks4_t *callbacks,
438 void *callback_baton,
440 svn_boolean_t ignore_ancestry,
441 svn_boolean_t show_copies_as_adds,
442 svn_boolean_t use_git_diff_format,
443 const apr_array_header_t *changelist_filter,
444 svn_cancel_func_t cancel_func,
446 apr_pool_t *scratch_pool)
448 struct diff_baton eb = { 0 };
449 svn_node_kind_t kind;
450 svn_boolean_t get_all;
451 const svn_diff_tree_processor_t *processor;
453 SVN_ERR_ASSERT(svn_dirent_is_absolute(local_abspath));
454 SVN_ERR(svn_wc__db_read_kind(&kind, wc_ctx->db, local_abspath,
455 FALSE /* allow_missing */,
456 TRUE /* show_deleted */,
457 FALSE /* show_hidden */,
460 if (kind == svn_node_dir)
461 eb.anchor_abspath = local_abspath;
463 eb.anchor_abspath = svn_dirent_dirname(local_abspath, scratch_pool);
465 SVN_ERR(svn_wc__wrap_diff_callbacks(&processor,
466 callbacks, callback_baton, TRUE,
467 scratch_pool, scratch_pool));
469 if (use_git_diff_format)
470 show_copies_as_adds = TRUE;
471 if (show_copies_as_adds)
472 ignore_ancestry = FALSE;
478 processor = svn_diff__tree_processor_reverse_create(processor, NULL, pool);
481 if (! show_copies_as_adds && !use_git_diff_format)
482 processor = svn_diff__tree_processor_copy_as_changed_create(processor,
486 eb.processor = processor;
487 eb.ignore_ancestry = ignore_ancestry;
488 eb.show_copies_as_adds = show_copies_as_adds;
489 eb.pool = scratch_pool;
491 if (changelist_filter && changelist_filter->nelts)
492 SVN_ERR(svn_hash_from_cstring_keys(&eb.changelist_hash, changelist_filter,
495 if (show_copies_as_adds || use_git_diff_format || !ignore_ancestry)
496 get_all = TRUE; /* We need unmodified descendants of copies */
500 /* Walk status handles files and directories */
501 SVN_ERR(svn_wc__internal_walk_status(wc_ctx->db, local_abspath, depth,
503 TRUE /* no_ignore */,
504 FALSE /* ignore_text_mods */,
505 NULL /* ignore_patterns */,
506 diff_status_callback, &eb,
507 cancel_func, cancel_baton,
510 /* Close the remaining open directories */
513 struct node_state_t *ns = eb.cur;
518 SVN_ERR(processor->dir_changed(ns->relpath,
528 SVN_ERR(processor->dir_closed(ns->relpath,
536 svn_pool_clear(ns->pool);