restore: support --patch

git-restore is different from git-checkout that it only restores the worktree by default, not both worktree and index. add--interactive needs some update to support this mode. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 25, 2019 at 16:45 UTC 2f0896ec3ad4731d970d22f61daf62046bc766a9
2 files changed +56 -2
builtin/checkout.c
+4 -2
@@ -454,9 +454,11 @@ static int checkout_paths(const struct checkout_opts *opts,
454 patch_mode = "--patch=checkout";
455 else if (opts->checkout_index && !opts->checkout_worktree)
456 patch_mode = "--patch=reset";
457 + else if (!opts->checkout_index && opts->checkout_worktree)
458 + patch_mode = "--patch=worktree";
459 else
458 - die(_("'%s' with only '%s' is not currently supported"),
459 - "--patch", "--worktree");
460 + BUG("either flag must have been set, worktree=%d, index=%d",
461 + opts->checkout_worktree, opts->checkout_index);
462 return run_add_interactive(revision, patch_mode, &opts->pathspec);
463 }
464
git-add--interactive.perl
+52
@@ -149,6 +149,20 @@ my %patch_modes = (
149 FILTER => undef,
150 IS_REVERSE => 0,
151 },
152 + 'worktree_head' => {
153 + DIFF => 'diff-index -p',
154 + APPLY => sub { apply_patch 'apply -R', @_ },
155 + APPLY_CHECK => 'apply -R',
156 + FILTER => undef,
157 + IS_REVERSE => 1,
158 + },
159 + 'worktree_nothead' => {
160 + DIFF => 'diff-index -R -p',
161 + APPLY => sub { apply_patch 'apply', @_ },
162 + APPLY_CHECK => 'apply',
163 + FILTER => undef,
164 + IS_REVERSE => 0,
165 + },
166 );
167
168 $patch_mode = 'stage';
@@ -1049,6 +1063,12 @@ marked for discarding."),
1063 marked for discarding."),
1064 checkout_nothead => N__(
1065 "If the patch applies cleanly, the edited hunk will immediately be
1066 +marked for applying."),
1067 + worktree_head => N__(
1068 +"If the patch applies cleanly, the edited hunk will immediately be
1069 +marked for discarding."),
1070 + worktree_nothead => N__(
1071 +"If the patch applies cleanly, the edited hunk will immediately be
1072 marked for applying."),
1073 );
1074
@@ -1259,6 +1279,18 @@ d - do not discard this hunk or any of the later hunks in the file"),
1279 n - do not apply this hunk to index and worktree
1280 q - quit; do not apply this hunk or any of the remaining ones
1281 a - apply this hunk and all later hunks in the file
1282 +d - do not apply this hunk or any of the later hunks in the file"),
1283 + worktree_head => N__(
1284 +"y - discard this hunk from worktree
1285 +n - do not discard this hunk from worktree
1286 +q - quit; do not discard this hunk or any of the remaining ones
1287 +a - discard this hunk and all later hunks in the file
1288 +d - do not discard this hunk or any of the later hunks in the file"),
1289 + worktree_nothead => N__(
1290 +"y - apply this hunk to worktree
1291 +n - do not apply this hunk to worktree
1292 +q - quit; do not apply this hunk or any of the remaining ones
1293 +a - apply this hunk and all later hunks in the file
1294 d - do not apply this hunk or any of the later hunks in the file"),
1295 );
1296
@@ -1421,6 +1453,16 @@ my %patch_update_prompt_modes = (
1453 deletion => N__("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
1454 hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
1455 },
1456 + worktree_head => {
1457 + mode => N__("Discard mode change from worktree [y,n,q,a,d%s,?]? "),
1458 + deletion => N__("Discard deletion from worktree [y,n,q,a,d%s,?]? "),
1459 + hunk => N__("Discard this hunk from worktree [y,n,q,a,d%s,?]? "),
1460 + },
1461 + worktree_nothead => {
1462 + mode => N__("Apply mode change to worktree [y,n,q,a,d%s,?]? "),
1463 + deletion => N__("Apply deletion to worktree [y,n,q,a,d%s,?]? "),
1464 + hunk => N__("Apply this hunk to worktree [y,n,q,a,d%s,?]? "),
1465 + },
1466 );
1467
1468 sub patch_update_file {
@@ -1756,6 +1798,16 @@ sub process_args {
1798 'checkout_head' : 'checkout_nothead');
1799 $arg = shift @ARGV or die __("missing --");
1800 }
1801 + } elsif ($1 eq 'worktree') {
1802 + $arg = shift @ARGV or die __("missing --");
1803 + if ($arg eq '--') {
1804 + $patch_mode = 'checkout_index';
1805 + } else {
1806 + $patch_mode_revision = $arg;
1807 + $patch_mode = ($arg eq 'HEAD' ?
1808 + 'worktree_head' : 'worktree_nothead');
1809 + $arg = shift @ARGV or die __("missing --");
1810 + }
1811 } elsif ($1 eq 'stage' or $1 eq 'stash') {
1812 $patch_mode = $1;
1813 $arg = shift @ARGV or die __("missing --");