Merge remote-tracking branch 'dscho/add-p' into add-p-g4w

Let's test this for a while.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2019-04-03 12:15:02 +02:00
22 changed files with 3452 additions and 64 deletions

View File

@@ -5,3 +5,8 @@ add.ignore-errors (deprecated)::
option of linkgit:git-add[1]. `add.ignore-errors` is deprecated,
as it does not follow the usual naming convention for configuration
variables.
add.interactive.useBuiltin::
[EXPERIMENTAL] Set to `true` to use the experimental built-in
implementation of the interactive version of linkgit:git-add[1]
instead of the Perl script version. Is `false` by default.

View File

@@ -823,6 +823,8 @@ LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentat
-name '*.h' -print)))
LIB_OBJS += abspath.o
LIB_OBJS += add-interactive.o
LIB_OBJS += add-patch.o
LIB_OBJS += advice.o
LIB_OBJS += alias.o
LIB_OBJS += alloc.o

1173
add-interactive.c Normal file

File diff suppressed because it is too large Load Diff

53
add-interactive.h Normal file
View File

@@ -0,0 +1,53 @@
#ifndef ADD_INTERACTIVE_H
#define ADD_INTERACTIVE_H
#include "color.h"
struct add_i_state {
struct repository *r;
int use_color;
char header_color[COLOR_MAXLEN];
char help_color[COLOR_MAXLEN];
char prompt_color[COLOR_MAXLEN];
char error_color[COLOR_MAXLEN];
char reset_color[COLOR_MAXLEN];
char fraginfo_color[COLOR_MAXLEN];
char context_color[COLOR_MAXLEN];
char file_old_color[COLOR_MAXLEN];
char file_new_color[COLOR_MAXLEN];
int use_single_key;
char *interactive_diff_filter, *interactive_diff_algorithm;
};
void init_add_i_state(struct add_i_state *s, struct repository *r);
void clear_add_i_state(struct add_i_state *s);
enum color_add_i {
COLOR_HEADER = 0,
COLOR_HELP,
COLOR_PROMPT,
COLOR_ERROR,
COLOR_RESET,
};
const char *get_add_i_color(enum color_add_i ix);
const char *get_interactive_diff_filter(void);
const char *get_interactive_diff_algorithm(void);
int get_interactive_use_single_key(void);
struct repository;
struct pathspec;
int run_add_i(struct repository *r, const struct pathspec *ps);
enum add_p_mode {
ADD_P_STAGE,
ADD_P_STASH,
ADD_P_RESET,
ADD_P_CHECKOUT,
ADD_P_WORKTREE,
};
int run_add_p(struct repository *r, enum add_p_mode mode,
const char *revision, const struct pathspec *ps);
#endif

1651
add-patch.c Normal file

File diff suppressed because it is too large Load Diff

10
apply.c
View File

