1
0
mirror of https://github.com/git/git synced 2024-06-30 22:54:27 +00:00
git/t/t3910-mac-os-precompose.sh
Torsten Bögershausen 71fa8d2212 macOS: ls-files path fails if path of workdir is NFD
Under macOS, `git ls-files path` does not work (gives an error)
if the absolute 'path' contains characters in NFD (decomposed).
This happens when core.precomposeunicode is true, which is the
most common case. The bug report says:

$ cd somewhere          # some safe place, /tmp or ~/tmp etc.
$ mkdir $'u\xcc\x88'    # ü in NFD
$ cd ü                  # or cd $'u\xcc\x88' or cd $'\xc3\xbc'
$ git init
$ git ls-files $'/somewhere/u\xcc\x88'   # NFD
  fatal: /somewhere/ü: '/somewhere/ü' is outside repository at '/somewhere/ü'
$ git ls-files $'/somewhere/\xc3\xbc'    # NFC
(the same error as above)

In the 'fatal:' error message, there are three ü;
the 1st and 2nd are in NFC, the 3rd is in NFD.

Add test cases that follows the bug report, with the simplification
that the 'ü' is replaced by an 'ä', which is already used as NFD and
NFC in t3910.

The solution is to add a call to precompose_string_if_needed()
to this code in setup.c :
`work_tree = precompose_string_if_needed(get_git_work_tree());`

There is, however, a limitation with this very usage of Git:
The (repo) local .gitconfig file is not used, only the global
"core.precomposeunicode" is taken into account, if it is set (or not).
To set it to true is a good recommendation anyway, and here is the
analyzes from Jun T :

The problem is the_repository->config->hash_initialized
is set to 1 before the_repository->commondir is set to ".git".
Due to this, .git/config is never read, and precomposed_unicode
is never set to 1 (remains -1).

