Skip to contentSkip to conversation

git on Mac OS and precomposed unicode

PATCH Superseded Torsten Bögershausen 2 participants 2 messages Jun 15, 2016

Subsystems (2)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 git on Mac OS and precomposed unicode

Mac OS X mangles file names containing unicode on file systems HFS+, VFAT or SAMBA. When a file using unicode code points outside ASCII is created on a HFS+ drive, the file name is converted into decomposed unicode and written to disk. No conversion is done if the file name is already decomposed unicode.

Calling open("\xc3\x84", ...) with a precomposed "Ä" yields the same result as open("\x41\xcc\x88",...) with a decomposed "Ä".

As a consequence, readdir() returns the file names in decomposed unicode, even if the user expects precomposed unicode. Unlike on HFS+, Mac OS X files on a VFAT drive (e.g. an USB drive), in precomposed unicode, but readdir() still returns file names in decomposed unicode. When a git repository is stored on a network share using SAMBA, file names are send over the wire and written to disk on the remote system in precomposed unicode, but Mac OS X readdir() returns decomposed unicode to be compatible with its behaviour on HFS+ and VFAT.

The unicode decomposition causes many problems:

- The names "git add" and other commands get from the end user may often be precomposed form (the decomposed form is not easily input from the keyboard), but when the commands read from the filesystem to see what it is going to update the index with already is on the filesystem, readdir() will give decomposed form, which is different.

- Similarly "git log", "git mv" and all other commands that need to compare pathnames found on the command line (often but not always precomposed form; a command line input resulting from globbing may be in decomposed) with pathnames found in the tree objects (should be precomposed form to be compatible with other systems and for consistency in general).

- The same for names stored in the index, which should be precomposed, that may need to be compared with the names read from readdir().

NFS mounted from Linux is fully transparent and does not suffer from the above.

As Mac OS X treats precomposed and decomposed file names as equal, we can

- wrap readdir() on Mac OS X to return the precomposed form, and

- normalize decomposed form given from the command line also to the precomposed form,

to ensure that all pathnames used in Git are always in the precomposed form. This behaviour can be requested by setting "core.precomposedunicode" configuration variable to true.

The code in compat/precomposed_utf8.c implements basically 4 new
functions: precomposed_utf8_opendir(), precomposed_utf8_readdir(), precomposed_utf8_closedir() and precompose_argv(). The first three are to wrap opendir(3), readdir(3), and closedir(3) functions.

The argv[] conversion allows to use the TAB filename completion done by the shell on command line. It tolerates other tools which use readdir() to feed decomposed file names into git.

When creating a new git repository with "git init" or "git clone", "core.precomposedunicode" will be set "false".

The user needs to activate this feature manually. She typically sets core.precomposedunicode to "true" on HFS and VFAT, or file systems mounted via SAMBA.

---
Thanks to Junio for intensive rewiew

Changes since v6:
Use Junios commit msg from pu; removed "mounted onto.."

s/mac_os_precomposed_unicode/precomposed_unicode

compat/precomposed_utf8.h:
- Introduced a more Posix/Unix like struct dirent_prec_psx which has d_ino, max_name_len, d_type, d_name

