config: Introduce diff.algorithm variable

Some users or projects prefer different algorithms over others, e.g. patience over myers or similar. However, specifying appropriate argument every time diff is to be used is impractical. Moreover, creating an alias doesn't play nicely with other tools based on diff (git-show for instance). Hence, a configuration variable which is able to set specific algorithm is needed. For now, these four values are accepted: 'myers' (which has the same effect as not setting the config variable at all), 'minimal', 'patience' and 'histogram'. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michal Privoznik committed Jan 16, 2013 at 08:51 UTC 07ab4dec80f1c24660ed4bc371849fb4f11a4ee3
3 files changed +42
Documentation/diff-config.txt
+17
@@ -155,3 +155,20 @@ diff.tool::
155 "kompare". Any other value is treated as a custom diff tool,
156 and there must be a corresponding `difftool.<tool>.cmd`
157 option.
158 +
159 +diff.algorithm::
160 + Choose a diff algorithm. The variants are as follows:
161 ++
162 +--
163 +`default`, `myers`;;
164 + The basic greedy diff algorithm. Currently, this is the default.
165 +`minimal`;;
166 + Spend extra time to make sure the smallest possible diff is
167 + produced.
168 +`patience`;;
169 + Use "patience diff" algorithm when generating patches.
170 +`histogram`;;
171 + This algorithm extends the patience algorithm to "support
172 + low-occurrence common elements".
173 +--
174 ++
contrib/completion/git-completion.bash
+1
@@ -1839,6 +1839,7 @@ _git_config ()
1839 diff.suppressBlankEmpty
1840 diff.tool
1841 diff.wordRegex
1842 + diff.algorithm
1843 difftool.
1844 difftool.prompt
1845 fetch.recurseSubmodules
diff.c
+24
@@ -36,6 +36,7 @@ static int diff_no_prefix;
36 static int diff_stat_graph_width;
37 static int diff_dirstat_permille_default = 30;
38 static struct diff_options default_diff_options;
39 +static long diff_algorithm;
40
41 static char diff_colors[][COLOR_MAXLEN] = {
42 GIT_COLOR_RESET,
@@ -143,6 +144,21 @@ static int git_config_rename(const char *var, const char *value)
144 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
145 }
146
147 +static long parse_algorithm_value(const char *value)
148 +{
149 + if (!value)
150 + return -1;
151 + else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
152 + return 0;
153 + else if (!strcasecmp(value, "minimal"))
154 + return XDF_NEED_MINIMAL;
155 + else if (!strcasecmp(value, "patience"))
156 + return XDF_PATIENCE_DIFF;
157 + else if (!strcasecmp(value, "histogram"))
158 + return XDF_HISTOGRAM_DIFF;
159 + return -1;
160 +}
161 +
162 /*
163 * These are to give UI layer defaults.
164 * The core-level commands such as git-diff-files should
@@ -196,6 +212,13 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
212 return 0;
213 }
214
215 + if (!strcmp(var, "diff.algorithm")) {
216 + diff_algorithm = parse_algorithm_value(value);
217 + if (diff_algorithm < 0)
218 + return -1;
219 + return 0;
220 + }
221 +
222 if (git_color_config(var, value, cb) < 0)
223 return -1;
224
@@ -3213,6 +3236,7 @@ void diff_setup(struct diff_options *options)
3236 options->add_remove = diff_addremove;
3237 options->use_color = diff_use_color_default;
3238 options->detect_rename = diff_detect_rename_default;
3239 + options->xdl_opts |= diff_algorithm;
3240
3241 if (diff_no_prefix) {
3242 options->a_prefix = options->b_prefix = "";