Skip to content

Commit ae969c4

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 3d1dc5f commit ae969c4

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
@@ -993,9 +993,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
993993
{
994994
int ret = 0;
995995
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
996-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
997996
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
998997
struct index_state istate = { NULL };
998+
char *old_index_env = NULL, *old_repo_index_file;
999999

10001000
remove_path(stash_index_path.buf);
10011001

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

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

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

0 commit comments

Comments
 (0)