compat/precomposed_utf8.c:
- Dynamically realloc with larger dirent_prec_psx.d_name,
if d_name should become too small
(Side note: my Mac OS X 10.6 defines d_name[1024], but readdir() doesn't handle length >= 255 correct)
- When iconv_open() fails: let opendir() return, but die() when readdir() detects UTF-8 which needs the conversion.
- When iconv() fails in precomposed_utf8_readdir(), return original string

utf8.[ch]:
- Moved reencode_string_iconv() into utf8.c

t/t3910:
- Added test case to test "long" (<255 bytes) filenames

Other small changes indicated by code review.

 Documentation/config.txt     |   9 +++
 Makefile                     |   3 +
 builtin/init-db.c            |   1 +
 cache.h                      |   1 +
 compat/precomposed_utf8.c    | 189 +++++++++++++++++++++++++++++++++++++++++++
 compat/precomposed_utf8.h    |  43 ++++++++++
 config.c                     |   5 ++
 environment.c                |   1 +
 git-compat-util.h            |   9 +++
 parse-options.c              |   1 +
 t/t3910-mac-os-precompose.sh | 164 +++++++++++++++++++++++++++++++++++++
 utf8.c                       |  26 +++---
 utf8.h                       |   1 +
 13 files changed, 443 insertions(+), 10 deletions(-)
 create mode 100644 compat/precomposed_utf8.c
 create mode 100644 compat/precomposed_utf8.h
 create mode 100755 t/t3910-mac-os-precompose.sh
Documentation/config.txt +9 -0
index 915cb5a..4162cab 100644
@@ -210,6 +210,15 @@ The default is false, except linkgit:git-clone[1] or linkgit:git-init[1]
 will probe and set core.ignorecase true if appropriate when the repository
 is created.
 
+core.precomposedunicode::
+	This option is only used by Mac OS implementation of git.
+	When core.precomposedunicode=true, git reverts the unicode decomposition
+	of filenames done by Mac OS. This is useful when sharing a repository
+	between Mac OS and Linux or Windows.
+	(Git for Windows/msysGit 1.7.10 is needed, or git under cygwin 1.7).
+	When false, file names are handled fully transparent by git, which means
+	that file names are stored as decomposed unicode in the repository.
+
 core.trustctime::
 	If false, the ctime differences between the index and the
 	working tree are ignored; useful when the inode change time
Makefile +3 -0
index f62ca2a..55ceb10 100644
@@ -607,6 +607,7 @@ LIB_H += compat/bswap.h
 LIB_H += compat/cygwin.h
 LIB_H += compat/mingw.h
 LIB_H += compat/obstack.h
+LIB_H += compat/precomposed_utf8.h
 LIB_H += compat/terminal.h
 LIB_H += compat/win32/dirent.h
 LIB_H += compat/win32/poll.h
@@ -1000,6 +1001,8 @@ ifeq ($(uname_S),Darwin)
 	NO_MEMMEM = YesPlease
 	USE_ST_TIMESPEC = YesPlease
 	HAVE_DEV_TTY = YesPlease
+	COMPAT_OBJS += compat/precomposed_utf8.o
+	BASIC_CFLAGS += -DPRECOMPOSED_UNICODE
 endif
 ifeq ($(uname_S),SunOS)
 	NEEDS_SOCKET = YesPlease
builtin/init-db.c +1 -0
index 0dacb8b..244fb7f 100644
@@ -290,6 +290,7 @@ static int create_default_files(const char *template_path)
 		strcpy(path + len, "CoNfIg");
 		if (!access(path, F_OK))
 			git_config_set("core.ignorecase", "true");
+		probe_utf8_pathname_composition(path, len);
 	}
 
 	return reinit;
cache.h +1 -0
index 06413e1..5e551d3 100644
@@ -560,6 +560,7 @@ extern int read_replace_refs;
 extern int fsync_object_files;
 extern int core_preload_index;
 extern int core_apply_sparse_checkout;
+extern int precomposed_unicode;
 
 enum branch_track {
 	BRANCH_TRACK_UNSPECIFIED = -1,
compat/precomposed_utf8.c new file +189 -0

189 lines not on this page · raw message

compat/precomposed_utf8.h new file +43 -0

43 lines not on this page · raw message

config.c +5 -0
index 71ef171..918f426 100644
@@ -758,6 +758,11 @@ static int git_default_core_config(const char *var, const char *value)
 		return 0;
 	}
 
+	if (!strcmp(var, "core.precomposedunicode")) {
+		precomposed_unicode = git_config_bool(var, value);
+		return 0;
+	}
+
 	/* Add other config variables here and to Documentation/config.txt. */
 	return 0;
 }
environment.c +1 -0
index 669e498..85edd7f 100644
@@ -58,6 +58,7 @@ char *notes_ref_name;
 int grafts_replace_parents = 1;
 int core_apply_sparse_checkout;
 int merge_log_config = -1;
+int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
 struct startup_info *startup_info;
 unsigned long pack_size_limit_cfg;
 
git-compat-util.h +9 -0
index 5bd9ad7..600fa9e 100644
@@ -153,6 +153,15 @@
 #endif
 #endif
 
+/* used on Mac OS X */
+#ifdef PRECOMPOSED_UNICODE
+#include "compat/precomposed_utf8.h"
+#else
+#define precompose_str(in,i_nfd2nfc)
+#define precompose_argv(c,v)
+#define probe_utf8_pathname_composition(a,b)
+#endif
+
 #ifndef NO_LIBGEN_H
 #include <libgen.h>
 #else
parse-options.c +1 -0
index ab70c29..c1c66bd 100644
@@ -476,6 +476,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
 		usage_with_options(usagestr, options);
 	}
 
+	precompose_argv(argc, argv);
 	return parse_options_end(&ctx);
 }
 
t/t3910-mac-os-precompose.sh new file +164 -0

164 lines not on this page · raw message

utf8.c +16 -10
index 8acbc66..a544f15 100644
@@ -433,19 +433,12 @@ int is_encoding_utf8(const char *name)
 #else
 	typedef char * iconv_ibp;
 #endif
