2 * diff_memory.c : routines for doing diffs on in-memory data
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 * ====================================================================
28 #include <apr_tables.h>
33 #include "svn_pools.h"
34 #include "svn_types.h"
35 #include "svn_string.h"
38 #include "svn_private_config.h"
39 #include "private/svn_adler32.h"
40 #include "private/svn_diff_private.h"
42 typedef struct source_tokens_t
44 /* A token simply is an svn_string_t pointing to
45 the data of the in-memory data source, containing
46 the raw token text, with length stored in the string */
47 apr_array_header_t *tokens;
49 /* Next token to be consumed */
50 apr_size_t next_token;
52 /* The source, containing the in-memory data to be diffed */
53 const svn_string_t *source;
55 /* The last token ends with a newline character (sequence) */
56 svn_boolean_t ends_without_eol;
59 typedef struct diff_mem_baton_t
61 /* The tokens for each of the sources */
62 source_tokens_t sources[4];
64 /* Normalization buffer; we only ever compare 2 tokens at the same time */
65 char *normalization_buf[2];
67 /* Options for normalized comparison of the data sources */
68 const svn_diff_file_options_t *normalization_options;
73 datasource_to_index(svn_diff_datasource_e datasource)
77 case svn_diff_datasource_original:
80 case svn_diff_datasource_modified:
83 case svn_diff_datasource_latest:
86 case svn_diff_datasource_ancestor:
94 /* Implements svn_diff_fns2_t::datasources_open */
96 datasources_open(void *baton,
97 apr_off_t *prefix_lines,
98 apr_off_t *suffix_lines,
99 const svn_diff_datasource_e *datasources,
100 apr_size_t datasources_len)
102 /* Do nothing: everything is already there and initialized to 0 */
109 /* Implements svn_diff_fns2_t::datasource_close */
111 datasource_close(void *baton, svn_diff_datasource_e datasource)
113 /* Do nothing. The compare_token function needs previous datasources
114 * to stay available until all datasources are processed.
121 /* Implements svn_diff_fns2_t::datasource_get_next_token */
123 datasource_get_next_token(apr_uint32_t *hash, void **token, void *baton,
124 svn_diff_datasource_e datasource)
126 diff_mem_baton_t *mem_baton = baton;
127 source_tokens_t *src = &(mem_baton->sources[datasource_to_index(datasource)]);
129 if ((apr_size_t)src->tokens->nelts > src->next_token)
131 /* There are actually tokens to be returned */
132 char *buf = mem_baton->normalization_buf[0];
133 svn_string_t *tok = (*token)
134 = APR_ARRAY_IDX(src->tokens, src->next_token, svn_string_t *);
135 apr_off_t len = tok->len;
136 svn_diff__normalize_state_t state
137 = svn_diff__normalize_state_normal;
139 svn_diff__normalize_buffer(&buf, &len, &state, tok->data,
140 mem_baton->normalization_options);
141 *hash = svn__adler32(0, buf, len);
150 /* Implements svn_diff_fns2_t::token_compare */
152 token_compare(void *baton, void *token1, void *token2, int *result)
154 /* Implement the same behaviour as diff_file.c:token_compare(),
155 but be simpler, because we know we'll have all data in memory */
156 diff_mem_baton_t *btn = baton;
157 svn_string_t *t1 = token1;
158 svn_string_t *t2 = token2;
159 char *buf1 = btn->normalization_buf[0];
160 char *buf2 = btn->normalization_buf[1];
161 apr_off_t len1 = t1->len;
162 apr_off_t len2 = t2->len;
163 svn_diff__normalize_state_t state = svn_diff__normalize_state_normal;
165 svn_diff__normalize_buffer(&buf1, &len1, &state, t1->data,
166 btn->normalization_options);
167 state = svn_diff__normalize_state_normal;
168 svn_diff__normalize_buffer(&buf2, &len2, &state, t2->data,
169 btn->normalization_options);
172 *result = (len1 < len2) ? -1 : 1;
174 *result = (len1 == 0) ? 0 : memcmp(buf1, buf2, (size_t) len1);
179 /* Implements svn_diff_fns2_t::token_discard */
181 token_discard(void *baton, void *token)
183 /* No-op, we have no use for discarded tokens... */
187 /* Implements svn_diff_fns2_t::token_discard_all */
189 token_discard_all(void *baton)
192 Note that in the file case, this function discards all
193 tokens allocated, but we're geared toward small in-memory diffs.
194 Meaning that there's no special pool to clear.
199 static const svn_diff_fns2_t svn_diff__mem_vtable =
203 datasource_get_next_token,
209 /* Fill SRC with the diff tokens (e.g. lines).
211 TEXT is assumed to live long enough for the tokens to
212 stay valid during their lifetime: no data is copied,
213 instead, svn_string_t's are allocated pointing straight
217 fill_source_tokens(source_tokens_t *src,
218 const svn_string_t *text,
225 src->tokens = apr_array_make(pool, 0, sizeof(svn_string_t *));
229 for (startp = curp = text->data, endp = curp + text->len;
230 curp != endp; curp++)
232 if (curp != endp && *curp == '\r' && *(curp + 1) == '\n')
235 if (*curp == '\r' || *curp == '\n')
237 APR_ARRAY_PUSH(src->tokens, svn_string_t *) =
238 svn_string_ncreate(startp, curp - startp + 1, pool);
244 /* If there's anything remaining (ie last line doesn't have a newline) */
247 APR_ARRAY_PUSH(src->tokens, svn_string_t *) =
248 svn_string_ncreate(startp, endp - startp, pool);
249 src->ends_without_eol = TRUE;
252 src->ends_without_eol = FALSE;
257 alloc_normalization_bufs(diff_mem_baton_t *btn,
261 apr_size_t max_len = 0;
265 for (i = 0; i < sources; i++)
267 apr_array_header_t *tokens = btn->sources[i].tokens;
268 if (tokens->nelts > 0)
269 for (idx = 0; idx < tokens->nelts; idx++)
272 = APR_ARRAY_IDX(tokens, idx, svn_string_t *)->len;
273 max_len = (max_len < token_len) ? token_len : max_len;
277 btn->normalization_buf[0] = apr_palloc(pool, max_len);
278 btn->normalization_buf[1] = apr_palloc(pool, max_len);
282 svn_diff_mem_string_diff(svn_diff_t **diff,
283 const svn_string_t *original,
284 const svn_string_t *modified,
285 const svn_diff_file_options_t *options,
288 diff_mem_baton_t baton;
290 fill_source_tokens(&(baton.sources[0]), original, pool);
291 fill_source_tokens(&(baton.sources[1]), modified, pool);
292 alloc_normalization_bufs(&baton, 2, pool);
294 baton.normalization_options = options;
296 return svn_diff_diff_2(diff, &baton, &svn_diff__mem_vtable, pool);
300 svn_diff_mem_string_diff3(svn_diff_t **diff,
301 const svn_string_t *original,
302 const svn_string_t *modified,
303 const svn_string_t *latest,
304 const svn_diff_file_options_t *options,
307 diff_mem_baton_t baton;
309 fill_source_tokens(&(baton.sources[0]), original, pool);
310 fill_source_tokens(&(baton.sources[1]), modified, pool);
311 fill_source_tokens(&(baton.sources[2]), latest, pool);
312 alloc_normalization_bufs(&baton, 3, pool);
314 baton.normalization_options = options;
316 return svn_diff_diff3_2(diff, &baton, &svn_diff__mem_vtable, pool);
321 svn_diff_mem_string_diff4(svn_diff_t **diff,
322 const svn_string_t *original,
323 const svn_string_t *modified,
324 const svn_string_t *latest,
325 const svn_string_t *ancestor,
326 const svn_diff_file_options_t *options,
329 diff_mem_baton_t baton;
331 fill_source_tokens(&(baton.sources[0]), original, pool);
332 fill_source_tokens(&(baton.sources[1]), modified, pool);
333 fill_source_tokens(&(baton.sources[2]), latest, pool);
334 fill_source_tokens(&(baton.sources[3]), ancestor, pool);
335 alloc_normalization_bufs(&baton, 4, pool);
337 baton.normalization_options = options;
339 return svn_diff_diff4_2(diff, &baton, &svn_diff__mem_vtable, pool);
343 typedef enum unified_output_e
345 unified_output_context = 0,
346 unified_output_delete,
347 unified_output_insert,
351 /* Baton for generating unified diffs */
352 typedef struct unified_output_baton_t
354 svn_stream_t *output_stream;
355 const char *header_encoding;
356 source_tokens_t sources[2]; /* 0 == original; 1 == modified */
357 apr_off_t current_token[2]; /* current token per source */
361 /* Cached markers, in header_encoding,
362 indexed using unified_output_e */
363 const char *prefix_str[3];
365 svn_stringbuf_t *hunk; /* in-progress hunk data */
366 apr_off_t hunk_length[2]; /* 0 == original; 1 == modified */
367 apr_off_t hunk_start[2]; /* 0 == original; 1 == modified */
369 /* The delimiters of the hunk header, '@@' for text hunks and '##' for
371 const char *hunk_delimiter;
372 /* The string to print after a line that does not end with a newline.
373 * It must start with a '\'. Typically "\ No newline at end of file". */
374 const char *no_newline_string;
376 /* Pool for allocation of temporary memory in the callbacks
377 Should be cleared on entry of each iteration of a callback */
382 /* Append tokens (lines) FIRST up to PAST_LAST
383 from token-source index TOKENS with change-type TYPE
387 output_unified_token_range(output_baton_t *btn,
389 unified_output_e type,
392 source_tokens_t *source = &btn->sources[tokens];
394 if (until > source->tokens->nelts)
395 until = source->tokens->nelts;
397 if (until <= btn->current_token[tokens])
400 /* Do the loop with prefix and token */
403 svn_string_t *token =
404 APR_ARRAY_IDX(source->tokens, btn->current_token[tokens],
407 if (type != unified_output_skip)
409 svn_stringbuf_appendcstr(btn->hunk, btn->prefix_str[type]);
410 svn_stringbuf_appendbytes(btn->hunk, token->data, token->len);
413 if (type == unified_output_context)
415 btn->hunk_length[0]++;
416 btn->hunk_length[1]++;
418 else if (type == unified_output_delete)
419 btn->hunk_length[0]++;
420 else if (type == unified_output_insert)
421 btn->hunk_length[1]++;
423 /* ### TODO: Add skip processing for -p handling? */
425 btn->current_token[tokens]++;
426 if (btn->current_token[tokens] == until)
430 if (btn->current_token[tokens] == source->tokens->nelts
431 && source->ends_without_eol)
435 SVN_ERR(svn_utf_cstring_from_utf8_ex2(
436 &out_str, btn->no_newline_string,
437 btn->header_encoding, btn->pool));
438 svn_stringbuf_appendcstr(btn->hunk, out_str);
446 /* Flush the hunk currently built up in BATON
447 into the BATON's output_stream.
448 Use the specified HUNK_DELIMITER.
449 If HUNK_DELIMITER is NULL, fall back to the default delimiter. */
451 output_unified_flush_hunk(output_baton_t *baton,
452 const char *hunk_delimiter)
454 apr_off_t target_token;
459 if (svn_stringbuf_isempty(baton->hunk))
462 svn_pool_clear(baton->pool);
464 /* Write the trailing context */
465 target_token = baton->hunk_start[0] + baton->hunk_length[0]
466 + baton->context_size;
467 SVN_ERR(output_unified_token_range(baton, 0 /*original*/,
468 unified_output_context,
470 if (hunk_delimiter == NULL)
471 hunk_delimiter = "@@";
473 old_start = baton->hunk_start[0];
474 new_start = baton->hunk_start[1];
476 /* If the file is non-empty, convert the line indexes from
477 zero based to one based */
478 if (baton->hunk_length[0])
480 if (baton->hunk_length[1])
483 /* Write the hunk header */
484 SVN_ERR(svn_diff__unified_write_hunk_header(
485 baton->output_stream, baton->header_encoding, hunk_delimiter,
486 old_start, baton->hunk_length[0],
487 new_start, baton->hunk_length[1],
488 NULL /* hunk_extra_context */,
491 hunk_len = baton->hunk->len;
492 SVN_ERR(svn_stream_write(baton->output_stream,
493 baton->hunk->data, &hunk_len));
495 /* Prepare for the next hunk */
496 baton->hunk_length[0] = 0;
497 baton->hunk_length[1] = 0;
498 baton->hunk_start[0] = 0;
499 baton->hunk_start[1] = 0;
500 svn_stringbuf_setempty(baton->hunk);
505 /* Implements svn_diff_output_fns_t::output_diff_modified */
507 output_unified_diff_modified(void *baton,
508 apr_off_t original_start,
509 apr_off_t original_length,
510 apr_off_t modified_start,
511 apr_off_t modified_length,
512 apr_off_t latest_start,
513 apr_off_t latest_length)
515 output_baton_t *output_baton = baton;
516 apr_off_t context_prefix_length;
517 apr_off_t prev_context_end;
518 svn_boolean_t init_hunk = FALSE;
520 if (original_start > output_baton->context_size)
521 context_prefix_length = output_baton->context_size;
523 context_prefix_length = original_start;
525 /* Calculate where the previous hunk will end if we would write it now
526 (including the necessary context at the end) */
527 if (output_baton->hunk_length[0] > 0 || output_baton->hunk_length[1] > 0)
529 prev_context_end = output_baton->hunk_start[0]
530 + output_baton->hunk_length[0]
531 + output_baton->context_size;
535 prev_context_end = -1;
537 if (output_baton->hunk_start[0] == 0
538 && (original_length > 0 || modified_length > 0))
542 /* If the changed range is far enough from the previous range, flush the current
545 apr_off_t new_hunk_start = (original_start - context_prefix_length);
547 if (output_baton->current_token[0] < new_hunk_start
548 && prev_context_end <= new_hunk_start)
550 SVN_ERR(output_unified_flush_hunk(output_baton,
551 output_baton->hunk_delimiter));
554 else if (output_baton->hunk_length[0] > 0
555 || output_baton->hunk_length[1] > 0)
557 /* We extend the current hunk */
559 /* Original: Output the context preceding the changed range */
560 SVN_ERR(output_unified_token_range(output_baton, 0 /* original */,
561 unified_output_context,
566 /* Original: Skip lines until we are at the beginning of the context we want
568 SVN_ERR(output_unified_token_range(output_baton, 0 /* original */,
570 original_start - context_prefix_length));
574 SVN_ERR_ASSERT(output_baton->hunk_length[0] == 0
575 && output_baton->hunk_length[1] == 0);
577 output_baton->hunk_start[0] = original_start - context_prefix_length;
578 output_baton->hunk_start[1] = modified_start - context_prefix_length;
581 /* Modified: Skip lines until we are at the start of the changed range */
582 SVN_ERR(output_unified_token_range(output_baton, 1 /* modified */,
586 /* Original: Output the context preceding the changed range */
587 SVN_ERR(output_unified_token_range(output_baton, 0 /* original */,
588 unified_output_context,
591 /* Both: Output the changed range */
592 SVN_ERR(output_unified_token_range(output_baton, 0 /* original */,
593 unified_output_delete,
594 original_start + original_length));
595 SVN_ERR(output_unified_token_range(output_baton, 1 /* modified */,
596 unified_output_insert,
597 modified_start + modified_length));
602 static const svn_diff_output_fns_t mem_output_unified_vtable =
604 NULL, /* output_common */
605 output_unified_diff_modified,
606 NULL, /* output_diff_latest */
607 NULL, /* output_diff_common */
608 NULL /* output_conflict */
613 svn_diff_mem_string_output_unified3(svn_stream_t *output_stream,
615 svn_boolean_t with_diff_header,
616 const char *hunk_delimiter,
617 const char *original_header,
618 const char *modified_header,
619 const char *header_encoding,
620 const svn_string_t *original,
621 const svn_string_t *modified,
623 svn_cancel_func_t cancel_func,
625 apr_pool_t *scratch_pool)
628 if (svn_diff_contains_diffs(diff))
630 output_baton_t baton;
632 memset(&baton, 0, sizeof(baton));
633 baton.output_stream = output_stream;
634 baton.pool = svn_pool_create(scratch_pool);
635 baton.header_encoding = header_encoding;
636 baton.hunk = svn_stringbuf_create_empty(scratch_pool);
637 baton.hunk_delimiter = hunk_delimiter;
638 baton.no_newline_string
639 = (hunk_delimiter == NULL || strcmp(hunk_delimiter, "##") != 0)
640 ? APR_EOL_STR SVN_DIFF__NO_NEWLINE_AT_END_OF_FILE APR_EOL_STR
641 : APR_EOL_STR SVN_DIFF__NO_NEWLINE_AT_END_OF_PROPERTY APR_EOL_STR;
642 baton.context_size = context_size >= 0 ? context_size
643 : SVN_DIFF__UNIFIED_CONTEXT_SIZE;
645 SVN_ERR(svn_utf_cstring_from_utf8_ex2
646 (&(baton.prefix_str[unified_output_context]), " ",
647 header_encoding, scratch_pool));
648 SVN_ERR(svn_utf_cstring_from_utf8_ex2
649 (&(baton.prefix_str[unified_output_delete]), "-",
650 header_encoding, scratch_pool));
651 SVN_ERR(svn_utf_cstring_from_utf8_ex2
652 (&(baton.prefix_str[unified_output_insert]), "+",
653 header_encoding, scratch_pool));
655 fill_source_tokens(&baton.sources[0], original, scratch_pool);
656 fill_source_tokens(&baton.sources[1], modified, scratch_pool);
658 if (with_diff_header)
660 SVN_ERR(svn_diff__unidiff_write_header(
661 output_stream, header_encoding,
662 original_header, modified_header, scratch_pool));
665 SVN_ERR(svn_diff_output2(diff, &baton,
666 &mem_output_unified_vtable,
667 cancel_func, cancel_baton));
669 SVN_ERR(output_unified_flush_hunk(&baton, hunk_delimiter));
671 svn_pool_destroy(baton.pool);
680 /* diff3 merge output */
682 /* A stream to remember *leading* context. Note that this stream does
683 *not* copy the data that it is remembering; it just saves
685 typedef struct context_saver_t {
686 svn_stream_t *stream;
688 const char **data; /* const char *data[context_size] */
689 apr_size_t *len; /* apr_size_t len[context_size] */
690 apr_size_t next_slot;
691 apr_size_t total_written;
696 context_saver_stream_write(void *baton,
700 context_saver_t *cs = baton;
701 cs->data[cs->next_slot] = data;
702 cs->len[cs->next_slot] = *len;
703 cs->next_slot = (cs->next_slot + 1) % cs->context_size;
709 typedef struct merge_output_baton_t
711 svn_stream_t *output_stream;
713 /* Tokenized source text */
714 source_tokens_t sources[3];
715 apr_off_t next_token[3];
717 /* Markers for marking conflicted sections */
718 const char *markers[4]; /* 0 = original, 1 = modified,
719 2 = separator, 3 = latest (end) */
720 const char *marker_eol;
722 svn_diff_conflict_display_style_t conflict_style;
726 svn_cancel_func_t cancel_func;
729 /* The rest of the fields are for
730 svn_diff_conflict_display_only_conflicts only. Note that for
731 these batons, OUTPUT_STREAM is either CONTEXT_SAVER->STREAM or
732 (soon after a conflict) a "trailing context stream", never the
733 actual output stream.*/
734 /* The actual output stream. */
735 svn_stream_t *real_output_stream;
736 context_saver_t *context_saver;
737 /* Used to allocate context_saver and trailing context streams, and
740 } merge_output_baton_t;
744 flush_context_saver(context_saver_t *cs,
745 svn_stream_t *output_stream)
748 for (i = 0; i < cs->context_size; i++)
750 apr_size_t slot = (i + cs->next_slot) % cs->context_size;
753 apr_size_t len = cs->len[slot];
754 SVN_ERR(svn_stream_write(output_stream, cs->data[slot], &len));
762 make_context_saver(merge_output_baton_t *mob)
766 assert(mob->context_size > 0); /* Or nothing to save */
768 svn_pool_clear(mob->pool);
769 cs = apr_pcalloc(mob->pool, sizeof(*cs));
770 cs->stream = svn_stream_empty(mob->pool);
771 svn_stream_set_baton(cs->stream, cs);
772 svn_stream_set_write(cs->stream, context_saver_stream_write);
773 mob->context_saver = cs;
774 mob->output_stream = cs->stream;
775 cs->context_size = mob->context_size;
776 cs->data = apr_pcalloc(mob->pool, sizeof(*cs->data) * cs->context_size);
777 cs->len = apr_pcalloc(mob->pool, sizeof(*cs->len) * cs->context_size);
781 /* A stream which prints LINES_TO_PRINT (based on context_size) lines to
782 BATON->REAL_OUTPUT_STREAM, and then changes BATON->OUTPUT_STREAM to
783 a context_saver; used for *trailing* context. */
785 struct trailing_context_printer {
786 apr_size_t lines_to_print;
787 merge_output_baton_t *mob;
792 trailing_context_printer_write(void *baton,
796 struct trailing_context_printer *tcp = baton;
797 SVN_ERR_ASSERT(tcp->lines_to_print > 0);
798 SVN_ERR(svn_stream_write(tcp->mob->real_output_stream, data, len));
799 tcp->lines_to_print--;
800 if (tcp->lines_to_print == 0)
801 make_context_saver(tcp->mob);
807 make_trailing_context_printer(merge_output_baton_t *btn)
809 struct trailing_context_printer *tcp;
812 svn_pool_clear(btn->pool);
814 tcp = apr_pcalloc(btn->pool, sizeof(*tcp));
815 tcp->lines_to_print = btn->context_size;
817 s = svn_stream_empty(btn->pool);
818 svn_stream_set_baton(s, tcp);
819 svn_stream_set_write(s, trailing_context_printer_write);
820 btn->output_stream = s;
825 output_merge_token_range(apr_size_t *lines_printed_p,
826 merge_output_baton_t *btn,
827 int idx, apr_off_t first,
830 apr_array_header_t *tokens = btn->sources[idx].tokens;
831 apr_size_t lines_printed = 0;
833 for (; length > 0 && first < tokens->nelts; length--, first++)
835 svn_string_t *token = APR_ARRAY_IDX(tokens, first, svn_string_t *);
836 apr_size_t len = token->len;
838 /* Note that the trailing context printer assumes that
839 svn_stream_write is called exactly once per line. */
840 SVN_ERR(svn_stream_write(btn->output_stream, token->data, &len));
845 *lines_printed_p = lines_printed;
851 output_marker_eol(merge_output_baton_t *btn)
853 return svn_stream_puts(btn->output_stream, btn->marker_eol);
857 output_merge_marker(merge_output_baton_t *btn, int idx)
859 SVN_ERR(svn_stream_puts(btn->output_stream, btn->markers[idx]));
860 return output_marker_eol(btn);
864 output_common_modified(void *baton,
865 apr_off_t original_start, apr_off_t original_length,
866 apr_off_t modified_start, apr_off_t modified_length,
867 apr_off_t latest_start, apr_off_t latest_length)
869 return output_merge_token_range(NULL, baton, 1/*modified*/,
870 modified_start, modified_length);
874 output_latest(void *baton,
875 apr_off_t original_start, apr_off_t original_length,
876 apr_off_t modified_start, apr_off_t modified_length,
877 apr_off_t latest_start, apr_off_t latest_length)
879 return output_merge_token_range(NULL, baton, 2/*latest*/,
880 latest_start, latest_length);
884 output_conflict(void *baton,
885 apr_off_t original_start, apr_off_t original_length,
886 apr_off_t modified_start, apr_off_t modified_length,
887 apr_off_t latest_start, apr_off_t latest_length,
890 static const svn_diff_output_fns_t merge_output_vtable =
892 output_common_modified, /* common */
893 output_common_modified, /* modified */
895 output_common_modified, /* output_diff_common */
900 output_conflict(void *baton,
901 apr_off_t original_start, apr_off_t original_length,
902 apr_off_t modified_start, apr_off_t modified_length,
903 apr_off_t latest_start, apr_off_t latest_length,
906 merge_output_baton_t *btn = baton;
908 svn_diff_conflict_display_style_t style = btn->conflict_style;
910 if (style == svn_diff_conflict_display_resolved_modified_latest)
913 return svn_diff_output2(diff, baton, &merge_output_vtable,
914 btn->cancel_func, btn->cancel_baton);
916 style = svn_diff_conflict_display_modified_latest;
919 if (style == svn_diff_conflict_display_modified_latest ||
920 style == svn_diff_conflict_display_modified_original_latest)
922 SVN_ERR(output_merge_marker(btn, 1/*modified*/));
923 SVN_ERR(output_merge_token_range(NULL, btn, 1/*modified*/,
924 modified_start, modified_length));
926 if (style == svn_diff_conflict_display_modified_original_latest)
928 SVN_ERR(output_merge_marker(btn, 0/*original*/));
929 SVN_ERR(output_merge_token_range(NULL, btn, 0/*original*/,
930 original_start, original_length));
933 SVN_ERR(output_merge_marker(btn, 2/*separator*/));
934 SVN_ERR(output_merge_token_range(NULL, btn, 2/*latest*/,
935 latest_start, latest_length));
936 SVN_ERR(output_merge_marker(btn, 3/*latest (end)*/));
938 else if (style == svn_diff_conflict_display_modified)
939 SVN_ERR(output_merge_token_range(NULL, btn, 1/*modified*/,
940 modified_start, modified_length));
941 else if (style == svn_diff_conflict_display_latest)
942 SVN_ERR(output_merge_token_range(NULL, btn, 2/*latest*/,
943 latest_start, latest_length));
944 else /* unknown style */
945 SVN_ERR_MALFUNCTION();
951 output_conflict_with_context_marker(merge_output_baton_t *btn,
957 SVN_ERR(svn_stream_printf(btn->output_stream, btn->pool,
958 "%s (%" APR_OFF_T_FMT ")",
961 SVN_ERR(svn_stream_printf(btn->output_stream, btn->pool,
962 "%s (%" APR_OFF_T_FMT ",%" APR_OFF_T_FMT ")",
963 label, start + 1, length));
965 SVN_ERR(output_marker_eol(btn));
971 output_conflict_with_context(void *baton,
972 apr_off_t original_start,
973 apr_off_t original_length,
974 apr_off_t modified_start,
975 apr_off_t modified_length,
976 apr_off_t latest_start,
977 apr_off_t latest_length,
980 merge_output_baton_t *btn = baton;
982 /* Are we currently saving starting context (as opposed to printing
983 trailing context)? If so, flush it. */
984 if (btn->output_stream == btn->context_saver->stream)
986 if (btn->context_saver->total_written > btn->context_size)
987 SVN_ERR(svn_stream_puts(btn->real_output_stream, "@@\n"));
988 SVN_ERR(flush_context_saver(btn->context_saver, btn->real_output_stream));
991 /* Print to the real output stream. */
992 btn->output_stream = btn->real_output_stream;
994 /* Output the conflict itself. */
995 SVN_ERR(output_conflict_with_context_marker(btn, btn->markers[1],
998 SVN_ERR(output_merge_token_range(NULL, btn, 1/*modified*/,
999 modified_start, modified_length));
1001 SVN_ERR(output_conflict_with_context_marker(btn, btn->markers[0],
1004 SVN_ERR(output_merge_token_range(NULL, btn, 0/*original*/,
1005 original_start, original_length));
1007 SVN_ERR(output_merge_marker(btn, 2/*separator*/));
1008 SVN_ERR(output_merge_token_range(NULL, btn, 2/*latest*/,
1009 latest_start, latest_length));
1010 SVN_ERR(output_conflict_with_context_marker(btn, btn->markers[3],
1014 /* Go into print-trailing-context mode instead. */
1015 make_trailing_context_printer(btn);
1017 return SVN_NO_ERROR;
1021 static const svn_diff_output_fns_t merge_only_conflicts_output_vtable =
1023 output_common_modified,
1024 output_common_modified,
1026 output_common_modified,
1027 output_conflict_with_context
1031 /* TOKEN is the first token in the modified file.
1032 Return its line-ending, if any. */
1034 detect_eol(svn_string_t *token)
1038 if (token->len == 0)
1041 curp = token->data + token->len - 1;
1044 else if (*curp != '\n')
1049 || *(--curp) != '\r')
1057 svn_diff_mem_string_output_merge3(svn_stream_t *output_stream,
1059 const svn_string_t *original,
1060 const svn_string_t *modified,
1061 const svn_string_t *latest,
1062 const char *conflict_original,
1063 const char *conflict_modified,
1064 const char *conflict_latest,
1065 const char *conflict_separator,
1066 svn_diff_conflict_display_style_t style,
1067 svn_cancel_func_t cancel_func,
1069 apr_pool_t *scratch_pool)
1071 merge_output_baton_t btn;
1073 svn_boolean_t conflicts_only =
1074 (style == svn_diff_conflict_display_only_conflicts);
1075 const svn_diff_output_fns_t *vtable = conflicts_only
1076 ? &merge_only_conflicts_output_vtable : &merge_output_vtable;
1078 memset(&btn, 0, sizeof(btn));
1079 btn.context_size = SVN_DIFF__UNIFIED_CONTEXT_SIZE;
1083 btn.pool = svn_pool_create(scratch_pool);
1084 make_context_saver(&btn);
1085 btn.real_output_stream = output_stream;
1088 btn.output_stream = output_stream;
1090 fill_source_tokens(&(btn.sources[0]), original, scratch_pool);
1091 fill_source_tokens(&(btn.sources[1]), modified, scratch_pool);
1092 fill_source_tokens(&(btn.sources[2]), latest, scratch_pool);
1094 btn.conflict_style = style;
1096 if (btn.sources[1].tokens->nelts > 0)
1098 eol = detect_eol(APR_ARRAY_IDX(btn.sources[1].tokens, 0, svn_string_t *));
1100 eol = APR_EOL_STR; /* use the platform default */
1103 eol = APR_EOL_STR; /* use the platform default */
1105 btn.marker_eol = eol;
1106 btn.cancel_func = cancel_func;
1107 btn.cancel_baton = cancel_baton;
1109 SVN_ERR(svn_utf_cstring_from_utf8(&btn.markers[1],
1112 : "<<<<<<< (modified)",
1114 SVN_ERR(svn_utf_cstring_from_utf8(&btn.markers[0],
1117 : "||||||| (original)",
1119 SVN_ERR(svn_utf_cstring_from_utf8(&btn.markers[2],
1121 ? conflict_separator
1124 SVN_ERR(svn_utf_cstring_from_utf8(&btn.markers[3],
1127 : ">>>>>>> (latest)",
1130 SVN_ERR(svn_diff_output2(diff, &btn, vtable, cancel_func, cancel_baton));
1132 svn_pool_destroy(btn.pool);
1134 return SVN_NO_ERROR;