2011-12-06 21:43:36 +04:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description="Test whether cache-tree is properly updated
|
|
|
|
|
|
|
|
Tests whether various commands properly update and/or rewrite the
|
|
|
|
cache-tree extension.
|
|
|
|
"
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
cmp_cache_tree () {
|
2018-03-24 10:44:39 +03:00
|
|
|
test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
|
2018-05-13 05:24:15 +03:00
|
|
|
sed "s/$OID_REGEX/SHA/" <actual >filtered &&
|
2021-01-23 16:00:37 +03:00
|
|
|
test_cmp "$1" filtered &&
|
|
|
|
rm filtered
|
2011-12-06 21:43:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# We don't bother with actually checking the SHA1:
|
2018-03-24 10:44:39 +03:00
|
|
|
# test-tool dump-cache-tree already verifies that all existing data is
|
2011-12-06 21:43:36 +04:00
|
|
|
# correct.
|
2021-01-23 16:00:39 +03:00
|
|
|
generate_expected_cache_tree () {
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
pathspec="$1" &&
|
|
|
|
dir="$2${2:+/}" &&
|
|
|
|
git ls-tree --name-only HEAD -- "$pathspec" >files &&
|
|
|
|
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
|
|
|
|
printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
|
|
|
|
while read subtree
|
2014-07-14 00:28:19 +04:00
|
|
|
do
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
|
|
|
|
done <subtrees
|
2014-07-14 00:28:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
test_cache_tree () {
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
generate_expected_cache_tree "." >expect &&
|
|
|
|
cmp_cache_tree expect &&
|
|
|
|
rm expect actual files subtrees &&
|
|
|
|
git status --porcelain -- ':!status' ':!expected.status' >status &&
|
|
|
|
if test -n "$1"
|
|
|
|
then
|
|
|
|
test_cmp "$1" status
|
|
|
|
else
|
|
|
|
test_must_be_empty status
|
|
|
|
fi
|
2011-12-06 21:43:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
test_invalid_cache_tree () {
|
2014-07-11 04:31:25 +04:00
|
|
|
printf "invalid %s ()\n" "" "$@" >expect &&
|
2018-03-24 10:44:39 +03:00
|
|
|
test-tool dump-cache-tree |
|
2014-07-11 04:31:25 +04:00
|
|
|
sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
|
|
|
|
test_cmp expect actual
|
2011-12-06 21:43:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
test_no_cache_tree () {
|
2021-01-23 16:00:37 +03:00
|
|
|
>expect &&
|
2011-12-06 21:43:36 +04:00
|
|
|
cmp_cache_tree expect
|
|
|
|
}
|
|
|
|
|
2014-07-14 00:28:19 +04:00
|
|
|
test_expect_success 'initial commit has cache-tree' '
|
2011-12-06 21:43:36 +04:00
|
|
|
test_commit foo &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'read-tree HEAD establishes cache-tree' '
|
|
|
|
git read-tree HEAD &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git-add invalidates cache-tree' '
|
|
|
|
test_when_finished "git reset --hard; git read-tree HEAD" &&
|
2014-07-06 08:06:56 +04:00
|
|
|
echo "I changed this file" >foo &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git add foo &&
|
|
|
|
test_invalid_cache_tree
|
|
|
|
'
|
|
|
|
|
2014-07-11 04:31:25 +04:00
|
|
|
test_expect_success 'git-add in subdir invalidates cache-tree' '
|
|
|
|
test_when_finished "git reset --hard; git read-tree HEAD" &&
|
|
|
|
mkdir dirx &&
|
|
|
|
echo "I changed this file" >dirx/foo &&
|
|
|
|
git add dirx/foo &&
|
|
|
|
test_invalid_cache_tree
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' '
|
|
|
|
git tag no-children &&
|
|
|
|
test_when_finished "git reset --hard no-children; git read-tree HEAD" &&
|
|
|
|
mkdir dir1 dir2 &&
|
|
|
|
test_commit dir1/a &&
|
|
|
|
test_commit dir2/b &&
|
|
|
|
echo "I changed this file" >dir1/a &&
|
2021-01-23 16:00:37 +03:00
|
|
|
test_when_finished "rm before" &&
|
|
|
|
cat >before <<-\EOF &&
|
|
|
|
SHA (3 entries, 2 subtrees)
|
|
|
|
SHA dir1/ (1 entries, 0 subtrees)
|
|
|
|
SHA dir2/ (1 entries, 0 subtrees)
|
|
|
|
EOF
|
2014-07-14 00:28:19 +04:00
|
|
|
cmp_cache_tree before &&
|
|
|
|
echo "I changed this file" >dir1/a &&
|
2014-07-11 04:31:25 +04:00
|
|
|
git add dir1/a &&
|
2021-01-23 16:00:37 +03:00
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
invalid (2 subtrees)
|
|
|
|
invalid dir1/ (0 subtrees)
|
|
|
|
SHA dir2/ (1 entries, 0 subtrees)
|
|
|
|
EOF
|
2014-07-14 00:28:19 +04:00
|
|
|
cmp_cache_tree expect
|
2014-07-11 04:31:25 +04:00
|
|
|
'
|
|
|
|
|
2011-12-06 21:43:36 +04:00
|
|
|
test_expect_success 'update-index invalidates cache-tree' '
|
|
|
|
test_when_finished "git reset --hard; git read-tree HEAD" &&
|
2014-07-06 08:06:56 +04:00
|
|
|
echo "I changed this file" >foo &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git update-index --add foo &&
|
|
|
|
test_invalid_cache_tree
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'write-tree establishes cache-tree' '
|
2018-03-24 10:44:56 +03:00
|
|
|
test-tool scrap-cache-tree &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git write-tree &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
2018-03-24 10:44:56 +03:00
|
|
|
test_expect_success 'test-tool scrap-cache-tree works' '
|
2011-12-06 21:43:36 +04:00
|
|
|
git read-tree HEAD &&
|
2018-03-24 10:44:56 +03:00
|
|
|
test-tool scrap-cache-tree &&
|
2011-12-06 21:43:36 +04:00
|
|
|
test_no_cache_tree
|
|
|
|
'
|
|
|
|
|
2011-12-06 21:43:38 +04:00
|
|
|
test_expect_success 'second commit has cache-tree' '
|
2011-12-06 21:43:36 +04:00
|
|
|
test_commit bar &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
|
|
|
'
|
|
|
|
|
2014-11-18 20:22:31 +03:00
|
|
|
test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
test_when_finished "git reset --hard" &&
|
2014-07-14 00:28:19 +04:00
|
|
|
cat <<-\EOT >foo.c &&
|
|
|
|
int foo()
|
|
|
|
{
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
int bar()
|
|
|
|
{
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
git add foo.c &&
|
|
|
|
test_invalid_cache_tree &&
|
|
|
|
git commit -m "add a file" &&
|
|
|
|
test_cache_tree &&
|
|
|
|
cat <<-\EOT >foo.c &&
|
|
|
|
int foo()
|
|
|
|
{
|
|
|
|
return 43;
|
|
|
|
}
|
|
|
|
int bar()
|
|
|
|
{
|
|
|
|
return 44;
|
|
|
|
}
|
|
|
|
EOT
|
2018-07-02 03:23:42 +03:00
|
|
|
test_write_lines p 1 "" s n y q |
|
2014-07-14 00:28:19 +04:00
|
|
|
git commit --interactive -m foo &&
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
cat <<-\EOF >expected.status &&
|
|
|
|
M foo.c
|
|
|
|
EOF
|
|
|
|
test_cache_tree expected.status
|
2014-07-14 00:28:19 +04:00
|
|
|
'
|
|
|
|
|
reopen_tempfile(): truncate opened file
We provide a reopen_tempfile() function, which is in turn
used by reopen_lockfile(). The idea is that a caller may
want to rewrite the tempfile without letting go of the lock.
And that's what our one caller does: after running
add--interactive, "commit -p" will update the cache-tree
extension of the index and write out the result, all while
holding the lock.
However, because we open the file with only the O_WRONLY
flag, the existing index content is left in place, and we
overwrite it starting at position 0. If the new index after
updating the cache-tree is smaller than the original, those
final bytes are not overwritten and remain in the file. This
results in a corrupt index, since those cruft bytes are
interpreted as part of the trailing hash (or even as an
extension, if there are enough bytes).
This bug actually pre-dates reopen_tempfile(); the original
code from 9c4d6c0297 (cache-tree: Write updated cache-tree
after commit, 2014-07-13) has the same bug, and those lines
were eventually refactored into the tempfile module. Nobody
noticed until now for two reasons:
- the bug can only be triggered in interactive mode
("commit -p" or "commit -i")
- the size of the index must shrink after updating the
cache-tree, which implies a non-trivial deletion. Notice
that the included test actually has to create a 2-deep
hierarchy. A single level is not enough to actually cause
shrinkage.
The fix is to truncate the file before writing out the
second index. We can do that at the caller by using
ftruncate(). But we shouldn't have to do that. There is no
other place in Git where we want to open a file and
overwrite bytes, making reopen_tempfile() a confusing and
error-prone interface. Let's pass O_TRUNC there, which gives
callers the same state they had after initially opening the
file or lock.
It's possible that we could later add a caller that wants
something else (e.g., to open with O_APPEND). But this is
the only caller we've had in the history of the codebase.
Let's punt on doing anything more clever until another one
comes along.
Reported-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-09-05 02:36:43 +03:00
|
|
|
test_expect_success PERL 'commit -p with shrinking cache-tree' '
|
2019-06-29 01:59:25 +03:00
|
|
|
mkdir -p deep/very-long-subdir &&
|
|
|
|
echo content >deep/very-long-subdir/file &&
|
reopen_tempfile(): truncate opened file
We provide a reopen_tempfile() function, which is in turn
used by reopen_lockfile(). The idea is that a caller may
want to rewrite the tempfile without letting go of the lock.
And that's what our one caller does: after running
add--interactive, "commit -p" will update the cache-tree
extension of the index and write out the result, all while
holding the lock.
However, because we open the file with only the O_WRONLY
flag, the existing index content is left in place, and we
overwrite it starting at position 0. If the new index after
updating the cache-tree is smaller than the original, those
final bytes are not overwritten and remain in the file. This
results in a corrupt index, since those cruft bytes are
interpreted as part of the trailing hash (or even as an
extension, if there are enough bytes).
This bug actually pre-dates reopen_tempfile(); the original
code from 9c4d6c0297 (cache-tree: Write updated cache-tree
after commit, 2014-07-13) has the same bug, and those lines
were eventually refactored into the tempfile module. Nobody
noticed until now for two reasons:
- the bug can only be triggered in interactive mode
("commit -p" or "commit -i")
- the size of the index must shrink after updating the
cache-tree, which implies a non-trivial deletion. Notice
that the included test actually has to create a 2-deep
hierarchy. A single level is not enough to actually cause
shrinkage.
The fix is to truncate the file before writing out the
second index. We can do that at the caller by using
ftruncate(). But we shouldn't have to do that. There is no
other place in Git where we want to open a file and
overwrite bytes, making reopen_tempfile() a confusing and
error-prone interface. Let's pass O_TRUNC there, which gives
callers the same state they had after initially opening the
file or lock.
It's possible that we could later add a caller that wants
something else (e.g., to open with O_APPEND). But this is
the only caller we've had in the history of the codebase.
Let's punt on doing anything more clever until another one
comes along.
Reported-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-09-05 02:36:43 +03:00
|
|
|
git add deep &&
|
|
|
|
git commit -m add &&
|
|
|
|
git rm -r deep &&
|
|
|
|
|
|
|
|
before=$(wc -c <.git/index) &&
|
|
|
|
git commit -m delete -p &&
|
|
|
|
after=$(wc -c <.git/index) &&
|
|
|
|
|
|
|
|
# double check that the index shrank
|
|
|
|
test $before -gt $after &&
|
|
|
|
|
|
|
|
# and that our index was not corrupted
|
|
|
|
git fsck
|
|
|
|
'
|
|
|
|
|
2014-07-14 00:28:19 +04:00
|
|
|
test_expect_success 'commit in child dir has cache-tree' '
|
|
|
|
mkdir dir &&
|
|
|
|
>dir/child.t &&
|
|
|
|
git add dir/child.t &&
|
|
|
|
git commit -m dir/child.t &&
|
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
2011-12-06 21:43:39 +04:00
|
|
|
test_expect_success 'reset --hard gives cache-tree' '
|
2018-03-24 10:44:56 +03:00
|
|
|
test-tool scrap-cache-tree &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git reset --hard &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
2011-12-06 21:43:39 +04:00
|
|
|
test_expect_success 'reset --hard without index gives cache-tree' '
|
2011-12-06 21:43:36 +04:00
|
|
|
rm -f .git/index &&
|
2021-09-11 20:08:42 +03:00
|
|
|
git clean -fd &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git reset --hard &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
2014-07-06 08:06:56 +04:00
|
|
|
test_expect_success 'checkout gives cache-tree' '
|
|
|
|
git tag current &&
|
2011-12-06 21:43:36 +04:00
|
|
|
git checkout HEAD^ &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2011-12-06 21:43:36 +04:00
|
|
|
'
|
|
|
|
|
2014-07-06 08:06:56 +04:00
|
|
|
test_expect_success 'checkout -b gives cache-tree' '
|
|
|
|
git checkout current &&
|
|
|
|
git checkout -b prev HEAD^ &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
2014-07-06 08:06:56 +04:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -B gives cache-tree' '
|
|
|
|
git checkout current &&
|
|
|
|
git checkout -B prev HEAD^ &&
|
2014-07-14 00:28:19 +04:00
|
|
|
test_cache_tree
|
|
|
|
'
|
|
|
|
|
2015-07-28 22:30:40 +03:00
|
|
|
test_expect_success 'merge --ff-only maintains cache-tree' '
|
|
|
|
git checkout current &&
|
|
|
|
git checkout -b changes &&
|
|
|
|
test_commit llamas &&
|
|
|
|
test_commit pachyderm &&
|
|
|
|
test_cache_tree &&
|
|
|
|
git checkout current &&
|
|
|
|
test_cache_tree &&
|
|
|
|
git merge --ff-only changes &&
|
|
|
|
test_cache_tree
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'merge maintains cache-tree' '
|
|
|
|
git checkout current &&
|
|
|
|
git checkout -b changes2 &&
|
|
|
|
test_commit alpacas &&
|
|
|
|
test_cache_tree &&
|
|
|
|
git checkout current &&
|
|
|
|
test_commit struthio &&
|
|
|
|
test_cache_tree &&
|
|
|
|
git merge changes2 &&
|
|
|
|
test_cache_tree
|
|
|
|
'
|
|
|
|
|
2014-07-14 00:28:19 +04:00
|
|
|
test_expect_success 'partial commit gives cache-tree' '
|
|
|
|
git checkout -b partial no-children &&
|
|
|
|
test_commit one &&
|
|
|
|
test_commit two &&
|
|
|
|
echo "some change" >one.t &&
|
|
|
|
git add one.t &&
|
|
|
|
echo "some other change" >two.t &&
|
|
|
|
git commit two.t -m partial &&
|
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0297 (cache-tree: Write updated
cache-tree after commit, 2014-07-13) used "ls-files" in lieu of
"ls-tree" because it wanted to test the data in the index, since this
test is testing the cache-tree extension.
Change the test to instead use "ls-tree" for traversal, and then
explicitly check how HEAD differs from the index. This is more easily
understood, and less fragile as numerous past bug fixes[1][2][3] to
the old code we're replacing demonstrate.
As an aside this would be a bit easier if empty pathspecs hadn't been
made an error in d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (pathspec: die on empty strings
as pathspec, 2017-06-06).
If that was still allowed this code could be simplified slightly:
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
index 9bf66c9e68..0b02881f55 100755
--- a/t/t0090-cache-tree.sh
+++ b/t/t0090-cache-tree.sh
@@ -18,19 +18,18 @@ cmp_cache_tree () {
# test-tool dump-cache-tree already verifies that all existing data is
# correct.
generate_expected_cache_tree () {
- pathspec="$1" &&
- dir="$2${2:+/}" &&
+ pathspec="$1${1:+/}" &&
git ls-tree --name-only HEAD -- "$pathspec" >files &&
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
- printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) &&
while read subtree
do
- generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ generate_expected_cache_tree "$subtree" || return 1
done <subtrees
}
test_cache_tree () {
- generate_expected_cache_tree "." >expect &&
+ generate_expected_cache_tree >expect &&
cmp_cache_tree expect &&
rm expect actual files subtrees &&
git status --porcelain -- ':!status' ':!expected.status' >status &&
1. c8db708d5d (t0090: avoid passing empty string to printf %d,
2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
/usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
2019-11-27)
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-01-23 16:00:40 +03:00
|
|
|
cat <<-\EOF >expected.status &&
|
|
|
|
M one.t
|
|
|
|
EOF
|
|
|
|
test_cache_tree expected.status
|
2014-07-06 08:06:56 +04:00
|
|
|
'
|
|
|
|
|
2014-09-03 01:16:20 +04:00
|
|
|
test_expect_success 'no phantom error when switching trees' '
|
|
|
|
mkdir newdir &&
|
|
|
|
>newdir/one &&
|
|
|
|
git add newdir/one &&
|
|
|
|
git checkout 2>errors &&
|
2018-08-20 00:57:22 +03:00
|
|
|
test_must_be_empty errors
|
2014-09-03 01:16:20 +04:00
|
|
|
'
|
|
|
|
|
2015-08-27 20:07:54 +03:00
|
|
|
test_expect_success 'switching trees does not invalidate shared index' '
|
2018-09-06 05:48:07 +03:00
|
|
|
(
|
|
|
|
sane_unset GIT_TEST_SPLIT_INDEX &&
|
|
|
|
git update-index --split-index &&
|
|
|
|
>split &&
|
|
|
|
git add split &&
|
|
|
|
test-tool dump-split-index .git/index | grep -v ^own >before &&
|
|
|
|
git commit -m "as-is" &&
|
|
|
|
test-tool dump-split-index .git/index | grep -v ^own >after &&
|
|
|
|
test_cmp before after
|
|
|
|
)
|
2015-08-27 20:07:54 +03:00
|
|
|
'
|
|
|
|
|
2011-12-06 21:43:36 +04:00
|
|
|
test_done
|