builtin-rm: fix index lock file path
When hold_locked_index() is called with a relative git_dir and you are outside the work tree, the lock file become relative to the current directory. So when later setup_work_tree() change the current directory it breaks lock file path and commit_locked_index() fails. This patch move index locking code after setup_work_tree() call to make lock file relative to the working tree as it should be and add a test case. Noticed by Nick Andrew. Signed-off-by: Olivier Marin <dkr@freesurf.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Olivier Marin committed
Jul 19, 2008 at 18:24 UTC
4d2646727997c82c04a82ce38e56d76e7b5d268b
2 files changed
+17
-5
builtin-rm.c
+5
-5
@@ -146,11 +146,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
146
147
git_config(git_default_config, NULL);
148
149
- newfd = hold_locked_index(&lock_file, 1);
150
-
151
- if (read_cache() < 0)
152
- die("index file corrupt");
153
-
149
argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0);
150
if (!argc)
151
usage_with_options(builtin_rm_usage, builtin_rm_options);
@@ -158,6 +153,11 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
153
if (!index_only)
154
setup_work_tree();
155
156
+ newfd = hold_locked_index(&lock_file, 1);
157
+
158
+ if (read_cache() < 0)
159
+ die("index file corrupt");
160
+
161
pathspec = get_pathspec(prefix, argv);
162
seen = NULL;
163
for (i = 0; pathspec[i] ; i++)
t/t3600-rm.sh
+12
@@ -217,4 +217,16 @@ test_expect_success 'Remove nonexistent file returns nonzero exit status' '
217
! git rm nonexistent
218
'
219
220
+test_expect_success 'Call "rm" from outside the work tree' '
221
+ mkdir repo &&
222
+ cd repo &&
223
+ git init &&
224
+ echo something > somefile &&
225
+ git add somefile &&
226
+ git commit -m "add a file" &&
227
+ (cd .. &&
228
+ git --git-dir=repo/.git --work-tree=repo rm somefile) &&
229
+ test_must_fail git ls-files --error-unmatch somefile
230
+'
231
+
232
test_done