Skip to content

Commit 6162a1d

Browse files
peffgitster
authored andcommitted
utf8: add is_hfs_dotgit() helper
We do not allow paths with a ".git" component to be added to the index, as that would mean repository contents could overwrite our repository files. However, asking "is this path the same as .git" is not as simple as strcmp() on some filesystems. HFS+'s case-folding does more than just fold uppercase into lowercase (which we already handle with strcasecmp). It may also skip past certain "ignored" Unicode code points, so that (for example) ".gi\u200ct" is mapped ot ".git". The full list of folds can be found in the tables at: https://www.opensource.apple.com/source/xnu/xnu-1504.15.3/bsd/hfs/hfscommon/Unicode/UCStringCompareData.h Implementing a full "is this path the same as that path" comparison would require us importing the whole set of tables. However, what we want to do is much simpler: we only care about checking ".git". We know that 'G' is the only thing that folds to 'g', and so on, so we really only need to deal with the set of ignored code points, which is much smaller. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 76e86fc commit 6162a1d

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

utf8.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,67 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
628628

629629
return chrlen;
630630
}
631+
632+
/*
633+
* Pick the next char from the stream, folding as an HFS+ filename comparison
634+
* would. Note that this is _not_ complete by any means. It's just enough
635+
* to make is_hfs_dotgit() work, and should not be used otherwise.
636+
*/
637+
static ucs_char_t next_hfs_char(const char **in)
638+
{
639+
while (1) {
640+
ucs_char_t out = pick_one_utf8_char(in, NULL);
641+
/*
642+
* check for malformed utf8. Technically this
643+
* gets converted to a percent-sequence, but
644+
* returning 0 is good enough for is_hfs_dotgit
645+
* to realize it cannot be .git
646+
*/
647+
if (!*in)
648+
return 0;
649+
650+
/* these code points are ignored completely */
651+
switch (out) {
652+
case 0x200c: /* ZERO WIDTH NON-JOINER */
653+
case 0x200d: /* ZERO WIDTH JOINER */
654+
case 0x200e: /* LEFT-TO-RIGHT MARK */
655+
case 0x200f: /* RIGHT-TO-LEFT MARK */
656+
case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
657+
case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
658+
case 0x202c: /* POP DIRECTIONAL FORMATTING */
659+
case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
660+
case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
661+
case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
662+
case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
663+
case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
664+
case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
665+
case 0x206e: /* NATIONAL DIGIT SHAPES */
666+
case 0x206f: /* NOMINAL DIGIT SHAPES */
667+
case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
668+
continue;
669+
}
670+
671+
/*
672+
* there's a great deal of other case-folding that occurs,
673+
* but this is enough to catch anything that will convert
674+
* to ".git"
675+
*/
676+
return tolower(out);
677+
}
678+
}
679+
680+
int is_hfs_dotgit(const char *path)
681+
{
682+
ucs_char_t c;
683+
684+
if (next_hfs_char(&path) != '.' ||
685+
next_hfs_char(&path) != 'g' ||
686+
next_hfs_char(&path) != 'i' ||
687+
next_hfs_char(&path) != 't')
688+
return 0;
689+
c = next_hfs_char(&path);
690+
if (c && !is_dir_sep(c))
691+
return 0;
692+
693+
return 1;
694+
}

utf8.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ static inline char *reencode_string(const char *in,
4242

4343
int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
4444

45+
/*
46+
* Returns true if the the path would match ".git" after HFS case-folding.
47+
* The path should be NUL-terminated, but we will match variants of both ".git\0"
48+
* and ".git/..." (but _not_ ".../.git"). This makes it suitable for both fsck
49+
* and verify_path().
50+
*/
51+
int is_hfs_dotgit(const char *path);
52+
4553
#endif

0 commit comments

Comments
 (0)