2 * util.c : routines for doing diffs
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 * ====================================================================
26 #include <apr_general.h>
29 #include "svn_pools.h"
30 #include "svn_dirent_uri.h"
31 #include "svn_props.h"
32 #include "svn_mergeinfo.h"
33 #include "svn_error.h"
35 #include "svn_types.h"
36 #include "svn_ctype.h"
38 #include "svn_version.h"
40 #include "private/svn_diff_private.h"
41 #include "private/svn_sorts_private.h"
44 #include "svn_private_config.h"
48 svn_diff_contains_conflicts(svn_diff_t *diff)
52 if (diff->type == svn_diff__type_conflict)
64 svn_diff_contains_diffs(svn_diff_t *diff)
68 if (diff->type != svn_diff__type_common)
80 svn_diff_output2(svn_diff_t *diff,
82 const svn_diff_output_fns_t *vtable,
83 svn_cancel_func_t cancel_func,
86 svn_error_t *(*output_fn)(void *,
89 apr_off_t, apr_off_t);
94 SVN_ERR(cancel_func(cancel_baton));
98 case svn_diff__type_common:
99 output_fn = vtable->output_common;
102 case svn_diff__type_diff_common:
103 output_fn = vtable->output_diff_common;
106 case svn_diff__type_diff_modified:
107 output_fn = vtable->output_diff_modified;
110 case svn_diff__type_diff_latest:
111 output_fn = vtable->output_diff_latest;
114 case svn_diff__type_conflict:
116 if (vtable->output_conflict != NULL)
118 SVN_ERR(vtable->output_conflict(output_baton,
119 diff->original_start, diff->original_length,
120 diff->modified_start, diff->modified_length,
121 diff->latest_start, diff->latest_length,
122 diff->resolved_diff));
131 if (output_fn != NULL)
133 SVN_ERR(output_fn(output_baton,
134 diff->original_start, diff->original_length,
135 diff->modified_start, diff->modified_length,
136 diff->latest_start, diff->latest_length));
147 svn_diff__normalize_buffer(char **tgt,
149 svn_diff__normalize_state_t *statep,
151 const svn_diff_file_options_t *opts)
153 /* Variables for looping through BUF */
154 const char *curp, *endp;
156 /* Variable to record normalizing state */
157 svn_diff__normalize_state_t state = *statep;
159 /* Variables to track what needs copying into the target buffer */
160 const char *start = buf;
161 apr_size_t include_len = 0;
162 svn_boolean_t last_skipped = FALSE; /* makes sure we set 'start' */
164 /* Variable to record the state of the target buffer */
165 char *tgt_newend = *tgt;
167 /* If this is a noop, then just get out of here. */
168 if (! opts->ignore_space && ! opts->ignore_eol_style)
175 /* It only took me forever to get this routine right,
176 so here my thoughts go:
178 Below, we loop through the data, doing 2 things:
183 The routine tries its hardest *not* to copy data, but instead
184 returning a pointer into already normalized existing data.
186 To this end, a block 'other data' shouldn't be copied when found,
187 but only as soon as it can't be returned in-place.
189 On a character level, there are 3 possible operations:
191 - Skip the character (don't include in the normalized data)
192 - Include the character (do include in the normalizad data)
193 - Include as another character
194 This is essentially the same as skipping the current character
195 and inserting a given character in the output data.
197 The macros below (SKIP, INCLUDE and INCLUDE_AS) are defined to
198 handle the character based operations. The macros themselves
199 collect character level data into blocks.
201 At all times designate the START, INCLUDED_LEN and CURP pointers
202 an included and and skipped block like this:
204 [ start, start + included_len ) [ start + included_len, curp )
207 When the routine flips from skipping to including, the last
208 included block has to be flushed to the output buffer.
211 /* Going from including to skipping; only schedules the current
212 included section for flushing.
213 Also, simply chop off the character if it's the first in the buffer,
214 so we can possibly just return the remainder of the buffer */
219 last_skipped = TRUE; \
225 COPY_INCLUDED_SECTION; \
227 last_skipped = FALSE; \
230 #define COPY_INCLUDED_SECTION \
232 if (include_len > 0) \
234 memmove(tgt_newend, start, include_len); \
235 tgt_newend += include_len; \
241 /* Include the current character as character X.
242 If the current character already *is* X, add it to the
243 currently included region, increasing chances for consecutive
244 fully normalized blocks. */
245 #define INCLUDE_AS(x) \
256 /* Insert character X in the output buffer */
259 COPY_INCLUDED_SECTION; \
260 *tgt_newend++ = (x); \
263 for (curp = buf, endp = buf + *lengthp; curp != endp; ++curp)
268 if (opts->ignore_eol_style)
272 state = svn_diff__normalize_state_cr;
276 if (state == svn_diff__normalize_state_cr
277 && opts->ignore_eol_style)
281 state = svn_diff__normalize_state_normal;
285 if (svn_ctype_isspace(*curp)
286 && opts->ignore_space != svn_diff_file_ignore_space_none)
288 /* Whitespace but not '\r' or '\n' */
289 if (state != svn_diff__normalize_state_whitespace
290 && opts->ignore_space
291 == svn_diff_file_ignore_space_change)
292 /*### If we can postpone insertion of the space
293 until the next non-whitespace character,
294 we have a potential of reducing the number of copies:
295 If this space is followed by more spaces,
296 this will cause a block-copy.
297 If the next non-space block is considered normalized
298 *and* preceded by a space, we can take advantage of that. */
299 /* Note, the above optimization applies to 90% of the source
300 lines in our own code, since it (generally) doesn't use
301 more than one space per blank section, except for the
302 beginning of a line. */
306 state = svn_diff__normalize_state_whitespace;
310 /* Non-whitespace character, or whitespace character in
311 svn_diff_file_ignore_space_none mode. */
313 state = svn_diff__normalize_state_normal;
318 /* If we're not in whitespace, flush the last chunk of data.
319 * Note that this will work correctly when this is the last chunk of the
321 * * If there is an eol, it will either have been output when we entered
322 * the state_cr, or it will be output now.
323 * * If there is no eol and we're not in whitespace, then we just output
325 * * If there's no eol and we are in whitespace, we want to ignore
326 * whitespace unconditionally. */
328 if (*tgt == tgt_newend)
330 /* we haven't copied any data in to *tgt and our chunk consists
331 only of one block of (already normalized) data.
332 Just return the block. */
333 *tgt = (char *)start;
334 *lengthp = include_len;
338 COPY_INCLUDED_SECTION;
339 *lengthp = tgt_newend - *tgt;
348 #undef COPY_INCLUDED_SECTION
352 svn_diff__unified_append_no_newline_msg(svn_stringbuf_t *stringbuf,
353 const char *header_encoding,
354 apr_pool_t *scratch_pool)
358 SVN_ERR(svn_utf_cstring_from_utf8_ex2(
361 SVN_DIFF__NO_NEWLINE_AT_END_OF_FILE APR_EOL_STR,
362 header_encoding, scratch_pool));
363 svn_stringbuf_appendcstr(stringbuf, out_str);
368 svn_diff__unified_write_hunk_header(svn_stream_t *output_stream,
369 const char *header_encoding,
370 const char *hunk_delimiter,
372 apr_off_t old_length,
374 apr_off_t new_length,
375 const char *hunk_extra_context,
376 apr_pool_t *scratch_pool)
378 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
380 "%s -%" APR_OFF_T_FMT,
381 hunk_delimiter, old_start));
382 /* If the hunk length is 1, suppress the number of lines in the hunk
383 * (it is 1 implicitly) */
386 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
388 ",%" APR_OFF_T_FMT, old_length));
391 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
393 " +%" APR_OFF_T_FMT, new_start));
396 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
398 ",%" APR_OFF_T_FMT, new_length));
401 if (hunk_extra_context == NULL)
402 hunk_extra_context = "";
403 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
405 " %s%s%s" APR_EOL_STR,
407 hunk_extra_context[0] ? " " : "",
408 hunk_extra_context));
413 svn_diff__unidiff_write_header(svn_stream_t *output_stream,
414 const char *header_encoding,
415 const char *old_header,
416 const char *new_header,
417 apr_pool_t *scratch_pool)
419 SVN_ERR(svn_stream_printf_from_utf8(output_stream, header_encoding,
422 "+++ %s" APR_EOL_STR,
428 /* A helper function for display_prop_diffs. Output the differences between
429 the mergeinfo stored in ORIG_MERGEINFO_VAL and NEW_MERGEINFO_VAL in a
430 human-readable form to OUTSTREAM, using ENCODING. Use POOL for temporary
433 display_mergeinfo_diff(const char *old_mergeinfo_val,
434 const char *new_mergeinfo_val,
435 const char *encoding,
436 svn_stream_t *outstream,
439 apr_hash_t *old_mergeinfo_hash, *new_mergeinfo_hash, *added, *deleted;
440 apr_pool_t *iterpool = svn_pool_create(pool);
441 apr_hash_index_t *hi;
443 if (old_mergeinfo_val)
444 SVN_ERR(svn_mergeinfo_parse(&old_mergeinfo_hash, old_mergeinfo_val, pool));
446 old_mergeinfo_hash = NULL;
448 if (new_mergeinfo_val)
449 SVN_ERR(svn_mergeinfo_parse(&new_mergeinfo_hash, new_mergeinfo_val, pool));
451 new_mergeinfo_hash = NULL;
453 SVN_ERR(svn_mergeinfo_diff2(&deleted, &added, old_mergeinfo_hash,
457 /* Print a hint for 'svn patch' or smilar tools, indicating the
458 * number of reverse-merges and forward-merges. */
459 SVN_ERR(svn_stream_printf_from_utf8(outstream, encoding, pool,
460 "## -0,%u +0,%u ##%s",
461 apr_hash_count(deleted),
462 apr_hash_count(added),
465 for (hi = apr_hash_first(pool, deleted);
466 hi; hi = apr_hash_next(hi))
468 const char *from_path = apr_hash_this_key(hi);
469 svn_rangelist_t *merge_revarray = apr_hash_this_val(hi);
470 svn_string_t *merge_revstr;
472 svn_pool_clear(iterpool);
473 SVN_ERR(svn_rangelist_to_string(&merge_revstr, merge_revarray,
476 SVN_ERR(svn_stream_printf_from_utf8(outstream, encoding, iterpool,
477 _(" Reverse-merged %s:r%s%s"),
478 from_path, merge_revstr->data,
482 for (hi = apr_hash_first(pool, added);
483 hi; hi = apr_hash_next(hi))
485 const char *from_path = apr_hash_this_key(hi);
486 svn_rangelist_t *merge_revarray = apr_hash_this_val(hi);
487 svn_string_t *merge_revstr;
489 svn_pool_clear(iterpool);
490 SVN_ERR(svn_rangelist_to_string(&merge_revstr, merge_revarray,
493 SVN_ERR(svn_stream_printf_from_utf8(outstream, encoding, iterpool,
494 _(" Merged %s:r%s%s"),
495 from_path, merge_revstr->data,
499 svn_pool_destroy(iterpool);
503 /* svn_sort__array callback handling svn_prop_t by name */
505 propchange_sort(const void *k1, const void *k2)
507 const svn_prop_t *propchange1 = k1;
508 const svn_prop_t *propchange2 = k2;
510 return strcmp(propchange1->name, propchange2->name);
514 svn_diff__display_prop_diffs(svn_stream_t *outstream,
515 const char *encoding,
516 const apr_array_header_t *propchanges,
517 apr_hash_t *original_props,
518 svn_boolean_t pretty_print_mergeinfo,
520 svn_cancel_func_t cancel_func,
522 apr_pool_t *scratch_pool)
524 apr_pool_t *pool = scratch_pool;
525 apr_pool_t *iterpool = svn_pool_create(pool);
526 apr_array_header_t *changes = apr_array_copy(scratch_pool, propchanges);
529 svn_sort__array(changes, propchange_sort);
531 for (i = 0; i < changes->nelts; i++)
534 const svn_string_t *original_value;
535 const svn_prop_t *propchange
536 = &APR_ARRAY_IDX(changes, i, svn_prop_t);
539 original_value = svn_hash_gets(original_props, propchange->name);
541 original_value = NULL;
543 /* If the property doesn't exist on either side, or if it exists
544 with the same value, skip it. This can happen if the client is
545 hitting an old mod_dav_svn server that doesn't understand the
546 "send-all" REPORT style. */
547 if ((! (original_value || propchange->value))
548 || (original_value && propchange->value
549 && svn_string_compare(original_value, propchange->value)))
552 svn_pool_clear(iterpool);
554 if (! original_value)
556 else if (! propchange->value)
560 SVN_ERR(svn_stream_printf_from_utf8(outstream, encoding, iterpool,
562 propchange->name, APR_EOL_STR));
564 if (pretty_print_mergeinfo
565 && strcmp(propchange->name, SVN_PROP_MERGEINFO) == 0)
567 const char *orig = original_value ? original_value->data : NULL;
568 const char *val = propchange->value ? propchange->value->data : NULL;
569 svn_error_t *err = display_mergeinfo_diff(orig, val, encoding,
570 outstream, iterpool);
572 /* Issue #3896: If we can't pretty-print mergeinfo differences
573 because invalid mergeinfo is present, then don't let the diff
574 fail, just print the diff as any other property. */
575 if (err && err->apr_err == SVN_ERR_MERGEINFO_PARSE_ERROR)
577 svn_error_clear(err);
588 svn_diff_file_options_t options = { 0 };
589 const svn_string_t *orig
590 = original_value ? original_value
591 : svn_string_create_empty(iterpool);
592 const svn_string_t *val
593 = propchange->value ? propchange->value
594 : svn_string_create_empty(iterpool);
596 SVN_ERR(svn_diff_mem_string_diff(&diff, orig, val, &options,
599 /* UNIX patch will try to apply a diff even if the diff header
600 * is missing. It tries to be helpful by asking the user for a
601 * target filename when it can't determine the target filename
602 * from the diff header. But there usually are no files which
603 * UNIX patch could apply the property diff to, so we use "##"
604 * instead of "@@" as the default hunk delimiter for property diffs.
605 * We also suppress the diff header. */
606 SVN_ERR(svn_diff_mem_string_output_unified3(
607 outstream, diff, FALSE /* no header */, "##", NULL, NULL,
608 encoding, orig, val, context_size,
609 cancel_func, cancel_baton, iterpool));
612 svn_pool_destroy(iterpool);
618 /* Return the library version number. */
619 const svn_version_t *
620 svn_diff_version(void)