summaryrefslogtreecommitdiff
path: root/add-patch.c
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2019-12-21 21:57:11 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-12-22 00:06:21 (GMT)
commit36bae1dc0ee777aa529dd955f2e619281265f262 (patch)
tree07a56e06c48b599b948e235ca0cd626232dc193e /add-patch.c
parentd2a233cb8b9e0f7210df68c35e751e00419a8488 (diff)
downloadgit-36bae1dc0ee777aa529dd955f2e619281265f262.zip
git-36bae1dc0ee777aa529dd955f2e619281265f262.tar.gz
git-36bae1dc0ee777aa529dd955f2e619281265f262.tar.bz2
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 that command to be able to perform that work, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'add-patch.c')
-rw-r--r--add-patch.c83
1 files changed, 79 insertions, 4 deletions
diff --git a/add-patch.c b/add-patch.c
index 71356fb..af0a86f 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -19,7 +19,7 @@ struct patch_mode {
* trailing `NULL`.
*/
const char *diff_cmd[4], *apply_args[4], *apply_check_args[4];
- unsigned is_reverse:1, apply_for_checkout:1;
+ unsigned is_reverse:1, index_only:1, apply_for_checkout:1;
const char *prompt_mode[PROMPT_MODE_MAX];
const char *edit_hunk_hint, *help_patch_text;
};
@@ -45,6 +45,72 @@ static struct patch_mode patch_mode_add = {
"the file\n")
};
+static struct patch_mode patch_mode_stash = {
+ .diff_cmd = { "diff-index", "HEAD", NULL },
+ .apply_args = { "--cached", NULL },
+ .apply_check_args = { "--cached", NULL },
+ .prompt_mode = {
+ N_("Stash mode change [y,n,q,a,d%s,?]? "),
+ N_("Stash deletion [y,n,q,a,d%s,?]? "),
+ N_("Stash this hunk [y,n,q,a,d%s,?]? "),
+ },
+ .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
+ "will immediately be marked for stashing."),
+ .help_patch_text =
+ N_("y - stash this hunk\n"
+ "n - do not stash this hunk\n"
+ "q - quit; do not stash this hunk or any of the remaining "
+ "ones\n"
+ "a - stash this hunk and all later hunks in the file\n"
+ "d - do not stash this hunk or any of the later hunks in "
+ "the file\n"),
+};
+
+static struct patch_mode patch_mode_reset_head = {
+ .diff_cmd = { "diff-index", "--cached", NULL },
+ .apply_args = { "-R", "--cached", NULL },
+ .apply_check_args = { "-R", "--cached", NULL },
+ .is_reverse = 1,
+ .index_only = 1,
+ .prompt_mode = {
+ N_("Unstage mode change [y,n,q,a,d%s,?]? "),
+ N_("Unstage deletion [y,n,q,a,d%s,?]? "),
+ N_("Unstage this hunk [y,n,q,a,d%s,?]? "),
+ },
+ .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
+ "will immediately be marked for unstaging."),
+ .help_patch_text =
+ N_("y - unstage this hunk\n"
+ "n - do not unstage this hunk\n"
+ "q - quit; do not unstage this hunk or any of the remaining "
+ "ones\n"
+ "a - unstage this hunk and all later hunks in the file\n"
+ "d - do not unstage this hunk or any of the later hunks in "
+ "the file\n"),
+};
+
+static struct patch_mode patch_mode_reset_nothead = {
+ .diff_cmd = { "diff-index", "-R", "--cached", NULL },
+ .apply_args = { "--cached", NULL },
+ .apply_check_args = { "--cached", NULL },
+ .index_only = 1,
+ .prompt_mode = {
+ N_("Apply mode change to index [y,n,q,a,d%s,?]? "),
+ N_("Apply deletion to index [y,n,q,a,d%s,?]? "),
+ N_("Apply this hunk to index [y,n,q,a,d%s,?]? "),
+ },
+ .edit_hunk_hint = N_("If the patch applies cleanly, the edited hunk "
+ "will immediately be marked for applying."),
+ .help_patch_text =
+ N_("y - apply this hunk to index\n"
+ "n - do not apply this hunk to index\n"
+ "q - quit; do not apply this hunk or any of the remaining "
+ "ones\n"
+ "a - apply this hunk and all later hunks in the file\n"
+ "d - do not apply this hunk or any of the later hunks in "
+ "the file\n"),
+};
+
struct hunk_header {
unsigned long old_offset, old_count, new_offset, new_count;
/*
@@ -1350,12 +1416,21 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
init_add_i_state(&s.s, r);
- s.mode = &patch_mode_add;
+ if (mode == ADD_P_STASH)
+ s.mode = &patch_mode_stash;
+ else if (mode == ADD_P_RESET) {
+ if (!revision || !strcmp(revision, "HEAD"))
+ s.mode = &patch_mode_reset_head;
+ else
+ s.mode = &patch_mode_reset_nothead;
+ } else
+ s.mode = &patch_mode_add;
s.revision = revision;
if (discard_index(r->index) < 0 || repo_read_index(r) < 0 ||
- repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
- NULL, NULL, NULL) < 0 ||
+ (!s.mode->index_only &&
+ repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
+ NULL, NULL, NULL) < 0) ||
parse_diff(&s, ps) < 0) {
strbuf_release(&s.plain);
strbuf_release(&s.colored);