Skip to content

Commit 84e82c2

Browse files
committed
stash -p: respect the add.interactive.usebuiltin setting
As `git add` traditionally did not expose the `--patch=<mode>` modes via command-line options, `git stash` had to call `git add--interactive` directly. But this prevents the built-in `add -p` from kicking in, as `add--interactive` is the Perl script. So let's introduce support for an optional `<mode>` argument in `git add --patch[=<mode>]`, and use that in `git stash -p`, so that the built-in interactive add can do its job if configured. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 11766f6 commit 84e82c2

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

builtin/add.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static const char * const builtin_add_usage[] = {
2626
N_("git add [<options>] [--] <pathspec>..."),
2727
NULL
2828
};
29-
static int patch_interactive, add_interactive, edit_interactive;
29+
static const char *patch_interactive;
30+
static int add_interactive, edit_interactive;
3031
static int take_worktree_changes;
3132
static int add_renormalize;
3233

@@ -224,19 +225,25 @@ int run_add_interactive(const char *revision, const char *patch_mode,
224225
return status;
225226
}
226227

227-
int interactive_add(int argc, const char **argv, const char *prefix, int patch)
228+
int interactive_add(int argc, const char **argv, const char *prefix,
229+
const char *patch_mode)
228230
{
229231
struct pathspec pathspec;
232+
char buffer[64];
230233

231234
parse_pathspec(&pathspec, 0,
232235
PATHSPEC_PREFER_FULL |
233236
PATHSPEC_SYMLINK_LEADING_PATH |
234237
PATHSPEC_PREFIX_ORIGIN,
235238
prefix, argv);
236239

237-
return run_add_interactive(NULL,
238-
patch ? "--patch" : NULL,
239-
&pathspec);
240+
if (patch_mode) {
241+
xsnprintf(buffer, sizeof(buffer), "--patch%s%s",
242+
*patch_mode ? "=" : "", patch_mode);
243+
patch_mode = buffer;
244+
}
245+
246+
return run_add_interactive(NULL, patch_mode, &pathspec);
240247
}
241248

242249
static int edit_patch(int argc, const char **argv, const char *prefix)
@@ -314,7 +321,9 @@ static struct option builtin_add_options[] = {
314321
OPT__VERBOSE(&verbose, N_("be verbose")),
315322
OPT_GROUP(""),
316323
OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
317-
OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
324+
{ OPTION_STRING, 'p', "patch", &patch_interactive, N_("patch-mode"),
325+
N_("select hunks interactively"), PARSE_OPT_OPTARG, NULL,
326+
(intptr_t) "" },
318327
OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
319328
OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
320329
OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
355355
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
356356
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
357357

358-
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
358+
if (interactive_add(argc, argv, prefix,
359+
patch_interactive ? "" : NULL) != 0)
359360
die(_("interactive add failed"));
360361

361362
if (old_index_env && *old_index_env)

commit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ extern int delayed_reachability_test(struct shallow_info *si, int c);
284284
extern void prune_shallow(unsigned options);
285285
extern struct trace_key trace_shallow;
286286

287-
extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);
287+
extern int interactive_add(int argc, const char **argv, const char *prefix,
288+
const char *patch_mode);
288289
extern int run_add_interactive(const char *revision, const char *patch_mode,
289290
const struct pathspec *pathspec);
290291

git-stash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ create_stash () {
181181

182182
# find out what the user wants
183183
GIT_INDEX_FILE="$TMP-index" \
184-
git add--interactive --patch=stash -- "$@" &&
184+
git add --patch=stash -- "$@" &&
185185

186186
# state of the working tree
187187
w_tree=$(GIT_INDEX_FILE="$TMP-index" git write-tree) ||

0 commit comments

Comments
 (0)