{"thread":{"id":"14533","subject":"Out-of-repository file remove error","startedAt":"2008-07-19T08:23:14Z","lastAt":"2008-07-19T16:24:46Z","messageCount":3,"participants":["Nick Andrew","Olivier Marin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"83939","messageId":"20080719082314.GA15419@mail.local.tull.net","threadId":"14533","inReplyTo":null,"subject":"Out-of-repository file remove error","fromName":"Nick Andrew","fromEmail":"nick@nick-andrew.net","sentAt":"2008-07-19T08:23:14Z","receivedAt":"2008-07-19T08:23:14Z","isPatch":false,"sender":{"key":"nick@nick-andrew.net","avatar":"https://gravatar.com/avatar/85f25a67ca6eaa4016ed374f6d07f3cd853c886aeb7e1507eb7dbc47b00082fe?d=mp&s=160"},"body":"git rm seems to have a problem with removing a file from a repository\nwhen the repository .git and working tree are not in the current\ndirectory. It leaves an index.lock file.\n\nHere's a script to show the bug:\n\nmkdir Bugtest\ncd Bugtest\ngit init\ndate > newfile\ngit add newfile\ngit commit -m 'Added' newfile\ncd ..\ngit --git-dir=Bugtest/.git --work-tree=Bugtest rm newfile\nls -l Bugtest/.git/index.lock\n\nOutput:\n\nInitialized empty Git repository in .../Bugtest/.git/\nCreated initial commit 43dec15: Added\n 1 files changed, 1 insertions(+), 0 deletions(-)\n create mode 100644 newfile\nrm 'newfile'\nfatal: Unable to write new index file\n-rw-r--r-- 1 nick nick 32 Jul 19 18:20 Bugtest/.git/index.lock\n\nI tested on:\n\ngit version 1.5.6.2\ngit version 1.5.6.3.440.g9d8f\n\nNick.\n"},{"id":"83947","messageId":"48821485.6050507@free.fr","threadId":"14533","inReplyTo":"20080719082314.GA15419@mail.local.tull.net","subject":"[PATCH] builtin-rm: fix index lock file path","fromName":"Olivier Marin","fromEmail":"dkr+ml.git@free.fr","sentAt":"2008-07-19T16:21:25Z","receivedAt":"2008-07-19T16:21:25Z","isPatch":true,"sender":{"key":"dkr+ml.git@free.fr","avatar":null},"body":"From: Olivier Marin <dkr@freesurf.fr>\n\nWhen hold_locked_index() is called with a relative git_dir and you are\noutside the work tree, the lock file become relative to the current\ndirectory. So when later setup_work_tree() change the current directory\nit breaks lock file path and commit_locked_index() fails.\n\nThis patch move index locking code after setup_work_tree() call to make\nlock file relative to the working tree as it should be and add a test\ncase.\n\nNoticed by Nick Andrew.\n\nSigned-off-by: Olivier Marin <dkr@freesurf.fr>\n---\n builtin-rm.c |   10 +++++-----\n 1 files changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin-rm.c b/builtin-rm.c\nindex 56454ec..ee8247b 100644\n--- a/builtin-rm.c\n+++ b/builtin-rm.c\n@@ -146,11 +146,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_default_config, NULL);\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n-\n-\tif (read_cache() < 0)\n-\t\tdie(\"index file corrupt\");\n-\n \targc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0);\n \tif (!argc)\n \t\tusage_with_options(builtin_rm_usage, builtin_rm_options);\n@@ -158,6 +153,11 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \tif (!index_only)\n \t\tsetup_work_tree();\n \n+\tnewfd = hold_locked_index(&lock_file, 1);\n+\n+\tif (read_cache() < 0)\n+\t\tdie(\"index file corrupt\");\n+\n \tpathspec = get_pathspec(prefix, argv);\n \tseen = NULL;\n \tfor (i = 0; pathspec[i] ; i++)\n-- \n1.5.6.3.440.g489d7\n"},{"id":"83948","messageId":"4882154E.5050806@free.fr","threadId":"14533","inReplyTo":"48821485.6050507@free.fr","subject":"[PATCH V2] builtin-rm: fix index lock file path","fromName":"Olivier Marin","fromEmail":"dkr+ml.git@free.fr","sentAt":"2008-07-19T16:24:46Z","receivedAt":"2008-07-19T16:24:46Z","isPatch":true,"sender":{"key":"dkr+ml.git@free.fr","avatar":null},"body":"From: Olivier Marin <dkr@freesurf.fr>\n\nWhen hold_locked_index() is called with a relative git_dir and you are\noutside the work tree, the lock file become relative to the current\ndirectory. So when later setup_work_tree() change the current directory\nit breaks lock file path and commit_locked_index() fails.\n\nThis patch move index locking code after setup_work_tree() call to make\nlock file relative to the working tree as it should be and add a test\ncase.\n\nNoticed by Nick Andrew.\n\nSigned-off-by: Olivier Marin <dkr@freesurf.fr>\n---\n\n The same with the test case!\n\n builtin-rm.c  |   10 +++++-----\n t/t3600-rm.sh |   12 ++++++++++++\n 2 files changed, 17 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin-rm.c b/builtin-rm.c\nindex 56454ec..ee8247b 100644\n--- a/builtin-rm.c\n+++ b/builtin-rm.c\n@@ -146,11 +146,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \n \tgit_config(git_default_config, NULL);\n \n-\tnewfd = hold_locked_index(&lock_file, 1);\n-\n-\tif (read_cache() < 0)\n-\t\tdie(\"index file corrupt\");\n-\n \targc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0);\n \tif (!argc)\n \t\tusage_with_options(builtin_rm_usage, builtin_rm_options);\n@@ -158,6 +153,11 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \tif (!index_only)\n \t\tsetup_work_tree();\n \n+\tnewfd = hold_locked_index(&lock_file, 1);\n+\n+\tif (read_cache() < 0)\n+\t\tdie(\"index file corrupt\");\n+\n \tpathspec = get_pathspec(prefix, argv);\n \tseen = NULL;\n \tfor (i = 0; pathspec[i] ; i++)\ndiff --git a/t/t3600-rm.sh b/t/t3600-rm.sh\nindex 316775e..79c06ad 100755\n--- a/t/t3600-rm.sh\n+++ b/t/t3600-rm.sh\n@@ -217,4 +217,16 @@ test_expect_success 'Remove nonexistent file returns nonzero exit status' '\n \ttest_must_fail git rm nonexistent\n '\n \n+test_expect_success 'Call \"rm\" from outside the work tree' '\n+\tmkdir repo &&\n+\tcd repo &&\n+\tgit init &&\n+\techo something > somefile &&\n+\tgit add somefile &&\n+\tgit commit -m \"add a file\" &&\n+\t(cd .. &&\n+\t git --git-dir=repo/.git --work-tree=repo rm somefile) &&\n+\ttest_must_fail git ls-files --error-unmatch somefile\n+'\n+\n test_done\n-- \n1.5.6.3.440.g489d7\n"}]}