Skip to content

Commit 41758ec

Browse files
authored
Merge pull request go-git#115 from blaueled/fix/gitignore-crlf
.gitignore crlf fix
2 parents ef33fff + a8a0f9f commit 41758ec

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Diff for: plumbing/format/gitignore/dir.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitignore
22

33
import (
4+
"bufio"
45
"bytes"
56
"io/ioutil"
67
"os"
@@ -15,7 +16,6 @@ import (
1516
const (
1617
commentPrefix = "#"
1718
coreSection = "core"
18-
eol = "\n"
1919
excludesfile = "excludesfile"
2020
gitDir = ".git"
2121
gitignoreFile = ".gitignore"
@@ -29,11 +29,11 @@ func readIgnoreFile(fs billy.Filesystem, path []string, ignoreFile string) (ps [
2929
if err == nil {
3030
defer f.Close()
3131

32-
if data, err := ioutil.ReadAll(f); err == nil {
33-
for _, s := range strings.Split(string(data), eol) {
34-
if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 {
35-
ps = append(ps, ParsePattern(s, path))
36-
}
32+
scanner := bufio.NewScanner(f)
33+
for scanner.Scan() {
34+
s := scanner.Text()
35+
if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 {
36+
ps = append(ps, ParsePattern(s, path))
3737
}
3838
}
3939
} else if !os.IsNotExist(err) {

Diff for: plumbing/format/gitignore/dir_test.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type MatcherSuite struct {
1515
RFS billy.Filesystem // root that contains user home
1616
MCFS billy.Filesystem // root that contains user home, but missing ~/.gitconfig
1717
MEFS billy.Filesystem // root that contains user home, but missing excludesfile entry
18-
MIFS billy.Filesystem // root that contains user home, but missing .gitnignore
18+
MIFS billy.Filesystem // root that contains user home, but missing .gitignore
1919

2020
SFS billy.Filesystem // root that contains /etc/gitconfig
2121
}
@@ -29,6 +29,8 @@ func (s *MatcherSuite) SetUpTest(c *C) {
2929
c.Assert(err, IsNil)
3030
_, err = f.Write([]byte("vendor/g*/\n"))
3131
c.Assert(err, IsNil)
32+
_, err = f.Write([]byte("ignore.crlf\r\n"))
33+
c.Assert(err, IsNil)
3234
err = f.Close()
3335
c.Assert(err, IsNil)
3436

@@ -41,9 +43,14 @@ func (s *MatcherSuite) SetUpTest(c *C) {
4143
err = f.Close()
4244
c.Assert(err, IsNil)
4345

44-
fs.MkdirAll("another", os.ModePerm)
45-
fs.MkdirAll("vendor/github.com", os.ModePerm)
46-
fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
46+
err = fs.MkdirAll("another", os.ModePerm)
47+
c.Assert(err, IsNil)
48+
err = fs.MkdirAll("ignore.crlf", os.ModePerm)
49+
c.Assert(err, IsNil)
50+
err = fs.MkdirAll("vendor/github.com", os.ModePerm)
51+
c.Assert(err, IsNil)
52+
err = fs.MkdirAll("vendor/gopkg.in", os.ModePerm)
53+
c.Assert(err, IsNil)
4754

4855
s.GFS = fs
4956

@@ -167,9 +174,10 @@ func (s *MatcherSuite) SetUpTest(c *C) {
167174
func (s *MatcherSuite) TestDir_ReadPatterns(c *C) {
168175
ps, err := ReadPatterns(s.GFS, nil)
169176
c.Assert(err, IsNil)
170-
c.Assert(ps, HasLen, 2)
177+
c.Assert(ps, HasLen, 3)
171178

172179
m := NewMatcher(ps)
180+
c.Assert(m.Match([]string{"ignore.crlf"}, true), Equals, true)
173181
c.Assert(m.Match([]string{"vendor", "gopkg.in"}, true), Equals, true)
174182
c.Assert(m.Match([]string{"vendor", "github.com"}, true), Equals, false)
175183
}

0 commit comments

Comments
 (0)