| 1 |
package merkletrie |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
ErrEmptyFileName = errors.New("empty filename in tree entry") |
| 13 |
) |
| 14 |
|
| 15 |
// Action values represent the kind of things a Change can represent: |
| 16 |
// insertion, deletions or modifications of files. |
| 17 |
type Action int |
| 18 |
|
| 19 |
// The set of possible actions in a change. |
| 20 |
const ( |
| 21 |
_ Action = iota |
| 22 |
Insert |
| 23 |
Delete |
| 24 |
Modify |
| 25 |
) |
| 26 |
|
| 27 |
// String returns the action as a human readable text. |
| 28 |
func (a Action) String() string { |
| 29 |
switch a { |
| 30 |
case Insert: |
| 31 |
return "Insert" |
| 32 |
case Delete: |
| 33 |
return "Delete" |
| 34 |
case Modify: |
| 35 |
return "Modify" |
| 36 |
default: |
| 37 |
panic(fmt.Sprintf("unsupported action: %d", a)) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// A Change value represent how a noder has change between to merkletries. |
| 42 |
type Change struct { |
| 43 |
// The noder before the change or nil if it was inserted. |
| 44 |
From noder.Path |
| 45 |
// The noder after the change or nil if it was deleted. |
| 46 |
To noder.Path |
| 47 |
} |
| 48 |
|
| 49 |
// Action is convenience method that returns what Action c represents. |
| 50 |
func (c *Change) Action() (Action, error) { |
| 51 |
if c.From == nil && c.To == nil { |
| 52 |
return Action(0), fmt.Errorf("malformed change: nil from and to") |
| 53 |
} |
| 54 |
if c.From == nil { |
| 55 |
return Insert, nil |
| 56 |
} |
| 57 |
if c.To == nil { |
| 58 |
return Delete, nil |
| 59 |
} |
| 60 |
|
| 61 |
return Modify, nil |
| 62 |
} |
| 63 |
|
| 64 |
// NewInsert returns a new Change representing the insertion of n. |
| 65 |
func NewInsert(n noder.Path) Change { return Change{To: n} } |
| 66 |
|
| 67 |
// NewDelete returns a new Change representing the deletion of n. |
| 68 |
func NewDelete(n noder.Path) Change { return Change{From: n} } |
| 69 |
|
| 70 |
// NewModify returns a new Change representing that a has been modified and |
| 71 |
// it is now b. |
| 72 |
func NewModify(a, b noder.Path) Change { |
| 73 |
return Change{ |
| 74 |
From: a, |
| 75 |
To: b, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// String returns a single change in human readable form, using the |
| 80 |
// format: '<' + action + space + path + '>'. The contents of the file |
| 81 |
// before or after the change are not included in this format. |
| 82 |
// |
| 83 |
// Example: inserting a file at the path a/b/c.txt will return "<Insert |
| 84 |
// a/b/c.txt>". |
| 85 |
func (c Change) String() string { |
| 86 |
action, err := c.Action() |
| 87 |
if err != nil { |
| 88 |
panic(err) |
| 89 |
} |
| 90 |
|
| 91 |
var path string |
| 92 |
if action == Delete { |
| 93 |
path = c.From.String() |
| 94 |
} else { |
| 95 |
path = c.To.String() |
| 96 |
} |
| 97 |
|
| 98 |
return fmt.Sprintf("<%s %s>", action, path) |
| 99 |
} |
| 100 |
|
| 101 |
// Changes is a list of changes between to merkletries. |
| 102 |
type Changes []Change |
| 103 |
|
| 104 |
// NewChanges returns an empty list of changes. |
| 105 |
func NewChanges() Changes { |
| 106 |
return Changes{} |
| 107 |
} |
| 108 |
|
| 109 |
// Add adds the change c to the list of changes. |
| 110 |
func (l *Changes) Add(c Change) { |
| 111 |
*l = append(*l, c) |
| 112 |
} |
| 113 |
|
| 114 |
// AddRecursiveInsert adds the required changes to insert all the |
| 115 |
// file-like noders found in root, recursively. |
| 116 |
func (l *Changes) AddRecursiveInsert(root noder.Path) error { |
| 117 |
return l.addRecursive(root, NewInsert) |
| 118 |
} |
| 119 |
|
| 120 |
// AddRecursiveDelete adds the required changes to delete all the |
| 121 |
// file-like noders found in root, recursively. |
| 122 |
func (l *Changes) AddRecursiveDelete(root noder.Path) error { |
| 123 |
return l.addRecursive(root, NewDelete) |
| 124 |
} |
| 125 |
|
| 126 |
type noderToChangeFn func(noder.Path) Change // NewInsert or NewDelete |
| 127 |
|
| 128 |
func (l *Changes) addRecursive(root noder.Path, ctor noderToChangeFn) error { |
| 129 |
if root.String() == "" { |
| 130 |
return ErrEmptyFileName |
| 131 |
} |
| 132 |
|
| 133 |
if !root.IsDir() { |
| 134 |
if !root.Skip() { |
| 135 |
l.Add(ctor(root)) |
| 136 |
} |
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
i, err := NewIterFromPath(root) |
| 141 |
if err != nil { |
| 142 |
return err |
| 143 |
} |
| 144 |
|
| 145 |
var current noder.Path |
| 146 |
for { |
| 147 |
if current, err = i.Step(); err != nil { |
| 148 |
if err == io.EOF { |
| 149 |
break |
| 150 |
} |
| 151 |
return err |
| 152 |
} |
| 153 |
if current.IsDir() || current.Skip() { |
| 154 |
continue |
| 155 |
} |
| 156 |
l.Add(ctor(current)) |
| 157 |
} |
| 158 |
|
| 159 |
return nil |
| 160 |
} |