Skip to content

Commit dc578d8

Browse files
cpruijsensumneko
andauthored
fix: whitespace-only .gitignore lines no longer match every file (#3462)
* fix: whitespace-only .gitignore lines no longer match every file * test: cover whitespace-only gitignore lines matching every file --------- Co-authored-by: 最萌小汐 <sumneko@hotmail.com>
1 parent aa258bc commit dc578d8

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `FIX` A `.gitignore` line containing only whitespace no longer ignores the entire workspace [#3458](https://github.com/LuaLS/lua-language-server/issues/3458)
56
* `FIX` `need-check-nil` diagnostic is no longer reported on safe navigation access (e.g. `x?.field`, `f?.()`, `t?.[key]`), since the optional access itself already handles the nil check. Note that a non-safe access chained after a safe one (e.g. `x.upper()?.field`) still reports, because the safe access only protects its own result.
67

78
## 3.19.1

‎script/glob/matcher.lua‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ function mt:slash(_, state, index)
122122
end
123123

124124
function mt:pattern(state)
125+
if not state[1] then
126+
return nil
127+
end
125128
if state.root then
126129
local after = self:exp(state, 1)
127130
if after then

‎test/basic/gitignore.lua‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local glob = require 'glob'
2+
3+
local function ignored(patterns, path)
4+
return glob.gitignore(patterns)(path)
5+
end
6+
7+
-- #3458: a whitespace-only gitignore pattern must not match every path
8+
assert(ignored({ ' ' }, 'a.lua') == false)
9+
assert(ignored({ ' ' }, 'foo/bar.lua') == false)
10+
assert(ignored({ '\t' }, 'a.lua') == false)
11+
assert(ignored({ '' }, 'a.lua') == false)
12+
13+
-- Real patterns in the same list still match; the blank line does not take over
14+
assert(ignored({ ' ', '*.log' }, 'a.lua') == false)
15+
assert(ignored({ ' ', '*.log' }, 'a.log') == true)
16+
17+
-- Intentional match-all and ordinary names are unchanged
18+
assert(ignored({ '*' }, 'a.lua') == true)
19+
assert(ignored({ 'foo' }, 'foo') == true)
20+
assert(ignored({ 'foo' }, 'bar') == false)
21+
22+
-- Quoted trailing space is a real pattern (gitignore spec), not a blank line
23+
assert(ignored({ '\\ ' }, 'a.lua') == false)
24+
assert(ignored({ '\\ ' }, ' ') == true)
25+
26+
-- glob.glob shares the same matcher; empty patterns must not match everything
27+
assert(glob.glob({ ' ' })('a.lua') == false)
28+
assert(glob.glob({ ' ', 'foo' })('foo') == true)
29+
assert(glob.glob({ ' ', 'foo' })('bar') == false)
30+
assert(glob.glob({ '*' })('a.lua') == true)

‎test/basic/init.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require 'basic.textmerger'
22
require 'basic.filewatch'
3+
require 'basic.gitignore'

0 commit comments

Comments
 (0)