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

Commit d3cec13

Browse files
committed
worktree: solve merge conflicts
2 parents 174f373 + f0c4318 commit d3cec13

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

worktree_commit.go

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"bytes"
55
"path"
6+
"sort"
67
"strings"
78

89
"golang.org/x/crypto/openpgp"
@@ -188,7 +189,20 @@ func (h *buildTreeHelper) doBuildTree(e *index.Entry, parent, fullpath string) {
188189
h.trees[parent].Entries = append(h.trees[parent].Entries, te)
189190
}
190191

192+
type sortableEntries []object.TreeEntry
193+
194+
func (sortableEntries) sortName(te object.TreeEntry) string {
195+
if te.Mode == filemode.Dir {
196+
return te.Name + "/"
197+
}
198+
return te.Name
199+
}
200+
func (se sortableEntries) Len() int { return len(se) }
201+
func (se sortableEntries) Less(i int, j int) bool { return se.sortName(se[i]) < se.sortName(se[j]) }
202+
func (se sortableEntries) Swap(i int, j int) { se[i], se[j] = se[j], se[i] }
203+
191204
func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tree) (plumbing.Hash, error) {
205+
sort.Sort(sortableEntries(t.Entries))
192206
for i, e := range t.Entries {
193207
if e.Mode != filemode.Dir && !e.Hash.IsZero() {
194208
continue

worktree_commit_test.go

+56-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ package git
22

33
import (
44
"bytes"
5+
"io/ioutil"
6+
"os"
7+
"os/exec"
58
"strings"
69
"time"
710

8-
"golang.org/x/crypto/openpgp"
9-
"golang.org/x/crypto/openpgp/armor"
10-
"golang.org/x/crypto/openpgp/errors"
1111
"gopkg.in/src-d/go-git.v4/plumbing"
1212
"gopkg.in/src-d/go-git.v4/plumbing/object"
1313
"gopkg.in/src-d/go-git.v4/plumbing/storer"
14+
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1415
"gopkg.in/src-d/go-git.v4/storage/memory"
1516

17+
"golang.org/x/crypto/openpgp"
18+
"golang.org/x/crypto/openpgp/armor"
19+
"golang.org/x/crypto/openpgp/errors"
1620
. "gopkg.in/check.v1"
1721
"gopkg.in/src-d/go-billy.v4/memfs"
22+
"gopkg.in/src-d/go-billy.v4/osfs"
1823
"gopkg.in/src-d/go-billy.v4/util"
1924
)
2025

@@ -196,6 +201,54 @@ func (s *WorktreeSuite) TestCommitSignBadKey(c *C) {
196201
c.Assert(err, Equals, errors.InvalidArgumentError("signing key is encrypted"))
197202
}
198203

204+
func (s *WorktreeSuite) TestCommitTreeSort(c *C) {
205+
path, err := ioutil.TempDir(os.TempDir(), "test-commit-tree-sort")
206+
c.Assert(err, IsNil)
207+
fs := osfs.New(path)
208+
st, err := filesystem.NewStorage(fs)
209+
c.Assert(err, IsNil)
210+
r, err := Init(st, nil)
211+
c.Assert(err, IsNil)
212+
213+
r, err = Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
214+
URL: path,
215+
})
216+
217+
w, err := r.Worktree()
218+
c.Assert(err, IsNil)
219+
220+
mfs := w.Filesystem
221+
222+
err = mfs.MkdirAll("delta", 0755)
223+
c.Assert(err, IsNil)
224+
225+
for _, p := range []string{"delta_last", "Gamma", "delta/middle", "Beta", "delta-first", "alpha"} {
226+
util.WriteFile(mfs, p, []byte("foo"), 0644)
227+
_, err = w.Add(p)
228+
c.Assert(err, IsNil)
229+
}
230+
231+
_, err = w.Commit("foo\n", &CommitOptions{
232+
All: true,
233+
Author: defaultSignature(),
234+
})
235+
c.Assert(err, IsNil)
236+
237+
err = r.Push(&PushOptions{})
238+
c.Assert(err, IsNil)
239+
240+
cmd := exec.Command("git", "fsck")
241+
cmd.Dir = path
242+
cmd.Env = os.Environ()
243+
buf := &bytes.Buffer{}
244+
cmd.Stderr = buf
245+
cmd.Stdout = buf
246+
247+
err = cmd.Run()
248+
249+
c.Assert(err, IsNil, Commentf("%s", buf.Bytes()))
250+
}
251+
199252
func assertStorageStatus(
200253
c *C, r *Repository,
201254
treesCount, blobCount, commitCount int, head plumbing.Hash,

0 commit comments

Comments
 (0)