aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/change_adaptor.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-02-24 11:26:08 +0100
committerGitHub <noreply@github.com>2017-02-24 11:26:08 +0100
commit39f43b52a2bdfbc73703e2d09b575d49cd70ede8 (patch)
tree81d9845f9ba1c16975c24084e9023492e9e7ee33 /plumbing/object/change_adaptor.go
parentf93e5712a86b96370673a02fa7ffbe5e0aec0c66 (diff)
parentcb51ef358e081b5214e3884bd618d1d3fedf8eb1 (diff)
downloadgo-git-39f43b52a2bdfbc73703e2d09b575d49cd70ede8.tar.gz
Merge pull request #284 from ajnavarro/improvement/move-difftree-to-object
plumbing/object: move difftree to object package
Diffstat (limited to 'plumbing/object/change_adaptor.go')
-rw-r--r--plumbing/object/change_adaptor.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/plumbing/object/change_adaptor.go b/plumbing/object/change_adaptor.go
new file mode 100644
index 0000000..e97fd93
--- /dev/null
+++ b/plumbing/object/change_adaptor.go
@@ -0,0 +1,60 @@
+package object
+
+import (
+ "fmt"
+
+ "srcd.works/go-git.v4/utils/merkletrie"
+ "srcd.works/go-git.v4/utils/merkletrie/noder"
+)
+
+// The folowing functions transform changes types form the merkletrie
+// package to changes types from this package.
+
+func newChange(c merkletrie.Change) (*Change, error) {
+ ret := &Change{}
+
+ var err error
+ if ret.From, err = newChangeEntry(c.From); err != nil {
+ return nil, fmt.Errorf("From field: ", err)
+ }
+
+ if ret.To, err = newChangeEntry(c.To); err != nil {
+ return nil, fmt.Errorf("To field: ", err)
+ }
+
+ return ret, nil
+}
+
+func newChangeEntry(p noder.Path) (ChangeEntry, error) {
+ if p == nil {
+ return empty, nil
+ }
+
+ asTreeNoder, ok := p.Last().(*treeNoder)
+ if !ok {
+ return ChangeEntry{}, fmt.Errorf("cannot transform non-TreeNoders")
+ }
+
+ return ChangeEntry{
+ Name: p.String(),
+ Tree: asTreeNoder.parent,
+ TreeEntry: TreeEntry{
+ Name: asTreeNoder.name,
+ Mode: asTreeNoder.mode,
+ Hash: asTreeNoder.hash,
+ },
+ }, nil
+}
+
+func newChanges(src merkletrie.Changes) (Changes, error) {
+ ret := make(Changes, len(src))
+ var err error
+ for i, e := range src {
+ ret[i], err = newChange(e)
+ if err != nil {
+ return nil, fmt.Errorf("change #%d: %s", err)
+ }
+ }
+
+ return ret, nil
+}