Skip to content

Commit 07ab4de

Browse files
zippy2gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 216120a commit 07ab4de

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Documentation/diff-config.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,20 @@ diff.tool::
155155
"kompare". Any other value is treated as a custom diff tool,
156156
and there must be a corresponding `difftool.<tool>.cmd`
157157
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,7 @@ _git_config ()
18391839
diff.suppressBlankEmpty
18401840
diff.tool
18411841
diff.wordRegex
1842+
diff.algorithm
18421843
difftool.
18431844
difftool.prompt
18441845
fetch.recurseSubmodules

diff.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static int diff_no_prefix;
3636
static int diff_stat_graph_width;
3737
static int diff_dirstat_permille_default = 30;
3838
static struct diff_options default_diff_options;
39+
static long diff_algorithm;
3940

4041
static char diff_colors[][COLOR_MAXLEN] = {
4142
GIT_COLOR_RESET,
@@ -143,6 +144,21 @@ static int git_config_rename(const char *var, const char *value)
143144
return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
144145
}
145146

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+
146162
/*
147163
* These are to give UI layer defaults.
148164
* 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)
196212
return 0;
197213
}
198214

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+
199222
if (git_color_config(var, value, cb) < 0)
200223
return -1;
201224

@@ -3213,6 +3236,7 @@ void diff_setup(struct diff_options *options)
32133236
options->add_remove = diff_addremove;
32143237
options->use_color = diff_use_color_default;
32153238
options->detect_rename = diff_detect_rename_default;
3239+
options->xdl_opts |= diff_algorithm;
32163240

32173241
if (diff_no_prefix) {
32183242
options->a_prefix = options->b_prefix = "";

0 commit comments

Comments
 (0)