diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-09-06 22:05:00 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2018-09-06 22:05:00 +0200 |
commit | d3cec13ac0b195bfb897ed038a08b5130ab9969e (patch) | |
tree | b54b622402057e530cc4b05fb2ffa36767ba726e | |
parent | 174f373cf066afc3e3be35576adf3709b0ee278b (diff) | |
parent | f0c4318e7b8d5cbf91723c71a4ba20d1bd0cfaf5 (diff) | |
download | go-git-d3cec13ac0b195bfb897ed038a08b5130ab9969e.tar.gz |
worktree: solve merge conflictsv4.7.0
-rw-r--r-- | worktree_commit.go | 14 | ||||
-rw-r--r-- | worktree_commit_test.go | 59 |
2 files changed, 70 insertions, 3 deletions
diff --git a/worktree_commit.go b/worktree_commit.go index b83bf0c..673eb16 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -3,6 +3,7 @@ package git import ( "bytes" "path" + "sort" "strings" "golang.org/x/crypto/openpgp" @@ -188,7 +189,20 @@ func (h *buildTreeHelper) doBuildTree(e *index.Entry, parent, fullpath string) { h.trees[parent].Entries = append(h.trees[parent].Entries, te) } +type sortableEntries []object.TreeEntry + +func (sortableEntries) sortName(te object.TreeEntry) string { + if te.Mode == filemode.Dir { + return te.Name + "/" + } + return te.Name +} +func (se sortableEntries) Len() int { return len(se) } +func (se sortableEntries) Less(i int, j int) bool { return se.sortName(se[i]) < se.sortName(se[j]) } +func (se sortableEntries) Swap(i int, j int) { se[i], se[j] = se[j], se[i] } + func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tree) (plumbing.Hash, error) { + sort.Sort(sortableEntries(t.Entries)) for i, e := range t.Entries { if e.Mode != filemode.Dir && !e.Hash.IsZero() { continue diff --git a/worktree_commit_test.go b/worktree_commit_test.go index 004926f..6979bd5 100644 --- a/worktree_commit_test.go +++ b/worktree_commit_test.go @@ -2,19 +2,24 @@ package git import ( "bytes" + "io/ioutil" + "os" + "os/exec" "strings" "time" - "golang.org/x/crypto/openpgp" - "golang.org/x/crypto/openpgp/armor" - "golang.org/x/crypto/openpgp/errors" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/storer" + "gopkg.in/src-d/go-git.v4/storage/filesystem" "gopkg.in/src-d/go-git.v4/storage/memory" + "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/armor" + "golang.org/x/crypto/openpgp/errors" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4/memfs" + "gopkg.in/src-d/go-billy.v4/osfs" "gopkg.in/src-d/go-billy.v4/util" ) @@ -196,6 +201,54 @@ func (s *WorktreeSuite) TestCommitSignBadKey(c *C) { c.Assert(err, Equals, errors.InvalidArgumentError("signing key is encrypted")) } +func (s *WorktreeSuite) TestCommitTreeSort(c *C) { + path, err := ioutil.TempDir(os.TempDir(), "test-commit-tree-sort") + c.Assert(err, IsNil) + fs := osfs.New(path) + st, err := filesystem.NewStorage(fs) + c.Assert(err, IsNil) + r, err := Init(st, nil) + c.Assert(err, IsNil) + + r, err = Clone(memory.NewStorage(), memfs.New(), &CloneOptions{ + URL: path, + }) + + w, err := r.Worktree() + c.Assert(err, IsNil) + + mfs := w.Filesystem + + err = mfs.MkdirAll("delta", 0755) + c.Assert(err, IsNil) + + for _, p := range []string{"delta_last", "Gamma", "delta/middle", "Beta", "delta-first", "alpha"} { + util.WriteFile(mfs, p, []byte("foo"), 0644) + _, err = w.Add(p) + c.Assert(err, IsNil) + } + + _, err = w.Commit("foo\n", &CommitOptions{ + All: true, + Author: defaultSignature(), + }) + c.Assert(err, IsNil) + + err = r.Push(&PushOptions{}) + c.Assert(err, IsNil) + + cmd := exec.Command("git", "fsck") + cmd.Dir = path + cmd.Env = os.Environ() + buf := &bytes.Buffer{} + cmd.Stderr = buf + cmd.Stdout = buf + + err = cmd.Run() + + c.Assert(err, IsNil, Commentf("%s", buf.Bytes())) +} + func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, |