@@ -2661,6 +2661,16 @@ static int find_pos(struct apply_state *state,
unsigned long backwards, forwards, current;
int backwards_lno, forwards_lno, current_lno;
/*
* When running with --allow-overlap, it is possible that a hunk is
* seen that pretends to start at the beginning (but no longer does),
* and that *still* needs to match the end. So trust `match_end` more
* than `match_beginning`.
*/
if (state->allow_overlap && match_beginning && match_end &&
img->nr - preimage->nr != 0)
match_beginning = 0;
/*
* If match_beginning or match_end is specified, there is no
* point starting from a wrong line that will never match and

View File

@@ -20,12 +20,14 @@
#include "bulk-checkin.h"
#include "argv-array.h"
#include "submodule.h"
#include "add-interactive.h"
static const char * const builtin_add_usage[] = {
N_("git add [<options>] [--] <pathspec>..."),
NULL
};
static int patch_interactive, add_interactive, edit_interactive;
static const char *patch_interactive;
static int add_interactive, edit_interactive;
static int take_worktree_changes;
static int add_renormalize;
@@ -180,11 +182,41 @@ static void refresh(int verbose, const struct pathspec *pathspec)
free(seen);
}
static int add_config(const char *var, const char *value, void *cb);
int run_add_interactive(const char *revision, const char *patch_mode,
const struct pathspec *pathspec)
{
int status, i;
struct argv_array argv = ARGV_ARRAY_INIT;
int use_builtin_add_i =
git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
if (use_builtin_add_i < 0)
git_config_get_bool("add.interactive.usebuiltin",
&use_builtin_add_i);
if (use_builtin_add_i == 1) {
enum add_p_mode mode;
if (!patch_mode)
return !!run_add_i(the_repository, pathspec);
if (!strcmp(patch_mode, "--patch"))
mode = ADD_P_STAGE;
else if (!strcmp(patch_mode, "--patch=stash"))
mode = ADD_P_STASH;
else if (!strcmp(patch_mode, "--patch=reset"))
mode = ADD_P_RESET;
else if (!strcmp(patch_mode, "--patch=checkout"))
mode = ADD_P_CHECKOUT;
else if (!strcmp(patch_mode, "--patch=worktree"))
mode = ADD_P_WORKTREE;
else
die("'%s' not supported", patch_mode);
return !!run_add_p(the_repository, mode, revision, pathspec);
}
argv_array_push(&argv, "add--interactive");
if (patch_mode)
@@ -201,9 +233,11 @@ int run_add_interactive(const char *revision, const char *patch_mode,
return status;
}
int interactive_add(int argc, const char **argv, const char *prefix, int patch)
int interactive_add(int argc, const char **argv, const char *prefix,
const char *patch_mode)
{
struct pathspec pathspec;
char buffer[64];
parse_pathspec(&pathspec, 0,
PATHSPEC_PREFER_FULL |
@@ -211,9 +245,13 @@ int interactive_add(int argc, const char **argv, const char *prefix, int patch)
PATHSPEC_PREFIX_ORIGIN,
prefix, argv);
return run_add_interactive(NULL,
patch ? "--patch" : NULL,
&pathspec);
if (patch_mode) {
xsnprintf(buffer, sizeof(buffer), "--patch%s%s",
*patch_mode ? "=" : "", patch_mode);
patch_mode = buffer;
}
return run_add_interactive(NULL, patch_mode, &pathspec);
}
static int edit_patch(int argc, const char **argv, const char *prefix)
@@ -291,7 +329,9 @@ static struct option builtin_add_options[] = {
OPT__VERBOSE(&verbose, N_("be verbose")),
OPT_GROUP(""),
OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
{ OPTION_STRING, 'p', "patch", &patch_interactive, N_("patch-mode"),
N_("select hunks interactively"), PARSE_OPT_OPTARG, NULL,
(intptr_t) "" },
OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
@@ -319,6 +359,7 @@ static int add_config(const char *var, const char *value, void *cb)
ignore_add_errors = git_config_bool(var, value);
return 0;
}
return git_default_config(var, value, cb);
}

View File

@@ -347,7 +347,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
die(_("index file corrupt"));
if (interactive) {
char *old_index_env = NULL;
char *old_index_env = NULL, *old_repo_index_file;
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
refresh_cache_or_die(refresh_flags);
@@ -355,12 +355,17 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
if (write_locked_index(&the_index, &index_lock, 0))
die(_("unable to create temporary index"));
old_repo_index_file = the_repository->index_file;
the_repository->index_file =
(char *)get_lock_file_path(&index_lock);
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
if (interactive_add(argc, argv, prefix,
patch_interactive ? "" : NULL) != 0)
die(_("interactive add failed"));
the_repository->index_file = old_repo_index_file;
if (old_index_env && *old_index_env)
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
else

View File

@@ -999,9 +999,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
{
int ret = 0;
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
struct child_process cp_add_i = CHILD_PROCESS_INIT;
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
struct index_state istate = { NULL };
char *old_index_env = NULL, *old_repo_index_file;
remove_path(stash_index_path.buf);
@@ -1015,16 +1015,19 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
}
/* Find out what the user wants. */
cp_add_i.git_cmd = 1;
argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
"--", NULL);
add_pathspecs(&cp_add_i.args, ps);
argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
stash_index_path.buf);
if (run_command(&cp_add_i)) {
ret = -1;
goto done;
}
old_repo_index_file = the_repository->index_file;
the_repository->index_file = stash_index_path.buf;
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
ret = run_add_interactive(NULL, "--patch=stash", ps);
the_repository->index_file = old_repo_index_file;
if (old_index_env && *old_index_env)
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
else
unsetenv(INDEX_ENVIRONMENT);
FREE_AND_NULL(old_index_env);
/* State of the working tree. */
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
@@ -1034,7 +1037,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
}
cp_diff_tree.git_cmd = 1;
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "HEAD",
argv_array_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
oid_to_hex(&info->w_tree), "--", NULL);
if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
ret = -1;

