Skip to content

Commit 6546277

Browse files
committed
Merge branch 'gt/at-is-synonym-for-head-in-add-patch'
Teach "git checkout -p" and friends that "@" is a synonym for "HEAD". * gt/at-is-synonym-for-head-in-add-patch: add -p tests: remove PERL prerequisites add-patch: classify '@' as a synonym for 'HEAD'
2 parents cf258a9 + 7abc186 commit 6546277

11 files changed

+92
-82
lines changed

add-patch.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,14 +1729,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
17291729
if (mode == ADD_P_STASH)
17301730
s.mode = &patch_mode_stash;
17311731
else if (mode == ADD_P_RESET) {
1732-
/*
1733-
* NEEDSWORK: Instead of comparing to the literal "HEAD",
1734-
* compare the commit objects instead so that other ways of
1735-
* saying the same thing (such as "@") are also handled
1736-
* appropriately.
1737-
*
1738-
* This applies to the cases below too.
1739-
*/
17401732
if (!revision || !strcmp(revision, "HEAD"))
17411733
s.mode = &patch_mode_reset_head;
17421734
else

builtin/checkout.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,9 @@ static void setup_new_branch_info_and_source_tree(
12241224
struct tree **source_tree = &opts->source_tree;
12251225
struct object_id branch_rev;
12261226

1227-
new_branch_info->name = xstrdup(arg);
1227+
/* treat '@' as a shortcut for 'HEAD' */
1228+
new_branch_info->name = !strcmp(arg, "@") ? xstrdup("HEAD") :
1229+
xstrdup(arg);
12281230
setup_branch_path(new_branch_info);
12291231

12301232
if (!check_refname_format(new_branch_info->path, 0) &&

builtin/reset.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ static void parse_args(struct pathspec *pathspec,
281281
verify_filename(prefix, argv[0], 1);
282282
}
283283
}
284-
*rev_ret = rev;
284+
285+
/* treat '@' as a shortcut for 'HEAD' */
286+
*rev_ret = !strcmp("@", rev) ? "HEAD" : rev;
285287

286288
parse_pathspec(pathspec, 0,
287289
PATHSPEC_PREFER_FULL |

t/t2016-checkout-patch.sh

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,32 @@ test_expect_success 'git checkout -p with staged changes' '
3838
verify_state dir/foo index index
3939
'
4040

41-
test_expect_success 'git checkout -p HEAD with NO staged changes: abort' '
42-
set_and_save_state dir/foo work head &&
43-
test_write_lines n y n | git checkout -p HEAD &&
44-
verify_saved_state bar &&
45-
verify_saved_state dir/foo
46-
'
47-
48-
test_expect_success 'git checkout -p HEAD with NO staged changes: apply' '
49-
test_write_lines n y y | git checkout -p HEAD &&
50-
verify_saved_state bar &&
51-
verify_state dir/foo head head
52-
'
53-
54-
test_expect_success 'git checkout -p HEAD with change already staged' '
55-
set_state dir/foo index index &&
56-
# the third n is to get out in case it mistakenly does not apply
57-
test_write_lines n y n | git checkout -p HEAD &&
58-
verify_saved_state bar &&
59-
verify_state dir/foo head head
60-
'
41+
for opt in "HEAD" "@"
42+
do
43+
test_expect_success "git checkout -p $opt with NO staged changes: abort" '
44+
set_and_save_state dir/foo work head &&
45+
test_write_lines n y n | git checkout -p $opt >output &&
46+
verify_saved_state bar &&
47+
verify_saved_state dir/foo &&
48+
test_grep "Discard" output
49+
'
50+
51+
test_expect_success "git checkout -p $opt with NO staged changes: apply" '
52+
test_write_lines n y y | git checkout -p $opt >output &&
53+
verify_saved_state bar &&
54+
verify_state dir/foo head head &&
55+
test_grep "Discard" output
56+
'
57+
58+
test_expect_success "git checkout -p $opt with change already staged" '
59+
set_state dir/foo index index &&
60+
# the third n is to get out in case it mistakenly does not apply
61+
test_write_lines n y n | git checkout -p $opt >output &&
62+
verify_saved_state bar &&
63+
verify_state dir/foo head head &&
64+
test_grep "Discard" output
65+
'
66+
done
6167

6268
test_expect_success 'git checkout -p HEAD^...' '
6369
# the third n is to get out in case it mistakenly does not apply

t/t2020-checkout-detach.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ test_expect_success 'checkout branch does not detach' '
4545
check_not_detached
4646
'
4747

48+
for opt in "HEAD" "@"
49+
do
50+
test_expect_success "checkout $opt no-op/don't detach" '
51+
reset &&
52+
cat .git/HEAD >expect &&
53+
git checkout $opt &&
54+
cat .git/HEAD >actual &&
55+
check_not_detached &&
56+
test_cmp expect actual
57+
'
58+
done
59+
4860
test_expect_success 'checkout tag detaches' '
4961
reset &&
5062
git checkout tag &&

t/t2024-checkout-dwim.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test_expect_success 'checkout of branch from multiple remotes fails with advice'
113113
test_grep ! "^hint: " stderr
114114
'
115115

116-
test_expect_success PERL 'checkout -p with multiple remotes does not print advice' '
116+
test_expect_success 'checkout -p with multiple remotes does not print advice' '
117117
git checkout -B main &&
118118
test_might_fail git branch -D foo &&
119119

t/t2071-restore-patch.sh

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test_description='git restore --patch'
44

55
. ./lib-patch-mode.sh
66

7-
test_expect_success PERL 'setup' '
7+
test_expect_success 'setup' '
88
mkdir dir &&
99
echo parent >dir/foo &&
1010
echo dummy >bar &&
@@ -16,59 +16,63 @@ test_expect_success PERL 'setup' '
1616
save_head
1717
'
1818

19-
test_expect_success PERL 'restore -p without pathspec is fine' '
19+
test_expect_success 'restore -p without pathspec is fine' '
2020
echo q >cmd &&
2121
git restore -p <cmd
2222
'
2323

2424
# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
2525

26-
test_expect_success PERL 'saying "n" does nothing' '
26+
test_expect_success 'saying "n" does nothing' '
2727
set_and_save_state dir/foo work head &&
2828
test_write_lines n n | git restore -p &&
2929
verify_saved_state bar &&
3030
verify_saved_state dir/foo
3131
'
3232

33-
test_expect_success PERL 'git restore -p' '
33+
test_expect_success 'git restore -p' '
3434
set_and_save_state dir/foo work head &&
3535
test_write_lines n y | git restore -p &&
3636
verify_saved_state bar &&
3737
verify_state dir/foo head head
3838
'
3939

40-
test_expect_success PERL 'git restore -p with staged changes' '
40+
test_expect_success 'git restore -p with staged changes' '
4141
set_state dir/foo work index &&
4242
test_write_lines n y | git restore -p &&
4343
verify_saved_state bar &&
4444
verify_state dir/foo index index
4545
'
4646

47-
test_expect_success PERL 'git restore -p --source=HEAD' '
48-
set_state dir/foo work index &&
49-
# the third n is to get out in case it mistakenly does not apply
50-
test_write_lines n y n | git restore -p --source=HEAD &&
51-
verify_saved_state bar &&
52-
verify_state dir/foo head index
53-
'
54-
55-
test_expect_success PERL 'git restore -p --source=HEAD^' '
47+
for opt in "HEAD" "@"
48+
do
49+
test_expect_success "git restore -p --source=$opt" '
50+
set_state dir/foo work index &&
51+
# the third n is to get out in case it mistakenly does not apply
52+
test_write_lines n y n | git restore -p --source=$opt >output &&
53+
verify_saved_state bar &&
54+
verify_state dir/foo head index &&
55+
test_grep "Discard" output
56+
'
57+
done
58+
59+
test_expect_success 'git restore -p --source=HEAD^' '
5660
set_state dir/foo work index &&
5761
# the third n is to get out in case it mistakenly does not apply
5862
test_write_lines n y n | git restore -p --source=HEAD^ &&
5963
verify_saved_state bar &&
6064
verify_state dir/foo parent index
6165
'
6266

63-
test_expect_success PERL 'git restore -p --source=HEAD^...' '
67+
test_expect_success 'git restore -p --source=HEAD^...' '
6468
set_state dir/foo work index &&
6569
# the third n is to get out in case it mistakenly does not apply
6670
test_write_lines n y n | git restore -p --source=HEAD^... &&
6771
verify_saved_state bar &&
6872
verify_state dir/foo parent index
6973
'
7074

71-
test_expect_success PERL 'git restore -p handles deletion' '
75+
test_expect_success 'git restore -p handles deletion' '
7276
set_state dir/foo work index &&
7377
rm dir/foo &&
7478
test_write_lines n y | git restore -p &&
@@ -81,37 +85,37 @@ test_expect_success PERL 'git restore -p handles deletion' '
8185
# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
8286
# the failure case (and thus get out of the loop).
8387

84-
test_expect_success PERL 'path limiting works: dir' '
88+
test_expect_success 'path limiting works: dir' '
8589
set_state dir/foo work head &&
8690
test_write_lines y n | git restore -p dir &&
8791
verify_saved_state bar &&
8892
verify_state dir/foo head head
8993
'
9094

91-
test_expect_success PERL 'path limiting works: -- dir' '
95+
test_expect_success 'path limiting works: -- dir' '
9296
set_state dir/foo work head &&
9397
test_write_lines y n | git restore -p -- dir &&
9498
verify_saved_state bar &&
9599
verify_state dir/foo head head
96100
'
97101

98-
test_expect_success PERL 'path limiting works: HEAD^ -- dir' '
102+
test_expect_success 'path limiting works: HEAD^ -- dir' '
99103
set_state dir/foo work head &&
100104
# the third n is to get out in case it mistakenly does not apply
101105
test_write_lines y n n | git restore -p --source=HEAD^ -- dir &&
102106
verify_saved_state bar &&
103107
verify_state dir/foo parent head
104108
'
105109

106-
test_expect_success PERL 'path limiting works: foo inside dir' '
110+
test_expect_success 'path limiting works: foo inside dir' '
107111
set_state dir/foo work head &&
108112
# the third n is to get out in case it mistakenly does not apply
109113
test_write_lines y n n | (cd dir && git restore -p foo) &&
110114
verify_saved_state bar &&
111115
verify_state dir/foo head head
112116
'
113117

114-
test_expect_success PERL 'none of this moved HEAD' '
118+
test_expect_success 'none of this moved HEAD' '
115119
verify_saved_head
116120
'
117121

t/t3904-stash-patch.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
test_description='stash -p'
44
. ./lib-patch-mode.sh
55

6-
if ! test_have_prereq PERL
7-
then
8-
skip_all='skipping stash -p tests, perl not available'
9-
test_done
10-
fi
11-
126
test_expect_success 'setup' '
137
mkdir dir &&
148
echo parent > dir/foo &&

t/t7105-reset-patch.sh

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ test_description='git reset --patch'
55
TEST_PASSES_SANITIZE_LEAK=true
66
. ./lib-patch-mode.sh
77

8-
test_expect_success PERL 'setup' '
8+
test_expect_success 'setup' '
99
mkdir dir &&
1010
echo parent > dir/foo &&
1111
echo dummy > bar &&
@@ -19,42 +19,46 @@ test_expect_success PERL 'setup' '
1919

2020
# note: bar sorts before foo, so the first 'n' is always to skip 'bar'
2121

22-
test_expect_success PERL 'saying "n" does nothing' '
22+
test_expect_success 'saying "n" does nothing' '
2323
set_and_save_state dir/foo work work &&
2424
test_write_lines n n | git reset -p &&
2525
verify_saved_state dir/foo &&
2626
verify_saved_state bar
2727
'
2828

29-
test_expect_success PERL 'git reset -p' '
30-
test_write_lines n y | git reset -p >output &&
31-
verify_state dir/foo work head &&
32-
verify_saved_state bar &&
33-
test_grep "Unstage" output
34-
'
35-
36-
test_expect_success PERL 'git reset -p HEAD^' '
29+
for opt in "HEAD" "@" ""
30+
do
31+
test_expect_success "git reset -p $opt" '
32+
set_and_save_state dir/foo work work &&
33+
test_write_lines n y | git reset -p $opt >output &&
34+
verify_state dir/foo work head &&
35+
verify_saved_state bar &&
36+
test_grep "Unstage" output
37+
'
38+
done
39+
40+
test_expect_success 'git reset -p HEAD^' '
3741
test_write_lines n y | git reset -p HEAD^ >output &&
3842
verify_state dir/foo work parent &&
3943
verify_saved_state bar &&
4044
test_grep "Apply" output
4145
'
4246

43-
test_expect_success PERL 'git reset -p HEAD^^{tree}' '
47+
test_expect_success 'git reset -p HEAD^^{tree}' '
4448
test_write_lines n y | git reset -p HEAD^^{tree} >output &&
4549
verify_state dir/foo work parent &&
4650
verify_saved_state bar &&
4751
test_grep "Apply" output
4852
'
4953

50-
test_expect_success PERL 'git reset -p HEAD^:dir/foo (blob fails)' '
54+
test_expect_success 'git reset -p HEAD^:dir/foo (blob fails)' '
5155
set_and_save_state dir/foo work work &&
5256
test_must_fail git reset -p HEAD^:dir/foo &&
5357
verify_saved_state dir/foo &&
5458
verify_saved_state bar
5559
'
5660

57-
test_expect_success PERL 'git reset -p aaaaaaaa (unknown fails)' '
61+
test_expect_success 'git reset -p aaaaaaaa (unknown fails)' '
5862
set_and_save_state dir/foo work work &&
5963
test_must_fail git reset -p aaaaaaaa &&
6064
verify_saved_state dir/foo &&
@@ -66,27 +70,27 @@ test_expect_success PERL 'git reset -p aaaaaaaa (unknown fails)' '
6670
# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
6771
# the failure case (and thus get out of the loop).
6872

69-
test_expect_success PERL 'git reset -p dir' '
73+
test_expect_success 'git reset -p dir' '
7074
set_state dir/foo work work &&
7175
test_write_lines y n | git reset -p dir &&
7276
verify_state dir/foo work head &&
7377
verify_saved_state bar
7478
'
7579

76-
test_expect_success PERL 'git reset -p -- foo (inside dir)' '
80+
test_expect_success 'git reset -p -- foo (inside dir)' '
7781
set_state dir/foo work work &&
7882
test_write_lines y n | (cd dir && git reset -p -- foo) &&
7983
verify_state dir/foo work head &&
8084
verify_saved_state bar
8185
'
8286

83-
test_expect_success PERL 'git reset -p HEAD^ -- dir' '
87+
test_expect_success 'git reset -p HEAD^ -- dir' '
8488
test_write_lines y n | git reset -p HEAD^ -- dir &&
8589
verify_state dir/foo work parent &&
8690
verify_saved_state bar
8791
'
8892

89-
test_expect_success PERL 'none of this moved HEAD' '
93+
test_expect_success 'none of this moved HEAD' '
9094
verify_saved_head
9195
'
9296

t/t7106-reset-unborn-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_expect_success 'reset $file' '
3434
test_cmp expect actual
3535
'
3636

37-
test_expect_success PERL 'reset -p' '
37+
test_expect_success 'reset -p' '
3838
rm .git/index &&
3939
git add a &&
4040
echo y >yes &&

0 commit comments

Comments
 (0)