Skip to content

Commit 2125656

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 954b915 commit 2125656

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
@@ -981,9 +981,9 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
981981
{
982982
int ret = 0;
983983
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
984-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
985984
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
986985
struct index_state istate = { NULL };
986+
char *old_index_env = NULL, *old_repo_index_file;
987987

988988
remove_path(stash_index_path.buf);
989989

@@ -997,16 +997,19 @@ static int stash_patch(struct stash_info *info, struct pathspec ps,
997997
}
998998

999999
/* Find out what the user wants. */
1000-
cp_add_i.git_cmd = 1;
1001-
argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1002-
"--", NULL);
1003-
add_pathspecs(&cp_add_i.args, ps);
1004-
argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1005-
stash_index_path.buf);
1006-
if (run_command(&cp_add_i)) {
1007-
ret = -1;
1008-
goto done;
1009-
}
1000+
old_repo_index_file = the_repository->index_file;
1001+
the_repository->index_file = stash_index_path.buf;
1002+
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1003+
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1004+
1005+
ret = run_add_interactive(NULL, "--patch=stash", &ps);
1006+
1007+
the_repository->index_file = old_repo_index_file;
1008+
if (old_index_env && *old_index_env)
1009+
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1010+
else
1011+
unsetenv(INDEX_ENVIRONMENT);
1012+
FREE_AND_NULL(old_index_env);
10101013

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

0 commit comments

Comments
 (0)