Skip to content

Commit 2985f43

Browse files
committed
built-in add -p: handle diff.algorithm
The Perl version of `git add -p` reads the config setting `diff.algorithm` and if set, uses it to generate the diff using the specified algorithm. This patch ports that functionality to the C version. To make sure that this works as intended, we add a regression test case that tries to specify a bogus diff algorithm and then verifies that `git diff-files` produced the expected error message. Note: In that new test case, we actually ignore the exit code of `git add -p`. The reason is that the C version exits with failure (as one might expect), but the Perl version does not. In fact, the Perl version continues happily after the uncolored diff failed, trying to generate the colored diff, still not catching the problem, and then it pretends to have succeeded (with exit code 0). This is arguably a bug in the Perl version, and fixing it is safely outside the scope of this patch. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b18d702 commit 2985f43

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

add-interactive.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ int init_add_i_state(struct repository *r, struct add_i_state *s)
6161
&s->interactive_diff_filter))
6262
s->interactive_diff_filter = NULL;
6363

64+
free(s->interactive_diff_algorithm);
65+
if (git_config_get_string("diff.algorithm",
66+
&s->interactive_diff_algorithm))
67+
s->interactive_diff_algorithm = NULL;
68+
6469
return 0;
6570
}
6671

add-interactive.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct add_i_state {
1616
char file_old_color[COLOR_MAXLEN];
1717
char file_new_color[COLOR_MAXLEN];
1818

19-
char *interactive_diff_filter;
19+
char *interactive_diff_filter, *interactive_diff_algorithm;
2020
};
2121

2222
int init_add_i_state(struct repository *r, struct add_i_state *s);
@@ -30,6 +30,7 @@ enum color_add_i {
3030
};
3131
const char *get_add_i_color(enum color_add_i ix);
3232
const char *get_interactive_diff_filter(void);
33+
const char *get_interactive_diff_algorithm(void);
3334

3435
struct repository;
3536
struct pathspec;

add-patch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ static int is_octal(const char *p, size_t len)
310310
static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
311311
{
312312
struct argv_array args = ARGV_ARRAY_INIT;
313+
const char *diff_algorithm = s->s.interactive_diff_algorithm;
313314
struct strbuf *plain = &s->plain, *colored = NULL;
314315
struct child_process cp = CHILD_PROCESS_INIT;
315316
char *p, *pend, *colored_p = NULL, *colored_pend = NULL, marker = '\0';
@@ -319,6 +320,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
319320
int res;
320321

321322
argv_array_pushv(&args, s->mode->diff);
323+
if (diff_algorithm)
324+
argv_array_pushf(&args, "--diff-algorithm=%s", diff_algorithm);
322325
if (s->revision) {
323326
struct object_id oid;
324327
argv_array_push(&args,

0 commit comments

Comments
 (0)