Skip to content

Commit a42643a

Browse files
peffgitster
authored andcommitted
read-cache: optionally disallow HFS+ .git variants
The point of disallowing ".git" in the index is that we would never want to accidentally overwrite files in the repository directory. But this means we need to respect the filesystem's idea of when two paths are equal. The prior commit added a helper to make such a comparison for HFS+; let's use it in verify_path. We make this check optional for two reasons: 1. It restricts the set of allowable filenames, which is unnecessary for people who are not on HFS+. In practice this probably doesn't matter, though, as the restricted names are rather obscure and almost certainly would never come up in practice. 2. It has a minor performance penalty for every path we insert into the index. This patch ties the check to the core.protectHFS config option. Though this is expected to be most useful on OS X, we allow it to be set everywhere, as HFS+ may be mounted on other platforms. The variable does default to on for OS X, though. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6162a1d commit a42643a

File tree

8 files changed

+45
-5
lines changed

8 files changed

+45
-5
lines changed

Documentation/config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ core.precomposeunicode::
234234
When false, file names are handled fully transparent by Git,
235235
which is backward compatible with older versions of Git.
236236

237+
core.protectHFS::
238+
If set to true, do not allow checkout of paths that would
239+
be considered equivalent to `.git` on an HFS+ filesystem.
240+
Defaults to `true` on Mac OS, and `false` elsewhere.
241+
237242
core.trustctime::
238243
If false, the ctime differences between the index and the
239244
working tree are ignored; useful when the inode change time

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ extern int fsync_object_files;
584584
extern int core_preload_index;
585585
extern int core_apply_sparse_checkout;
586586
extern int precomposed_unicode;
587+
extern int protect_hfs;
587588

588589
/*
589590
* The character that begins a commented line in user-editable file

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,11 @@ static int git_default_core_config(const char *var, const char *value)
881881
return 0;
882882
}
883883

884+
if (!strcmp(var, "core.protecthfs")) {
885+
protect_hfs = git_config_bool(var, value);
886+
return 0;
887+
}
888+
884889
/* Add other config variables here and to Documentation/config.txt. */
885890
return 0;
886891
}

config.mak.uname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ ifeq ($(uname_S),Darwin)
9797
HAVE_DEV_TTY = YesPlease
9898
COMPAT_OBJS += compat/precompose_utf8.o
9999
BASIC_CFLAGS += -DPRECOMPOSE_UNICODE
100+
BASIC_CFLAGS += -DPROTECT_HFS_DEFAULT=1
100101
endif
101102
ifeq ($(uname_S),SunOS)
102103
NEEDS_SOCKET = YesPlease

environment.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
6363
struct startup_info *startup_info;
6464
unsigned long pack_size_limit_cfg;
6565

66+
#ifndef PROTECT_HFS_DEFAULT
67+
#define PROTECT_HFS_DEFAULT 0
68+
#endif
69+
int protect_hfs = PROTECT_HFS_DEFAULT;
70+
6671
/*
6772
* The character that begins a commented line in user-editable file
6873
* that is subject to stripspace.

read-cache.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "resolve-undo.h"
1515
#include "strbuf.h"
1616
#include "varint.h"
17+
#include "utf8.h"
1718

1819
static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
1920

@@ -786,6 +787,8 @@ int verify_path(const char *path)
786787
return 1;
787788
if (is_dir_sep(c)) {
788789
inside:
790+
if (protect_hfs && is_hfs_dotgit(path))
791+
return 0;
789792
c = *path++;
790793
if ((c == '.' && !verify_dotfile(path)) ||
791794
is_dir_sep(c) || c == '\0')

t/t1014-read-tree-confusing.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,39 @@ test_expect_success 'create base tree' '
1111
tree=$(git rev-parse HEAD^{tree})
1212
'
1313

14-
while read path; do
15-
test_expect_success "reject $path at end of path" '
14+
test_expect_success 'enable core.protectHFS for rejection tests' '
15+
git config core.protectHFS true
16+
'
17+
18+
while read path pretty; do
19+
: ${pretty:=$path}
20+
test_expect_success "reject $pretty at end of path" '
1621
printf "100644 blob %s\t%s" "$blob" "$path" >tree &&
1722
bogus=$(git mktree <tree) &&
1823
test_must_fail git read-tree $bogus
1924
'
2025

21-
test_expect_success "reject $path as subtree" '
26+
test_expect_success "reject $pretty as subtree" '
2227
printf "040000 tree %s\t%s" "$tree" "$path" >tree &&
2328
bogus=$(git mktree <tree) &&
2429
test_must_fail git read-tree $bogus
2530
'
26-
done <<-\EOF
31+
done <<-EOF
2732
.
2833
..
2934
.git
3035
.GIT
36+
${u200c}.Git {u200c}.Git
37+
.gI${u200c}T .gI{u200c}T
38+
.GiT${u200c} .GiT{u200c}
3139
EOF
3240

41+
test_expect_success 'utf-8 paths allowed with core.protectHFS off' '
42+
test_when_finished "git read-tree HEAD" &&
43+
test_config core.protectHFS false &&
44+
printf "100644 blob %s\t%s" "$blob" ".gi${u200c}t" >tree &&
45+
ok=$(git mktree <tree) &&
46+
git read-tree $ok
47+
'
48+
3349
test_done

t/test-lib.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ _z40=0000000000000000000000000000000000000000
154154
LF='
155155
'
156156

157-
export _x05 _x40 _z40 LF
157+
# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
158+
# when case-folding filenames
159+
u200c=$(printf '\342\200\214')
160+
161+
export _x05 _x40 _z40 LF u200c
158162

159163
# Each test should start with something like this, after copyright notices:
160164
#

0 commit comments

Comments
 (0)