dir: allow a BOM at the beginning of exclude files

Some text editors like Notepad or LibreOffice write an UTF-8 BOM in order to indicate that the file is Unicode text rather than whatever the current locale would indicate. If someone uses such an editor to edit a gitignore file, we are left with those three bytes at the beginning of the file. If we do not skip them, we will attempt to match a filename with the BOM as prefix, which won't match the files the user is expecting. Signed-off-by: Carlos Martín Nieto <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlos Martín Nieto committed Apr 16, 2015 at 16:05 UTC 245e1c196dab226675a02a8caca5a83373f5e4d4
2 files changed +16 -1
dir.c
+7 -1
@@ -538,6 +538,7 @@ int add_excludes_from_file_to_list(const char *fname,
538 struct stat st;
539 int fd, i, lineno = 1;
540 size_t size = 0;
541 + static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
542 char *buf, *entry;
543
544 fd = open(fname, O_RDONLY);
@@ -574,7 +575,12 @@ int add_excludes_from_file_to_list(const char *fname,
575 }
576
577 el->filebuf = buf;
577 - entry = buf;
578 +
579 + if (size >= 3 && !memcmp(buf, utf8_bom, 3))
580 + entry = buf + 3;
581 + else
582 + entry = buf;
583 +
584 for (i = 0; i < size; i++) {
585 if (buf[i] == '\n') {
586 if (entry != buf + i && entry[0] != '#') {
t/t7061-wtstatus-ignore.sh
+9
@@ -20,6 +20,15 @@ test_expect_success 'status untracked directory with --ignored' '
20 test_cmp expected actual
21 '
22
23 +test_expect_success 'same with gitignore starting with BOM' '
24 + printf "\357\273\277ignored\n" >.gitignore &&
25 + mkdir -p untracked &&
26 + : >untracked/ignored &&
27 + : >untracked/uncommitted &&
28 + git status --porcelain --ignored >actual &&
29 + test_cmp expected actual
30 +'
31 +
32 cat >expected <<\EOF
33 ?? .gitignore
34 ?? actual