Skip to contentSkip to conversation

git on Mac OS and precomposed unicode

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

Subsystems (2)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 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            |   22 +++++
 compat/darwin.c              |  208 ++++++++++++++++++++++++++++++++++++++++++
 compat/darwin.h              |   31 ++++++
 git-compat-util.h            |    8 ++
 git.c                        |    1 +
 t/t0050-filesystem.sh        |    1 +
 t/t3910-mac-os-precompose.sh |  117 +++++++++++++++++++++++
 9 files changed, 400 insertions(+), 0 deletions(-)
 create mode 100644 compat/darwin.c
 create mode 100644 compat/darwin.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..596900e 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/darwin.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/darwin.o
+	BASIC_CFLAGS += -DPRECOMPOSED_UNICODE
 endif
 ifeq ($(uname_S),SunOS)
 	NEEDS_SOCKET = YesPlease
builtin/init-db.c +22 -0
index 0dacb8b..88c9de1 100644
@@ -290,6 +290,28 @@ static int create_default_files(const char *template_path)
 		strcpy(path + len, "CoNfIg");
 		if (!access(path, F_OK))
 			git_config_set("core.ignorecase", "true");
+#if defined (PRECOMPOSED_UNICODE)
+		{
+			const static char *auml_nfc = "\xc3\xa4";
+			const static char *auml_nfd = "\x61\xcc\x88";
+			int output_fd;
+			path[len] = 0;
+			strcpy(path + len, auml_nfc);
+			output_fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600);
+			if (output_fd >=0) {
+				close(output_fd);
+				path[len] = 0;
+				strcpy(path + len, auml_nfd);
+				if (0 == access(path, R_OK))
+					git_config_set("core.precomposedunicode", "true");
+				else
+					git_config_set("core.precomposedunicode", "false");
+				path[len] = 0;
+				strcpy(path + len, auml_nfc);
+				unlink(path);
+			}
+		}
+#endif
 	}
 
 	return reinit;
compat/darwin.c new file +208 -0

208 lines not on this page · raw message

compat/darwin.h new file +31 -0
new file mode 100644
index 0000000..094f930
@@ -0,0 +1,31 @@
+#ifndef __DARWIN_H__
+#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;
+} DARWIN_DIR;
+
+char *str_precompose(const char *in, iconv_t ic_precompose);
+
+void argv_precompose(int argc, const char **argv);
+
+DARWIN_DIR *darwin_opendir(const char *dirname);
+struct dirent *darwin_readdir(DARWIN_DIR *dirp);
+int darwin_closedir(DARWIN_DIR *dirp);
+
+#ifndef __DARWIN_C__
+#define opendir(n) darwin_opendir(n)
+#define readdir(d) darwin_readdir(d)
+#define closedir(d) darwin_closedir(d)
+#define DIR DARWIN_DIR
+
+#endif  /* __DARWIN_C__ */
+
+#define  __DARWIN_H__
+#endif /* __DARWIN_H__ */
git-compat-util.h +8 -0
index 230e198..859dfcf 100644
@@ -90,6 +90,14 @@
 #include <windows.h>
 #endif
 
+#if defined (PRECOMPOSED_UNICODE)
+#include "compat/darwin.h"
+#else
+#define str_precompose(in,i_nfd2nfc) (NULL)
+#define argv_precompose(c,v)
+
+#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
#1 lore raw

builtin/init-db.c static int create_default_files(const char *template_path) · line 296

Quoted 12 lines · Torsten Bögershausen
builtin/init-db.c +4 -0
index 0dacb8b..88c9de1 100644
@@ -290,6 +290,28 @@ static int create_default_files(const char *template_path)
 		strcpy(path + len, "CoNfIg");
 		if (!access(path, F_OK))
 			git_config_set("core.ignorecase", "true");
+#if defined (PRECOMPOSED_UNICODE)
+		{
+...
+#endif

I'd prefer just a single, unconditional call here:

		probe_utf8_pathname_composition(path, len);

with something like this at the top of the file:

	#ifndef PRECOMPOSED_UNICODE
	#define probe_utf8_pathname_composition(a,b) /* nothing */
        #else
        void probe_utf8_pathname_composition(char *, int);
	#endif

and implementation of the function body in compat/darwin.c (Didn't I see a comment on the name of this file, by the way? What was the conclusion of the discussion?).

builtin/init-db.c static int create_default_files(const char *template_path) · line 298

Quoted 2 lines
builtin/init-db.c +2 -0
+void
+argv_precompose(int argc, const char **argv)

Style.

builtin/init-db.c static int create_default_files(const char *template_path) · line 306

Quoted 8 lines
builtin/init-db.c +8 -0
+{
+	int i = 0;
+	int first_arg = 0; /* convert everything */
+	const char *oldarg;
+	char *newarg;
+	iconv_t ic_precompose;
+
+	git_config(precomposed_unicode_config, NULL);

Hmmmmm. Is it safe to call git_config() this early in the program? Have we determined if we are in a git managed repository and where its $GIT_DIR is?

#2 lore raw

On 01/09/2012 08:52 PM, Junio C Hamano wrote:
[snip]

Quoted 1 line

probe_utf8_pathname_composition(path, len);

Done

[snip]

Quoted 3 lines

and implementation of the function body in compat/darwin.c (Didn't I see a comment on the name of this file, by the way? What was the conclusion of the discussion?).

I renamed the compat/darwin.[ch] into compat/precomposed_utf8.[ch]

Quoted 4 lines
Quoted 4 lines
compat/darwin.c new file +4 -0
+{
+	int i = 0;
+	int first_arg = 0; /* convert everything */
+	const char *oldarg;
Quoted 9 lines

compat/darwin.c line 107

Quoted 4 lines
compat/darwin.c new file +4 -0
+	char *newarg;
+	iconv_t ic_precompose;
+
+	git_config(precomposed_unicode_config, NULL);

Hmmmmm. Is it safe to call git_config() this early in the program? Have we determined if we are in a git managed repository and where its $GIT_DIR is?

According to my understanding, yes:
git_config is called with the custom function precomposed_unicode_config():

git_config(precomposed_unicode_config, NULL);

where
precomposed_unicode_config()

fishes for the config variable "core.precomposedunicode" and puts the result into int precomposed_unicode_config.

After that, when precomposed_unicode_config==0, argv_precomposed() will return.

I'll send the V3 RFC/PATCH
/Torsten