Skip to content

Commit 90db07e

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 58fbd8e commit 90db07e

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
@@ -997,9 +997,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
997997
{
998998
int ret = 0;
999999
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
1000-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
10011000
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
10021001
struct index_state istate = { NULL };
1002+
char *old_index_env = NULL, *old_repo_index_file;
10031003

10041004
remove_path(stash_index_path.buf);
10051005

@@ -1013,16 +1013,19 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
10131013
}
10141014

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

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

0 commit comments

Comments
 (0)