Skip to content

Commit 1719b5e

Browse files
Miklos Vajnagitster
authored andcommitted
builtin-merge: give a proper error message for invalid strategies in config
'git merge -s foobar' diagnosed invalid "foobar" strategy and errored out with a message, but foobar in pull.twohead or pull.octopus was just silently ignored. This makes invalid strategy both on the command line and in the configuration file to trigger the same error. Signed-off-by: Miklos Vajna <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac2e28c commit 1719b5e

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

builtin-merge.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ static int option_parse_message(const struct option *opt,
7777
static struct strategy *get_strategy(const char *name)
7878
{
7979
int i;
80+
struct strbuf err;
8081

8182
if (!name)
8283
return NULL;
8384

8485
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
8586
if (!strcmp(name, all_strategy[i].name))
8687
return &all_strategy[i];
87-
return NULL;
88+
89+
strbuf_init(&err, 0);
90+
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
91+
strbuf_addf(&err, " %s", all_strategy[i].name);
92+
fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
93+
fprintf(stderr, "Available strategies are:%s.\n", err.buf);
94+
exit(1);
8895
}
8996

9097
static void append_strategy(struct strategy *s)
@@ -96,25 +103,10 @@ static void append_strategy(struct strategy *s)
96103
static int option_parse_strategy(const struct option *opt,
97104
const char *name, int unset)
98105
{
99-
int i;
100-
struct strategy *s;
101-
102106
if (unset)
103107
return 0;
104108

105-
s = get_strategy(name);
106-
107-
if (s)
108-
append_strategy(s);
109-
else {
110-
struct strbuf err;
111-
strbuf_init(&err, 0);
112-
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
113-
strbuf_addf(&err, " %s", all_strategy[i].name);
114-
fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
115-
fprintf(stderr, "Available strategies are:%s.\n", err.buf);
116-
exit(1);
117-
}
109+
append_strategy(get_strategy(name));
118110
return 0;
119111
}
120112

@@ -643,14 +635,9 @@ static void add_strategies(const char *string, unsigned attr)
643635

644636
memset(&list, 0, sizeof(list));
645637
split_merge_strategies(string, &list, &list_nr, &list_alloc);
646-
if (list != NULL) {
647-
for (i = 0; i < list_nr; i++) {
648-
struct strategy *s;
649-
650-
s = get_strategy(list[i].name);
651-
if (s)
652-
append_strategy(s);
653-
}
638+
if (list) {
639+
for (i = 0; i < list_nr; i++)
640+
append_strategy(get_strategy(list[i].name));
654641
return;
655642
}
656643
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)

t/t7601-merge-pull-config.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ test_expect_success 'merge picks up the best result' '
126126
test $auto_count != $resolve_count
127127
'
128128

129+
test_expect_success 'merge errors out on invalid strategy' '
130+
git config pull.twohead "foobar" &&
131+
git reset --hard c5 &&
132+
test_must_fail git merge c6
133+
'
134+
135+
test_expect_success 'merge errors out on invalid strategy' '
136+
git config --unset-all pull.twohead &&
137+
git reset --hard c5 &&
138+
test_must_fail git merge -s "resolve recursive" c6
139+
'
140+
129141
test_done

0 commit comments

Comments
 (0)