diff mbox series

[02/19] t1010: fix unnoticed failure on Windows

Message ID 20211209051115.52629-3-sunshine@sunshineco.com (mailing list archive)
State Accepted
Commit afb31ad95f035e5abff7c8a6b4a12195f41e857a
Headers show
Series tests: fix broken &&-chains & abort loops on error | expand

Commit Message

Eric Sunshine Dec. 9, 2021, 5:10 a.m. UTC
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 actual directory
names or even the number of tree entries. They care only 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`, and
the failure of `git add "a./one"` doesn't change that outcome.

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 (i.e. "a-" rather than "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>
---
 t/t1010-mktree.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Elijah Newren Dec. 9, 2021, 4:27 p.m. UTC | #1
On Thu, Dec 9, 2021 at 12:22 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> 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 actual directory
> names or even the number of tree entries.

Is this true?  The names look like they were selected on the basis that
    '.' < '/' < '0'

> They care only 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`, and
> the failure of `git add "a./one"` doesn't change that outcome.

We have multiple paths in the code that write tree structures, and
it's important that the order be
  100644 blob $OID1 a.
  040000 tree $OID2 a
  100644 blob $OID3 a0

i.e. that 'a' as a tree object sorts as though it were actually named
'a/'.  I suspect the code might have been making sure that the
different paths creating tree objects did so consistently, and the
special handling of subdirectories is the edge case that needs careful
checks.

> 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 (i.e. "a-" rather than "a."). While at it, add the
> missing `|| return 1` to the loop body in order to catch this sort of
> problem in the future.

The choice to replace '.' with '-' is excellent since '-' < '/' as well.

>
> [1]: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>  t/t1010-mktree.sh | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
> index 48bfad07ab..3c08194526 100755
> --- a/t/t1010-mktree.sh
> +++ b/t/t1010-mktree.sh
> @@ -6,10 +6,10 @@ TEST_PASSES_SANITIZE_LEAK=true
>  . ./test-lib.sh
>
>  test_expect_success setup '
> -       for d in a a. a0
> +       for d in a a- a0
>         do
>                 mkdir "$d" && echo "$d/one" >"$d/one" &&
> -               git add "$d"
> +               git add "$d" || return 1
>         done &&
>         echo zero >one &&
>         git update-index --add --info-only one &&
> --
> 2.34.1.307.g9b7440fafd

The patch itself looks good; I'm just slightly unsure about the one
claim in the commit message.
Eric Sunshine Dec. 9, 2021, 4:45 p.m. UTC | #2
On Thu, Dec 9, 2021 at 11:28 AM Elijah Newren <newren@gmail.com> wrote:
> On Thu, Dec 9, 2021 at 12:22 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > 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 actual directory
> > names or even the number of tree entries.
>
> Is this true?  The names look like they were selected on the basis that
>     '.' < '/' < '0'
>
> We have multiple paths in the code that write tree structures, and
> it's important that the order be
>   100644 blob $OID1 a.
>   040000 tree $OID2 a
>   100644 blob $OID3 a0
>
> i.e. that 'a' as a tree object sorts as though it were actually named
> 'a/'.  I suspect the code might have been making sure that the
> different paths creating tree objects did so consistently, and the
> special handling of subdirectories is the edge case that needs careful
> checks.

I meant it in the sense that the tests don't care about the literal
names "a", "a.", "a0". They do care about ordering...

> > 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.

... which I hoped to convey to readers by saying "stable results ...
when input order is not consistent", but I guess I wasn't clear enough
or obvious enough, or this mention is too far removed from the "don't
care about the actual names" statement.

> > Therefore, resolve the problem simply by using a directory name which is
> > legal on Windows (i.e. "a-" rather than "a."). While at it, add the
> > missing `|| return 1` to the loop body in order to catch this sort of
> > problem in the future.
>
> The choice to replace '.' with '-' is excellent since '-' < '/' as well.

I can rewrite the earlier paragraph to mention name ordering so it's
more obvious that it has significance.

I am a bit hesitant about spamming the list with a reroll of this
lengthy series just for this change, though I can certainly do it if
you consider it important.
diff mbox series

Patch

diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
index 48bfad07ab..3c08194526 100755
--- a/t/t1010-mktree.sh
+++ b/t/t1010-mktree.sh
@@ -6,10 +6,10 @@  TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
-	for d in a a. a0
+	for d in a a- a0
 	do
 		mkdir "$d" && echo "$d/one" >"$d/one" &&
-		git add "$d"
+		git add "$d" || return 1
 	done &&
 	echo zero >one &&
 	git update-index --add --info-only one &&