本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.0KB

  1. package index
  2. import (
  3. "path"
  4. "strings"
  5. "gopkg.in/src-d/go-git.v4/plumbing/format/index"
  6. "gopkg.in/src-d/go-git.v4/utils/merkletrie/noder"
  7. )
  8. // The node represents a index.Entry or a directory inferred from the path
  9. // of all entries. It implements the interface noder.Noder of merkletrie
  10. // package.
  11. //
  12. // This implementation implements a "standard" hash method being able to be
  13. // compared with any other noder.Noder implementation inside of go-git
  14. type node struct {
  15. path string
  16. entry *index.Entry
  17. children []noder.Noder
  18. isDir bool
  19. }
  20. // NewRootNode returns the root node of a computed tree from a index.Index,
  21. func NewRootNode(idx *index.Index) noder.Noder {
  22. const rootNode = ""
  23. m := map[string]*node{rootNode: {isDir: true}}
  24. for _, e := range idx.Entries {
  25. parts := strings.Split(e.Name, string("/"))
  26. var fullpath string
  27. for _, part := range parts {
  28. parent := fullpath
  29. fullpath = path.Join(fullpath, part)
  30. if _, ok := m[fullpath]; ok {
  31. continue
  32. }
  33. n := &node{path: fullpath}
  34. if fullpath == e.Name {
  35. n.entry = e
  36. } else {
  37. n.isDir = true
  38. }
  39. m[n.path] = n
  40. m[parent].children = append(m[parent].children, n)
  41. }
  42. }
  43. return m[rootNode]
  44. }
  45. func (n *node) String() string {
  46. return n.path
  47. }
  48. // Hash the hash of a filesystem is a 24-byte slice, is the result of
  49. // concatenating the computed plumbing.Hash of the file as a Blob and its
  50. // plumbing.FileMode; that way the difftree algorithm will detect changes in the
  51. // contents of files and also in their mode.
  52. //
  53. // If the node is computed and not based on a index.Entry the hash is equals
  54. // to a 24-bytes slices of zero values.
  55. func (n *node) Hash() []byte {
  56. if n.entry == nil {
  57. return make([]byte, 24)
  58. }
  59. return append(n.entry.Hash[:], n.entry.Mode.Bytes()...)
  60. }
  61. func (n *node) Name() string {
  62. return path.Base(n.path)
  63. }
  64. func (n *node) IsDir() bool {
  65. return n.isDir
  66. }
  67. func (n *node) Children() ([]noder.Noder, error) {
  68. return n.children, nil
  69. }
  70. func (n *node) NumChildren() (int, error) {
  71. return len(n.children), nil
  72. }
上海开阖软件有限公司 沪ICP备12045867号-1