From: tboegi@web.de
To: tboegi@web.de, git@vger.kernel.org, takimoto-j@kba.biglobe.ne.jp
Subject: [PATCH v3 1/1] macOS: ls-files path fails if path of workdir is NFD
Date: Tue, 21 May 2024 16:14:52 +0200 [thread overview]
Message-ID: <20240521141452.26210-1-tboegi@web.de> (raw)
In-Reply-To: <20240430032717281.IXLP.121462.mail.biglobe.ne.jp@biglobe.ne.jp>
From: Torsten Bögershausen <tboegi@web.de>
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 a test case that follows the bug report, with the simplification
that the 'ü' is replaced by an 'ä', which is already used as NFD and
NFC in t0050.
Precompose the result of getcwd(), if needed, just like all other
paths we use internally. That way, paths comparisons are all done
in NFC and we would correctly notice that the early part of the
path given as an absolute path matches the current directory.
One possible implementation would be to re-define getcwd() similar
to opendir(), readdir() and closedir(), but since there is already a
strbuf wrapper around getcwd(), and only this wrapper is used inside
the whole codebase, equip strbuf_getcwd() with a call to the newly
created function precompose_strbuf_if_needed().
Note that precompose_strbuf_if_needed() is a function under macOS,
and is a "no-op" on all other systems.
Add a missing call to precompose_string_if_needed() to this code
in setup.c :
`work_tree = precompose_string_if_needed(get_git_work_tree());`
Reported-by: Jun T <takimoto-j@kba.biglobe.ne.jp>
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
compat/precompose_utf8.c | 10 ++++++++++
compat/precompose_utf8.h | 1 +
git-compat-util.h | 1 +
setup.c | 2 +-
strbuf.c | 1 +
t/t0050-filesystem.sh | 26 ++++++++++++++++++++++++++
6 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index 0bd5c24250..5a7c90c90d 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -94,6 +94,16 @@ const char *precompose_string_if_needed(const char *in)
return in;
}
+void precompose_strbuf_if_needed(struct strbuf *sb)
+{
+ char *buf_prec = (char *)precompose_string_if_needed(sb->buf);
+ if (buf_prec != sb->buf) {
+ size_t buf_prec_len = strlen(buf_prec);
+ free(strbuf_detach(sb, NULL));
+ strbuf_attach(sb, buf_prec, buf_prec_len, buf_prec_len + 1);
+ }
+}
+
const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
{
int i = 0;
diff --git a/compat/precompose_utf8.h b/compat/precompose_utf8.h
index fea06cf28a..7c3cfcadb0 100644
--- a/compat/precompose_utf8.h
+++ b/compat/precompose_utf8.h
@@ -30,6 +30,7 @@ typedef struct {
const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix);
const char *precompose_string_if_needed(const char *in);
+void precompose_strbuf_if_needed(struct strbuf *sb);
void probe_utf8_pathname_composition(void);
PREC_DIR *precompose_utf8_opendir(const char *dirname);
diff --git a/git-compat-util.h b/git-compat-util.h
index 3e7a59b5ff..8b63108f16 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -331,6 +331,7 @@ static inline const char *precompose_string_if_needed(const char *in)
return in;
}
+#define precompose_strbuf_if_needed(a)
#define probe_utf8_pathname_composition()
#endif
diff --git a/setup.c b/setup.c
index 2e607632db..61f61496ec 100644
--- a/setup.c
+++ b/setup.c
@@ -48,7 +48,7 @@ static int abspath_part_inside_repo(char *path)
size_t wtlen;
char *path0;
int off;
- const char *work_tree = get_git_work_tree();
+ const char *work_tree = precompose_string_if_needed(get_git_work_tree());
struct strbuf realpath = STRBUF_INIT;
if (!work_tree)
diff --git a/strbuf.c b/strbuf.c
index 4c9ac6dc5e..b05581d8e7 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -569,6 +569,7 @@ int strbuf_getcwd(struct strbuf *sb)
strbuf_grow(sb, guessed_len);
if (getcwd(sb->buf, sb->alloc)) {
strbuf_setlen(sb, strlen(sb->buf));
+ precompose_strbuf_if_needed(sb);
return 0;
}
diff --git a/t/t0050-filesystem.sh b/t/t0050-filesystem.sh
index 325eb1c3cd..5a9ee5be92 100755
--- a/t/t0050-filesystem.sh
+++ b/t/t0050-filesystem.sh
@@ -156,4 +156,30 @@ test_expect_success CASE_INSENSITIVE_FS 'checkout with no pathspec and a case in
)
'
+test_expect_success 'git ls-files under NFD' '
+ (
+ mkdir -p "somewhere/$aumlcdiar" &&
+ mypwd=$PWD &&
+ cd "somewhere/$aumlcdiar" &&
+ git init &&
+ git --literal-pathspecs ls-files "$mypwd/somewhere/$aumlcdiar" 2>err &&
+ >expected &&
+ test_cmp expected err
+ )
+'
+
+# Re-do the same test. Note: global core.precomposeunicode is changed
+test_expect_success 'git ls-files under NFD. global precompose false' '
+ test_when_finished "git config --global --unset core.precomposeunicode" &&
+ (
+ mypwd=$PWD &&
+ cd "somewhere/$aumlcdiar" &&
+ git config --global core.precomposeunicode false &&
+ git config core.precomposeunicode true &&
+ git --literal-pathspecs ls-files "$mypwd/somewhere/$aumlcdiar" 2>err &&
+ >expected &&
+ test_cmp expected err
+ )
+'
+
test_done
--
2.41.0.394.ge43f4fd0bd
next prev parent reply other threads:[~2024-05-21 14:15 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240430032717281.IXLP.121462.mail.biglobe.ne.jp@biglobe.ne.jp>
2024-05-07 8:44 ` [PATCH v1 1/2] t0050: ls-files path fails if path of workdir is NFD tboegi
2024-05-07 17:30 ` Junio C Hamano
2024-05-07 8:44 ` [PATCH v1 2/2] strbuf_getcwd() needs precompse_strbuf_if_needed() tboegi
2024-05-07 17:22 ` Junio C Hamano
2024-05-09 15:24 ` Junio C Hamano
2024-05-09 15:29 ` Torsten Bögershausen
2024-05-07 17:47 ` Junio C Hamano
2024-05-08 0:32 ` brian m. carlson
2024-05-09 16:11 ` [PATCH v2 1/1] macOS: ls-files path fails if path of workdir is NFD tboegi
2024-05-09 16:37 ` Junio C Hamano
2024-05-19 7:03 ` Jun. T
2024-05-20 16:06 ` Torsten Bögershausen
2024-05-20 18:08 ` Junio C Hamano
2024-05-20 19:21 ` Torsten Bögershausen
2024-05-21 14:14 ` tboegi [this message]
2024-05-21 17:50 ` [PATCH v3 " Junio C Hamano
2024-05-21 20:57 ` Torsten Bögershausen
2024-05-21 22:15 ` Junio C Hamano
2024-05-23 15:33 ` Jun. T
2024-05-25 20:01 ` Torsten Bögershausen
2024-05-31 19:31 ` [PATCH v4 " tboegi
2024-06-01 15:55 ` Junio C Hamano
2024-06-02 19:40 ` Torsten Bögershausen
2024-06-04 0:56 ` Jun T
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240521141452.26210-1-tboegi@web.de \
--to=tboegi@web.de \
--cc=git@vger.kernel.org \
--cc=takimoto-j@kba.biglobe.ne.jp \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).