GeezerVison is showing github.com — internal: pathutil, port HFS+ and dotgit detection · hiddeco/go-git@bfb0daf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +1-97Lines changed: 1 addition & 97 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package git | 2 | 2 |
| 3 | | -import ( | 4 | | - "runtime" | 5 | | - "unicode" | 6 | | -) | | 3 | +import "runtime" | 7 | 4 |
| 8 | 5 | // defaultProtectHFS returns the default value for core.protectHFS | 9 | 6 | // when not explicitly configured. Matches upstream Git behaviour: | 10 | 7 | // enabled by default on macOS. | 11 | 8 | func defaultProtectHFS() bool { | 12 | 9 | return runtime.GOOS == "darwin" | 13 | 10 | } | 14 | | - | 15 | | -// hfsIgnoredCodepoints contains Unicode code points that HFS+ ignores | 16 | | -// during path normalization. A path containing these characters between | 17 | | -// the characters of ".git" will be treated as ".git" by HFS+. | 18 | | -// | 19 | | -// See upstream Git utf8.c next_hfs_char() for the full list. | 20 | | -var hfsIgnoredCodepoints = map[rune]struct{}{ | 21 | | - 0x200c: {}, // ZERO WIDTH NON-JOINER | 22 | | - 0x200d: {}, // ZERO WIDTH JOINER | 23 | | - 0x200e: {}, // LEFT-TO-RIGHT MARK | 24 | | - 0x200f: {}, // RIGHT-TO-LEFT MARK | 25 | | - 0x202a: {}, // LEFT-TO-RIGHT EMBEDDING | 26 | | - 0x202b: {}, // RIGHT-TO-LEFT EMBEDDING | 27 | | - 0x202c: {}, // POP DIRECTIONAL FORMATTING | 28 | | - 0x202d: {}, // LEFT-TO-RIGHT OVERRIDE | 29 | | - 0x202e: {}, // RIGHT-TO-LEFT OVERRIDE | 30 | | - 0x206a: {}, // INHIBIT SYMMETRIC SWAPPING | 31 | | - 0x206b: {}, // ACTIVATE SYMMETRIC SWAPPING | 32 | | - 0x206c: {}, // INHIBIT ARABIC FORM SHAPING | 33 | | - 0x206d: {}, // ACTIVATE ARABIC FORM SHAPING | 34 | | - 0x206e: {}, // NATIONAL DIGIT SHAPES | 35 | | - 0x206f: {}, // NOMINAL DIGIT SHAPES | 36 | | - 0xfeff: {}, // ZERO WIDTH NO-BREAK SPACE | 37 | | -} | 38 | | - | 39 | | -// isHFSDot returns true if the given path component would be | 40 | | -// treated as ".<needle>" on an HFS+ filesystem after stripping | 41 | | -// ignored Unicode code points and folding to lower case. The needle | 42 | | -// is the lowercase ASCII suffix without the leading dot (e.g. "git", | 43 | | -// "gitmodules"). This generalisation mirrors upstream Git's | 44 | | -// is_hfs_dotgit / is_hfs_dotgitmodules / is_hfs_dotgitignore / | 45 | | -// is_hfs_dotgitattributes family in utf8.c. | 46 | | -// | 47 | | -// Reference: upstream Git utf8.c is_hfs_dot_generic at L741-L774 and | 48 | | -// the dotgit family at L784-L809 in tag v2.54.0[1]. | 49 | | -// | 50 | | -// [1]: https://github.com/git/git/blob/v2.54.0/utf8.c#L741-L809 | 51 | | -func isHFSDot(part, needle string) bool { | 52 | | - runes := []rune(part) | 53 | | - i := 0 | 54 | | - | 55 | | - // skip ignored code points, then expect '.' | 56 | | - for i < len(runes) { | 57 | | - if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok { | 58 | | - break | 59 | | - } | 60 | | - i++ | 61 | | - } | 62 | | - if i >= len(runes) || runes[i] != '.' { | 63 | | - return false | 64 | | - } | 65 | | - i++ | 66 | | - | 67 | | - // match needle case-insensitively, skipping ignored code points | 68 | | - for _, expected := range needle { | 69 | | - for i < len(runes) { | 70 | | - if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok { | 71 | | - break | 72 | | - } | 73 | | - i++ | 74 | | - } | 75 | | - if i >= len(runes) { | 76 | | - return false | 77 | | - } | 78 | | - r := runes[i] | 79 | | - if r > 127 { | 80 | | - return false | 81 | | - } | 82 | | - if unicode.ToLower(r) != expected { | 83 | | - return false | 84 | | - } | 85 | | - i++ | 86 | | - } | 87 | | - | 88 | | - // skip trailing ignored code points | 89 | | - for i < len(runes) { | 90 | | - if _, ok := hfsIgnoredCodepoints[runes[i]]; !ok { | 91 | | - break | 92 | | - } | 93 | | - i++ | 94 | | - } | 95 | | - | 96 | | - // must be at end of component | 97 | | - return i == len(runes) | 98 | | -} | 99 | | - | 100 | | -// isHFSDotGit reports whether part is an HFS+ equivalent of ".git". | 101 | | -func isHFSDotGit(part string) bool { return isHFSDot(part, "git") } | 102 | | - | 103 | | -// isHFSDotGitmodules reports whether part is an HFS+ equivalent of | 104 | | -// ".gitmodules", catching attempts to plant the file via Unicode | 105 | | -// code points that HFS+ would strip during normalisation. | 106 | | -func isHFSDotGitmodules(part string) bool { return isHFSDot(part, "gitmodules") } |
Collapse file Copy file name to clipboardExpand all lines: hfs_test.go -102Lines changed: 0 additions & 102 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,40 +7,6 @@ import ( | 7 | 7 | "github.com/stretchr/testify/assert" | 8 | 8 | ) | 9 | 9 |
| 10 | | -func TestIsHFSDotGit(t *testing.T) { | 11 | | - t.Parallel() | 12 | | - | 13 | | - tests := []struct { | 14 | | - part string | 15 | | - want bool | 16 | | - }{ | 17 | | - {".git", true}, | 18 | | - {".Git", true}, | 19 | | - {".GIT", true}, | 20 | | - {".gIt", true}, | 21 | | - {".g\u200cit", true}, | 22 | | - {".gi\u200dt", true}, | 23 | | - {".gi\ufefft", true}, | 24 | | - {"\u200e.git", true}, | 25 | | - {".g\u200ci\u200dt", true}, | 26 | | - {".gitmodules", false}, | 27 | | - {".gitignore", false}, | 28 | | - {".git2", false}, | 29 | | - {"git", false}, | 30 | | - {".gxt", false}, | 31 | | - {"", false}, | 32 | | - {".", false}, | 33 | | - {".g\x80it", false}, | 34 | | - } | 35 | | - | 36 | | - for _, tc := range tests { | 37 | | - t.Run(tc.part, func(t *testing.T) { | 38 | | - t.Parallel() | 39 | | - assert.Equal(t, tc.want, isHFSDotGit(tc.part)) | 40 | | - }) | 41 | | - } | 42 | | -} | 43 | | - | 44 | 10 | func TestValidPathProtectHFS(t *testing.T) { | 45 | 11 | t.Parallel() | 46 | 12 |
| @@ -111,74 +77,6 @@ func TestWorktreeFilesystemRejectsHFSPaths(t *testing.T) { | 111 | 77 | } | 112 | 78 | } | 113 | 79 |
| 114 | | -func TestIsHFSDot(t *testing.T) { | 115 | | - t.Parallel() | 116 | | - | 117 | | - tests := []struct { | 118 | | - name string | 119 | | - part string | 120 | | - needle string | 121 | | - want bool | 122 | | - }{ | 123 | | - // .git \u2014 matches existing isHFSDotGit semantics. | 124 | | - {"plain .git", ".git", "git", true}, | 125 | | - {"zwj in .git", ".g\u200dit", "git", true}, | 126 | | - {"zwnj in .git", ".g\u200cit", "git", true}, | 127 | | - {"leading lrm", "\u200e.git", "git", true}, | 128 | | - {".gitmodules is not .git", ".gitmodules", "git", false}, | 129 | | - {"empty string for git", "", "git", false}, | 130 | | - | 131 | | - // .gitmodules \u2014 the parameterised needle. | 132 | | - {"plain .gitmodules", ".gitmodules", "gitmodules", true}, | 133 | | - {"uppercase .GITMODULES", ".GITMODULES", "gitmodules", true}, | 134 | | - {"zwnj in .gitmodules", ".g\u200citmodules", "gitmodules", true}, | 135 | | - {"zwj in .gitmodules", ".gitmod\u200dules", "gitmodules", true}, | 136 | | - {"trailing zwsp", ".gitmodules\ufeff", "gitmodules", true}, | 137 | | - {"leading lrm", "\u200e.gitmodules", "gitmodules", true}, | 138 | | - {".gitmodulesfoo", ".gitmodulesfoo", "gitmodules", false}, | 139 | | - {".git is not .gitmodules", ".git", "gitmodules", false}, | 140 | | - {"plain readme.md", "readme.md", "gitmodules", false}, | 141 | | - {"empty string", "", "gitmodules", false}, | 142 | | - {"only dot", ".", "gitmodules", false}, | 143 | | - {"non-ascii in needle position", ".g\x80itmodules", "gitmodules", false}, | 144 | | - } | 145 | | - | 146 | | - for _, tc := range tests { | 147 | | - t.Run(tc.name, func(t *testing.T) { | 148 | | - t.Parallel() | 149 | | - got := isHFSDot(tc.part, tc.needle) | 150 | | - assert.Equal(t, tc.want, got, "isHFSDot(%q, %q)", tc.part, tc.needle) | 151 | | - }) | 152 | | - } | 153 | | -} | 154 | | - | 155 | | -func TestIsHFSDotGitmodules(t *testing.T) { | 156 | | - t.Parallel() | 157 | | - | 158 | | - tests := []struct { | 159 | | - part string | 160 | | - want bool | 161 | | - }{ | 162 | | - {".gitmodules", true}, | 163 | | - {".GITMODULES", true}, | 164 | | - {".g\u200citmodules", true}, | 165 | | - {".gitmod\u200dules", true}, | 166 | | - {"\u200e.gitmodules", true}, | 167 | | - {".gitmodules\ufeff", true}, | 168 | | - {".gitmodulesfoo", false}, | 169 | | - {".git", false}, | 170 | | - {"readme.md", false}, | 171 | | - {"", false}, | 172 | | - } | 173 | | - | 174 | | - for _, tc := range tests { | 175 | | - t.Run(tc.part, func(t *testing.T) { | 176 | | - t.Parallel() | 177 | | - assert.Equal(t, tc.want, isHFSDotGitmodules(tc.part)) | 178 | | - }) | 179 | | - } | 180 | | -} | 181 | | - | 182 | 80 | func TestWorktreeFilesystemRejectsHFSDotGitmodulesSymlink(t *testing.T) { | 183 | 81 | t.Parallel() | 184 | 82 |
| |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pathutil | | 2 | + | | 3 | +import "strings" | | 4 | + | | 5 | +// IsDotGitName reports whether name is `.git` or its 8.3 NTFS short | | 6 | +// alias `git~1`, case-insensitively. Both are forbidden as path | | 7 | +// components (and as submodule names) because they refer to the | | 8 | +// repository's own metadata directory. | | 9 | +// | | 10 | +// File names that do not conform to the 8.3 format (up to eight | | 11 | +// characters for the basename, three for the file extension) are | | 12 | +// associated with a so-called "short name" on NTFS — at least on | | 13 | +// the `C:` drive by default — which means that `git~1/` is a valid | | 14 | +// way to refer to `.git/`. | | 15 | +func IsDotGitName(name string) bool { | | 16 | + switch strings.ToLower(name) { | | 17 | + case ".git", "git~1": | | 18 | + return true | | 19 | + } | | 20 | + return false | | 21 | +} |
Collapse file Copy file name to clipboard +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package pathutil | | 2 | + | | 3 | +import ( | | 4 | + "testing" | | 5 | + | | 6 | + "github.com/stretchr/testify/assert" | | 7 | +) | | 8 | + | | 9 | +func TestIsDotGitName(t *testing.T) { | | 10 | + t.Parallel() | | 11 | + | | 12 | + tests := []struct { | | 13 | + name string | | 14 | + want bool | | 15 | + }{ | | 16 | + {".git", true}, | | 17 | + {".GIT", true}, | | 18 | + {".Git", true}, | | 19 | + {".gIt", true}, | | 20 | + {"git~1", true}, | | 21 | + {"GIT~1", true}, | | 22 | + {"Git~1", true}, | | 23 | + | | 24 | + {"git", false}, | | 25 | + {"GIT", false}, | | 26 | + {".gitmodules", false}, | | 27 | + {".gitignore", false}, | | 28 | + {"git~", false}, | | 29 | + {"git~10", false}, | | 30 | + {"git~2", false}, // canonical short name is git~1; others are not denied | | 31 | + {"", false}, | | 32 | + } | | 33 | + | | 34 | + for _, tc := range tests { | | 35 | + t.Run(tc.name, func(t *testing.T) { | | 36 | + t.Parallel() | | 37 | + assert.Equal(t, tc.want, IsDotGitName(tc.name)) | | 38 | + }) | | 39 | + } | | 40 | +} |
Collapse file Copy file name to clipboard +99Lines changed: 99 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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") } |
|