-char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
+char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv)
 {
-	iconv_t conv;
-	size_t insz, outsz, outalloc;
+	size_t outsz, outalloc;
 	char *out, *outpos;
 	iconv_ibp cp;
 
-	if (!in_encoding)
-		return NULL;
-	conv = iconv_open(out_encoding, in_encoding);
-	if (conv == (iconv_t) -1)
-		return NULL;
-	insz = strlen(in);
 	outsz = insz;
 	outalloc = outsz + 1; /* for terminating NUL */
 	out = xmalloc(outalloc);
@@ -459,7 +452,6 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e
 			size_t sofar;
 			if (errno != E2BIG) {
 				free(out);
-				iconv_close(conv);
 				return NULL;
 			}
 			/* insz has remaining number of bytes.
@@ -478,6 +470,20 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e
 			break;
 		}
 	}
+	return out;
+}
+
+char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
+{
+	iconv_t conv;
+	char *out;
+
+	if (!in_encoding)
+		return NULL;
+	conv = iconv_open(out_encoding, in_encoding);
+	if (conv == (iconv_t) -1)
+		return NULL;
+	out = reencode_string_iconv(in, strlen(in), conv);
 	iconv_close(conv);
 	return out;
 }
utf8.h +1 -0
index 81f2c82..3c0ae76 100644
@@ -14,6 +14,7 @@ int strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
 			     int indent, int indent2, int width);
 
 #ifndef NO_ICONV
+char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv);
 char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);
 #else
 #define reencode_string(a,b,c) NULL
Signature · 1 line
1.7.11.1.47.ge34cb30
#1 lore raw

Documentation/config.txt The default is false, except linkgit:git-clone[1] or linkgit:git-init[1] · line 220

Quoted 4 lines · Torsten Bögershausen
Documentation/config.txt +4 -0
+core.precomposedunicode::
+	...
+	When false, file names are handled fully transparent by git, which means
+	that file names are stored as decomposed unicode in the repository.

I do not think it means any such thing.

We just take whatever the platform throws at us and shove that in the repository. On MacOS X with HFS+, it may be decomposed UTF-8, but we do not even try to ensure everything (like the path added by somebody else on a BSD system in a commit that you fetched) is in a particular encoding.

Makefile LIB_H += compat/bswap.h · line 610

Quoted 9 lines
Makefile +1 -0
index f62ca2a..55ceb10 100644
@@ -607,6 +607,7 @@ LIB_H += compat/bswap.h
 LIB_H += compat/cygwin.h
 LIB_H += compat/mingw.h
 LIB_H += compat/obstack.h
+LIB_H += compat/precomposed_utf8.h

Micronit. Shouldn't these all be called "precompose_utf8" throughout the patch?

We are asking Git "please normalize by precompose any UTF-8 pathnames" when we give the -DPRECOMPOSE_UNICODE C-preprocessor macro, and compat/precompose_utf8.[ch] are to implement the machinery to do so.

compat/precomposed_utf8.c line 3

Quoted 9 lines
compat/precomposed_utf8.c new file +3 -0
new file mode 100644
index 0000000..14bb0ce
@@ -0,0 +1,189 @@
+/* Converts filenames from decomposed unicode into precomposed unicode.
+   Used on MacOS X.
+*/

Micronit.

/*
* Multi-line comments begin by slash asterisk newline.
* and ends with a run of SP to align asterisk, asterisk
* and then newline, like this.
*/

compat/precomposed_utf8.c line 11

Quoted 8 lines
compat/precomposed_utf8.c new file +6 -0
+#define __PRECOMPOSED_UNICODE_C__
+
+#include "cache.h"
+#include "utf8.h"
+#include "precomposed_utf8.h"
 
 
+#include "stdio.h"

You shouldn't need "stdio.h" as you are including "git-compat-util.h" via "cache.h".

compat/precomposed_utf8.h line 40

Quoted 13 lines
compat/precomposed_utf8.h new file +7 -0
new file mode 100644
index 0000000..708a1c6
...
+#ifndef __PRECOMPOSED_UNICODE_C__
+#define dirent dirent_prec_psx
+#define opendir(n) precomposed_utf8_opendir(n)
+#define readdir(d) precomposed_utf8_readdir(d)
+#define closedir(d) precomposed_utf8_closedir(d)
+#define DIR PREC_DIR
+#endif /* __PRECOMPOSED_UNICODE_C__ */

Hrm, this is not wrong per-se, but looks somewhat unwieldy.

Quoted 20 lines
compat/precomposed_utf8.h new file +2 -0
+#define  __PRECOMPOSED_UNICODE_H__
+#endif /* __PRECOMPOSED_UNICODE_H__ */
utf8.c +7 -0
index 8acbc66..a544f15 100644
@@ -433,19 +433,12 @@ int is_encoding_utf8(const char *name)
...
@@ -478,6 +470,20 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e
 			break;
 		}
 	}
+	return out;
+}
+
+char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
+{
+	iconv_t conv;
+	char *out;

utf8.c char *reencode_string(const char *in, const char *out_encoding, const char *in_e · line 489

Quoted 10 lines
utf8.c +7 -0
+
+	if (!in_encoding)
+		return NULL;
+	conv = iconv_open(out_encoding, in_encoding);
+	if (conv == (iconv_t) -1)
+		return NULL;
+	out = reencode_string_iconv(in, strlen(in), conv);
 	iconv_close(conv);
 	return out;
 }

Much nicer ;-).