git/t/t1010-mktree.sh

71 lines
1.7 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='git mktree'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success setup '
t1010: fix unnoticed failure on Windows On Microsoft Windows, a directory name should never end with a period. Quoting from Microsoft documentation[1]: Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. Naming a directory with a trailing period is indeed perilous: % git init foo % cd foo % mkdir a. % git status warning: could not open directory 'a./': No such file or directory The t1010 "setup" test: for d in a a. a0 do mkdir "$d" && echo "$d/one" >"$d/one" && git add "$d" done && runs afoul of this Windows limitation, as can be observed when running the test verbosely: error: open("a./one"): No such file or directory error: unable to index file 'a./one' fatal: adding files failed The reason this problem has gone unnoticed for so long is twofold. First, the failed `git add` is swallowed silently because the loop is not terminated explicitly by `|| return 1` to signal the failure. Second, none of the tests in this script care about the literal directory names ("a", "a.", "a0") or the specific number of tree entries. They care instead about the order of entries in the tree, and that the tree synthesized in the index and created by `git write-tree` matches the tree created by the output of `git ls-tree` fed into `git mktree`, thus the absence of "a./one" has no impact on the tests. Skipping these tests on Windows by, for instance, checking the FUNNYNAMES predicate would avoid the problem, however, the funny-looking name is not what is being tested here. Rather, the tests are about checking that `git mktree` produces stable results for various input conditions, such as when the input order is not consistent or when an object is missing. Therefore, resolve the problem simply by using a directory name which is legal on Windows and sorts the same as "a.". While at it, add the missing `|| return 1` to the loop body in order to catch this sort of problem in the future. [1]: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-12-11 09:58:37 +00:00
for d in a a- a0
do
mkdir "$d" && echo "$d/one" >"$d/one" &&
t1010: fix unnoticed failure on Windows On Microsoft Windows, a directory name should never end with a period. Quoting from Microsoft documentation[1]: Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. Naming a directory with a trailing period is indeed perilous: % git init foo % cd foo % mkdir a. % git status warning: could not open directory 'a./': No such file or directory The t1010 "setup" test: for d in a a. a0 do mkdir "$d" && echo "$d/one" >"$d/one" && git add "$d" done && runs afoul of this Windows limitation, as can be observed when running the test verbosely: error: open("a./one"): No such file or directory error: unable to index file 'a./one' fatal: adding files failed The reason this problem has gone unnoticed for so long is twofold. First, the failed `git add` is swallowed silently because the loop is not terminated explicitly by `|| return 1` to signal the failure. Second, none of the tests in this script care about the literal directory names ("a", "a.", "a0") or the specific number of tree entries. They care instead about the order of entries in the tree, and that the tree synthesized in the index and created by `git write-tree` matches the tree created by the output of `git ls-tree` fed into `git mktree`, thus the absence of "a./one" has no impact on the tests. Skipping these tests on Windows by, for instance, checking the FUNNYNAMES predicate would avoid the problem, however, the funny-looking name is not what is being tested here. Rather, the tests are about checking that `git mktree` produces stable results for various input conditions, such as when the input order is not consistent or when an object is missing. Therefore, resolve the problem simply by using a directory name which is legal on Windows and sorts the same as "a.". While at it, add the missing `|| return 1` to the loop body in order to catch this sort of problem in the future. [1]: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-12-11 09:58:37 +00:00
git add "$d" || return 1
done &&
echo zero >one &&
git update-index --add --info-only one &&
git write-tree --missing-ok >tree.missing &&
git ls-tree $(cat tree.missing) >top.missing &&
git ls-tree -r $(cat tree.missing) >all.missing &&
echo one >one &&
git add one &&
git write-tree >tree &&
git ls-tree $(cat tree) >top &&
git ls-tree -r $(cat tree) >all &&
test_tick &&
git commit -q -m one &&
H=$(git rev-parse HEAD) &&
git update-index --add --cacheinfo 160000 $H sub &&
test_tick &&
git commit -q -m two &&
git rev-parse HEAD^{tree} >tree.withsub &&
git ls-tree HEAD >top.withsub &&
git ls-tree -r HEAD >all.withsub
'
test_expect_success 'ls-tree piped to mktree (1)' '
git mktree <top >actual &&
test_cmp tree actual
'
test_expect_success 'ls-tree piped to mktree (2)' '
git mktree <top.withsub >actual &&
test_cmp tree.withsub actual
'
test_expect_success 'ls-tree output in wrong order given to mktree (1)' '
perl -e "print reverse <>" <top |
git mktree >actual &&
test_cmp tree actual
'
test_expect_success 'ls-tree output in wrong order given to mktree (2)' '
perl -e "print reverse <>" <top.withsub |
git mktree >actual &&
test_cmp tree.withsub actual
'
test_expect_success 'allow missing object with --missing' '
git mktree --missing <top.missing >actual &&
test_cmp tree.missing actual
'
test_expect_success 'mktree refuses to read ls-tree -r output (1)' '
test_must_fail git mktree <all >actual
'
test_expect_success 'mktree refuses to read ls-tree -r output (2)' '
test_must_fail git mktree <all.withsub >actual
'
test_done