Skip to content

Commit

Permalink
worktree: solve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Sep 6, 2018
2 parents 174f373 + f0c4318 commit d3cec13
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
14 changes: 14 additions & 0 deletions worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"bytes"
"path"
"sort"
"strings"

"golang.org/x/crypto/openpgp"
Expand Down Expand Up @@ -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
Expand Down
59 changes: 56 additions & 3 deletions worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d3cec13

Please sign in to comment.