Skip to content

Commit f46bcf1

Browse files
committed
built-in add -p: implement the "stash" and "reset" patch modes
The `git stash` and `git reset` commands support a `--patch` option, and both simply hand off to `git add -p` to perform that work. Let's teach the built-in version of `git add -p` do perform that work, too. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 84e82c2 commit f46bcf1

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

add-interactive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int run_add_i(struct repository *r, const struct pathspec *ps);
3434

3535
enum add_p_mode {
3636
ADD_P_STAGE,
37+
ADD_P_STASH,
38+
ADD_P_RESET,
3739
};
3840

3941
int run_add_p(struct repository *r, enum add_p_mode mode,

add-patch.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,72 @@ static struct patch_mode patch_mode_stage = {
4040
"the file\n")
4141
};
4242

43+
static struct patch_mode patch_mode_stash = {
44+
.diff = { "diff-index", "HEAD", NULL },
45+
.apply = { "--cached", NULL },
46+
.apply_check = { "--cached", NULL },
47+
.is_reverse = 0,
48+
.prompt_mode = {
49+
N_("Stash mode change [y,n,q,a,d%s,?]? "),
50+
N_("Stash deletion [y,n,q,a,d%s,?]? "),
51+
N_("Stash this hunk [y,n,q,a,d%s,?]? "),
52+
},
53+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
54+
"will immediately be marked for stashing."),
55+
.help_patch_text =
56+
N_("y - stash this hunk\n"
57+
"n - do not stash this hunk\n"
58+
"q - quit; do not stash this hunk or any of the remaining "
59+
"ones\n"
60+
"a - stash this hunk and all later hunks in the file\n"
61+
"d - do not stash this hunk or any of the later hunks in "
62+
"the file\n"),
63+
};
64+
65+
static struct patch_mode patch_mode_reset_head = {
66+
.diff = { "diff-index", "--cached", NULL },
67+
.apply = { "-R", "--cached", NULL },
68+
.apply_check = { "-R", "--cached", NULL },
69+
.is_reverse = 1,
70+
.prompt_mode = {
71+
N_("Unstage mode change [y,n,q,a,d%s,?]? "),
72+
N_("Unstage deletion [y,n,q,a,d%s,?]? "),
73+
N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
74+
},
75+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
76+
"will immediately be marked for unstaging."),
77+
.help_patch_text =
78+
N_("y - unstage this hunk\n"
79+
"n - do not unstage this hunk\n"
80+
"q - quit; do not unstage this hunk or any of the remaining "
81+
"ones\n"
82+
"a - unstage this hunk and all later hunks in the file\n"
83+
"d - do not unstage this hunk or any of the later hunks in "
84+
"the file\n"),
85+
};
86+
87+
static struct patch_mode patch_mode_reset_nothead = {
88+
.diff = { "diff-index", "-R", "--cached", NULL },
89+
.apply = { "--cached", NULL },
90+
.apply_check = { "--cached", NULL },
91+
.is_reverse = 0,
92+
.prompt_mode = {
93+
N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
94+
N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
95+
N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
96+
},
97+
.edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
98+
"will immediately be marked for applying."),
99+
.help_patch_text =
100+
N_("y - apply this hunk to index\n"
101+
"n - do not apply this hunk to index\n"
102+
"q - quit; do not apply this hunk or any of the remaining "
103+
"ones\n"
104+
"a - apply this hunk and all later hunks in the file\n"
105+
"d - do not apply this hunk or any of the later hunks in "
106+
"the file\n"),
107+
};
108+
43109
struct hunk_header {
44110
unsigned long old_offset, old_count, new_offset, new_count;
45111
/*
@@ -1284,7 +1350,15 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
12841350
if (init_add_i_state(r, &s.s))
12851351
return error("Could not read `add -i` config");
12861352

1287-
s.mode = &patch_mode_stage;
1353+
if (mode == ADD_P_STASH)
1354+
s.mode = &patch_mode_stash;
1355+
else if (mode == ADD_P_RESET) {
1356+
if (!revision || !strcmp(revision, "HEAD"))
1357+
s.mode = &patch_mode_reset_head;
1358+
else
1359+
s.mode = &patch_mode_reset_nothead;
1360+
} else
1361+
s.mode = &patch_mode_stage;
12881362
s.revision = revision;
12891363

12901364
if (repo_refresh_and_write_index(r, REFRESH_QUIET, 0) < 0 ||

builtin/add.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ int run_add_interactive(const char *revision, const char *patch_mode,
203203

204204
if (!strcmp(patch_mode, "--patch"))
205205
mode = ADD_P_STAGE;
206+
else if (!strcmp(patch_mode, "--patch=stash"))
207+
mode = ADD_P_STASH;
208+
else if (!strcmp(patch_mode, "--patch=reset"))
209+
mode = ADD_P_RESET;
206210
else
207211
die("'%s' not yet supported in the built-in add -p",
208212
patch_mode);

0 commit comments

Comments
 (0)