@matheusbsbs / matheus-bagliano / commits / bdecd9d41b
Fork 0

More permissive "git-rm --cached" behavior without -f.

In the previous behavior, "git-rm --cached" (without -f) had the same restriction as "git-rm". This forced the user to use the -f flag in situations which weren't actually dangerous, like: $ git add foo # oops, I didn't want this $ git rm --cached foo # back to initial situation Previously, the index had to match the file *and* the HEAD. With --cached, the index must now match the file *or* the HEAD. The behavior without --cached is unchanged, but provides better error messages. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Jul 13, 2007 at 19:41 UTC bdecd9d41b3528e17aea2290344c584412e2424e
3 files changed +62 -7
Documentation/git-rm.txt
+2 -1
@@ -14,7 +14,8 @@ DESCRIPTION
14 Remove files from the working tree and from the index. The
15 files have to be identical to the tip of the branch, and no
16 updates to its contents must have been placed in the staging
17 -area (aka index).
17 +area (aka index). When --cached is given, the staged content has to
18 +match either the tip of the branch *or* the file on disk.
19
20
21 OPTIONS
builtin-rm.c
+26 -6
@@ -46,7 +46,7 @@ static int remove_file(const char *name)
46 return ret;
47 }
48
49 -static int check_local_mod(unsigned char *head)
49 +static int check_local_mod(unsigned char *head, int index_only)
50 {
51 /* items in list are already sorted in the cache order,
52 * so we could do this a lot more efficiently by using
@@ -65,6 +65,8 @@ static int check_local_mod(unsigned char *head)
65 const char *name = list.name[i];
66 unsigned char sha1[20];
67 unsigned mode;
68 + int local_changes = 0;
69 + int staged_changes = 0;
70
71 pos = cache_name_pos(name, strlen(name));
72 if (pos < 0)
@@ -87,14 +89,32 @@ static int check_local_mod(unsigned char *head)
89 continue;
90 }
91 if (ce_match_stat(ce, &st, 0))
90 - errs = error("'%s' has local modifications "
91 - "(hint: try -f)", ce->name);
92 + local_changes = 1;
93 if (no_head
94 || get_tree_entry(head, name, sha1, &mode)
95 || ce->ce_mode != create_ce_mode(mode)
96 || hashcmp(ce->sha1, sha1))
96 - errs = error("'%s' has changes staged in the index "
97 - "(hint: try -f)", name);
97 + staged_changes = 1;
98 +
99 + if (local_changes && staged_changes)
100 + errs = error("'%s' has staged content different "
101 + "from both the file and the HEAD\n"
102 + "(use -f to force removal)", name);
103 + else if (!index_only) {
104 + /* It's not dangerous to git-rm --cached a
105 + * file if the index matches the file or the
106 + * HEAD, since it means the deleted content is
107 + * still available somewhere.
108 + */
109 + if (staged_changes)
110 + errs = error("'%s' has changes staged in the index\n"
111 + "(use --cached to keep the file, "
112 + "or -f to force removal)", name);
113 + if (local_changes)
114 + errs = error("'%s' has local modifications\n"
115 + "(use --cached to keep the file, "
116 + "or -f to force removal)", name);
117 + }
118 }
119 return errs;
120 }
@@ -192,7 +212,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
212 unsigned char sha1[20];
213 if (get_sha1("HEAD", sha1))
214 hashclr(sha1);
195 - if (check_local_mod(sha1))
215 + if (check_local_mod(sha1, index_only))
216 exit(1);
217 }
218
t/t3600-rm.sh
+34
@@ -45,6 +45,40 @@ test_expect_success \
45 'Test that git rm foo succeeds' \
46 'git rm --cached foo'
47
48 +test_expect_success \
49 + 'Test that git rm --cached foo succeeds if the index matches the file' \
50 + 'echo content > foo
51 + git add foo
52 + git rm --cached foo'
53 +
54 +test_expect_success \
55 + 'Test that git rm --cached foo succeeds if the index matches the file' \
56 + 'echo content > foo
57 + git add foo
58 + git commit -m foo
59 + echo "other content" > foo
60 + git rm --cached foo'
61 +
62 +test_expect_failure \
63 + 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' \
64 + 'echo content > foo
65 + git add foo
66 + git commit -m foo
67 + echo "other content" > foo
68 + git add foo
69 + echo "yet another content" > foo
70 + git rm --cached foo'
71 +
72 +test_expect_success \
73 + 'Test that git rm --cached -f foo works in case where --cached only did not' \
74 + 'echo content > foo
75 + git add foo
76 + git commit -m foo
77 + echo "other content" > foo
78 + git add foo
79 + echo "yet another content" > foo
80 + git rm --cached -f foo'
81 +
82 test_expect_success \
83 'Post-check that foo exists but is not in index after git rm foo' \
84 '[ -f foo ] && ! git ls-files --error-unmatch foo'