aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/difftree/difftree.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2017-02-21 10:24:10 +0100
committerGitHub <noreply@github.com>2017-02-21 10:24:10 +0100
commit0b8b8da617d5a077f282e57d0300dc106a604236 (patch)
treef481480a0311764e2a306e079b48b5f9d33bc304 /plumbing/difftree/difftree.go
parentefe9ecf9a2b4d9346a1ae105210b4dabb26aa2a8 (diff)
downloadgo-git-0b8b8da617d5a077f282e57d0300dc106a604236.tar.gz
difftree for git.Trees (#273)
Last PR to fix #82: This PR modifies the difftree package itself. The old version extracted the files in both trees and compare them by hand. The new version turn the trees into merkletrie.Noders and call the merkletrie.Difftree function on them. How to review this PR: treenoder.go: defines the treeNoder type that wraps a git.Tree and implements merkletrie.Noder. change.go: defines the type of the output of a difftree operation. The type is the same as before, but I have moved it into its own file to keep the package organized. The old package defines the Action type too (insert, delete, modify), now, we reuse merkletrie.Action and it is no longer a field, but a method. change_adaptor.go: defines functions to turn merkletrie.Changes into difftree.Changes. difftree.go: before this patch this file holds all the logic to do a difftree, now it just turns the git.Trees into treeNoders, call merkletrie.difftree on them, and turns the resulting merkletrie.Changes into difftree.Changes. The only interesting piece of code here is that noders don't have the concept of mode (file permissions). The treenoder type codifies git.Tree modes into the merkletrie.Noder hash, so changes in the mode of a file are detected as modifications, just as the original git diff-tree command does.
Diffstat (limited to 'plumbing/difftree/difftree.go')
-rw-r--r--plumbing/difftree/difftree.go246
1 files changed, 9 insertions, 237 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)
}