run_builtin() {
    setup_git_directory() {
        strbuf_getcwd() {   # setup.c:1542
            precompose_{strbuf,string}_if_needed() {
                # precomposed_unicode is still -1
                git_congig_get_bool("core.precomposeunicode") {
                    git_config_check_init() {
                        repo_read_config() {
                            git_config_init() {
                                # !!!
                                the_repository->config->hash_initialized=1
                                # !!!
                            }
                            # does not read .git/config since
                            # the_repository->commondir is still NULL
                        }
                    }
                }
                returns without converting to NFC
            }
            returns cwd in NFD
        }

        setup_discovered_git_dir() {
            set_git_work_tree(".") {
                repo_set_worktree() {
                    # this function indirectly calls strbuf_getcwd()
                    # --> precompose_{strbuf,string}_if_needed() -->
                    # {git,repo}_config_get_bool("core.precomposeunicode"),
                    # but does not try to read .git/config since
                    # the_repository->config->hash_initialized
                    # is already set to 1 above. And it will not read
                    # .git/config even if hash_initialized is 0
                    # since the_repository->commondir is still NULL.

                    the_repository->worktree = NFD
                }
            }
        }

        setup_git_env() {
            repo_setup_gitdir() {
                repo_set_commondir() {
                    # finally commondir is set here
                    the_repository->commondir = ".git"
                }
            }
        }

    } // END setup_git_directory

Reported-by: Jun T <takimoto-j@kba.biglobe.ne.jp>
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-05-31 13:13:40 -07:00

261 lines
7.4 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2012 Torsten Bögershausen
#
test_description='utf-8 decomposed (nfd) converted to precomposed (nfc)'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
if ! test_have_prereq UTF8_NFD_TO_NFC
then
skip_all="filesystem does not corrupt utf-8"
test_done
fi
# create utf-8 variables
Adiarnfc=$(printf '\303\204')
Adiarnfd=$(printf 'A\314\210')
Odiarnfc=$(printf '\303\226')
Odiarnfd=$(printf 'O\314\210')
AEligatu=$(printf '\303\206')
Invalidu=$(printf '\303\377')
#Create a string with 255 bytes (decomposed)
Alongd=$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd #21 Byte
Alongd=$Alongd$Alongd$Alongd #63 Byte
Alongd=$Alongd$Alongd$Alongd$Alongd$Adiarnfd #255 Byte
#Create a string with 254 bytes (precomposed)
Alongc=$AEligatu$AEligatu$AEligatu$AEligatu$AEligatu #10 Byte
Alongc=$Alongc$Alongc$Alongc$Alongc$Alongc #50 Byte
Alongc=$Alongc$Alongc$Alongc$Alongc$Alongc #250 Byte
Alongc=$Alongc$AEligatu$AEligatu #254 Byte
ls_files_nfc_nfd () {
test_when_finished "git config --global --unset core.precomposeunicode" &&
prglbl=$1
prlocl=$2
aumlcreat=$3
aumllist=$4
git config --global core.precomposeunicode $prglbl &&
(
rm -rf .git &&
mkdir -p "somewhere/$prglbl/$prlocl/$aumlcreat" &&
mypwd=$PWD &&
cd "somewhere/$prglbl/$prlocl/$aumlcreat" &&
git init &&
git config core.precomposeunicode $prlocl &&
git --literal-pathspecs ls-files "$mypwd/somewhere/$prglbl/$prlocl/$aumllist" 2>err &&
>expected &&
test_cmp expected err
)
}
test_expect_success "detect if nfd needed" '
precomposeunicode=$(git config core.precomposeunicode) &&
test "$precomposeunicode" = true &&
git config core.precomposeunicode true
'
test_expect_success "setup" '
>x &&
git add x &&
git commit -m "1st commit" &&
git rm x &&
git commit -m "rm x"
'
test_expect_success "setup case mac" '
git checkout -b mac_os
'
# This will test nfd2nfc in git diff
test_expect_success "git diff f.Adiar" '
touch f.$Adiarnfc &&
git add f.$Adiarnfc &&
echo f.Adiarnfc >f.$Adiarnfc &&
git diff f.$Adiarnfd >expect &&
git diff f.$Adiarnfc >actual &&
test_cmp expect actual &&
git reset HEAD f.Adiarnfc &&
rm f.$Adiarnfc expect actual
'
# This will test nfd2nfc in git diff-files
test_expect_success "git diff-files f.Adiar" '
touch f.$Adiarnfc &&
git add f.$Adiarnfc &&
echo f.Adiarnfc >f.$Adiarnfc &&
git diff-files f.$Adiarnfd >expect &&
git diff-files f.$Adiarnfc >actual &&
test_cmp expect actual &&
git reset HEAD f.Adiarnfc &&
rm f.$Adiarnfc expect actual
'
# This will test nfd2nfc in git diff-index
test_expect_success "git diff-index f.Adiar" '
touch f.$Adiarnfc &&
git add f.$Adiarnfc &&
echo f.Adiarnfc >f.$Adiarnfc &&
git diff-index HEAD f.$Adiarnfd >expect &&
git diff-index HEAD f.$Adiarnfc >actual &&
test_cmp expect actual &&
git reset HEAD f.Adiarnfc &&
rm f.$Adiarnfc expect actual
'
# This will test nfd2nfc in readdir()
test_expect_success "add file Adiarnfc" '
echo f.Adiarnfc >f.$Adiarnfc &&
git add f.$Adiarnfc &&
git commit -m "add f.$Adiarnfc"
'
# This will test nfd2nfc in git diff-tree
test_expect_success "git diff-tree f.Adiar" '
echo f.Adiarnfc >>f.$Adiarnfc &&
git diff-tree HEAD f.$Adiarnfd >expect &&
git diff-tree HEAD f.$Adiarnfc >actual &&
test_cmp expect actual &&
git checkout f.$Adiarnfc &&
rm expect actual
'
# This will test nfd2nfc in git stage()
test_expect_success "stage file d.Adiarnfd/f.Adiarnfd" '
mkdir d.$Adiarnfd &&
echo d.$Adiarnfd/f.$Adiarnfd >d.$Adiarnfd/f.$Adiarnfd &&
git stage d.$Adiarnfd/f.$Adiarnfd &&
git commit -m "add d.$Adiarnfd/f.$Adiarnfd"
'
test_expect_success "add link Adiarnfc" '
ln -s d.$Adiarnfd/f.$Adiarnfd l.$Adiarnfc &&
git add l.$Adiarnfc &&
git commit -m "add l.Adiarnfc"
'
# This will test git log
test_expect_success "git log f.Adiar" '
git log f.$Adiarnfc > f.Adiarnfc.log &&
git log f.$Adiarnfd > f.Adiarnfd.log &&
test -s f.Adiarnfc.log &&
test -s f.Adiarnfd.log &&
test_cmp f.Adiarnfc.log f.Adiarnfd.log &&
rm f.Adiarnfc.log f.Adiarnfd.log
'
# This will test git ls-files
test_expect_success "git lsfiles f.Adiar" '
git ls-files f.$Adiarnfc > f.Adiarnfc.log &&
git ls-files f.$Adiarnfd > f.Adiarnfd.log &&
test -s f.Adiarnfc.log &&
test -s f.Adiarnfd.log &&
test_cmp f.Adiarnfc.log f.Adiarnfd.log &&
rm f.Adiarnfc.log f.Adiarnfd.log
'
# This will test git mv
test_expect_success "git mv" '
git mv f.$Adiarnfd f.$Odiarnfc &&
git mv d.$Adiarnfd d.$Odiarnfc &&
git mv l.$Adiarnfd l.$Odiarnfc &&
git commit -m "mv Adiarnfd Odiarnfc"
'
# Files can be checked out as nfc
# And the link has been corrected from nfd to nfc
test_expect_success "git checkout nfc" '
rm f.$Odiarnfc &&
git checkout f.$Odiarnfc
'
# Make it possible to checkout files with their NFD names
test_expect_success "git checkout file nfd" '
rm -f f.* &&
git checkout f.$Odiarnfd
'
# Make it possible to checkout links with their NFD names
test_expect_success "git checkout link nfd" '
rm l.* &&
git checkout l.$Odiarnfd
'
test_expect_success "setup case mac2" '
git checkout main &&
git reset --hard &&
git checkout -b mac_os_2
'
# This will test nfd2nfc in git commit
test_expect_success "commit file d2.Adiarnfd/f.Adiarnfd" '
mkdir d2.$Adiarnfd &&
echo d2.$Adiarnfd/f.$Adiarnfd >d2.$Adiarnfd/f.$Adiarnfd &&
git add d2.$Adiarnfd/f.$Adiarnfd &&
git commit -m "add d2.$Adiarnfd/f.$Adiarnfd" -- d2.$Adiarnfd/f.$Adiarnfd
'
test_expect_success "setup for long decomposed filename" '
git checkout main &&
git reset --hard &&
git checkout -b mac_os_long_nfd_fn
'
test_expect_success "Add long decomposed filename" '
echo longd >$Alongd &&
git add * &&
git commit -m "Long filename"
'
test_expect_success "setup for long precomposed filename" '
git checkout main &&
git reset --hard &&
git checkout -b mac_os_long_nfc_fn
'
test_expect_success "Add long precomposed filename" '
echo longc >$Alongc &&
git add * &&
git commit -m "Long filename"
'
test_expect_failure 'handle existing decomposed filenames' '
echo content >"verbatim.$Adiarnfd" &&
git -c core.precomposeunicode=false add "verbatim.$Adiarnfd" &&
git commit -m "existing decomposed file" &&
git ls-files --exclude-standard -o "verbatim*" >untracked &&
test_must_be_empty untracked
'
test_expect_success "unicode decomposed: git restore -p . " '
DIRNAMEPWD=dir.Odiarnfc &&
DIRNAMEINREPO=dir.$Adiarnfc &&
export DIRNAMEPWD DIRNAMEINREPO &&
git init "$DIRNAMEPWD" &&
(
cd "$DIRNAMEPWD" &&
mkdir "$DIRNAMEINREPO" &&
cd "$DIRNAMEINREPO" &&
echo "Initial" >file &&
git add file &&
echo "More stuff" >>file &&
echo y | git restore -p .
)
'
# Test if the global core.precomposeunicode stops autosensing
test_expect_success "respect git config --global core.precomposeunicode" '
test_when_finished "git config --global --unset core.precomposeunicode" &&
git config --global core.precomposeunicode true &&
rm -rf .git &&
git init &&
precomposeunicode=$(git config core.precomposeunicode) &&
test "$precomposeunicode" = "true"
'
test_expect_success "ls-files false false nfd nfd" '
ls_files_nfc_nfd false false $Adiarnfd $Adiarnfd
'
test_expect_success "ls-files false true nfd nfd" '
ls_files_nfc_nfd false true $Adiarnfd $Adiarnfd
'
test_expect_success "ls-files true false nfd nfd" '
ls_files_nfc_nfd true false $Adiarnfd $Adiarnfd
'
test_expect_success "ls-files true true nfd nfd" '
ls_files_nfc_nfd true true $Adiarnfd $Adiarnfd
'
test_done