Debian Codegraph @

internal/pathutil/hfs.go

raw
1 package pathutil
2
3 import "unicode"
4
5 // hfsIgnoredCodepoints contains Unicode code points that HFS+ ignores
6 // during path normalization. A path component containing these
7 // characters between the bytes of ".git" (or ".gitmodules", etc.)
8 // will be treated as that name by HFS+, so they have to be filtered
9 // out before comparison.
10 //
11 // See upstream Git utf8.c next_hfs_char in tag v2.54.0[1].
12 //
13 // [1]: https://github.com/git/git/blob/v2.54.0/utf8.c#L703-L740
14 var hfsIgnoredCodepoints = map[rune]struct{}{
15 0x200c: {}, // ZERO WIDTH NON-JOINER
16 0x200d: {}, // ZERO WIDTH JOINER
17 0x200e: {}, // LEFT-TO-RIGHT MARK
18 0x200f: {}, // RIGHT-TO-LEFT MARK
19 0x202a: {}, // LEFT-TO-RIGHT EMBEDDING
20 0x202b: {}, // RIGHT-TO-LEFT EMBEDDING
21 0x202c: {}, // POP DIRECTIONAL FORMATTING
22 0x202d: {}, // LEFT-TO-RIGHT OVERRIDE
23 0x202e: {}, // RIGHT-TO-LEFT OVERRIDE
24 0x206a: {}, // INHIBIT SYMMETRIC SWAPPING
25 0x206b: {}, // ACTIVATE SYMMETRIC SWAPPING
26 0x206c: {}, // INHIBIT ARABIC FORM SHAPING
27 0x206d: {}, // ACTIVATE ARABIC FORM SHAPING
28 0x206e: {}, // NATIONAL DIGIT SHAPES
29 0x206f: {}, // NOMINAL DIGIT SHAPES
30 0xfeff: {}, // ZERO WIDTH NO-BREAK SPACE
31 }
32
33 // IsHFSDot reports whether part would be treated as ".<needle>" on an
34 // HFS+ filesystem after stripping ignored Unicode code points and
35 // folding ASCII to lower case. The needle is the lowercase ASCII
36 // suffix without the leading dot (e.g. "git", "gitmodules"). It
37 // mirrors upstream Git's is_hfs_dot_generic and is the building
38 // block of IsHFSDotGit / IsHFSDotGitmodules.
39 //
40 // Reference: upstream Git utf8.c is_hfs_dot_generic at L741-L774 and
41 // the dotgit family at L784-L809 in tag v2.54.0[1].
42 //
43 // [1]: https://github.com/git/git/blob/v2.54.0/utf8.c#L741-L809
44 func IsHFSDot(part, needle string) bool {
45 runes := []rune(part)
46 i := 0
47
48 // skip ignored code points, then expect '.'
49 for i < len(runes) {
50 if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok {
51 break
52 }
53 i++
54 }
55 if i >= len(runes) || runes[i] != '.' {
56 return false
57 }
58 i++
59
60 // match needle case-insensitively, skipping ignored code points
61 for _, expected := range needle {
62 for i < len(runes) {
63 if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok {
64 break
65 }
66 i++
67 }
68 if i >= len(runes) {
69 return false
70 }
71 r := runes[i]
72 if r > 127 {
73 return false
74 }
75 if unicode.ToLower(r) != expected {
76 return false
77 }
78 i++
79 }
80
81 // skip trailing ignored code points
82 for i < len(runes) {
83 if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok {
84 break
85 }
86 i++
87 }
88
89 // must be at end of component
90 return i == len(runes)
91 }
92
93 // IsHFSDotGit reports whether part is an HFS+ equivalent of ".git".
94 func IsHFSDotGit(part string) bool { return IsHFSDot(part, "git") }
95
96 // IsHFSDotGitmodules reports whether part is an HFS+ equivalent of
97 // ".gitmodules", catching attempts to plant the file via Unicode
98 // code points that HFS+ would strip during normalisation.
99 func IsHFSDotGitmodules(part string) bool { return IsHFSDot(part, "gitmodules") }