archiver/tree: Introduce functions Leaf() and NodeNames()

This commit is contained in:
Alexander Neumann
2021-01-29 11:10:28 +01:00
parent 43cb26010a
commit 81211750ba
2 changed files with 21 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package archiver
import (
"fmt"
"sort"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
@@ -199,6 +200,24 @@ func (t Tree) String() string {
return formatTree(t, "")
}
// Leaf returns true if this is a leaf node, which means Path is set to a
// non-empty string and the contents of Path should be inserted at this point
// in the tree.
func (t Tree) Leaf() bool {
return t.Path != ""
}
// NodeNames returns the sorted list of subtree names.
func (t Tree) NodeNames() []string {
// iterate over the nodes of atree in lexicographic (=deterministic) order
names := make([]string, 0, len(t.Nodes))
for name := range t.Nodes {
names = append(names, name)
}
sort.Strings(names)
return names
}
// formatTree returns a text representation of the tree t.
func formatTree(t Tree, indent string) (s string) {
for name, node := range t.Nodes {