assertion failure in builtin-mv.c with "git mv -k"
Hi,
Just found a bug in builtin-mv.c. Here's a script to reproduce:
mkdir git
cd git
git init
touch controled
git add controled && git commit -m "init"
touch foo1 foo2
mkdir dir
git mv -k foo* dir/
The output is the following:
Initialized empty Git repository in /tmp/git/.git/ [master (root-commit)]: created 694563b: "init"
0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 controled
git: builtin-mv.c:216: cmd_mv: Assertion `pos >= 0' failed.
./bug.sh: line 10: 12919 Aborted git mv -k foo* dir/
Apparently, this happens when using "git mv -k" with more than one unversionned file. The code ignores the first one, but still goes through this
for (i = 0; i < argc; i++) {
const char *src = source[i], *dst = destination[i];
enum update_mode mode = modes[i];
int pos;
if (show_only || verbose)
printf("Renaming %s to %s\n", src, dst);
if (!show_only && mode != INDEX &&
rename(src, dst) < 0 && !ignore_errors)
die ("renaming %s failed: %s", src, strerror(errno));
if (mode == WORKING_DIRECTORY)
continue;
pos = cache_name_pos(src, strlen(src));
assert(pos >= 0); /* <----- this is the one */
if (!show_only)
rename_cache_entry_at(pos, dst);
}for the second, and it crashes on the assertion (gdb says "pos" here is an unversionned file name).
If anyone has time to fix this ...
Thanks,
Signature · 1 line
Matthieu
PATCH 1/1 fix handling of multiple untracked files for git mv -k
The "-k" option to "git mv" should allow specifying multiple untracked files. Currently, multiple untracked files raise an assertion if they appear consecutively as arguments. Fix this by decrementing the loop index after removing one entry from the array of arguments.
--- builtin-mv.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
Reported by the OP. Do we need a test case for this? It's a really trivial change. The patch is off master but builtin-mv.c hasn't change since 81dc2307d0ad87a4da2e753a9d1b5586d6456eed tags/v1.6.0-rc1~1, so I suggest this patch for maint.
builtin-mv.c +1 -0
@@ -192,6 +192,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)memmove (destination + i, destination + i +1 , (argc - i) *sizeof (char *)); + i--; } }else die ("%s, source=%s, destination=%s",
Signature · 1 line
1.6.0.6
Matthieu Moy venit, vidit, dixit 14.01.2009 14:20:
Quoted 45 lines
Hi,
Just found a bug in
builtin-mv.c. Here's a script to reproduce:mkdir git
cd git
git init
touch controled
git add controled && git commit -m "init"
touch foo1 foo2
mkdir dir
git mv -k foo* dir/The output is the following:
Initialized empty Git repository in
/tmp/git/.git/[master (root-commit)]: created 694563b: "init"0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 controledgit: builtin-mv.c:216:
cmd_mv: Assertion `pos >= 0' failed.
./bug.sh: line 10: 12919 Aborted git mv -k foo* dir/Apparently, this happens when using "git mv -k" with more than one unversionned file. The code ignores the first one, but still goes through this
for (i = 0; i < argc; i++) { const char *src = source[i], *dst = destination[i]; enum update_mode mode = modes[i]; int pos; if (show_only || verbose) printf("Renaming %s to %s\n", src, dst); if (!show_only && mode != INDEX && rename(src, dst) < 0 && !ignore_errors) die ("renaming %s failed: %s", src, strerror(errno)); if (mode == WORKING_DIRECTORY) continue; pos = cache_name_pos(src, strlen(src)); assert(pos >= 0); /* <----- this is the one */ if (!show_only) rename_cache_entry_at(pos, dst); }
Quoted 7 lines
for the second, and it crashes on the assertion (gdb says "pos" here is an unversionned file name).
If anyone has time to fix this ...
Thanks,
The problem is actually in the loop before, with just one line missing. I'll send a patch but I'm not sure if this needs a test case.
Michael
Hi,
Quoted 1 line · Michael J Gruber
I'll send a patch but I'm not sure if this needs a test case.
Umm, Michael, you have been here long enough to know that the answer is a "YES!". If you fix something, you want to provide a test case just to make sure you do not need to fix it again later.
Ciao,
Dscho
v2 cover Add test cases for "git mv -k" and fix a known breakage.
This adds test cases for the "-k" option of "git mv", including one known breakage reported by Matthieu Moy <[email protected]> which appears when multiple untracked files are specified as consecutive arguments. This breakage is fixed in the second patch and marked "expect_pass" in the last one.
The cumulative code/other ratio is 1 line/27 lines which I blame solely on Dscho ;) Seriously, feel free to squash. But on the other hand: How else can I see the beautiful "1 known breakage fixed" other than by having 2 and 3 separate in this series...
The patch is off master but builtin-mv.c hasn't changed since 81dc2307d0ad87a4da2e753a9d1b5586d6456eed tags/v1.6.0-rc1~1, so I suggest this patch for maint.
Michael J Gruber (3):
add test cases for "git mv -k"
fix handling of multiple untracked files for git mv -k mark fixed breakage as expect pass for "git mv -k" multiple files
builtin-mv.c | 1 + t/t7001-mv.sh | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)
PATCH v2 1/3 add test cases for "git mv -k"
Add test cases for ignoring nonexisting and untracked files using the -k option to "git mv". There is one known breakage related to multiple untracked files specfied as consecutive arguments.
--- t/t7001-mv.sh | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-)
t/t7001-mv.sh +25 -0
@@ -39,6 +39,31 @@ test_expect_success \ grep "^R100..*path1/COPYING..*path0/COPYING"' test_expect_success \ + 'checking -k on non-existing file' \ + 'git mv -k idontexist path0' + +test_expect_success \ + 'checking -k on untracked file' \ + 'touch untracked1 && + git mv -k untracked1 path0 && + test -f untracked1 && + test ! -f path0/untracked1' + +test_expect_failure \ + 'checking -k on multiple untracked files' \ + 'touch untracked2 && + git mv -k untracked1 untracked2 path0 && + test -f untracked1 && + test -f untracked2 && + test ! -f path0/untracked1 + test ! -f path0/untracked2' + +# clean up the mess in case bad things happen +rm -f idontexist untracked1 untracked2 \ + path0/idontexist path0/untracked1 path0/untracked2 \ + .git/index.lock + +test_expect_success \ 'adding another file' \ 'cp "$TEST_DIRECTORY"/../README path0/README && git add path0/README &&
Signature · 1 line
1.6.0.6
PATCH v2 2/3 fix handling of multiple untracked files for git mv -k
The "-k" option to "git mv" should allow specifying multiple untracked files. Currently, multiple untracked files raise an assertion if they appear consecutively as arguments. Fix this by decrementing the loop index after removing one entry from the array of arguments.
--- builtin-mv.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
builtin-mv.c +1 -0
@@ -192,6 +192,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)memmove (destination + i, destination + i +1 , (argc - i) *sizeof (char *)); + i--; } }else die ("%s, source=%s, destination=%s",
Signature · 1 line
1.6.0.6
PATCH v2 3/3 mark fixed breakage as expect pass for "git mv -k" multiple files
The new tests pass now so mark them as such.
--- t/t7001-mv.sh | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
t/t7001-mv.sh +1 -1
@@ -49,7 +49,7 @@ test_expect_success \ test -f untracked1 && test ! -f path0/untracked1' -test_expect_failure \ +test_expect_success \ 'checking -k on multiple untracked files' \ 'touch untracked2 && git mv -k untracked1 untracked2 path0 &&
Signature · 1 line
1.6.0.6
Johannes Schindelin venit, vidit, dixit 14.01.2009 16:54:
Quoted 4 lines
Hi,
On Wed, 14 Jan 2009, Michael J Gruber wrote:
Quoted 9 lines
Quoted 1 line
I'll send a patch but I'm not sure if this needs a test case.
Umm, Michael, you have been here long enough to know that the answer is a "YES!". If you fix something, you want to provide a test case just to make sure you do not need to fix it again later.
Ciao,
Dscho
It was a lame attempt at getting around it, it's just one line... I didn't know I've been being noticed long enough ;)
So, should I prepare a series like:
1: test case and mark known fail
2: the 1 line fix
3: mark test pass
Or should 2+3 be squashed into one?
Cheers,
Michael
Quoted 9 lines · Michael J Gruber
It was a lame attempt at getting around it, it's just one line... I didn't know I've been being noticed long enough ;)
So, should I prepare a series like:1: test case and mark known fail
2: the 1 line fix
3: mark test passOr should 2+3 be squashed into one?
Definitely the fix and marking the test as passing should be one patch, since then you see that it is the fix which causes the test to pass. But really, for a simple fix I usually just squash it all into a single patch. Then somebody looking at the patch later says "Oh, this is what was broken, and here is how it was fixed."
-Peff
Quoted 7 lines · Michael J Gruber
So, should I prepare a series like:
1: test case and mark known fail
2: the 1 line fix
3: mark test passOr should 2+3 be squashed into one?
If "git mv" already has its own sets of tests with a good coverage, please strive to add a case that covers your fix to an existing script. Then step #1 above would be a patch to add a few "test_expect_failure" tests to an existing file, and step #3 would be a patch to flip expect_failure to expect_success.
And in such a case, for a single liner, all three can be squashed in to a single patch. It would show what changed in the code and have a few new test_expect_success tests added to the test suite, and it would be obvious to anybody who looks at such a change 6 months down the road that the test cases added by the patch are the cases that did not work without the changes to the code. It never makes sense to separate steps #2 and #3 for any fix.
If "git mv" did not have adequate test coverage, then please add a test script with both expect_success (for cases that should have been there when somebody worked on "git mv" originally), and expect_failure to expose the bug you found in your first patch. Again, the second patch would update the code and flip expect_failure to expect_success.
I see there is t7001-mv and even though it claims to concentrate on its operation from subdirectory, it has tests for more basic modes of the operation.
So my recommendation would be to have a single patch that:
(1) retitles t7001;
(2) adds your new -k tests to it; and
(3) adds the 1-liner fix.
Junio C Hamano venit, vidit, dixit 14.01.2009 20:02:
...
Quoted 10 lines
If "git mv" did not have adequate test coverage, then please add a test script with both
expect_success(for cases that should have been there when somebody worked on "git mv" originally), andexpect_failureto expose the bug you found in your first patch. Again, the second patch would update the code and flipexpect_failuretoexpect_success.I see there is t7001-mv and even though it claims to concentrate on its operation from subdirectory, it has tests for more basic modes of the operation.
Quoted 6 lines
So my recommendation would be to have a single patch that:
(1) retitles t7001;
(2) adds your new -k tests to it; and
(3) adds the 1-liner fix.
Sorry to bother you again, even after your detailed reply, but I'm a bit confused in multiple ways (I guess that makes for multiple bits...). First, you replied to my post, not my patch v2, but (time-wise) after my patch v2, so I'm not sure whether your reply applies to v2 as well or that one is OK.
Second, I took the title of t7001 to mean "mv into subdir" or "mv in and out subdir" in order to distiguish it from "mv oldname newname", albeit in English that leaves room from improvement.
Third, various parts of that test are in vastly different styles, and I haven't found any test writing directions other than "follow what is there", which leaves several alternatives. (Some use the test-lib repo, some create their own underneath, some make assumptions about the contents of "$TEST_DIRECTORY"/../.)
Fourth, t7001-mv is missing any test for "mv -k", and 2 of my 3 additional tests cover cases which work (pass) without the fix, which is why I kept it separate, being unrelated. Following all your arguments lead to the conclusion I should squash 2+3 (fix + mark expect_pass) - until I read your conclusion, which says squash all.
I'm happy to follow any variant ("1+2+3", "1 2+3", "1 2 3", in increasing order of preference) so there's no need to discuss or explain this further, just tell me "do x" ;)
Cheers,
Michael
Quoted 3 lines · Michael J Gruber
I'm happy to follow any variant ("1+2+3", "1 2+3", "1 2 3", in increasing order of preference) so there's no need to discuss or explain this further, just tell me "do x" ;)
Do nothing ;-) Your 1=3772923 and 2+3=be17262d are already in and we can include the fix in the next 1.6.1.X maintenance release.
Thanks.
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Michael J Gruber
I'm happy to follow any variant ("1+2+3", "1 2+3", "1 2 3", in increasing order of preference) so there's no need to discuss or explain this further, just tell me "do x" ;)
Do nothing ;-) Your 1=3772923 and 2+3=be17262d are already in and we can include the fix in the next 1.6.1.X maintenance release.
Thanks!
Signature · 1 line
Matthieu