git-merge-file --ours, --theirs
authorJunio C Hamano <gitster@pobox.com>
Fri, 20 Jun 2008 07:17:27 +0000 (00:17 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Nov 2009 07:11:46 +0000 (23:11 -0800)
Sometimes people want their conflicting merges autoresolved by
favouring upstream changes. The standard answer they are given is
to run "git diff --name-only | xargs git checkout MERGE_HEAD --" in
such a case. This is to accept automerge results for the paths that
are fully resolved automatically, while taking their version of the
file in full for paths that have conflicts.

This is problematic on two counts.

One is that this is not exactly what these people want. It discards
all changes they did on their branch for any paths that conflicted.
They usually want to salvage as much automerge result as possible in
a conflicted file, and want to take the upstream change only in the
conflicted part.

This patch teaches two new modes of operation to the lowest-lever
merge machinery, xdl_merge(). Instead of leaving the conflicted
lines from both sides enclosed in <<<, ===, and >>> markers, the
conflicts are resolved favouring our side or their side of changes.

A larger problem is that this tends to encourage a bad workflow by
allowing people to record such a mixed up half-merged result as a
full commit without auditing. This commit does not tackle this
issue at all. In git, we usually give long enough rope to users
with strange wishes as long as the risky features are not enabled by
default, and this is such a risky feature.

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-merge-file.txt
builtin-merge-file.c
xdiff/xdiff.h
xdiff/xmerge.c
index 303537357b2fb5d1a0d7fa291d51803ddb97749c..b9d22764a6c7504905e637ccfbb781ad3769a5a0 100644 (file)
@@ -10,7 +10,8 @@ SYNOPSIS
 --------
 [verse]
 'git merge-file' [-L <current-name> [-L <base-name> [-L <other-name>]]]
-       [-p|--stdout] [-q|--quiet] <current-file> <base-file> <other-file>
+       [--ours|--theirs] [-p|--stdout] [-q|--quiet]
+       <current-file> <base-file> <other-file>
 
 
 DESCRIPTION
@@ -34,7 +35,9 @@ normally outputs a warning and brackets the conflict with lines containing
        >>>>>>> B
 
 If there are conflicts, the user should edit the result and delete one of
-the alternatives.
+the alternatives.  When `--ours` or `--theirs` option is in effect, however,
+these conflicts are resolved favouring lines from `<current-file>` or
+lines from `<other-file>` respectively.
 
 The exit value of this program is negative on error, and the number of
 conflicts otherwise. If the merge was clean, the exit value is 0.
@@ -62,6 +65,11 @@ OPTIONS
 -q::
        Quiet; do not warn about conflicts.
 
+--ours::
+--theirs::
+       Instead of leaving conflicts in the file, resolve conflicts
+       favouring our (or their) side of the lines.
+
 
 EXAMPLES
 --------
index afd2ea7a732460c431328dbd4c0141a49d638244..1efc4e09bc12c73978789502d041fd596397aeb3 100644 (file)
@@ -27,13 +27,18 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
        mmbuffer_t result = {NULL, 0};
        xpparam_t xpp = {XDF_NEED_MINIMAL};
        int ret = 0, i = 0, to_stdout = 0;
-       int merge_level = XDL_MERGE_ZEALOUS_ALNUM;
-       int merge_style = 0, quiet = 0;
+       int level = XDL_MERGE_ZEALOUS_ALNUM;
+       int style = 0, quiet = 0;
+       int favor = 0;
        int nongit;
 
        struct option options[] = {
                OPT_BOOLEAN('p', "stdout", &to_stdout, "send results to standard output"),
-               OPT_SET_INT(0, "diff3", &merge_style, "use a diff3 based merge", XDL_MERGE_DIFF3),
+               OPT_SET_INT(0, "diff3", &style, "use a diff3 based merge", XDL_MERGE_DIFF3),
+               OPT_SET_INT(0, "ours", &favor, "for conflicts, use our version",
+                           XDL_MERGE_FAVOR_OURS),
+               OPT_SET_INT(0, "theirs", &favor, "for conflicts, use their version",
+                           XDL_MERGE_FAVOR_THEIRS),
                OPT__QUIET(&quiet),
                OPT_CALLBACK('L', NULL, names, "name",
                             "set labels for file1/orig_file/file2", &label_cb),
@@ -45,7 +50,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
                /* Read the configuration file */
                git_config(git_xmerge_config, NULL);
                if (0 <= git_xmerge_style)
-                       merge_style = git_xmerge_style;
+                       style = git_xmerge_style;
        }
 
        argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
@@ -68,7 +73,7 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
        }
 
        ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2],
-                       &xpp, merge_level | merge_style, &result);
+                       &xpp, XDL_MERGE_FLAGS(level, style, favor), &result);
 
        for (i = 0; i < 3; i++)
                free(mmfs[i].ptr);
index 4da052a3fff6768fca05b2f3399c5d87ec1daa2e..8a0efed3139fdf2f9dccbc0163e2943d66460a4d 100644 (file)
@@ -58,6 +58,12 @@ extern "C" {
 #define XDL_MERGE_ZEALOUS_ALNUM 3
 #define XDL_MERGE_LEVEL_MASK 0x0f
 
+/* merge favor modes */
+#define XDL_MERGE_FAVOR_OURS 1
+#define XDL_MERGE_FAVOR_THEIRS 2
+#define XDL_MERGE_FAVOR(flags) (((flags)>>4) & 3)
+#define XDL_MERGE_FLAGS(level, style, favor) ((level)|(style)|((favor)<<4))
+
 /* merge output styles */
 #define XDL_MERGE_DIFF3 0x8000
 #define XDL_MERGE_STYLE_MASK 0x8000
@@ -110,7 +116,7 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 
 int xdl_merge(mmfile_t *orig, mmfile_t *mf1, const char *name1,
                mmfile_t *mf2, const char *name2,
-               xpparam_t const *xpp, int level, mmbuffer_t *result);
+               xpparam_t const *xpp, int flags, mmbuffer_t *result);
 
 #ifdef __cplusplus
 }
index 1cb65a95166a8cb60af590118f59d78aa4d24b74..b2ddc75376f530a07cb951bc8314c4a635517af5 100644 (file)
@@ -214,11 +214,15 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
 
 static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
                                 xdfenv_t *xe2, const char *name2,
+                                int favor,
                                 xdmerge_t *m, char *dest, int style)
 {
        int size, i;
 
        for (size = i = 0; m; m = m->next) {
+               if (favor && !m->mode)
+                       m->mode = favor;
+
                if (m->mode == 0)
                        size = fill_conflict_hunk(xe1, name1, xe2, name2,
                                                  size, i, style, m, dest);
@@ -391,6 +395,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        int i0, i1, i2, chg0, chg1, chg2;
        int level = flags & XDL_MERGE_LEVEL_MASK;
        int style = flags & XDL_MERGE_STYLE_MASK;
+       int favor = XDL_MERGE_FAVOR(flags);
 
        if (style == XDL_MERGE_DIFF3) {
                /*
@@ -523,14 +528,14 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
        /* output */
        if (result) {
                int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
-                       changes, NULL, style);
+                       favor, changes, NULL, style);
                result->ptr = xdl_malloc(size);
                if (!result->ptr) {
                        xdl_cleanup_merge(changes);
                        return -1;
                }
                result->size = size;
-               xdl_fill_merge_buffer(xe1, name1, xe2, name2, changes,
+               xdl_fill_merge_buffer(xe1, name1, xe2, name2, favor, changes,
                                      result->ptr, style);
        }
        return xdl_cleanup_merge(changes);