Skip to content

Commit 2f0896e

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5e5f39 commit 2f0896e

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

builtin/checkout.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ static int checkout_paths(const struct checkout_opts *opts,
454454
patch_mode = "--patch=checkout";
455455
else if (opts->checkout_index && !opts->checkout_worktree)
456456
patch_mode = "--patch=reset";
457+
else if (!opts->checkout_index && opts->checkout_worktree)
458+
patch_mode = "--patch=worktree";
457459
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);
460462
return run_add_interactive(revision, patch_mode, &opts->pathspec);
461463
}
462464

git-add--interactive.perl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ sub colored {
149149
FILTER => undef,
150150
IS_REVERSE => 0,
151151
},
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+
},
152166
);
153167

154168
$patch_mode = 'stage';
@@ -1049,6 +1063,12 @@ sub color_diff {
10491063
marked for discarding."),
10501064
checkout_nothead => N__(
10511065
"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
10521072
marked for applying."),
10531073
);
10541074

@@ -1259,6 +1279,18 @@ sub edit_hunk_loop {
12591279
n - do not apply this hunk to index and worktree
12601280
q - quit; do not apply this hunk or any of the remaining ones
12611281
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
12621294
d - do not apply this hunk or any of the later hunks in the file"),
12631295
);
12641296

@@ -1421,6 +1453,16 @@ sub display_hunks {
14211453
deletion => N__("Apply deletion to index and worktree [y,n,q,a,d%s,?]? "),
14221454
hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d%s,?]? "),
14231455
},
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+
},
14241466
);
14251467

14261468
sub patch_update_file {
@@ -1756,6 +1798,16 @@ sub process_args {
17561798
'checkout_head' : 'checkout_nothead');
17571799
$arg = shift @ARGV or die __("missing --");
17581800
}
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+
}
17591811
} elsif ($1 eq 'stage' or $1 eq 'stash') {
17601812
$patch_mode = $1;
17611813
$arg = shift @ARGV or die __("missing --");

0 commit comments

Comments
 (0)