Skip to content

Commit 245e1c1

Browse files
carlosmngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdf96a2 commit 245e1c1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

dir.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ int add_excludes_from_file_to_list(const char *fname,
538538
struct stat st;
539539
int fd, i, lineno = 1;
540540
size_t size = 0;
541+
static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
541542
char *buf, *entry;
542543

543544
fd = open(fname, O_RDONLY);
@@ -574,7 +575,12 @@ int add_excludes_from_file_to_list(const char *fname,
574575
}
575576

576577
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+
578584
for (i = 0; i < size; i++) {
579585
if (buf[i] == '\n') {
580586
if (entry != buf + i && entry[0] != '#') {

t/t7061-wtstatus-ignore.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ test_expect_success 'status untracked directory with --ignored' '
2020
test_cmp expected actual
2121
'
2222

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+
2332
cat >expected <<\EOF
2433
?? .gitignore
2534
?? actual

0 commit comments

Comments
 (0)