diff options
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/difftree/difftree.go | 246 | ||||
-rw-r--r-- | plumbing/difftree/difftree_test.go | 523 | ||||
-rw-r--r-- | plumbing/format/index/encoder.go | 10 | ||||
-rw-r--r-- | plumbing/object/file_test.go | 4 | ||||
-rw-r--r-- | plumbing/object/tree_test.go | 5 | ||||
-rw-r--r-- | plumbing/storer/reference.go | 1 | ||||
-rw-r--r-- | plumbing/transport/file/common_test.go | 17 | ||||
-rw-r--r-- | plumbing/transport/file/server_test.go | 49 |
8 files changed, 262 insertions, 593 deletions
diff --git a/plumbing/difftree/difftree.go b/plumbing/difftree/difftree.go index 869f496..76c5f27 100644 --- a/plumbing/difftree/difftree.go +++ b/plumbing/difftree/difftree.go @@ -2,252 +2,24 @@ package difftree import ( "bytes" - "fmt" - "io" - "sort" - "strings" - "srcd.works/go-git.v4/plumbing" "srcd.works/go-git.v4/plumbing/object" + "srcd.works/go-git.v4/utils/merkletrie" + "srcd.works/go-git.v4/utils/merkletrie/noder" ) -type Action int - -func (a Action) String() string { - switch a { - case Insert: - return "Insert" - case Delete: - return "Delete" - case Modify: - return "Modify" - default: - panic(fmt.Sprintf("unsupported action: %d", a)) - } -} - -const ( - Insert Action = iota - Delete - Modify -) - -type Change struct { - Action - From ChangeEntry - To ChangeEntry -} - -type ChangeEntry struct { - Name string - Tree *object.Tree - TreeEntry object.TreeEntry -} - -func (c *Change) Files() (from, to *object.File, err error) { - if c.Action == Insert || c.Action == Modify { - to, err = c.To.Tree.TreeEntryFile(&c.To.TreeEntry) - if err != nil { - return - } - - } - - if c.Action == Delete || c.Action == Modify { - from, err = c.From.Tree.TreeEntryFile(&c.From.TreeEntry) - if err != nil { - return - } - } - - return -} - -func (c *Change) String() string { - return fmt.Sprintf("<Action: %s, Path: %s>", c.Action, c.name()) -} - -func (c *Change) name() string { - if c.From.Name != "" { - return c.From.Name - } - - return c.To.Name -} - -type Changes []*Change - -func newEmpty() Changes { - return make([]*Change, 0, 0) -} - func DiffTree(a, b *object.Tree) ([]*Change, error) { - if a == b { - return newEmpty(), nil - } - - if a == nil || b == nil { - return newWithEmpty(a, b) - } - - return newDiffTree(a, b) -} - -func (c Changes) Len() int { - return len(c) -} - -func (c Changes) Swap(i, j int) { - c[i], c[j] = c[j], c[i] -} - -func (c Changes) Less(i, j int) bool { - return strings.Compare(c[i].name(), c[j].name()) < 0 -} - -func (c Changes) String() string { - var buffer bytes.Buffer - buffer.WriteString("[") - comma := "" - for _, v := range c { - buffer.WriteString(comma) - buffer.WriteString(v.String()) - comma = ", " - } - buffer.WriteString("]") - - return buffer.String() -} - -func newWithEmpty(a, b *object.Tree) (Changes, error) { - changes := newEmpty() - - var action Action - var tree *object.Tree - if a == nil { - action = Insert - tree = b - } else { - action = Delete - tree = a - } - - w := object.NewTreeWalker(tree, true) - defer w.Close() - - for { - path, entry, err := w.Next() - if err == io.EOF { - break - } else if err != nil { - return nil, fmt.Errorf("cannot get next file: %s", err) - } - - if entry.Mode.IsDir() { - continue - } - - c := &Change{Action: action} - - if action == Insert { - c.To.Name = path - c.To.TreeEntry = entry - c.To.Tree = tree - } else { - c.From.Name = path - c.From.TreeEntry = entry - c.From.Tree = tree - } - - changes = append(changes, c) - } - - return changes, nil -} - -// FIXME: this is very inefficient, but correct. -// The proper way to do this is to implement a diff-tree algorithm, -// while taking advantage of the tree hashes to avoid traversing -// subtrees when the hash is equal in both inputs. -func newDiffTree(a, b *object.Tree) ([]*Change, error) { - var result []*Change - - aChanges, err := newWithEmpty(a, nil) - if err != nil { - return nil, fmt.Errorf("cannot create nil-diff of source tree: %s", err) - } - sort.Sort(aChanges) + from := newTreeNoder(a) + to := newTreeNoder(b) - bChanges, err := newWithEmpty(nil, b) - if err != nil { - return nil, fmt.Errorf("cannot create nil-diff of destination tree: %s", err) - } - sort.Sort(bChanges) - - for len(aChanges) > 0 && len(bChanges) > 0 { - switch comp := strings.Compare(aChanges[0].name(), bChanges[0].name()); { - case comp == 0: // append as "Modify" or ignore if not changed - modified, err := hasChange(a, b, aChanges[0].name()) - if err != nil { - return nil, err - } - - if modified { - c := mergeInsertAndDeleteIntoModify(aChanges[0], bChanges[0]) - result = append(result, c) - } - - aChanges = aChanges[1:] - bChanges = bChanges[1:] - case comp < 0: // delete first a change - result = append(result, aChanges[0]) - aChanges = aChanges[1:] - case comp > 0: // insert first b change - result = append(result, bChanges[0]) - bChanges = bChanges[1:] - } + hashEqual := func(a, b noder.Hasher) bool { + return bytes.Equal(a.Hash(), b.Hash()) } - // append all remaining changes in aChanges, if any, as deletes - // append all remaining changes in bChanges, if any, as inserts - result = append(result, aChanges...) - result = append(result, bChanges...) - - return result, nil -} - -func mergeInsertAndDeleteIntoModify(a, b *Change) *Change { - c := &Change{Action: Modify} - c.From.Name = a.From.Name - c.From.Tree = a.From.Tree - c.From.TreeEntry = a.From.TreeEntry - c.To.Name = b.To.Name - c.To.Tree = b.To.Tree - c.To.TreeEntry = b.To.TreeEntry - - return c -} - -func hasChange(a, b *object.Tree, path string) (bool, error) { - ha, err := hash(a, path) - if err != nil { - return false, err - } - - hb, err := hash(b, path) - if err != nil { - return false, err - } - - return ha != hb, nil -} - -func hash(tree *object.Tree, path string) (plumbing.Hash, error) { - file, err := tree.File(path) + merkletrieChanges, err := merkletrie.DiffTree(from, to, hashEqual) if err != nil { - var empty plumbing.Hash - return empty, fmt.Errorf("cannot find file %s in tree: %s", path, err) + return nil, err } - return file.Hash, nil + return newChanges(merkletrieChanges) } diff --git a/plumbing/difftree/difftree_test.go b/plumbing/difftree/difftree_test.go index 7679d0f..e2519b3 100644 --- a/plumbing/difftree/difftree_test.go +++ b/plumbing/difftree/difftree_test.go @@ -4,14 +4,15 @@ import ( "sort" "testing" - "github.com/src-d/go-git-fixtures" "srcd.works/go-git.v4/plumbing" "srcd.works/go-git.v4/plumbing/format/packfile" "srcd.works/go-git.v4/plumbing/object" "srcd.works/go-git.v4/plumbing/storer" "srcd.works/go-git.v4/storage/filesystem" "srcd.works/go-git.v4/storage/memory" + "srcd.works/go-git.v4/utils/merkletrie" + "github.com/src-d/go-git-fixtures" . "gopkg.in/check.v1" ) @@ -33,12 +34,6 @@ func (s *DiffTreeSuite) SetUpSuite(c *C) { s.cache = make(map[string]storer.EncodedObjectStorer) } -func (s *DiffTreeSuite) tree(c *C, h plumbing.Hash) *object.Tree { - t, err := object.GetTree(s.Storer, h) - c.Assert(err, IsNil) - return t -} - func (s *DiffTreeSuite) commitFromStorer(c *C, sto storer.EncodedObjectStorer, h plumbing.Hash) *object.Commit { @@ -76,108 +71,48 @@ func (s *DiffTreeSuite) storageFromPackfile(f *fixtures.Fixture) storer.EncodedO var _ = Suite(&DiffTreeSuite{}) -func (s *DiffTreeSuite) TestActionString(c *C) { - expected := "Insert" - action := Insert - obtained := action.String() - c.Assert(obtained, Equals, expected) - - expected = "Delete" - action = Delete - obtained = action.String() - c.Assert(obtained, Equals, expected) - - expected = "Modify" - action = Modify - obtained = action.String() - c.Assert(obtained, Equals, expected) - - action = 37 - c.Assert(func() { _ = action.String() }, - PanicMatches, "unsupported action: 37") -} - -func (s *DiffTreeSuite) TestChangeFilesInsert(c *C) { - tree := s.tree(c, plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")) - - change := &Change{Action: Insert} - change.To.Name = "json/long.json" - change.To.Tree = tree - change.To.TreeEntry.Hash = plumbing.NewHash("49c6bb89b17060d7b4deacb7b338fcc6ea2352a9") - - from, to, err := change.Files() - c.Assert(err, IsNil) - c.Assert(from, IsNil) - c.Assert(to.ID(), Equals, change.To.TreeEntry.Hash) -} - -func (s *DiffTreeSuite) TestChangeFilesDelete(c *C) { - tree := s.tree(c, plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")) - - change := &Change{Action: Delete} - change.From.Name = "json/long.json" - change.From.Tree = tree - change.From.TreeEntry.Hash = plumbing.NewHash("49c6bb89b17060d7b4deacb7b338fcc6ea2352a9") - - from, to, err := change.Files() - c.Assert(err, IsNil) - c.Assert(to, IsNil) - c.Assert(from.ID(), Equals, change.From.TreeEntry.Hash) -} - -func (s *DiffTreeSuite) TestChangeFilesModify(c *C) { - tree := s.tree(c, plumbing.NewHash("a8d315b2b1c615d43042c3a62402b8a54288cf5c")) - - change := &Change{Action: Modify} - change.To.Name = "json/long.json" - change.To.Tree = tree - change.To.TreeEntry.Hash = plumbing.NewHash("49c6bb89b17060d7b4deacb7b338fcc6ea2352a9") - change.From.Name = "json/long.json" - change.From.Tree = tree - change.From.TreeEntry.Hash = plumbing.NewHash("9a48f23120e880dfbe41f7c9b7b708e9ee62a492") - - from, to, err := change.Files() - c.Assert(err, IsNil) - c.Assert(to.ID(), Equals, change.To.TreeEntry.Hash) - c.Assert(from.ID(), Equals, change.From.TreeEntry.Hash) +type expectChange struct { + Action merkletrie.Action + Name string } -func (s *DiffTreeSuite) TestChangeString(c *C) { - expected := "<Action: Insert, Path: foo>" - change := &Change{Action: Insert} - change.From.Name = "foo" - - obtained := change.String() - c.Assert(obtained, Equals, expected) +func assertChanges(a Changes, c *C) { + for _, changes := range a { + action, err := changes.Action() + c.Assert(err, IsNil) + switch action { + case merkletrie.Insert: + c.Assert(changes.From.Tree, IsNil) + c.Assert(changes.To.Tree, NotNil) + case merkletrie.Delete: + c.Assert(changes.From.Tree, NotNil) + c.Assert(changes.To.Tree, IsNil) + case merkletrie.Modify: + c.Assert(changes.From.Tree, NotNil) + c.Assert(changes.To.Tree, NotNil) + default: + c.Fatalf("unknown action: %d", action) + } + } } -func (s *DiffTreeSuite) TestChangesString(c *C) { - expected := "[]" - changes := newEmpty() - obtained := changes.String() - c.Assert(obtained, Equals, expected) - - expected = "[<Action: Modify, Path: bla>]" - changes = make([]*Change, 1) - changes[0] = &Change{Action: Modify} - changes[0].From.Name = "bla" +func equalChanges(a Changes, b []expectChange, c *C) bool { + if len(a) != len(b) { + return false + } - obtained = changes.String() - c.Assert(obtained, Equals, expected) + sort.Sort(a) - expected = "[<Action: Modify, Path: bla>, <Action: Insert, Path: foo/bar>]" - changes = make([]*Change, 2) - changes[0] = &Change{Action: Modify} - changes[0].From.Name = "bla" - changes[1] = &Change{Action: Insert} - changes[1].From.Name = "foo/bar" - obtained = changes.String() - c.Assert(obtained, Equals, expected) -} + for i, va := range a { + vb := b[i] + action, err := va.Action() + c.Assert(err, IsNil) + if action != vb.Action || va.name() != vb.Name { + return false + } + } -type expectChange struct { - Action Action - Name string + return true } func (s *DiffTreeSuite) TestDiffTree(c *C) { @@ -186,186 +121,209 @@ func (s *DiffTreeSuite) TestDiffTree(c *C) { commit1 string // the commit of the first tree commit2 string // the commit of the second tree expected []expectChange // the expected list of []changeExpect - }{{ - "https://github.com/dezfowler/LiteMock.git", - "", - "", - []expectChange{}, - }, { - "https://github.com/dezfowler/LiteMock.git", - "b7965eaa2c4f245d07191fe0bcfe86da032d672a", - "b7965eaa2c4f245d07191fe0bcfe86da032d672a", - []expectChange{}, - }, { - "https://github.com/dezfowler/LiteMock.git", - "", - "b7965eaa2c4f245d07191fe0bcfe86da032d672a", - []expectChange{ - {Action: Insert, Name: "README"}, + }{ + { + "https://github.com/dezfowler/LiteMock.git", + "", + "", + []expectChange{}, + }, + { + "https://github.com/dezfowler/LiteMock.git", + "b7965eaa2c4f245d07191fe0bcfe86da032d672a", + "b7965eaa2c4f245d07191fe0bcfe86da032d672a", + []expectChange{}, + }, + { + "https://github.com/dezfowler/LiteMock.git", + "", + "b7965eaa2c4f245d07191fe0bcfe86da032d672a", + []expectChange{ + {Action: merkletrie.Insert, Name: "README"}, + }, }, - }, { - "https://github.com/dezfowler/LiteMock.git", - "b7965eaa2c4f245d07191fe0bcfe86da032d672a", - "", - []expectChange{ - {Action: Delete, Name: "README"}, + { + "https://github.com/dezfowler/LiteMock.git", + "b7965eaa2c4f245d07191fe0bcfe86da032d672a", + "", + []expectChange{ + {Action: merkletrie.Delete, Name: "README"}, + }, }, - }, { - "https://github.com/githubtraining/example-branches.git", - "", - "f0eb272cc8f77803478c6748103a1450aa1abd37", - []expectChange{ - {Action: Insert, Name: "README.md"}, + { + "https://github.com/githubtraining/example-branches.git", + "", + "f0eb272cc8f77803478c6748103a1450aa1abd37", + []expectChange{ + {Action: merkletrie.Insert, Name: "README.md"}, + }, }, - }, { - "https://github.com/githubtraining/example-branches.git", - "f0eb272cc8f77803478c6748103a1450aa1abd37", - "", - []expectChange{ - {Action: Delete, Name: "README.md"}, + { + "https://github.com/githubtraining/example-branches.git", + "f0eb272cc8f77803478c6748103a1450aa1abd37", + "", + []expectChange{ + {Action: merkletrie.Delete, Name: "README.md"}, + }, }, - }, { - "https://github.com/githubtraining/example-branches.git", - "f0eb272cc8f77803478c6748103a1450aa1abd37", - "f0eb272cc8f77803478c6748103a1450aa1abd37", - []expectChange{}, - }, { - "https://github.com/github/gem-builder.git", - "", - "9608eed92b3839b06ebf72d5043da547de10ce85", - []expectChange{ - {Action: Insert, Name: "README"}, - {Action: Insert, Name: "gem_builder.rb"}, - {Action: Insert, Name: "gem_eval.rb"}, + { + "https://github.com/githubtraining/example-branches.git", + "f0eb272cc8f77803478c6748103a1450aa1abd37", + "f0eb272cc8f77803478c6748103a1450aa1abd37", + []expectChange{}, }, - }, { - "https://github.com/github/gem-builder.git", - "9608eed92b3839b06ebf72d5043da547de10ce85", - "", - []expectChange{ - {Action: Delete, Name: "README"}, - {Action: Delete, Name: "gem_builder.rb"}, - {Action: Delete, Name: "gem_eval.rb"}, + { + "https://github.com/github/gem-builder.git", + "", + "9608eed92b3839b06ebf72d5043da547de10ce85", + []expectChange{ + {Action: merkletrie.Insert, Name: "README"}, + {Action: merkletrie.Insert, Name: "gem_builder.rb"}, + {Action: merkletrie.Insert, Name: "gem_eval.rb"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "9608eed92b3839b06ebf72d5043da547de10ce85", - "9608eed92b3839b06ebf72d5043da547de10ce85", - []expectChange{}, - }, { - "https://github.com/toqueteos/ts3.git", - "", - "764e914b75d6d6df1fc5d832aa9840f590abf1bb", - []expectChange{ - {Action: Insert, Name: "README.markdown"}, - {Action: Insert, Name: "examples/bot.go"}, - {Action: Insert, Name: "examples/raw_shell.go"}, - {Action: Insert, Name: "helpers.go"}, - {Action: Insert, Name: "ts3.go"}, + { + "https://github.com/github/gem-builder.git", + "9608eed92b3839b06ebf72d5043da547de10ce85", + "", + []expectChange{ + {Action: merkletrie.Delete, Name: "README"}, + {Action: merkletrie.Delete, Name: "gem_builder.rb"}, + {Action: merkletrie.Delete, Name: "gem_eval.rb"}, + }, }, - }, { - "https://github.com/toqueteos/ts3.git", - "764e914b75d6d6df1fc5d832aa9840f590abf1bb", - "", - []expectChange{ - {Action: Delete, Name: "README.markdown"}, - {Action: Delete, Name: "examples/bot.go"}, - {Action: Delete, Name: "examples/raw_shell.go"}, - {Action: Delete, Name: "helpers.go"}, - {Action: Delete, Name: "ts3.go"}, + { + "https://github.com/github/gem-builder.git", + "9608eed92b3839b06ebf72d5043da547de10ce85", + "9608eed92b3839b06ebf72d5043da547de10ce85", + []expectChange{}, }, - }, { - "https://github.com/toqueteos/ts3.git", - "764e914b75d6d6df1fc5d832aa9840f590abf1bb", - "764e914b75d6d6df1fc5d832aa9840f590abf1bb", - []expectChange{}, - }, { - "https://github.com/github/gem-builder.git", - "9608eed92b3839b06ebf72d5043da547de10ce85", - "6c41e05a17e19805879689414026eb4e279f7de0", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, + { + "https://github.com/toqueteos/ts3.git", + "", + "764e914b75d6d6df1fc5d832aa9840f590abf1bb", + []expectChange{ + {Action: merkletrie.Insert, Name: "README.markdown"}, + {Action: merkletrie.Insert, Name: "examples/bot.go"}, + {Action: merkletrie.Insert, Name: "examples/raw_shell.go"}, + {Action: merkletrie.Insert, Name: "helpers.go"}, + {Action: merkletrie.Insert, Name: "ts3.go"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "6c41e05a17e19805879689414026eb4e279f7de0", - "89be3aac2f178719c12953cc9eaa23441f8d9371", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, - {Action: Insert, Name: "gem_eval_test.rb"}, - {Action: Insert, Name: "security.rb"}, - {Action: Insert, Name: "security_test.rb"}, + { + "https://github.com/toqueteos/ts3.git", + "764e914b75d6d6df1fc5d832aa9840f590abf1bb", + "", + []expectChange{ + {Action: merkletrie.Delete, Name: "README.markdown"}, + {Action: merkletrie.Delete, Name: "examples/bot.go"}, + {Action: merkletrie.Delete, Name: "examples/raw_shell.go"}, + {Action: merkletrie.Delete, Name: "helpers.go"}, + {Action: merkletrie.Delete, Name: "ts3.go"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "89be3aac2f178719c12953cc9eaa23441f8d9371", - "597240b7da22d03ad555328f15abc480b820acc0", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, + { + "https://github.com/toqueteos/ts3.git", + "764e914b75d6d6df1fc5d832aa9840f590abf1bb", + "764e914b75d6d6df1fc5d832aa9840f590abf1bb", + []expectChange{}, }, - }, { - "https://github.com/github/gem-builder.git", - "597240b7da22d03ad555328f15abc480b820acc0", - "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, - {Action: Modify, Name: "gem_eval_test.rb"}, - {Action: Insert, Name: "lazy_dir.rb"}, - {Action: Insert, Name: "lazy_dir_test.rb"}, - {Action: Modify, Name: "security.rb"}, - {Action: Modify, Name: "security_test.rb"}, + { + "https://github.com/github/gem-builder.git", + "9608eed92b3839b06ebf72d5043da547de10ce85", + "6c41e05a17e19805879689414026eb4e279f7de0", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", - "597240b7da22d03ad555328f15abc480b820acc0", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, - {Action: Modify, Name: "gem_eval_test.rb"}, - {Action: Delete, Name: "lazy_dir.rb"}, - {Action: Delete, Name: "lazy_dir_test.rb"}, - {Action: Modify, Name: "security.rb"}, - {Action: Modify, Name: "security_test.rb"}, + { + "https://github.com/github/gem-builder.git", + "6c41e05a17e19805879689414026eb4e279f7de0", + "89be3aac2f178719c12953cc9eaa23441f8d9371", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + {Action: merkletrie.Insert, Name: "gem_eval_test.rb"}, + {Action: merkletrie.Insert, Name: "security.rb"}, + {Action: merkletrie.Insert, Name: "security_test.rb"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", - "ca9fd470bacb6262eb4ca23ee48bb2f43711c1ff", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, - {Action: Modify, Name: "security.rb"}, - {Action: Modify, Name: "security_test.rb"}, + { + "https://github.com/github/gem-builder.git", + "89be3aac2f178719c12953cc9eaa23441f8d9371", + "597240b7da22d03ad555328f15abc480b820acc0", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + }, }, - }, { - "https://github.com/github/gem-builder.git", - "fe3c86745f887c23a0d38c85cfd87ca957312f86", - "b7e3f636febf7a0cd3ab473b6d30081786d2c5b6", - []expectChange{ - {Action: Modify, Name: "gem_eval.rb"}, - {Action: Modify, Name: "gem_eval_test.rb"}, - {Action: Insert, Name: "git_mock"}, - {Action: Modify, Name: "lazy_dir.rb"}, - {Action: Modify, Name: "lazy_dir_test.rb"}, - {Action: Modify, Name: "security.rb"}, + { + "https://github.com/github/gem-builder.git", + "597240b7da22d03ad555328f15abc480b820acc0", + "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + {Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, + {Action: merkletrie.Insert, Name: "lazy_dir.rb"}, + {Action: merkletrie.Insert, Name: "lazy_dir_test.rb"}, + {Action: merkletrie.Modify, Name: "security.rb"}, + {Action: merkletrie.Modify, Name: "security_test.rb"}, + }, }, - }, { - "https://github.com/rumpkernel/rumprun-xen.git", - "1831e47b0c6db750714cd0e4be97b5af17fb1eb0", - "51d8515578ea0c88cc8fc1a057903675cf1fc16c", - []expectChange{ - {Action: Modify, Name: "Makefile"}, - {Action: Modify, Name: "netbsd_init.c"}, - {Action: Modify, Name: "rumphyper_stubs.c"}, - {Action: Delete, Name: "sysproxy.c"}, + { + "https://github.com/github/gem-builder.git", + "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", + "597240b7da22d03ad555328f15abc480b820acc0", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + {Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, + {Action: merkletrie.Delete, Name: "lazy_dir.rb"}, + {Action: merkletrie.Delete, Name: "lazy_dir_test.rb"}, + {Action: merkletrie.Modify, Name: "security.rb"}, + {Action: merkletrie.Modify, Name: "security_test.rb"}, + }, }, - }, { - "https://github.com/rumpkernel/rumprun-xen.git", - "1831e47b0c6db750714cd0e4be97b5af17fb1eb0", - "e13e678f7ee9badd01b120889e0ec5fdc8ae3802", - []expectChange{ - {Action: Modify, Name: "app-tools/rumprun"}, + { + "https://github.com/github/gem-builder.git", + "0260380e375d2dd0e1a8fcab15f91ce56dbe778e", + "ca9fd470bacb6262eb4ca23ee48bb2f43711c1ff", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + {Action: merkletrie.Modify, Name: "security.rb"}, + {Action: merkletrie.Modify, Name: "security_test.rb"}, + }, }, - }} { + { + "https://github.com/github/gem-builder.git", + "fe3c86745f887c23a0d38c85cfd87ca957312f86", + "b7e3f636febf7a0cd3ab473b6d30081786d2c5b6", + []expectChange{ + {Action: merkletrie.Modify, Name: "gem_eval.rb"}, + {Action: merkletrie.Modify, Name: "gem_eval_test.rb"}, + {Action: merkletrie.Insert, Name: "git_mock"}, + {Action: merkletrie.Modify, Name: "lazy_dir.rb"}, + {Action: merkletrie.Modify, Name: "lazy_dir_test.rb"}, + {Action: merkletrie.Modify, Name: "security.rb"}, + }, + }, + { + "https://github.com/rumpkernel/rumprun-xen.git", + "1831e47b0c6db750714cd0e4be97b5af17fb1eb0", + "51d8515578ea0c88cc8fc1a057903675cf1fc16c", + []expectChange{ + {Action: merkletrie.Modify, Name: "Makefile"}, + {Action: merkletrie.Modify, Name: "netbsd_init.c"}, + {Action: merkletrie.Modify, Name: "rumphyper_stubs.c"}, + {Action: merkletrie.Delete, Name: "sysproxy.c"}, + }, + }, + { + "https://github.com/rumpkernel/rumprun-xen.git", + "1831e47b0c6db750714cd0e4be97b5af17fb1eb0", + "e13e678f7ee9badd01b120889e0ec5fdc8ae3802", + []expectChange{ + {Action: merkletrie.Modify, Name: "app-tools/rumprun"}, + }, + }, + } { f := fixtures.ByURL(t.repository).One() sto := s.storageFromPackfile(f) @@ -388,43 +346,10 @@ func (s *DiffTreeSuite) TestDiffTree(c *C) { obtained, err := DiffTree(tree1, tree2) c.Assert(err, IsNil, Commentf("subtest %d: unable to calculate difftree: %s", i, err)) - c.Assert(equalChanges(obtained, t.expected), Equals, true, + c.Assert(equalChanges(obtained, t.expected, c), Equals, true, Commentf("subtest:%d\nrepo=%s\ncommit1=%s\ncommit2=%s\nexpected=%s\nobtained=%s", i, t.repository, t.commit1, t.commit2, t.expected, obtained)) assertChanges(obtained, c) } } - -func assertChanges(a Changes, c *C) { - for _, changes := range a { - switch changes.Action { - case Insert: - c.Assert(changes.From.Tree, IsNil) - c.Assert(changes.To.Tree, NotNil) - case Delete: - c.Assert(changes.From.Tree, NotNil) - c.Assert(changes.To.Tree, IsNil) - case Modify: - c.Assert(changes.From.Tree, NotNil) - c.Assert(changes.To.Tree, NotNil) - } - } -} - -func equalChanges(a Changes, b []expectChange) bool { - if len(a) != len(b) { - return false - } - - sort.Sort(a) - - for i, va := range a { - vb := b[i] - if va.Action != vb.Action || va.name() != vb.Name { - return false - } - } - - return true -} diff --git a/plumbing/format/index/encoder.go b/plumbing/format/index/encoder.go index 4699d43..bdb10c1 100644 --- a/plumbing/format/index/encoder.go +++ b/plumbing/format/index/encoder.go @@ -62,7 +62,7 @@ func (e *Encoder) encodeHeader(idx *Index) error { } func (e *Encoder) encodeEntries(idx *Index) error { - sort.Sort(ByName(idx.Entries)) + sort.Sort(byName(idx.Entries)) for _, entry := range idx.Entries { if err := e.encodeEntry(&entry); err != nil { @@ -143,8 +143,8 @@ func (e *Encoder) encodeFooter() error { return binary.Write(e.w, e.hash.Sum(nil)) } -type ByName []Entry +type byName []Entry -func (l ByName) Len() int { return len(l) } -func (l ByName) Swap(i, j int) { l[i], l[j] = l[j], l[i] } -func (l ByName) Less(i, j int) bool { return l[i].Name < l[j].Name } +func (l byName) Len() int { return len(l) } +func (l byName) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l byName) Less(i, j int) bool { return l[i].Name < l[j].Name } diff --git a/plumbing/object/file_test.go b/plumbing/object/file_test.go index 426fa8f..ff01c9f 100644 --- a/plumbing/object/file_test.go +++ b/plumbing/object/file_test.go @@ -249,7 +249,9 @@ func (s *FileSuite) TestFileIter(c *C) { } func (s *FileSuite) TestFileIterSubmodule(c *C) { - st, err := filesystem.NewStorage(fixtures.ByTag("submodule").One().DotGit()) + dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit() + st, err := filesystem.NewStorage(dotgit) + c.Assert(err, IsNil) hash := plumbing.NewHash("a692ec699bff9117c1ed91752afbb7d9d272ebef") diff --git a/plumbing/object/tree_test.go b/plumbing/object/tree_test.go index 0ddf391..8ea31bb 100644 --- a/plumbing/object/tree_test.go +++ b/plumbing/object/tree_test.go @@ -266,7 +266,8 @@ func (s *TreeSuite) TestTreeWalkerNextNonRecursive(c *C) { } func (s *TreeSuite) TestTreeWalkerNextSubmodule(c *C) { - st, err := filesystem.NewStorage(fixtures.ByTag("submodule").One().DotGit()) + dotgit := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().DotGit() + st, err := filesystem.NewStorage(dotgit) c.Assert(err, IsNil) hash := plumbing.NewHash("a692ec699bff9117c1ed91752afbb7d9d272ebef") @@ -284,6 +285,8 @@ func (s *TreeSuite) TestTreeWalkerNextSubmodule(c *C) { var count int walker := NewTreeWalker(tree, true) + defer walker.Close() + for { name, entry, err := walker.Next() if err == io.EOF { diff --git a/plumbing/storer/reference.go b/plumbing/storer/reference.go index 40474f9..692fe88 100644 --- a/plumbing/storer/reference.go +++ b/plumbing/storer/reference.go @@ -18,6 +18,7 @@ type ReferenceStorer interface { SetReference(*plumbing.Reference) error Reference(plumbing.ReferenceName) (*plumbing.Reference, error) IterReferences() (ReferenceIter, error) + RemoveReference(plumbing.ReferenceName) error } // ReferenceIter is a generic closable interface for iterating over references diff --git a/plumbing/transport/file/common_test.go b/plumbing/transport/file/common_test.go index 3dc4500..4f3ae8f 100644 --- a/plumbing/transport/file/common_test.go +++ b/plumbing/transport/file/common_test.go @@ -1,6 +1,7 @@ package file import ( + "io/ioutil" "os" "os/exec" "path/filepath" @@ -8,13 +9,13 @@ import ( "github.com/src-d/go-git-fixtures" . "gopkg.in/check.v1" - "io/ioutil" ) type CommonSuite struct { fixtures.Suite ReceivePackBin string UploadPackBin string + tmpDir string // to be removed at teardown } var _ = Suite(&CommonSuite{}) @@ -26,14 +27,20 @@ func (s *CommonSuite) SetUpSuite(c *C) { c.Skip("git command not found") } - binDir, err := ioutil.TempDir(os.TempDir(), "") + var err error + s.tmpDir, err = ioutil.TempDir("", "") c.Assert(err, IsNil) - s.ReceivePackBin = filepath.Join(binDir, "git-receive-pack") - s.UploadPackBin = filepath.Join(binDir, "git-upload-pack") - bin := filepath.Join(binDir, "go-git") + s.ReceivePackBin = filepath.Join(s.tmpDir, "git-receive-pack") + s.UploadPackBin = filepath.Join(s.tmpDir, "git-upload-pack") + bin := filepath.Join(s.tmpDir, "go-git") cmd := exec.Command("go", "build", "-o", bin, "../../../cli/go-git/...") c.Assert(cmd.Run(), IsNil) c.Assert(os.Symlink(bin, s.ReceivePackBin), IsNil) c.Assert(os.Symlink(bin, s.UploadPackBin), IsNil) } + +func (s *CommonSuite) TearDownSuite(c *C) { + defer s.Suite.TearDownSuite(c) + c.Assert(os.RemoveAll(s.tmpDir), IsNil) +} diff --git a/plumbing/transport/file/server_test.go b/plumbing/transport/file/server_test.go index 775b031..a7b4e34 100644 --- a/plumbing/transport/file/server_test.go +++ b/plumbing/transport/file/server_test.go @@ -2,8 +2,6 @@ package file import ( "fmt" - "io" - "io/ioutil" "os" "os/exec" @@ -48,8 +46,8 @@ func (s *ServerSuite) TestPush(c *C) { cmd.Dir = s.SrcPath cmd.Env = os.Environ() cmd.Env = append(cmd.Env, "GIT_TRACE=true", "GIT_TRACE_PACKET=true") - stdout, stderr, err := execAndGetOutput(c, cmd) - c.Assert(err, IsNil, Commentf("STDOUT:\n%s\nSTDERR:\n%s\n", stdout, stderr)) + out, err := cmd.CombinedOutput() + c.Assert(err, IsNil, Commentf("combined stdout and stderr:\n%s\n", out)) } func (s *ServerSuite) TestClone(c *C) { @@ -61,45 +59,6 @@ func (s *ServerSuite) TestClone(c *C) { ) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, "GIT_TRACE=true", "GIT_TRACE_PACKET=true") - stdout, stderr, err := execAndGetOutput(c, cmd) - c.Assert(err, IsNil, Commentf("STDOUT:\n%s\nSTDERR:\n%s\n", stdout, stderr)) -} - -func execAndGetOutput(c *C, cmd *exec.Cmd) (stdout, stderr string, err error) { - sout, err := cmd.StdoutPipe() - c.Assert(err, IsNil) - serr, err := cmd.StderrPipe() - c.Assert(err, IsNil) - - outChan, outErr := readAllAsync(sout) - errChan, errErr := readAllAsync(serr) - - c.Assert(cmd.Start(), IsNil) - - if err = cmd.Wait(); err != nil { - return <-outChan, <-errChan, err - } - - if err := <-outErr; err != nil { - return <-outChan, <-errChan, err - } - - return <-outChan, <-errChan, <-errErr -} - -func readAllAsync(r io.Reader) (out chan string, err chan error) { - out = make(chan string, 1) - err = make(chan error, 1) - go func() { - b, e := ioutil.ReadAll(r) - if e != nil { - err <- e - } else { - err <- nil - } - - out <- string(b) - }() - - return out, err + out, err := cmd.CombinedOutput() + c.Assert(err, IsNil, Commentf("combined stdout and stderr:\n%s\n", out)) } |