apply: accept -3/--3way command line option

Begin teaching the three-way merge fallback logic "git am -3" uses to the underlying "git apply". It only implements the command line parsing part, and does not do anything interesting yet, other than making sure that "--reject" and "--3way" are not given together, and making "--3way" imply "--index". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 8, 2012 at 13:21 UTC cfb6f9acc392561bb339763564e21b684e50ce83
2 files changed +31 -2
builtin/apply.c
+23 -2
@@ -46,6 +46,7 @@ static int apply_with_reject;
46 static int apply_verbosely;
47 static int allow_overlap;
48 static int no_add;
49 +static int threeway;
50 static const char *fake_ancestor;
51 static int line_termination = '\n';
52 static unsigned int p_context = UINT_MAX;
@@ -3139,6 +3140,12 @@ static int load_preimage(struct image *image,
3140 return 0;
3141 }
3142
3143 +static int try_threeway(struct image *image, struct patch *patch,
3144 + struct stat *st, struct cache_entry *ce)
3145 +{
3146 + return -1; /* for now */
3147 +}
3148 +
3149 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
3150 {
3151 struct image image;
@@ -3146,8 +3153,11 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
3153 if (load_preimage(&image, patch, st, ce) < 0)
3154 return -1;
3155
3149 - if (apply_fragments(&image, patch) < 0)
3150 - return -1; /* note with --reject this succeeds. */
3156 + if (apply_fragments(&image, patch) < 0) {
3157 + /* Note: with --reject, apply_fragments() returns 0 */
3158 + if (!threeway || try_threeway(&image, patch, st, ce) < 0)
3159 + return -1;
3160 + }
3161 patch->result = image.buf;
3162 patch->resultsize = image.len;
3163 add_to_fn_table(patch);
@@ -4079,6 +4089,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
4089 "apply a patch without touching the working tree"),
4090 OPT_BOOLEAN(0, "apply", &force_apply,
4091 "also apply the patch (use with --stat/--summary/--check)"),
4092 + OPT_BOOL('3', "3way", &threeway,
4093 + "attempt three-way merge if a patch does not apply"),
4094 OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
4095 "build a temporary index based on embedded index information"),
4096 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
@@ -4127,6 +4139,15 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
4139 argc = parse_options(argc, argv, prefix, builtin_apply_options,
4140 apply_usage, 0);
4141
4142 + if (apply_with_reject && threeway)
4143 + die("--reject and --3way cannot be used together.");
4144 + if (cached && threeway)
4145 + die("--cached and --3way cannot be used together.");
4146 + if (threeway) {
4147 + if (is_not_gitdir)
4148 + die(_("--3way outside a repository"));
4149 + check_index = 1;
4150 + }
4151 if (apply_with_reject)
4152 apply = apply_verbosely = 1;
4153 if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
t/t4117-apply-reject.sh
+8
@@ -46,6 +46,14 @@ test_expect_success setup '
46 cat file1 >saved.file1
47 '
48
49 +test_expect_success 'apply --reject is incompatible with --3way' '
50 + test_when_finished "cat saved.file1 >file1" &&
51 + git diff >patch.0 &&
52 + git checkout file1 &&
53 + test_must_fail git apply --reject --3way patch.0 &&
54 + git diff --exit-code
55 +'
56 +
57 test_expect_success 'apply without --reject should fail' '
58
59 if git apply patch.1