Skip to content
This repository was archived by the owner on Jul 2, 2019. It is now read-only.

Commit 952dfc6

Browse files
peffgitster
authored andcommitted
reset: improve worktree safety valves
The existing code checked to make sure we were not in a bare repository when doing a hard reset. However, we should take this one step further, and make sure we are in a worktree. Otherwise, we can end up munging files inside of '.git'. Furthermore, we should do the same check for --merge resets, which have the same properties. Actually, a merge reset of HEAD^ would already complain, since further down in the code we want a worktree. However, it is nicer to check up-front; then we are sure we cover all cases ("git reset --merge" would run, even though it wasn't doing anything) and we can give a more specific message. Add tests to t7103 to cover these cases and some missing ones. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 50d9bbb commit 952dfc6

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

builtin-reset.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
286286
if (reset_type == NONE)
287287
reset_type = MIXED; /* by default */
288288

289-
if (reset_type == HARD && is_bare_repository())
290-
die("hard reset makes no sense in a bare repository");
289+
if ((reset_type == HARD || reset_type == MERGE)
290+
&& !is_inside_work_tree())
291+
die("%s reset requires a work tree",
292+
reset_type_names[reset_type]);
291293

292294
/* Soft reset does not touch the index file nor the working tree
293295
* at all, but requires them in a good order. Other resets reset

t/t7103-reset-bare.sh

+29-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,42 @@ test_expect_success 'setup non-bare' '
1111
git commit -a -m two
1212
'
1313

14+
test_expect_success 'hard reset requires a worktree' '
15+
(cd .git &&
16+
test_must_fail git reset --hard)
17+
'
18+
19+
test_expect_success 'merge reset requires a worktree' '
20+
(cd .git &&
21+
test_must_fail git reset --merge)
22+
'
23+
24+
test_expect_success 'mixed reset is ok' '
25+
(cd .git && git reset)
26+
'
27+
28+
test_expect_success 'soft reset is ok' '
29+
(cd .git && git reset --soft)
30+
'
31+
1432
test_expect_success 'setup bare' '
1533
git clone --bare . bare.git &&
1634
cd bare.git
1735
'
1836

19-
test_expect_success 'hard reset is not allowed' '
20-
test_must_fail git reset --hard HEAD^
37+
test_expect_success 'hard reset is not allowed in bare' '
38+
test_must_fail git reset --hard HEAD^
39+
'
40+
41+
test_expect_success 'merge reset is not allowed in bare' '
42+
test_must_fail git reset --merge HEAD^
43+
'
44+
45+
test_expect_success 'mixed reset is not allowed in bare' '
46+
test_must_fail git reset --mixed HEAD^
2147
'
2248

23-
test_expect_success 'soft reset is allowed' '
49+
test_expect_success 'soft reset is allowed in bare' '
2450
git reset --soft HEAD^ &&
2551
test "`git show --pretty=format:%s | head -n 1`" = "one"
2652
'

0 commit comments

Comments
 (0)