View File

@@ -20,6 +20,7 @@ linux-gcc)
export GIT_TEST_OE_DELTA_SIZE=5
export GIT_TEST_COMMIT_GRAPH=1
export GIT_TEST_MULTI_PACK_INDEX=1
export GIT_TEST_ADD_I_USE_BUILTIN=1
make test
;;
linux-gcc-4.8)

View File

@@ -295,7 +295,8 @@ int delayed_reachability_test(struct shallow_info *si, int c);
void prune_shallow(unsigned options);
extern struct trace_key trace_shallow;
int interactive_add(int argc, const char **argv, const char *prefix, int patch);
int interactive_add(int argc, const char **argv, const char *prefix,
const char *patch_mode);
int run_add_interactive(const char *revision, const char *patch_mode,
const struct pathspec *pathspec);

View File

@@ -2,6 +2,9 @@
#include "compat/terminal.h"
#include "sigchain.h"
#include "strbuf.h"
#include "run-command.h"
#include "string-list.h"
#include "hashmap.h"
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
@@ -32,7 +35,7 @@ static void restore_term(void)
term_fd = -1;
}
static int disable_echo(void)
static int disable_bits(tcflag_t bits)
{
struct termios t;
@@ -43,7 +46,7 @@ static int disable_echo(void)
old_term = t;
sigchain_push_common(restore_term_on_signal);
t.c_lflag &= ~ECHO;
t.c_lflag &= ~bits;
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
return 0;
@@ -53,17 +56,44 @@ error:
return -1;
}
static int disable_echo(void)
{
return disable_bits(ECHO);
}
static int enable_non_canonical(void)
{
return disable_bits(ICANON | ECHO);
}
#elif defined(GIT_WINDOWS_NATIVE)
#define INPUT_PATH "CONIN$"
#define OUTPUT_PATH "CONOUT$"
#define FORCE_TEXT "t"
static int use_stty = 1;
static struct string_list stty_restore = STRING_LIST_INIT_DUP;
static HANDLE hconin = INVALID_HANDLE_VALUE;
static DWORD cmode;
static void restore_term(void)
{
if (use_stty) {
int i;
struct child_process cp = CHILD_PROCESS_INIT;
if (stty_restore.nr == 0)
return;
argv_array_push(&cp.args, "stty");
for (i = 0; i < stty_restore.nr; i++)
argv_array_push(&cp.args, stty_restore.items[i].string);
run_command(&cp);
string_list_clear(&stty_restore, 0);
return;
}
if (hconin == INVALID_HANDLE_VALUE)
return;
@@ -72,8 +102,39 @@ static void restore_term(void)
hconin = INVALID_HANDLE_VALUE;
}
static int disable_echo(void)
static int disable_bits(DWORD bits)
{
if (use_stty) {
struct child_process cp = CHILD_PROCESS_INIT;
argv_array_push(&cp.args, "stty");
if (bits & ENABLE_LINE_INPUT) {
string_list_append(&stty_restore, "icanon");
argv_array_push(&cp.args, "-icanon");
}
if (bits & ENABLE_ECHO_INPUT) {
string_list_append(&stty_restore, "echo");
argv_array_push(&cp.args, "-echo");
}
if (bits & ENABLE_PROCESSED_INPUT) {
string_list_append(&stty_restore, "-ignbrk");
string_list_append(&stty_restore, "intr");
string_list_append(&stty_restore, "^c");
argv_array_push(&cp.args, "ignbrk");
argv_array_push(&cp.args, "intr");
argv_array_push(&cp.args, "");
}
if (run_command(&cp) == 0)
return 0;
/* `stty` could not be executed; access the Console directly */
use_stty = 0;
}
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
@@ -82,7 +143,7 @@ static int disable_echo(void)
GetConsoleMode(hconin, &cmode);
sigchain_push_common(restore_term_on_signal);
if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) {
if (!SetConsoleMode(hconin, cmode & ~bits)) {
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
return -1;
@@ -91,6 +152,47 @@ static int disable_echo(void)
return 0;
}
static int disable_echo(void)
{
return disable_bits(ENABLE_ECHO_INPUT);
}
static int enable_non_canonical(void)
{
return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
}
/*
* Override `getchar()`, as the default implementation does not use
* `ReadFile()`.
*
* This poses a problem when we want to see whether the standard
* input has more characters, as the default of Git for Windows is to start the
* Bash in a MinTTY, which uses a named pipe to emulate a pty, in which case
* our `poll()` emulation calls `PeekNamedPipe()`, which seems to require
* `ReadFile()` to be called first to work properly (it only reports 0
* available bytes, otherwise).
*
* So let's just override `getchar()` with a version backed by `ReadFile()` and
* go our merry ways from here.
*/
static int mingw_getchar(void)
{
DWORD read = 0;
unsigned char ch;
if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL))
return EOF;
if (!read) {
error("Unexpected 0 read");
return EOF;
}
return ch;
}
#define getchar mingw_getchar
#endif
#ifndef FORCE_TEXT
@@ -137,6 +239,126 @@ char *git_terminal_prompt(const char *prompt, int echo)
return buf.buf;
}
/*
* The `is_known_escape_sequence()` function returns 1 if the passed string
* corresponds to an Escape sequence that the terminal capabilities contains.
*
* To avoid depending on ncurses or other platform-specific libraries, we rely
* on the presence of the `infocmp` executable to do the job for us (failing
* silently if the program is not available or refused to run).
*/
struct escape_sequence_entry {
struct hashmap_entry entry;
char sequence[FLEX_ARRAY];
};
static int sequence_entry_cmp(const void *hashmap_cmp_fn_data,
const struct escape_sequence_entry *e1,
const struct escape_sequence_entry *e2,
const void *keydata)
{
return strcmp(e1->sequence, keydata ? keydata : e2->sequence);
}
static int is_known_escape_sequence(const char *sequence)
{
static struct hashmap sequences;
static int initialized;
if (!initialized) {
struct child_process cp = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT;
char *p, *eol;
hashmap_init(&sequences, (hashmap_cmp_fn)sequence_entry_cmp,
NULL, 0);
argv_array_pushl(&cp.args, "infocmp", "-L", "-1", NULL);
if (pipe_command(&cp, NULL, 0, &buf, 0, NULL, 0))
strbuf_setlen(&buf, 0);
for (eol = p = buf.buf; *p; p = eol + 1) {
p = strchr(p, '=');
if (!p)
break;
p++;
eol = strchrnul(p, '\n');
if (starts_with(p, "\\E")) {
char *comma = memchr(p, ',', eol - p);
struct escape_sequence_entry *e;
p[0] = '^';
p[1] = '[';
FLEX_ALLOC_MEM(e, sequence, p, comma - p);
hashmap_entry_init(&e->entry,
strhash(e->sequence));
hashmap_add(&sequences, &e->entry);
}
if (!*eol)
break;
}
initialized = 1;
}
return !!hashmap_get_from_hash(&sequences, strhash(sequence), sequence);
}
int read_key_without_echo(struct strbuf *buf)
{
static int warning_displayed;
int ch;
if (warning_displayed || enable_non_canonical() < 0) {
if (!warning_displayed) {
warning("reading single keystrokes not supported on "
"this platform; reading line instead");
warning_displayed = 1;
}
return strbuf_getline(buf, stdin);
}
strbuf_reset(buf);
ch = getchar();
if (ch == EOF) {
restore_term();
return EOF;
}
strbuf_addch(buf, ch);
if (ch == '\033' /* ESC */) {
/*
* We are most likely looking at an Escape sequence. Let's try
* to read more bytes, waiting at most half a second, assuming
* that the sequence is complete if we did not receive any byte
* within that time.
*
* Start by replacing the Escape byte with ^[ */
strbuf_splice(buf, buf->len - 1, 1, "^[", 2);
/*
* Query the terminal capabilities once about all the Escape
* sequences it knows about, so that we can avoid waiting for
* half a second when we know that the sequence is complete.
*/
while (!is_known_escape_sequence(buf->buf)) {
struct pollfd pfd = { .fd = 0, .events = POLLIN };
if (poll(&pfd, 1, 500) < 1)
break;
ch = getchar();
if (ch == EOF)
return 0;
strbuf_addch(buf, ch);
}
}
restore_term();
return 0;
}
#else
char *git_terminal_prompt(const char *prompt, int echo)
@@ -144,4 +366,23 @@ char *git_terminal_prompt(const char *prompt, int echo)
return getpass(prompt);
}
int read_key_without_echo(struct strbuf *buf)
{
static int warning_displayed;
const char *res;
if (!warning_displayed) {
warning("reading single keystrokes not supported on this "
"platform; reading line instead");
warning_displayed = 1;
}
res = getpass("");
strbuf_reset(buf);
if (!res)
return EOF;
strbuf_addstr(buf, res);
return 0;
}
#endif

