Skip to content

Commit

Permalink
Merge branch 'master' of github.com:src-d/go-git into f-add-tagging-s…
Browse files Browse the repository at this point in the history
…upport
  • Loading branch information
mcuadros committed Sep 10, 2018
2 parents f8adfff + a2d62f5 commit bf0593d
Show file tree
Hide file tree
Showing 44 changed files with 1,028 additions and 187 deletions.
11 changes: 3 additions & 8 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
Expand Down Expand Up @@ -59,10 +60,7 @@ func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository {
dotgit = f.DotGit()
worktree = memfs.New()

st, err := filesystem.NewStorage(dotgit)
if err != nil {
panic(err)
}
st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())

r, err := Open(st, worktree)
if err != nil {
Expand All @@ -89,10 +87,7 @@ func (s *BaseSuite) NewRepositoryWithEmptyWorktree(f *fixtures.Fixture) *Reposit

worktree := memfs.New()

st, err := filesystem.NewStorage(dotgit)
if err != nil {
panic(err)
}
st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())

r, err := Open(st, worktree)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Config struct {
IsBare bool
// Worktree is the path to the root of the working tree.
Worktree string
// CommentChar is the character indicating the start of a
// comment for commands like commit and tag
CommentChar string
}

Pack struct {
Expand Down Expand Up @@ -113,6 +116,7 @@ const (
urlKey = "url"
bareKey = "bare"
worktreeKey = "worktree"
commentCharKey = "commentChar"
windowKey = "window"
mergeKey = "merge"

Expand Down Expand Up @@ -151,6 +155,7 @@ func (c *Config) unmarshalCore() {
}

c.Core.Worktree = s.Options.Get(worktreeKey)
c.Core.CommentChar = s.Options.Get(commentCharKey)
}

func (c *Config) unmarshalPack() error {
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (s *ConfigSuite) TestUnmarshall(c *C) {
input := []byte(`[core]
bare = true
worktree = foo
commentchar = bar
[pack]
window = 20
[remote "origin"]
Expand All @@ -38,6 +39,7 @@ func (s *ConfigSuite) TestUnmarshall(c *C) {

c.Assert(cfg.Core.IsBare, Equals, true)
c.Assert(cfg.Core.Worktree, Equals, "foo")
c.Assert(cfg.Core.CommentChar, Equals, "bar")
c.Assert(cfg.Pack.Window, Equals, uint(20))
c.Assert(cfg.Remotes, HasLen, 2)
c.Assert(cfg.Remotes["origin"].Name, Equals, "origin")
Expand Down
8 changes: 6 additions & 2 deletions plumbing/format/diff/unified_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ func (c *hunksGenerator) addLineNumbers(la, lb int, linesBefore int, i int, op O
// we need to search for a reference for the next diff
switch {
case linesBefore != 0 && c.ctxLines != 0:
clb = lb - c.ctxLines + 1
if lb > c.ctxLines {
clb = lb - c.ctxLines + 1
} else {
clb = 1
}
case c.ctxLines == 0:
clb = lb - c.ctxLines
clb = lb
case i != len(c.chunks)-1:
next := c.chunks[i+1]
if next.Type() == op || next.Type() == Equal {
Expand Down
37 changes: 37 additions & 0 deletions plumbing/format/diff/unified_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,43 @@ var oneChunkPatchInverted Patch = testPatch{
}

var fixtures []*fixture = []*fixture{{
patch: testPatch{
message: "",
filePatches: []testFilePatch{{
from: &testFile{
mode: filemode.Regular,
path: "README.md",
seed: "hello\nworld\n",
},
to: &testFile{
mode: filemode.Regular,
path: "README.md",
seed: "hello\nbug\n",
},
chunks: []testChunk{{
content: "hello",
op: Equal,
}, {
content: "world",
op: Delete,
}, {
content: "bug",
op: Add,
}},
}},
},
desc: "positive negative number",
context: 2,
diff: `diff --git a/README.md b/README.md
index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
hello
-world
+bug
`,
}, {
patch: testPatch{
message: "",
filePatches: []testFilePatch{{
Expand Down
3 changes: 3 additions & 0 deletions plumbing/format/gitignore/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (p *pattern) globMatch(path []string, isDir bool) bool {
} else if match {
matched = true
break
} else if len(path) == 0 {
// if nothing left then fail
matched = false
}
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions plumbing/format/gitignore/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,9 @@ func (s *PatternSuite) TestGlobMatch_wrongPattern_onTraversal_mismatch(c *C) {
r := p.Match([]string{"value", "head", "vol["}, false)
c.Assert(r, Equals, NoMatch)
}

func (s *PatternSuite) TestGlobMatch_issue_923(c *C) {
p := ParsePattern("**/android/**/GeneratedPluginRegistrant.java", nil)
r := p.Match([]string{"packages", "flutter_tools", "lib", "src", "android", "gradle.dart"}, false)
c.Assert(r, Equals, NoMatch)
}
69 changes: 69 additions & 0 deletions plumbing/format/idxfile/idxfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package idxfile
import (
"bytes"
"io"
"sort"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/utils/binary"
Expand Down Expand Up @@ -34,6 +35,9 @@ type Index interface {
Count() (int64, error)
// Entries returns an iterator to retrieve all index entries.
Entries() (EntryIter, error)
// EntriesByOffset returns an iterator to retrieve all index entries ordered
// by offset.
EntriesByOffset() (EntryIter, error)
}

// MemoryIndex is the in memory representation of an idx file.
Expand Down Expand Up @@ -215,6 +219,36 @@ func (idx *MemoryIndex) Entries() (EntryIter, error) {
return &idxfileEntryIter{idx, 0, 0, 0}, nil
}

// EntriesByOffset implements the Index interface.
func (idx *MemoryIndex) EntriesByOffset() (EntryIter, error) {
count, err := idx.Count()
if err != nil {
return nil, err
}

iter := &idxfileEntryOffsetIter{
entries: make(entriesByOffset, count),
}

entries, err := idx.Entries()
if err != nil {
return nil, err
}

for pos := 0; int64(pos) < count; pos++ {
entry, err := entries.Next()
if err != nil {
return nil, err
}

iter.entries[pos] = entry
}

sort.Sort(iter.entries)

return iter, nil
}

// EntryIter is an iterator that will return the entries in a packfile index.
type EntryIter interface {
// Next returns the next entry in the packfile index.
Expand Down Expand Up @@ -276,3 +310,38 @@ type Entry struct {
CRC32 uint32
Offset uint64
}

type idxfileEntryOffsetIter struct {
entries entriesByOffset
pos int
}

func (i *idxfileEntryOffsetIter) Next() (*Entry, error) {
if i.pos >= len(i.entries) {
return nil, io.EOF
}

entry := i.entries[i.pos]
i.pos++

return entry, nil
}

func (i *idxfileEntryOffsetIter) Close() error {
i.pos = len(i.entries) + 1
return nil
}

type entriesByOffset []*Entry

func (o entriesByOffset) Len() int {
return len(o)
}

func (o entriesByOffset) Less(i int, j int) bool {
return o[i].Offset < o[j].Offset
}

func (o entriesByOffset) Swap(i int, j int) {
o[i], o[j] = o[j], o[i]
}
15 changes: 15 additions & 0 deletions plumbing/format/idxfile/idxfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ func (s *IndexSuite) TestFindHash(c *C) {
}
}

func (s *IndexSuite) TestEntriesByOffset(c *C) {
idx, err := fixtureIndex()
c.Assert(err, IsNil)

entries, err := idx.EntriesByOffset()
c.Assert(err, IsNil)

for _, pos := range fixtureOffsets {
e, err := entries.Next()
c.Assert(err, IsNil)

c.Assert(e.Offset, Equals, uint64(pos))
}
}

var fixtureHashes = []plumbing.Hash{
plumbing.NewHash("303953e5aa461c203a324821bc1717f9b4fff895"),
plumbing.NewHash("5296768e3d9f661387ccbff18c4dea6c997fd78c"),
Expand Down
7 changes: 3 additions & 4 deletions plumbing/format/packfile/encoder_advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/format/idxfile"
. "gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
Expand All @@ -32,8 +33,7 @@ func (s *EncoderAdvancedSuite) TestEncodeDecode(c *C) {
fixs = append(fixs, fixtures.ByURL("https://github.com/src-d/go-git.git").
ByTag("packfile").ByTag(".git").One())
fixs.Test(c, func(f *fixtures.Fixture) {
storage, err := filesystem.NewStorage(f.DotGit())
c.Assert(err, IsNil)
storage := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
s.testEncodeDecode(c, storage, 10)
})
}
Expand All @@ -47,8 +47,7 @@ func (s *EncoderAdvancedSuite) TestEncodeDecodeNoDeltaCompression(c *C) {
fixs = append(fixs, fixtures.ByURL("https://github.com/src-d/go-git.git").
ByTag("packfile").ByTag(".git").One())
fixs.Test(c, func(f *fixtures.Fixture) {
storage, err := filesystem.NewStorage(f.DotGit())
c.Assert(err, IsNil)
storage := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
s.testEncodeDecode(c, storage, 0)
})
}
Expand Down
10 changes: 10 additions & 0 deletions plumbing/format/packfile/fsobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func NewFSObject(

// Reader implements the plumbing.EncodedObject interface.
func (o *FSObject) Reader() (io.ReadCloser, error) {
obj, ok := o.cache.Get(o.hash)
if ok {
reader, err := obj.Reader()
if err != nil {
return nil, err
}

return reader, nil
}

f, err := o.fs.Open(o.path)
if err != nil {
return nil, err
Expand Down
17 changes: 16 additions & 1 deletion plumbing/format/packfile/packfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ func (p *Packfile) nextObject() (plumbing.EncodedObject, error) {
}

func (p *Packfile) getObjectContent(offset int64) (io.ReadCloser, error) {
ref, err := p.FindHash(offset)
if err == nil {
obj, ok := p.cacheGet(ref)
if ok {
reader, err := obj.Reader()
if err != nil {
return nil, err
}

return reader, nil
}
}

if _, err := p.s.SeekFromStart(offset); err != nil {
return nil, err
}
Expand Down Expand Up @@ -306,6 +319,8 @@ func (p *Packfile) fillRegularObjectContent(obj plumbing.EncodedObject) error {
}

_, _, err = p.s.NextObject(w)
p.cachePut(obj)

return err
}

Expand Down Expand Up @@ -394,7 +409,7 @@ func (p *Packfile) GetByType(typ plumbing.ObjectType) (storer.EncodedObjectIter,
plumbing.TreeObject,
plumbing.CommitObject,
plumbing.TagObject:
entries, err := p.Entries()
entries, err := p.EntriesByOffset()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions plumbing/object/change_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
Expand All @@ -23,8 +24,7 @@ type ChangeAdaptorSuite struct {
func (s *ChangeAdaptorSuite) SetUpSuite(c *C) {
s.Suite.SetUpSuite(c)
s.Fixture = fixtures.Basic().One()
sto, err := filesystem.NewStorage(s.Fixture.DotGit())
c.Assert(err, IsNil)
sto := filesystem.NewStorage(s.Fixture.DotGit(), cache.NewObjectLRUDefault())
s.Storer = sto
}

Expand Down
7 changes: 3 additions & 4 deletions plumbing/object/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/format/diff"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
Expand All @@ -25,8 +26,7 @@ func (s *ChangeSuite) SetUpSuite(c *C) {
s.Suite.SetUpSuite(c)
s.Fixture = fixtures.ByURL("https://github.com/src-d/go-git.git").
ByTag(".git").One()
sto, err := filesystem.NewStorage(s.Fixture.DotGit())
c.Assert(err, IsNil)
sto := filesystem.NewStorage(s.Fixture.DotGit(), cache.NewObjectLRUDefault())
s.Storer = sto
}

Expand Down Expand Up @@ -253,8 +253,7 @@ func (s *ChangeSuite) TestNoFileFilemodes(c *C) {
s.Suite.SetUpSuite(c)
f := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One()

sto, err := filesystem.NewStorage(f.DotGit())
c.Assert(err, IsNil)
sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())

iter, err := sto.IterEncodedObjects(plumbing.AnyObject)
c.Assert(err, IsNil)
Expand Down
Loading

0 comments on commit bf0593d

Please sign in to comment.