Skip to content

Commit d1680df

Browse files
committed
built-in stash: use the built-in git add -p if so configured
The scripted version of `git stash` called directly into the Perl script `git-add--interactive.perl`, and this was faithfully converted to C. However, we have a much better way to do this now: call `git add --patch=<mode>`, which incidentally also respects the config setting `add.interactive.useBuiltin`. Let's do this. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ffc425e commit d1680df

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

builtin/stash.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
992992
{
993993
int ret = 0;
994994
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
995-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
996995
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
997996
struct index_state istate = { NULL };
997+
char *old_index_env = NULL, *old_repo_index_file;
998998

999999
remove_path(stash_index_path.buf);
10001000

@@ -1008,16 +1008,19 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
10081008
}
10091009

10101010
/* Find out what the user wants. */
1011-
cp_add_i.git_cmd = 1;
1012-
argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1013-
"--", NULL);
1014-
add_pathspecs(&cp_add_i.args, ps);
1015-
argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1016-
stash_index_path.buf);
1017-
if (run_command(&cp_add_i)) {
1018-
ret = -1;
1019-
goto done;
1020-
}
1011+
old_repo_index_file = the_repository->index_file;
1012+
the_repository->index_file = stash_index_path.buf;
1013+
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1014+
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1015+
1016+
ret = run_add_interactive(NULL, "--patch=stash", ps);
1017+
1018+
the_repository->index_file = old_repo_index_file;
1019+
if (old_index_env && *old_index_env)
1020+
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1021+
else
1022+
unsetenv(INDEX_ENVIRONMENT);
1023+
FREE_AND_NULL(old_index_env);
10211024

10221025
/* State of the working tree. */
10231026
if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,

0 commit comments

Comments
 (0)