@matheusbsbs / matheus-bagliano / commits / 952dfc6944
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 4, 2009 at 06:11 UTC 952dfc6944b29582482ff50a85c04879406c06ba
2 files changed +33 -5
builtin-reset.c
+4 -2
@@ -286,8 +286,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
286 if (reset_type == NONE)
287 reset_type = MIXED; /* by default */
288
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]);
293
294 /* Soft reset does not touch the index file nor the working tree
295 * at all, but requires them in a good order. Other resets reset
t/t7103-reset-bare.sh
+29 -3
@@ -11,16 +11,42 @@ test_expect_success 'setup non-bare' '
11 git commit -a -m two
12 '
13
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 +
32 test_expect_success 'setup bare' '
33 git clone --bare . bare.git &&
34 cd bare.git
35 '
36
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^
47 '
48
23 -test_expect_success 'soft reset is allowed' '
49 +test_expect_success 'soft reset is allowed in bare' '
50 git reset --soft HEAD^ &&
51 test "`git show --pretty=format:%s | head -n 1`" = "one"
52 '