@matheusbsbs / matheus-bagliano / commits / 87091b495e
Fork 0

builtin-merge: allow using a custom strategy

Allow using a custom strategy, as long as it's named git-merge-foo. The error handling is now done using is_git_command(). The list of available strategies is now shown by list_commands(). If an invalid strategy is supplied, like -s foobar, then git-merge would list all git-merge-* commands. This is not perfect, since for example git-merge-index is not a valid strategy. These are removed from the output by scanning the list of main commands; if the git-merge-foo command is listed in the all_strategy list, then it's shown, otherwise excluded. This does not exclude commands somewhere else in the PATH, where custom strategies are expected. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Miklos Vajna committed Jul 30, 2008 at 01:16 UTC 87091b495e8af0daf317c0d0e08ac3ead74a0bb9
1 file changed +35 -7
builtin-merge.c
+35 -7
@@ -22,6 +22,7 @@
22 #include "log-tree.h"
23 #include "color.h"
24 #include "rerere.h"
25 +#include "help.h"
26
27 #define DEFAULT_TWOHEAD (1<<0)
28 #define DEFAULT_OCTOPUS (1<<1)
@@ -77,7 +78,9 @@ static int option_parse_message(const struct option *opt,
78 static struct strategy *get_strategy(const char *name)
79 {
80 int i;
80 - struct strbuf err;
81 + struct strategy *ret;
82 + static struct cmdnames main_cmds, other_cmds;
83 + static int longest;
84
85 if (!name)
86 return NULL;
@@ -86,12 +89,37 @@ static struct strategy *get_strategy(const char *name)
89 if (!strcmp(name, all_strategy[i].name))
90 return &all_strategy[i];
91
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);
92 + if (!longest) {
93 + struct cmdnames not_strategies;
94 +
95 + memset(&main_cmds, 0, sizeof(struct cmdnames));
96 + memset(&other_cmds, 0, sizeof(struct cmdnames));
97 + memset(&not_strategies, 0, sizeof(struct cmdnames));
98 + longest = load_command_list("git-merge-", &main_cmds,
99 + &other_cmds);
100 + for (i = 0; i < main_cmds.cnt; i++) {
101 + int j, found = 0;
102 + struct cmdname *ent = main_cmds.names[i];
103 + for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
104 + if (!strncmp(ent->name, all_strategy[j].name, ent->len)
105 + && !all_strategy[j].name[ent->len])
106 + found = 1;
107 + if (!found)
108 + add_cmdname(&not_strategies, ent->name, ent->len);
109 + exclude_cmds(&main_cmds, &not_strategies);
110 + }
111 + }
112 + if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
113 +
114 + fprintf(stderr, "Could not find merge strategy '%s'.\n\n", name);
115 + list_commands("strategies", longest, &main_cmds, &other_cmds);
116 + exit(1);
117 + }
118 +
119 + ret = xmalloc(sizeof(struct strategy));
120 + memset(ret, 0, sizeof(struct strategy));
121 + ret->name = xstrdup(name);
122 + return ret;
123 }
124
125 static void append_strategy(struct strategy *s)