1 /* git-merge-changelog - git "merge" driver for GNU style ChangeLog files.
2 Copyright (C) 2008 Bruno Haible <bruno@clisp.org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 The default merge driver of 'git' *always* produces conflicts when
19 pulling public modifications into a privately modified ChangeLog file.
20 This is because ChangeLog files are always modified at the top; the
21 default merge driver has no clue how to deal with this. Furthermore
22 the conflicts are presented with more <<<< ==== >>>> markers than
23 necessary; this is because the default merge driver makes pointless
24 effects to look at the individual line changes inside a ChangeLog entry.
26 This program serves as a 'git' merge driver that avoids these problems.
27 1. It produces no conflict when ChangeLog entries have been inserted
28 at the top both in the public and in the private modification. It
29 puts the privately added entries above the publicly added entries.
30 2. It respects the structure of ChangeLog files: entries are not split
31 into lines but kept together.
32 3. It also handles the case of small modifications of past ChangeLog
33 entries, or of removed ChangeLog entries: they are merged as one
35 4. Conflicts are presented at the top of the file, rather than where
36 they occurred, so that the user will see them immediately. (Unlike
37 for source code written in some programming language, conflict markers
38 that are located several hundreds lines from the top will not cause
39 any syntax error and therefore would be likely to remain unnoticed.)
43 $ gnulib-tool --create-testdir --dir=/tmp/testdir123 git-merge-changelog
48 - Add to .git/config of the checkout (or to your $HOME/.gitconfig) the lines
50 [merge "merge-changelog"]
51 name = GNU-style ChangeLog merge driver
52 driver = /usr/local/bin/git-merge-changelog %O %A %B
54 - In every directory that contains a ChangeLog file, add a file
55 '.gitattributes' with this line:
57 ChangeLog merge=merge-changelog
59 (See "man 5 gitattributes" for more info.)
62 /* Calling convention:
63 A merge driver is called with three filename arguments:
64 1. %O = The common ancestor of %A and %B.
65 2. %A = The file's contents from the "current branch".
66 3. %B = The file's contents from the "other branch"; this is the contents
69 In case of a "git stash apply" or of an upstream pull (e.g. from a subsystem
70 maintainer to a central maintainer):
71 2. %A = The file's newest pulled contents; modified by other committers.
72 3. %B = The user's newest copy of the file; modified by the user.
73 In case of a downstream pull (e.g. from a central repository to the user):
74 2. %A = The user's newest copy of the file; modified by the user.
75 3. %B = The file's newest pulled contents; modified by other committers.
77 It should write its merged output into file %A. It can also echo some
78 remarks to stdout. It should exit with return code 0 if the merge could
79 be resolved cleanly, or with non-zero return code if there were conflicts.
83 The structure of a ChangeLog file: It consists of ChangeLog entries. A
84 ChangeLog entry starts at a line following a blank line and that starts with
85 a non-whitespace character, or at the beginning of a file.
86 The merge driver works as follows: It reads the three files into memory and
87 dissects them into ChangeLog entries. It then finds the differences between
88 %O and %B. They are classified as:
89 - removals (some consecutive entries removed),
90 - changes (some consecutive entries removed, some consecutive entries
92 - additions (some consecutive entries added).
93 The driver then attempts to apply the changes to %A.
94 To this effect, it first computes a correspondence between the entries in %O
95 and the entries in %A, using fuzzy string matching to still identify changed
97 - Removals are applied one by one. If the entry is present in %A, at any
98 position, it is removed. If not, the removal is marked as a conflict.
99 - Additions at the top of %B are applied at the top of %A.
100 - Additions between entry x and entry y (y may be the file end) in %B are
101 applied between entry x and entry y in %A (if they still exist and are
102 still consecutive in %A), otherwise the additions are marked as a
104 - Changes are categorized into "simple changes":
107 added_entry ... added_entry modified_entry1 ... modified_entryn,
108 where the correspondence between entry_i and modified_entry_i is still
109 clear; and "big changes": these are all the rest. Simple changes at the
110 top of %B are applied by putting the added entries at the top of %A. The
111 changes in simple changes are applied one by one; possibly leading to
112 single-entry conflicts. Big changes are applied en bloc, possibly
113 leading to conflicts spanning multiple entries.
114 - Conflicts are output at the top of the file and cause an exit status of
126 #include <sys/types.h>
129 #include "progname.h"
131 #include "read-file.h"
133 #include "gl_array_list.h"
134 #include "gl_linkedhash_list.h"
135 #include "gl_linked_list.h"
137 #include "xmalloca.h"
140 #include "fwriteerror.h"
142 #define ASSERT(expr) \
150 #define FSTRCMP_THRESHOLD 0.6
152 /* Representation of a ChangeLog entry.
153 The string may contain NUL bytes; therefore it is represented as a plain
154 opaque memory region. */
161 /* Compare two entries for equality. */
163 entry_equals (const void *elt1, const void *elt2)
165 const struct entry *entry1 = (const struct entry *) elt1;
166 const struct entry *entry2 = (const struct entry *) elt2;
167 return entry1->length == entry2->length
168 && memcmp (entry1->string, entry2->string, entry1->length) == 0;
171 /* Return a hash code of the contents of a ChangeLog entry. */
173 entry_hashcode (const void *elt)
175 const struct entry *entry = (const struct entry *) elt;
176 /* See http://www.haible.de/bruno/hashfunc.html. */
181 for (s = entry->string, n = entry->length; n > 0; s++, n--)
182 h = (unsigned char) *s + ((h << 9) | (h >> (sizeof (size_t) * CHAR_BIT - 9)));
187 /* Perform a fuzzy comparison of two ChangeLog entries.
188 Return a similarity measure of the two entries, a value between 0 and 1.
189 0 stands for very distinct, 1 for identical. */
191 entry_fstrcmp (const struct entry *entry1, const struct entry *entry2)
193 /* fstrcmp works only on NUL terminated strings. */
197 if (memchr (entry1->string, '\0', entry1->length) != NULL)
199 if (memchr (entry2->string, '\0', entry2->length) != NULL)
201 memory = (char *) xmalloca (entry1->length + 1 + entry2->length + 1);
204 memcpy (p, entry1->string, entry1->length);
207 memcpy (p, entry2->string, entry2->length);
211 similarity = fstrcmp (memory, memory + entry1->length + 1);
216 /* This structure represents an entire ChangeLog file, after it was read
218 struct changelog_file
220 /* The entries, as a list. */
221 gl_list_t /* <struct entry *> */ entries_list;
222 /* The entries, as a list in opposite direction. */
223 gl_list_t /* <struct entry *> */ entries_reversed;
224 /* The entries, as an array. */
226 struct entry **entries;
229 /* Read a ChangeLog file into memory.
230 Return the contents in *RESULT. */
232 read_changelog_file (const char *filename, struct changelog_file *result)
234 /* Read the file in text mode, otherwise it's hard to recognize empty
237 char *contents = read_file (filename, &length);
238 if (contents == NULL)
240 fprintf (stderr, "could not read file '%s'\n", filename);
244 result->entries_list =
245 gl_list_create_empty (GL_LINKEDHASH_LIST, entry_equals, entry_hashcode,
247 result->entries_reversed =
248 gl_list_create_empty (GL_LINKEDHASH_LIST, entry_equals, entry_hashcode,
250 /* A ChangeLog file consists of ChangeLog entries. A ChangeLog entry starts
251 at a line following a blank line and that starts with a non-whitespace
252 character, or at the beginning of a file.
253 Split the file contents into entries. */
255 char *contents_end = contents + length;
256 char *start = contents;
257 while (start < contents_end)
259 /* Search the end of the current entry. */
263 while (ptr < contents_end)
265 ptr = memchr (ptr, '\n', contents_end - ptr);
272 if (contents_end - ptr >= 2
274 && !(ptr[1] == '\n' || ptr[1] == '\t' || ptr[1] == ' '))
281 curr = XMALLOC (struct entry);
282 curr->string = start;
283 curr->length = ptr - start;
284 gl_list_add_last (result->entries_list, curr);
285 gl_list_add_first (result->entries_reversed, curr);
291 result->num_entries = gl_list_size (result->entries_list);
292 result->entries = XNMALLOC (result->num_entries, struct entry *);
295 gl_list_iterator_t iter = gl_list_iterator (result->entries_list);
298 while (gl_list_iterator_next (&iter, &elt, &node))
299 result->entries[index++] = (struct entry *) elt;
300 gl_list_iterator_free (&iter);
301 ASSERT (index == result->num_entries);
305 /* Compute a mapping (correspondence) between entries of FILE1 and of FILE2.
306 Return a set of two arrays:
307 - An array mapping FILE1 indices to FILE2 indices (or -1 when the entry
308 from FILE1 is not found in FILE2).
309 - An array mapping FILE2 indices to FILE1 indices (or -1 when the entry
310 from FILE2 is not found in FILE1).
311 The correspondence also takes into account small modifications; i.e. the
312 indicated relation is not equality of entries but best-match similarity
315 compute_mapping (struct changelog_file *file1, struct changelog_file *file2,
318 /* Mapping from indices in file1 to indices in file2. */
319 ssize_t *index_mapping;
320 /* Mapping from indices in file2 to indices in file1. */
321 ssize_t *index_mapping_reverse;
322 size_t n1 = file1->num_entries;
323 size_t n2 = file2->num_entries;
326 index_mapping = XNMALLOC (n1, ssize_t);
327 for (i = 0; i < n1; i++)
328 index_mapping[i] = -1;
330 index_mapping_reverse = XNMALLOC (n2, ssize_t);
331 for (j = 0; j < n2; j++)
332 index_mapping_reverse[j] = -1;
334 for (i = n1 - 1; i >= 0; i--)
335 /* Take an entry from file1. */
336 if (index_mapping[i] < 0)
338 struct entry *entry = file1->entries[i];
339 /* Search whether it occurs in file2. */
340 j = gl_list_indexof (file2->entries_reversed, entry);
344 /* Found an exact correspondence. */
345 ASSERT (index_mapping_reverse[j] < 0);
346 index_mapping[i] = j;
347 index_mapping_reverse[j] = i;
348 /* Look for more occurrences of the same entry. */
359 gl_list_indexof_from (file1->entries_reversed, n1 - curr_i,
364 gl_list_indexof_from (file2->entries_reversed, n2 - curr_j,
368 curr_i = n1 - 1 - next_i;
369 curr_j = n2 - 1 - next_j;
370 ASSERT (index_mapping[curr_i] < 0);
371 ASSERT (index_mapping_reverse[curr_j] < 0);
372 index_mapping[curr_i] = curr_j;
373 index_mapping_reverse[curr_j] = curr_i;
379 for (i = n1 - 1; i >= 0; i--)
380 /* Take an entry from file1. */
381 if (index_mapping[i] < 0)
383 struct entry *entry_i = file1->entries[i];
384 /* Search whether it approximately occurs in file2. */
386 double best_j_similarity = 0.0;
387 for (j = n2 - 1; j >= 0; j--)
388 if (index_mapping_reverse[j] < 0)
390 double similarity = entry_fstrcmp (entry_i, file2->entries[j]);
391 if (similarity > best_j_similarity)
394 best_j_similarity = similarity;
397 if (best_j_similarity >= FSTRCMP_THRESHOLD)
399 /* Found a similar entry in file2. */
400 struct entry *entry_j = file2->entries[best_j];
401 /* Search whether it approximately occurs in file1 at index i. */
403 double best_i_similarity = 0.0;
405 for (ii = n1 - 1; ii >= 0; ii--)
406 if (index_mapping[ii] < 0)
409 entry_fstrcmp (file1->entries[ii], entry_j);
410 if (similarity > best_i_similarity)
413 best_i_similarity = similarity;
416 if (best_i_similarity >= FSTRCMP_THRESHOLD && best_i == i)
418 index_mapping[i] = best_j;
419 index_mapping_reverse[best_j] = i;
424 result[0] = index_mapping;
425 result[1] = index_mapping_reverse;
428 /* An "edit" is a textual modification performed by the user, that needs to
429 be applied to the other file. */
432 /* Some consecutive entries were added. */
434 /* Some consecutive entries were removed; some other consecutive entries
435 were added at the same position. (Not necessarily the same number of
438 /* Some consecutive entries were removed. */
442 /* This structure represents an edit. */
446 /* Range of indices into the entries of FILE1. */
447 ssize_t i1, i2; /* first, last index; only used for CHANGE, REMOVAL */
448 /* Range of indices into the entries of FILE2. */
449 ssize_t j1, j2; /* first, last index; only used for ADDITION, CHANGE */
452 /* This structure represents the differences from one file, FILE1, to another
456 /* An array mapping FILE1 indices to FILE2 indices (or -1 when the entry
457 from FILE1 is not found in FILE2). */
458 ssize_t *index_mapping;
459 /* An array mapping FILE2 indices to FILE1 indices (or -1 when the entry
460 from FILE2 is not found in FILE1). */
461 ssize_t *index_mapping_reverse;
462 /* The edits that transform FILE1 into FILE2. */
467 /* Import the difference detection algorithm from GNU diff. */
468 #define ELEMENT struct entry *
469 #define EQUAL entry_equals
470 #define OFFSET ssize_t
471 #define EXTRA_CONTEXT_FIELDS \
472 ssize_t *index_mapping; \
473 ssize_t *index_mapping_reverse;
474 #define NOTE_DELETE(ctxt, xoff) \
475 ctxt->index_mapping[xoff] = -1
476 #define NOTE_INSERT(ctxt, yoff) \
477 ctxt->index_mapping_reverse[yoff] = -1
480 /* Compute the differences between the entries of FILE1 and the entries of
483 compute_differences (struct changelog_file *file1, struct changelog_file *file2,
484 struct differences *result)
486 /* Unlike compute_mapping, which mostly ignores the order of the entries and
487 therefore works well when some entries are permuted, here we use the order.
488 I think this is needed in order to distinguish changes from
489 additions+removals; I don't know how to say what is a "change" if the
490 files are considered as unordered sets of entries. */
492 size_t n1 = file1->num_entries;
493 size_t n2 = file2->num_entries;
496 gl_list_t /* <struct edit *> */ edits;
498 ctxt.xvec = file1->entries;
499 ctxt.yvec = file2->entries;
500 ctxt.index_mapping = XNMALLOC (n1, ssize_t);
501 for (i = 0; i < n1; i++)
502 ctxt.index_mapping[i] = 0;
503 ctxt.index_mapping_reverse = XNMALLOC (n2, ssize_t);
504 for (j = 0; j < n2; j++)
505 ctxt.index_mapping_reverse[j] = 0;
506 ctxt.fdiag = XNMALLOC (2 * (n1 + n2 + 3), ssize_t) + n2 + 1;
507 ctxt.bdiag = ctxt.fdiag + n1 + n2 + 3;
508 ctxt.too_expensive = n1 + n2;
510 /* Store in ctxt.index_mapping and ctxt.index_mapping_reverse a -1 for
511 each removed or added entry. */
512 compareseq (0, n1, 0, n2, 0, &ctxt);
514 /* Complete the index_mapping and index_mapping_reverse arrays. */
517 while (i < n1 || j < n2)
519 while (i < n1 && ctxt.index_mapping[i] < 0)
521 while (j < n2 && ctxt.index_mapping_reverse[j] < 0)
523 ASSERT ((i < n1) == (j < n2));
524 if (i == n1 && j == n2)
526 ctxt.index_mapping[i] = j;
527 ctxt.index_mapping_reverse[j] = i;
532 /* Create the edits. */
533 edits = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
536 while (i < n1 || j < n2)
542 e = XMALLOC (struct edit);
546 gl_list_add_last (edits, e);
553 e = XMALLOC (struct edit);
557 gl_list_add_last (edits, e);
560 if (ctxt.index_mapping[i] >= 0)
562 if (ctxt.index_mapping_reverse[j] >= 0)
564 ASSERT (ctxt.index_mapping[i] == j);
565 ASSERT (ctxt.index_mapping_reverse[j] == i);
572 ASSERT (ctxt.index_mapping_reverse[j] < 0);
573 e = XMALLOC (struct edit);
578 while (j < n2 && ctxt.index_mapping_reverse[j] < 0);
580 gl_list_add_last (edits, e);
585 if (ctxt.index_mapping_reverse[j] >= 0)
588 ASSERT (ctxt.index_mapping[i] < 0);
589 e = XMALLOC (struct edit);
594 while (i < n1 && ctxt.index_mapping[i] < 0);
596 gl_list_add_last (edits, e);
601 ASSERT (ctxt.index_mapping[i] < 0);
602 ASSERT (ctxt.index_mapping_reverse[j] < 0);
603 e = XMALLOC (struct edit);
608 while (i < n1 && ctxt.index_mapping[i] < 0);
613 while (j < n2 && ctxt.index_mapping_reverse[j] < 0);
615 gl_list_add_last (edits, e);
620 result->index_mapping = ctxt.index_mapping;
621 result->index_mapping_reverse = ctxt.index_mapping_reverse;
622 result->num_edits = gl_list_size (edits);
623 result->edits = XNMALLOC (result->num_edits, struct edit *);
626 gl_list_iterator_t iter = gl_list_iterator (edits);
629 while (gl_list_iterator_next (&iter, &elt, &node))
630 result->edits[index++] = (struct edit *) elt;
631 gl_list_iterator_free (&iter);
632 ASSERT (index == result->num_edits);
636 /* An empty entry. */
637 static struct entry empty_entry = { NULL, 0 };
639 /* Write the contents of an entry to the output stream FP. */
641 entry_write (FILE *fp, struct entry *entry)
643 if (entry->length > 0)
644 fwrite (entry->string, 1, entry->length, fp);
647 /* This structure represents a conflict.
648 A conflict can occur for various reasons. */
651 /* Parts from the ancestor file. */
652 size_t num_old_entries;
653 struct entry **old_entries;
654 /* Parts of the modified file. */
655 size_t num_modified_entries;
656 struct entry **modified_entries;
659 /* Write a conflict to the output stream FP, including markers. */
661 conflict_write (FILE *fp, struct conflict *c)
665 /* Use the same syntax as git's default merge driver.
666 Don't indent the contents of the entries (with things like ">" or "-"),
667 otherwise the user needs more textual editing to resolve the conflict. */
668 fputs ("<<<<<<<\n", fp);
669 for (i = 0; i < c->num_old_entries; i++)
670 entry_write (fp, c->old_entries[i]);
671 fputs ("=======\n", fp);
672 for (i = 0; i < c->num_modified_entries; i++)
673 entry_write (fp, c->modified_entries[i]);
674 fputs (">>>>>>>\n", fp);
678 static const struct option long_options[] =
680 { "help", no_argument, NULL, 'h' },
681 { "version", no_argument, NULL, 'V' },
685 /* Print a usage mesage and exit. */
689 if (status != EXIT_SUCCESS)
690 fprintf (stderr, "Try `%s --help' for more information.\n",
694 printf ("Usage: %s [OPTION] O-FILE-NAME A-FILE-NAME B-FILE-NAME\n",
697 printf ("Merges independent modifications of a ChangeLog style file.\n");
698 printf ("O-FILE-NAME names the original file, the ancestor of the two others.\n");
699 printf ("A-FILE-NAME names the publicly modified file.\n");
700 printf ("B-FILE-NAME names the user-modified file.\n");
701 printf ("Writes the merged file into A-FILE-NAME.\n");
703 printf ("Informative output:\n");
704 printf (" -h, --help display this help and exit\n");
705 printf (" -V, --version output version information and exit\n");
707 fputs ("Report bugs to <bug-gnu-gettext@gnu.org>.\n",
715 main (int argc, char *argv[])
721 /* Set program name for messages. */
722 set_program_name (argv[0]);
724 /* Set default values for variables. */
728 /* Parse command line options. */
729 while ((optchar = getopt_long (argc, argv, "hV", long_options, NULL)) != EOF)
732 case '\0': /* Long option. */
741 usage (EXIT_FAILURE);
746 /* Version information is requested. */
747 printf ("%s\n", program_name);
748 printf ("Copyright (C) %s Free Software Foundation, Inc.\n\
749 License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n\
750 This is free software: you are free to change and redistribute it.\n\
751 There is NO WARRANTY, to the extent permitted by law.\n\
754 printf ("Written by %s.\n", "Bruno Haible");
760 /* Help is requested. */
761 usage (EXIT_SUCCESS);
764 /* Test argument count. */
765 if (optind + 3 != argc)
766 error (EXIT_FAILURE, 0, "expected three arguments");
769 const char *ancestor_file_name; /* O-FILE-NAME */
770 const char *destination_file_name; /* A-FILE-NAME */
772 const char *other_file_name; /* B-FILE-NAME */
773 const char *mainstream_file_name;
774 const char *modified_file_name;
775 struct changelog_file ancestor_file;
776 struct changelog_file mainstream_file;
777 struct changelog_file modified_file;
778 /* Mapping from indices in ancestor_file to indices in mainstream_file. */
779 ssize_t *index_mapping;
780 /* Mapping from indices in mainstream_file to indices in ancestor_file. */
781 ssize_t *index_mapping_reverse;
782 struct differences diffs;
783 gl_list_node_t *result_entries_pointers; /* array of pointers into result_entries */
784 gl_list_t /* <struct entry *> */ result_entries;
785 gl_list_t /* <struct conflict *> */ result_conflicts;
787 ancestor_file_name = argv[optind];
788 destination_file_name = argv[optind + 1];
789 other_file_name = argv[optind + 2];
791 /* Heuristic to determine whether it's a pull in downstream direction
792 (e.g. pull from a centralized server) or a pull in upstream direction
793 (e.g. "git stash apply").
795 For ChangeLog this distinction is important. The difference between
796 an "upstream" and a "downstream" repository is that more people are
797 looking at the "upstream" repository. They want to be informed about
798 changes and expect them to be shown at the top of the ChangeLog.
799 When a user pulls downstream, on the other hand, he has two options:
800 a) He gets the change entries from the central repository also at the
801 top of his ChangeLog, and his own changes come after them.
802 b) He gets the change entries from the central repository after those
803 he has collected for his branch. His own change entries stay at
804 the top of the ChangeLog file.
805 In the case a) he has to reorder the ChangeLog before he can commit.
806 No one does that. So most people want b).
807 In other words, the order of entries in a ChangeLog should represent
808 the order in which they have flown (or will flow) into the *central*
811 But in git this is fundamentally indistinguishable, because when Linus
812 pulls patches from akpm and akpm pulls patches from Linus, it's not
813 clear which of the two is more "upstream". Also, when you have many
814 branches in a repository and pull from one to another, "git" has no way
815 to know which branch is more "upstream" than the other. The git-tag(1)
816 manual page also says:
817 "One important aspect of git is it is distributed, and being
818 distributed largely means there is no inherent "upstream" or
819 "downstream" in the system."
820 Therefore anyone who attempts to produce a ChangeLog from the merge
823 Here we allow the user to specify the pull direction through an
824 environment variable (GIT_UPSTREAM or GIT_DOWNSTREAM). If these two
825 environment variables are not set, we assume a "simple single user"
826 usage pattern: He manages local changes through stashes and uses
827 "git pull" only to pull downstream.
829 How to distinguish these situation? There are several hints:
830 - During a "git stash apply", GIT_REFLOG_ACTION is not set. During
831 a "git pull", it is set to 'pull '.
832 - During a "git stash apply", there is an environment variable of
833 the form GITHEAD_<40_hex_digits>='Stashed changes'. */
837 var = getenv ("GIT_DOWNSTREAM");
838 if (var != NULL && var[0] != '\0')
842 var = getenv ("GIT_UPSTREAM");
843 if (var != NULL && var[0] != '\0')
847 var = getenv ("GIT_REFLOG_ACTION");
848 #if 0 /* Debugging code */
849 printf ("GIT_REFLOG_ACTION=|%s|\n", var);
852 && (strncmp (var, "pull", 4) == 0
853 || strncmp (var, "merge origin", 12) == 0))
857 /* "git stash apply", "git rebase" and similar. */
864 #if 0 /* Debugging code */
867 printf ("First line of %%A:\n");
868 sprintf (buf, "head -1 %s", destination_file_name); system (buf);
869 printf ("First line of %%B:\n");
870 sprintf (buf, "head -1 %s", other_file_name); system (buf);
871 printf ("Guessing direction: %sstream\n", downstream ? "down" : "up");
877 mainstream_file_name = other_file_name;
878 modified_file_name = destination_file_name;
882 mainstream_file_name = destination_file_name;
883 modified_file_name = other_file_name;
886 /* Read the three files into memory. */
887 read_changelog_file (ancestor_file_name, &ancestor_file);
888 read_changelog_file (mainstream_file_name, &mainstream_file);
889 read_changelog_file (modified_file_name, &modified_file);
891 /* Compute correspondence between the entries of ancestor_file and of
895 compute_mapping (&ancestor_file, &mainstream_file, result);
896 index_mapping = result[0];
897 index_mapping_reverse = result[1];
900 /* Compute differences between the entries of ancestor_file and of
902 compute_differences (&ancestor_file, &modified_file, &diffs);
904 /* Compute the result. */
905 result_entries_pointers =
906 XNMALLOC (mainstream_file.num_entries, gl_list_node_t);
908 gl_list_create_empty (GL_LINKED_LIST, entry_equals, entry_hashcode,
912 for (k = 0; k < mainstream_file.num_entries; k++)
913 result_entries_pointers[k] =
914 gl_list_add_last (result_entries, mainstream_file.entries[k]);
917 gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
920 for (e = 0; e < diffs.num_edits; e++)
922 struct edit *edit = diffs.edits[e];
928 /* An addition to the top of modified_file.
929 Apply it to the top of mainstream_file. */
931 for (j = edit->j2; j >= edit->j1; j--)
933 struct entry *added_entry = modified_file.entries[j];
934 gl_list_add_first (result_entries, added_entry);
943 i_before = diffs.index_mapping_reverse[edit->j1 - 1];
944 ASSERT (i_before >= 0);
945 i_after = (edit->j2 + 1 == modified_file.num_entries
946 ? ancestor_file.num_entries
947 : diffs.index_mapping_reverse[edit->j2 + 1]);
948 ASSERT (i_after >= 0);
949 ASSERT (i_after == i_before + 1);
950 /* An addition between ancestor_file.entries[i_before] and
951 ancestor_file.entries[i_after]. See whether these two
952 entries still exist in mainstream_file and are still
954 k_before = index_mapping[i_before];
955 k_after = (i_after == ancestor_file.num_entries
956 ? mainstream_file.num_entries
957 : index_mapping[i_after]);
958 if (k_before >= 0 && k_after >= 0 && k_after == k_before + 1)
960 /* Yes, the entry before and after are still neighbours
961 in mainstream_file. Apply the addition between
963 if (k_after == mainstream_file.num_entries)
966 for (j = edit->j1; j <= edit->j2; j++)
968 struct entry *added_entry = modified_file.entries[j];
969 gl_list_add_last (result_entries, added_entry);
974 gl_list_node_t node_k_after = result_entries_pointers[k_after];
976 for (j = edit->j1; j <= edit->j2; j++)
978 struct entry *added_entry = modified_file.entries[j];
979 gl_list_add_before (result_entries, node_k_after, added_entry);
985 /* It's not clear where the additions should be applied.
986 Let the user decide. */
987 struct conflict *c = XMALLOC (struct conflict);
989 c->num_old_entries = 0;
990 c->old_entries = NULL;
991 c->num_modified_entries = edit->j2 - edit->j1 + 1;
992 c->modified_entries =
993 XNMALLOC (c->num_modified_entries, struct entry *);
994 for (j = edit->j1; j <= edit->j2; j++)
995 c->modified_entries[j - edit->j1] = modified_file.entries[j];
996 gl_list_add_last (result_conflicts, c);
1002 /* Apply the removals one by one. */
1004 for (i = edit->i1; i <= edit->i2; i++)
1006 struct entry *removed_entry = ancestor_file.entries[i];
1007 ssize_t k = index_mapping[i];
1009 && entry_equals (removed_entry,
1010 mainstream_file.entries[k]))
1012 /* The entry to be removed still exists in
1013 mainstream_file. Remove it. */
1014 gl_list_node_set_value (result_entries,
1015 result_entries_pointers[k],
1020 /* The entry to be removed was already removed or was
1021 modified. This is a conflict. */
1022 struct conflict *c = XMALLOC (struct conflict);
1023 c->num_old_entries = 1;
1025 XNMALLOC (c->num_old_entries, struct entry *);
1026 c->old_entries[0] = removed_entry;
1027 c->num_modified_entries = 0;
1028 c->modified_entries = NULL;
1029 gl_list_add_last (result_conflicts, c);
1038 /* Test whether the change is "simple", i.e. whether it
1039 consists of small changes to the old ChangeLog entries
1040 and additions before them:
1043 added_entry ... added_entry modified_entry_1 ... modified_entry_n. */
1044 if (edit->i2 - edit->i1 <= edit->j2 - edit->j1)
1048 for (i = edit->i1; i <= edit->i2; i++)
1049 if (entry_fstrcmp (ancestor_file.entries[i],
1050 modified_file.entries[i + edit->j2 - edit->i2])
1051 < FSTRCMP_THRESHOLD)
1062 /* Apply the additions and each of the single-entry changes
1064 size_t num_changed = edit->i2 - edit->i1 + 1; /* > 0 */
1065 size_t num_added = (edit->j2 - edit->j1 + 1) - num_changed;
1068 /* A simple change at the top of modified_file.
1069 Apply it to the top of mainstream_file. */
1071 for (j = edit->j1 + num_added - 1; j >= edit->j1; j--)
1073 struct entry *added_entry = modified_file.entries[j];
1074 gl_list_add_first (result_entries, added_entry);
1076 for (j = edit->j1 + num_added; j <= edit->j2; j++)
1078 struct entry *changed_entry = modified_file.entries[j];
1079 size_t i = j + edit->i2 - edit->j2;
1080 ssize_t k = index_mapping[i];
1082 && entry_equals (ancestor_file.entries[i],
1083 mainstream_file.entries[k]))
1085 gl_list_node_set_value (result_entries,
1086 result_entries_pointers[k],
1091 struct conflict *c = XMALLOC (struct conflict);
1092 c->num_old_entries = 1;
1094 XNMALLOC (c->num_old_entries, struct entry *);
1095 c->old_entries[0] = ancestor_file.entries[i];
1096 c->num_modified_entries = 1;
1097 c->modified_entries =
1098 XNMALLOC (c->num_modified_entries, struct entry *);
1099 c->modified_entries[0] = changed_entry;
1100 gl_list_add_last (result_conflicts, c);
1110 i_before = diffs.index_mapping_reverse[edit->j1 - 1];
1111 ASSERT (i_before >= 0);
1112 /* A simple change after ancestor_file.entries[i_before].
1113 See whether this entry and the following num_changed
1114 entries still exist in mainstream_file and are still
1116 k_before = index_mapping[i_before];
1117 linear = (k_before >= 0);
1121 for (i = i_before + 1; i <= i_before + num_changed; i++)
1122 if (index_mapping[i] != k_before + (i - i_before))
1130 gl_list_node_t node_for_insert =
1131 result_entries_pointers[k_before + 1];
1133 for (j = edit->j1 + num_added - 1; j >= edit->j1; j--)
1135 struct entry *added_entry = modified_file.entries[j];
1136 gl_list_add_before (result_entries, node_for_insert, added_entry);
1138 for (j = edit->j1 + num_added; j <= edit->j2; j++)
1140 struct entry *changed_entry = modified_file.entries[j];
1141 size_t i = j + edit->i2 - edit->j2;
1142 ssize_t k = index_mapping[i];
1144 if (entry_equals (ancestor_file.entries[i],
1145 mainstream_file.entries[k]))
1147 gl_list_node_set_value (result_entries,
1148 result_entries_pointers[k],
1153 struct conflict *c = XMALLOC (struct conflict);
1154 c->num_old_entries = 1;
1156 XNMALLOC (c->num_old_entries, struct entry *);
1157 c->old_entries[0] = ancestor_file.entries[i];
1158 c->num_modified_entries = 1;
1159 c->modified_entries =
1160 XNMALLOC (c->num_modified_entries, struct entry *);
1161 c->modified_entries[0] = changed_entry;
1162 gl_list_add_last (result_conflicts, c);
1172 See whether the num_changed entries still exist unchanged
1173 in mainstream_file and are still consecutive. */
1176 bool linear_unchanged;
1178 k_first = index_mapping[i_first];
1181 && entry_equals (ancestor_file.entries[i_first],
1182 mainstream_file.entries[k_first]));
1183 if (linear_unchanged)
1186 for (i = i_first + 1; i <= edit->i2; i++)
1187 if (!(index_mapping[i] == k_first + (i - i_first)
1188 && entry_equals (ancestor_file.entries[i],
1189 mainstream_file.entries[index_mapping[i]])))
1191 linear_unchanged = false;
1195 if (linear_unchanged)
1197 gl_list_node_t node_for_insert =
1198 result_entries_pointers[k_first];
1201 for (j = edit->j2; j >= edit->j1; j--)
1203 struct entry *new_entry = modified_file.entries[j];
1204 gl_list_add_before (result_entries, node_for_insert, new_entry);
1206 for (i = edit->i1; i <= edit->i2; i++)
1208 ssize_t k = index_mapping[i];
1210 ASSERT (entry_equals (ancestor_file.entries[i],
1211 mainstream_file.entries[k]));
1212 gl_list_node_set_value (result_entries,
1213 result_entries_pointers[k],
1221 struct conflict *c = XMALLOC (struct conflict);
1223 c->num_old_entries = edit->i2 - edit->i1 + 1;
1225 XNMALLOC (c->num_old_entries, struct entry *);
1226 for (i = edit->i1; i <= edit->i2; i++)
1227 c->old_entries[i - edit->i1] = ancestor_file.entries[i];
1228 c->num_modified_entries = edit->j2 - edit->j1 + 1;
1229 c->modified_entries =
1230 XNMALLOC (c->num_modified_entries, struct entry *);
1231 for (j = edit->j1; j <= edit->j2; j++)
1232 c->modified_entries[j - edit->j1] = modified_file.entries[j];
1233 gl_list_add_last (result_conflicts, c);
1241 /* Output the result. */
1243 FILE *fp = fopen (destination_file_name, "w");
1246 fprintf (stderr, "could not write file '%s'\n", destination_file_name);
1247 exit (EXIT_FAILURE);
1250 /* Output the conflicts at the top. */
1252 size_t n = gl_list_size (result_conflicts);
1254 for (i = 0; i < n; i++)
1255 conflict_write (fp, (struct conflict *) gl_list_get_at (result_conflicts, i));
1258 size_t n = gl_list_size (result_entries);
1260 for (i = 0; i < n; i++)
1261 entry_write (fp, (struct entry *) gl_list_get_at (result_entries, i));
1264 if (fwriteerror (fp))
1266 fprintf (stderr, "error writing to file '%s'\n", destination_file_name);
1267 exit (EXIT_FAILURE);
1271 exit (gl_list_size (result_conflicts) > 0 ? EXIT_FAILURE : EXIT_SUCCESS);