View File

@@ -3,4 +3,7 @@
char *git_terminal_prompt(const char *prompt, int echo);
/* Read a single keystroke, without echoing it to the terminal */
int read_key_without_echo(struct strbuf *buf);
#endif /* COMPAT_TERMINAL_H */

39
diff.c
View File

@@ -2495,22 +2495,6 @@ static void pprint_rename(struct strbuf *name, const char *a, const char *b)
}
}
struct diffstat_t {
int nr;
int alloc;
struct diffstat_file {
char *from_name;
char *name;
char *print_name;
const char *comments;
unsigned is_unmerged:1;
unsigned is_binary:1;
unsigned is_renamed:1;
unsigned is_interesting:1;
uintmax_t added, deleted;
} **files;
};
static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
const char *name_a,
const char *name_b)
@@ -3157,7 +3141,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
gather_dirstat(options, &dir, changed, "", 0);
}
static void free_diffstat_info(struct diffstat_t *diffstat)
void free_diffstat_info(struct diffstat_t *diffstat)
{
int i;
for (i = 0; i < diffstat->nr; i++) {
@@ -6283,12 +6267,7 @@ void diff_flush(struct diff_options *options)
dirstat_by_line) {
struct diffstat_t diffstat;
memset(&diffstat, 0, sizeof(struct diffstat_t));
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if (check_pair_status(p))
diff_flush_stat(p, options, &diffstat);
}
compute_diffstat(options, &diffstat, q);
if (output_format & DIFF_FORMAT_NUMSTAT)
show_numstat(&diffstat, options);
if (output_format & DIFF_FORMAT_DIFFSTAT)
@@ -6621,6 +6600,20 @@ static int is_submodule_ignored(const char *path, struct diff_options *options)
return ignored;
}
void compute_diffstat(struct diff_options *options,
struct diffstat_t *diffstat,
struct diff_queue_struct *q)
{
int i;
memset(diffstat, 0, sizeof(struct diffstat_t));
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if (check_pair_status(p))
diff_flush_stat(p, options, diffstat);
}
}
void diff_addremove(struct diff_options *options,
int addremove, unsigned mode,
const struct object_id *oid,

20
diff.h
View File

@@ -245,6 +245,22 @@ void diff_emit_submodule_error(struct diff_options *o, const char *err);
void diff_emit_submodule_pipethrough(struct diff_options *o,
const char *line, int len);
struct diffstat_t {
int nr;
int alloc;
struct diffstat_file {
char *from_name;
char *name;
char *print_name;
const char *comments;
unsigned is_unmerged:1;
unsigned is_binary:1;
unsigned is_renamed:1;
unsigned is_interesting:1;
uintmax_t added, deleted;
} **files;
};
enum color_diff {
DIFF_RESET = 0,
DIFF_CONTEXT = 1,
@@ -334,6 +350,10 @@ void diff_change(struct diff_options *,
struct diff_filepair *diff_unmerge(struct diff_options *, const char *path);
void compute_diffstat(struct diff_options *options, struct diffstat_t *diffstat,
struct diff_queue_struct *q);
void free_diffstat_info(struct diffstat_t *diffstat);
#define DIFF_SETUP_REVERSE 1
#define DIFF_SETUP_USE_SIZE_CACHE 4

View File

@@ -177,7 +177,9 @@ sub run_cmd_pipe {
} else {
my $fh = undef;
open($fh, '-|', @_) or die;
return <$fh>;
my @out = <$fh>;
close $fh || die "Cannot close @_ ($!)";
return @out;
}
}
@@ -224,7 +226,7 @@ my $status_head = sprintf($status_fmt, __('staged'), __('unstaged'), __('path'))
sub get_empty_tree {
return $empty_tree if defined $empty_tree;
$empty_tree = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
($empty_tree) = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
chomp $empty_tree;
return $empty_tree;
}
@@ -1127,7 +1129,7 @@ aborted and the hunk is left unchanged.
EOF2
close $fh;
chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
chomp(my ($editor) = run_cmd_pipe(qw(git var GIT_EDITOR)));
system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
if ($? != 0) {

View File

@@ -206,13 +206,13 @@ create_stash () {
# find out what the user wants
GIT_INDEX_FILE="$TMP-index" \
git add--interactive --patch=stash -- "$@" &&
git add --patch=stash -- "$@" &&
# state of the working tree
w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||
die "$(gettext "Cannot save the current worktree state")"
git diff-tree -p HEAD $w_tree -- >"$TMP-patch" &&
git diff-tree -p -U1 HEAD $w_tree -- >"$TMP-patch" &&
test -s "$TMP-patch" ||
die "$(gettext "No changes selected")"

View File

@@ -1125,3 +1125,31 @@ int strbuf_normalize_path(struct strbuf *src)
strbuf_release(&dst);
return 0;
}
int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
const char *const *env)
{
char *path2 = NULL;
int fd, res = 0;
if (!is_absolute_path(path))
path = path2 = xstrdup(git_path("%s", path));
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
res = error_errno(_("could not open '%s' for writing"), path);
else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
res = error_errno(_("could not write to '%s'"), path);
close(fd);
} else if (close(fd) < 0)
res = error_errno(_("could not close '%s'"), path);
else {
strbuf_reset(buffer);
if (launch_editor(path, buffer, env) < 0)
res = error_errno(_("could not edit '%s'"), path);
unlink(path);
}
free(path2);
return res;
}

View File

@@ -621,6 +621,17 @@ int launch_editor(const char *path, struct strbuf *buffer,
int launch_sequence_editor(const char *path, struct strbuf *buffer,
const char *const *env);
/*
* In contrast to `launch_editor()`, this function writes out the contents
* of the specified file first, then clears the `buffer`, then launches
* the editor and reads back in the file contents into the `buffer`.
* Finally, it deletes the temporary file.
*
* If `path` is relative, it refers to a file in the `.git` directory.
*/
int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
const char *const *env);
void strbuf_add_lines(struct strbuf *sb,
const char *prefix,
const char *buf,

View File

@@ -397,6 +397,10 @@ GIT_TEST_STASH_USE_BUILTIN=<boolean>, when false, disables the
built-in version of git-stash. See 'stash.useBuiltin' in
git-config(1).
GIT_TEST_ADD_I_USE_BUILTIN=<boolean>, when true, enables the
built-in version of git add -i. See 'add.interactive.useBuiltin' in
git-config(1).
GIT_TEST_INDEX_THREADS=<n> enables exercising the multi-threaded loading
of the index for the whole test suite by bypassing the default number of
cache entries and thread minimums. Setting this to 1 will make the

View File

@@ -23,6 +23,17 @@ diff_cmp () {
test_cmp "$1.filtered" "$2.filtered"
}
# This function uses a trick to manipulate the interactive add to use color:
# the `want_color()` function special-cases the situation where a pager was
# spawned and Git now wants to output colored text: to detect that situation,
# the environment variable `GIT_PAGER_IN_USE` is set. However, color is
# suppressed despite that environment variable if the `TERM` variable
# indicates a dumb terminal, so we set that variable, too.
force_color () {
env GIT_PAGER_IN_USE=true TERM=vt100 "$@"
}
test_expect_success 'setup (initial)' '
echo content >file &&
git add file &&
@@ -94,7 +105,6 @@ test_expect_success 'revert works (commit)' '
grep "unchanged *+3/-0 file" output
'
test_expect_success 'setup expected' '
cat >expected <<-\EOF
EOF
@@ -263,6 +273,35 @@ test_expect_success FILEMODE 'stage mode and hunk' '
# end of tests disabled when filemode is not usable
test_expect_success 'different prompts for mode change/deleted' '
git reset --hard &&
>file &&
>deleted &&
git add --chmod=+x file deleted &&
echo changed >file &&
rm deleted &&
test_write_lines n n n |
git -c core.filemode=true add -p >actual &&
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
cat >expect <<-\EOF &&
(1/1) Stage deletion [y,n,q,a,d,?]?
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
EOF
test_cmp expect actual.filtered
'
test_expect_success 'correct message when there is nothing to do' '
git reset --hard &&
git add -p 2>err &&
test_i18ngrep "No changes" err &&
printf "\\0123" >binary &&
git add binary &&
printf "\\0abc" >binary &&
git add -p 2>err &&
test_i18ngrep "Only binary files changed" err
'
test_expect_success 'setup again' '
git reset --hard &&
test_chmod +x file &&
@@ -374,6 +413,36 @@ test_expect_success 'split hunk setup' '
test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
'
test_expect_success 'goto hunk' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
_ 2: -2,4 +3,8 +21
go to which hunk? @@ -1,2 +1,3 @@
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
EOF
test_write_lines s y g 1 | git add -p >actual &&
tail -n 7 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'navigate to hunk via regex' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
_10
+15
_20
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
EOF
test_write_lines s y /1,2 | git add -p >actual &&
tail -n 5 <actual >actual.trimmed &&
test_cmp expect actual.trimmed
'
test_expect_success 'split hunk "add -p (edit)"' '
# Split, say Edit and do nothing. Then:
#
@@ -403,6 +472,40 @@ test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
! grep "^+31" actual
'
test_expect_success 'split hunk with incomplete line at end' '
git reset --hard &&
printf "missing LF" >>test &&
git add test &&
test_write_lines before 10 20 30 40 50 60 70 >test &&
git grep --cached missing &&
test_write_lines s n y q | git add -p &&
test_must_fail git grep --cached missing &&
git grep before &&
test_must_fail git grep --cached before
'
test_expect_failure 'edit, adding lines to the first hunk' '
test_write_lines 10 11 20 30 40 50 51 60 >test &&
git reset &&
tr _ " " >patch <<-EOF &&
@@ -1,5 +1,6 @@
_10
+11
+12
_20
+21
+22
_30
EOF
# test sequence is s(plit), e(dit), n(o)
# q n q q is there to make sure we exit at the end.
printf "%s\n" s e n q n q q |
EDITOR=./fake_editor.sh git add -p 2>error &&
test_must_be_empty error &&
git diff --cached >actual &&
grep "^+22" actual
'
test_expect_success 'patch mode ignores unmerged entries' '
git reset --hard &&
test_commit conflict &&
@@ -429,35 +532,48 @@ test_expect_success 'patch mode ignores unmerged entries' '
diff_cmp expected diff
'
test_expect_success TTY 'diffs can be colorized' '
test_expect_success 'diffs can be colorized' '
git reset --hard &&
echo content >test &&
printf y | test_terminal git add -p >output 2>&1 &&
printf y >y &&
force_color git add -p >output 2>&1 <y &&
# We do not want to depend on the exact coloring scheme
# git uses for diffs, so just check that we saw some kind of color.
grep "$(printf "\\033")" output
'
test_expect_success TTY 'diffFilter filters diff' '
test_expect_success 'diffFilter filters diff' '
git reset --hard &&
echo content >test &&
test_config interactive.diffFilter "sed s/^/foo:/" &&
printf y | test_terminal git add -p >output 2>&1 &&
printf y >y &&
force_color git add -p >output 2>&1 <y &&
# avoid depending on the exact coloring or content of the prompts,
# and just make sure we saw our diff prefixed
grep foo:.*content output
'
test_expect_success TTY 'detect bogus diffFilter output' '
test_expect_success 'detect bogus diffFilter output' '
git reset --hard &&
echo content >test &&
test_config interactive.diffFilter "echo too-short" &&
printf y | test_must_fail test_terminal git add -p
printf y >y &&
test_must_fail force_color git add -p <y
'
test_expect_success 'diff.algorithm is passed to `git diff-files`' '
git reset --hard &&
>file &&
git add file &&
echo changed >file &&
test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
test_i18ngrep "error: option diff-algorithm accepts " err
'
test_expect_success 'patch-mode via -i prompts for files' '
@@ -647,4 +763,29 @@ test_expect_success 'checkout -p works with pathological context lines' '
test_write_lines a b a b a a b a b a >expect &&
test_cmp expect a
'
test_expect_success 'show help from add--helper' '
git reset --hard &&
cat >expect <<-EOF &&
<BOLD>*** Commands ***<RESET>
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
<BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
<BOLD;RED>update - add working tree state to the staged set of changes<RESET>
<BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
<BOLD;RED>patch - pick hunks and update selectively<RESET>
<BOLD;RED>diff - view diff between HEAD and index<RESET>
<BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
<BOLD>*** Commands ***<RESET>
1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
<BOLD;BLUE>What now<RESET>>$SP
Bye.
EOF
test_write_lines h | force_color git add -i >actual.colored &&
test_decode_color <actual.colored >actual &&
test_i18ncmp expect actual
'
test_done

View File

@@ -89,7 +89,7 @@ test_expect_success 'none of this moved HEAD' '
verify_saved_head
'
test_expect_failure 'stash -p with split hunk' '
test_expect_success 'stash -p with split hunk' '
git reset --hard &&
cat >test <<-\EOF &&
aaa
@@ -106,8 +106,8 @@ test_expect_failure 'stash -p with split hunk' '
ccc
EOF
printf "%s\n" s n y q |
test_might_fail git stash -p 2>error &&
! test_must_be_empty error &&
git stash -p 2>error &&
test_must_be_empty error &&
grep "added line 1" test &&
! grep "added line 2" test
'