Skip to contentSkip to conversation

git on Mac OS and precomposed unicode

RFC PATCH Superseded tb 1 participant 1 message Jun 15, 2016

Subsystems (2)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

tb

author
#0 lore raw

RFC PATCH v3 1/1 git on Mac OS and precomposed unicode

Allow git on Mac OS to store file names in the index in precomposed unicode, while the file system used decomposed unicode.

When a file called "LATIN CAPITAL LETTER A WITH DIAERESIS" (in utf-8 encoded as 0xc3 0x84) is created,
the filesystem converts "precomposed unicode" into "decomposed unicode", which means that readdir() will return 0x41 0xcc 0x88.
When true, git reverts the unicode decomposition of filenames. This is useful when pulling/pushing from repositories containing utf-8 encoded filenames using precomposed utf-8 (like Linux).

This feature is automatically switched on when "git init" is run, and the file system is doing UTF-8 decompostion.
(Which has been observed on HFS+, SMBFS and VFAT, but not on NFS) It can be switched off by setting core.macosforcenfc=false

It is implemented by re-defining the readdir() functions. File names are converted into precomposed UTF-8.

---
 Documentation/config.txt     |    9 ++
 Makefile                     |    3 +
 builtin/init-db.c            |    2 +
 compat/precomposed_utf8.c    |  213 ++++++++++++++++++++++++++++++++++++++++++
 compat/precomposed_utf8.h    |   39 ++++++++
 git-compat-util.h            |    9 ++
 git.c                        |    1 +
 t/t0050-filesystem.sh        |    1 +
 t/t3910-mac-os-precompose.sh |  117 +++++++++++++++++++++++
 9 files changed, 394 insertions(+), 0 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 2959390..01b9465 100644
@@ -175,6 +175,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 pulling/pushing from repositories containing utf-8
+	encoded filenames using precomposed unicode (like Linux).
+	When false, file names are handled fully transparent by git.
+	If in doubt, set core.precomposedunicode=false.
+
 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 b21d2f1..a912b45 100644
@@ -519,6 +519,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/win32/pthread.h
 LIB_H += compat/win32/syslog.h
 LIB_H += compat/win32/poll.h
@@ -884,6 +885,8 @@ ifeq ($(uname_S),Darwin)
 	endif
 	NO_MEMMEM = YesPlease
 	USE_ST_TIMESPEC = YesPlease
+	COMPAT_OBJS += compat/precomposed_utf8.o
+	BASIC_CFLAGS += -DPRECOMPOSED_UNICODE
 endif
 ifeq ($(uname_S),SunOS)
 	NEEDS_SOCKET = YesPlease
builtin/init-db.c +2 -0
index 0dacb8b..06953df 100644
@@ -290,6 +290,8 @@ 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;
compat/precomposed_utf8.c new file +213 -0

213 lines not on this page · raw message

compat/precomposed_utf8.h new file +39 -0
new file mode 100644
index 0000000..3eca7b9
@@ -0,0 +1,39 @@
+#ifndef __PRECOMPOSED_UNICODE_H__
+#if defined (PRECOMPOSED_UNICODE)
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <iconv.h>
+
+
+typedef struct {
+	iconv_t ic_precompose;
+	DIR *dirp;
+	struct dirent dirent_nfc;
+} PRECOMPOSED_UTF_DIR;
+
+char *str_precompose(const char *in, iconv_t ic_precompose);
+void argv_precompose(int argc, const char **argv);
+void probe_utf8_pathname_composition(char *, int);
+
+PRECOMPOSED_UTF_DIR *precomposed_utf8_opendir(const char *dirname);
+struct dirent *precomposed_utf8_readdir(PRECOMPOSED_UTF_DIR *dirp);
+int precomposed_utf8_closedir(PRECOMPOSED_UTF_DIR *dirp);
+
+#ifndef __PRECOMPOSED_UNICODE_C__
+
+#define opendir(n) precomposed_utf8_opendir(n)
+#define readdir(d) precomposed_utf8_readdir(d)
+#define closedir(d) precomposed_utf8_closedir(d)
+#define DIR PRECOMPOSED_UTF_DIR
+#endif  /* __PRECOMPOSED_UNICODE_C__ */
+
+#else
+
+#define str_precompose(in,i_nfd2nfc) (NULL)
+#define argv_precompose(c,v)
+#define probe_utf8_pathname_composition(a,b)
+#endif /* PRECOMPOSED_UNICODE */
+
+#define  __PRECOMPOSED_UNICODE_H__
+#endif /* __PRECOMPOSED_UNICODE_H__ */
git-compat-util.h +9 -0
index 230e198..708b178 100644
@@ -90,6 +90,15 @@
 #include <windows.h>
 #endif
 
+#if defined (PRECOMPOSED_UNICODE)
+#include "compat/precomposed_utf8.h"
+#else
+#define str_precompose(in,i_nfd2nfc) (NULL)
+#define argv_precompose(c,v)
+#define probe_utf8_pathname_composition(a,b)
+
+#endif
+
 #include <unistd.h>
 #include <stdio.h>
 #include <sys/stat.h>
git.c +1 -0
index 8e34903..6b2ffb7 100644
@@ -298,6 +298,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 		    startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
 			trace_repo_setup(prefix);
 	}
+	argv_precompose(argc, argv);
 	commit_pager_choice();
 
 	if (!help && p->option & NEED_WORK_TREE)
t/t0050-filesystem.sh +1 -0
index 1542cf6..befe39e 100755
@@ -126,6 +126,7 @@ test_expect_success "setup unicode normalization tests" '
 
   test_create_repo unicode &&
   cd unicode &&
+  git config core.precomposedunicode false &&
   touch "$aumlcdiar" &&
   git add "$aumlcdiar" &&
   git commit -m initial &&
t/t3910-mac-os-precompose.sh new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
1.7.8.rc0.43.gb49a8