Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #526 from joshbetz/fix/filename-form
Browse files Browse the repository at this point in the history
Normalize filenames before comparing.
  • Loading branch information
mcuadros authored Aug 2, 2017
2 parents a0885c5 + 5c1a2ec commit 3713791
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion utils/merkletrie/noder/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package noder
import (
"bytes"
"strings"

"golang.org/x/text/unicode/norm"
)

// Path values represent a noder and its ancestors. The root goes first
Expand Down Expand Up @@ -78,7 +80,11 @@ func (p Path) Compare(other Path) int {
case i == len(p):
return -1
default:
cmp := strings.Compare(p[i].Name(), other[i].Name())
form := norm.Form(norm.NFC)
this := form.String(p[i].Name())
that := form.String(other[i].Name())

cmp := strings.Compare(this, that)
if cmp != 0 {
return cmp
}
Expand Down
12 changes: 11 additions & 1 deletion utils/merkletrie/noder/path_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package noder

import . "gopkg.in/check.v1"
import (
"golang.org/x/text/unicode/norm"
. "gopkg.in/check.v1"
)

type PathSuite struct{}

Expand Down Expand Up @@ -149,3 +152,10 @@ func (s *PathSuite) TestCompareMixedDepths(c *C) {
c.Assert(p1.Compare(p2), Equals, 1)
c.Assert(p2.Compare(p1), Equals, -1)
}

func (s *PathSuite) TestCompareNormalization(c *C) {
p1 := Path([]Noder{&noderMock{name: norm.Form(norm.NFKC).String("페")}})
p2 := Path([]Noder{&noderMock{name: norm.Form(norm.NFKD).String("페")}})
c.Assert(p1.Compare(p2), Equals, 0)
c.Assert(p2.Compare(p1), Equals, 0)
}
43 changes: 43 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"

"golang.org/x/text/unicode/norm"

"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
Expand Down Expand Up @@ -304,6 +306,47 @@ func (s *WorktreeSuite) TestCheckoutSymlink(c *C) {
c.Assert(err, IsNil)
}

func (s *WorktreeSuite) TestFilenameNormalization(c *C) {
url := c.MkDir()
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()

server, err := PlainClone(url, false, &CloneOptions{
URL: path,
})

filename := "페"

w, err := server.Worktree()
c.Assert(err, IsNil)
util.WriteFile(w.Filesystem, filename, []byte("foo"), 0755)
_, err = w.Add(filename)
c.Assert(err, IsNil)
_, err = w.Commit("foo", &CommitOptions{Author: defaultSignature()})
c.Assert(err, IsNil)

r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
URL: url,
})

w, err = r.Worktree()
c.Assert(err, IsNil)

status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(status.IsClean(), Equals, true)

err = w.Filesystem.Remove(filename)
c.Assert(err, IsNil)

modFilename := norm.Form(norm.NFKD).String(filename)
util.WriteFile(w.Filesystem, modFilename, []byte("foo"), 0755)
_, err = w.Add(filename)

status, err = w.Status()
c.Assert(err, IsNil)
c.Assert(status.IsClean(), Equals, true)
}

func (s *WorktreeSuite) TestCheckoutSubmodule(c *C) {
url := "https://github.com/git-fixtures/submodule.git"
r := s.NewRepositoryWithEmptyWorktree(fixtures.ByURL(url).One())
Expand Down

0 comments on commit 3713791

Please sign in to comment.