mirror of
https://github.com/git-for-windows/git.git
synced 2026-05-03 02:27:35 -05:00
built-in add -p: support interactive.diffFilter
The Perl version supports post-processing the colored diff (that is generated in addition to the uncolored diff, intended to offer a prettier user experience) by a command configured via that config setting, and now the built-in version does that, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
@@ -56,6 +56,10 @@ int init_add_i_state(struct repository *r, struct add_i_state *s)
|
||||
strlcpy(s->file_new_color,
|
||||
diff_get_color(s->use_color, DIFF_FILE_NEW), COLOR_MAXLEN);
|
||||
|
||||
free(s->interactive_diff_filter);
|
||||
if (git_config_get_string("interactive.difffilter",
|
||||
&s->interactive_diff_filter))
|
||||
s->interactive_diff_filter = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ struct add_i_state {
|
||||
char context_color[COLOR_MAXLEN];
|
||||
char file_old_color[COLOR_MAXLEN];
|
||||
char file_new_color[COLOR_MAXLEN];
|
||||
|
||||
char *interactive_diff_filter;
|
||||
};
|
||||
|
||||
int init_add_i_state(struct repository *r, struct add_i_state *s);
|
||||
@@ -27,6 +29,7 @@ enum color_add_i {
|
||||
COLOR_RESET,
|
||||
};
|
||||
const char *get_add_i_color(enum color_add_i ix);
|
||||
const char *get_interactive_diff_filter(void);
|
||||
|
||||
struct repository;
|
||||
struct pathspec;
|
||||
|
||||
31
add-patch.c
31
add-patch.c
@@ -348,6 +348,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
||||
|
||||
if (want_color_fd(1, -1)) {
|
||||
struct child_process colored_cp = CHILD_PROCESS_INIT;
|
||||
const char *diff_filter = s->s.interactive_diff_filter;
|
||||
|
||||
setup_child_process(&colored_cp, s, NULL);
|
||||
xsnprintf((char *)args.argv[color_arg_index], 8, "--color");
|
||||
@@ -357,6 +358,24 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
||||
argv_array_clear(&args);
|
||||
if (res)
|
||||
return error(_("could not parse colored diff"));
|
||||
|
||||
if (diff_filter) {
|
||||
struct child_process filter_cp = CHILD_PROCESS_INIT;
|
||||
|
||||
setup_child_process(&filter_cp, s,
|
||||
diff_filter, NULL);
|
||||
filter_cp.git_cmd = 0;
|
||||
filter_cp.use_shell = 1;
|
||||
strbuf_reset(&s->buf);
|
||||
if (pipe_command(&filter_cp,
|
||||
colored->buf, colored->len,
|
||||
&s->buf, colored->len,
|
||||
NULL, 0) < 0)
|
||||
return error(_("failed to run '%s'"),
|
||||
diff_filter);
|
||||
strbuf_swap(colored, &s->buf);
|
||||
}
|
||||
|
||||
strbuf_complete_line(colored);
|
||||
colored_p = colored->buf;
|
||||
colored_pend = colored_p + colored->len;
|
||||
@@ -458,6 +477,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
||||
colored_pend - colored_p);
|
||||
if (colored_eol)
|
||||
colored_p = colored_eol + 1;
|
||||
else if (p != pend)
|
||||
/* colored shorter than non-colored? */
|
||||
goto mismatched_output;
|
||||
else
|
||||
colored_p = colored_pend;
|
||||
|
||||
@@ -479,6 +501,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
|
||||
*/
|
||||
hunk->splittable_into++;
|
||||
|
||||
/* non-colored shorter than colored? */
|
||||
if (colored_p != colored_pend) {
|
||||
mismatched_output:
|
||||
error(_("mismatched output from interactive.diffFilter"));
|
||||
advise(_("Your filter must maintain a one-to-one correspondence\n"
|
||||
"between its input and output lines."));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user