aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/change_adaptor.go
blob: f701188288f486bac01da79b5918515845932aff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package object

import (
	"errors"
	"fmt"

	"github.com/go-git/go-git/v5/utils/merkletrie"
	"github.com/go-git/go-git/v5/utils/merkletrie/noder"
)

// The following 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: %s", err)
	}

	if ret.To, err = newChangeEntry(c.To); err != nil {
		return nil, fmt.Errorf("To field: %s", 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{}, errors.New("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", i, err)
		}
	}

	return ret, nil
}