Skip to content

Commit 40c0030

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 the internal API directly, which will now incidentally also respect the `add.interactive.useBuiltin` setting. Let's just do this. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cddc2cb commit 40c0030

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
@@ -999,9 +999,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
999999
{
10001000
int ret = 0;
10011001
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1002-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
10031002
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
10041003
struct index_state istate = { NULL };
1004+
char *old_index_env = NULL, *old_repo_index_file;
10051005

10061006
remove_path(stash_index_path.buf);
10071007

@@ -1015,16 +1015,19 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
10151015
}
10161016

10171017
/* Find out what the user wants. */
1018-
cp_add_i.git_cmd = 1;
1019-
argv_array_pushl(&cp_add_i.args, "add--interactive", "--patch=stash",
1020-
"--", NULL);
1021-
add_pathspecs(&cp_add_i.args, ps);
1022-
argv_array_pushf(&cp_add_i.env_array, "GIT_INDEX_FILE=%s",
1023-
stash_index_path.buf);
1024-
if (run_command(&cp_add_i)) {
1025-
ret = -1;
1026-
goto done;
1027-
}
1018+
old_repo_index_file = the_repository->index_file;
1019+
the_repository->index_file = stash_index_path.buf;
1020+
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
1021+
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
1022+
1023+
ret = run_add_interactive(NULL, "--patch=stash", ps);
1024+
1025+
the_repository->index_file = old_repo_index_file;
1026+
if (old_index_env && *old_index_env)
1027+
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
1028+
else
1029+
unsetenv(INDEX_ENVIRONMENT);
1030+
FREE_AND_NULL(old_index_env);
10281031

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

0 commit comments

Comments
 (0)