Skip to content

Commit 37fff68

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 9835fd4 commit 37fff68

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
@@ -995,9 +995,9 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
995995
{
996996
int ret = 0;
997997
struct child_process cp_read_tree = CHILD_PROCESS_INIT;
998-
struct child_process cp_add_i = CHILD_PROCESS_INIT;
999998
struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
1000999
struct index_state istate = { NULL };
1000+
char *old_index_env = NULL, *old_repo_index_file;
10011001

10021002
remove_path(stash_index_path.buf);
10031003

@@ -1011,16 +1011,19 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
10111011
}
10121012

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

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

0 commit comments

Comments
 (0)