本站源代码
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.

112 lines
2.5KB

  1. // package plumbing implement the core interfaces and structs used by go-git
  2. package plumbing
  3. import (
  4. "errors"
  5. "io"
  6. )
  7. var (
  8. ErrObjectNotFound = errors.New("object not found")
  9. // ErrInvalidType is returned when an invalid object type is provided.
  10. ErrInvalidType = errors.New("invalid object type")
  11. )
  12. // Object is a generic representation of any git object
  13. type EncodedObject interface {
  14. Hash() Hash
  15. Type() ObjectType
  16. SetType(ObjectType)
  17. Size() int64
  18. SetSize(int64)
  19. Reader() (io.ReadCloser, error)
  20. Writer() (io.WriteCloser, error)
  21. }
  22. // DeltaObject is an EncodedObject representing a delta.
  23. type DeltaObject interface {
  24. EncodedObject
  25. // BaseHash returns the hash of the object used as base for this delta.
  26. BaseHash() Hash
  27. // ActualHash returns the hash of the object after applying the delta.
  28. ActualHash() Hash
  29. // Size returns the size of the object after applying the delta.
  30. ActualSize() int64
  31. }
  32. // ObjectType internal object type
  33. // Integer values from 0 to 7 map to those exposed by git.
  34. // AnyObject is used to represent any from 0 to 7.
  35. type ObjectType int8
  36. const (
  37. InvalidObject ObjectType = 0
  38. CommitObject ObjectType = 1
  39. TreeObject ObjectType = 2
  40. BlobObject ObjectType = 3
  41. TagObject ObjectType = 4
  42. // 5 reserved for future expansion
  43. OFSDeltaObject ObjectType = 6
  44. REFDeltaObject ObjectType = 7
  45. AnyObject ObjectType = -127
  46. )
  47. func (t ObjectType) String() string {
  48. switch t {
  49. case CommitObject:
  50. return "commit"
  51. case TreeObject:
  52. return "tree"
  53. case BlobObject:
  54. return "blob"
  55. case TagObject:
  56. return "tag"
  57. case OFSDeltaObject:
  58. return "ofs-delta"
  59. case REFDeltaObject:
  60. return "ref-delta"
  61. case AnyObject:
  62. return "any"
  63. default:
  64. return "unknown"
  65. }
  66. }
  67. func (t ObjectType) Bytes() []byte {
  68. return []byte(t.String())
  69. }
  70. // Valid returns true if t is a valid ObjectType.
  71. func (t ObjectType) Valid() bool {
  72. return t >= CommitObject && t <= REFDeltaObject
  73. }
  74. // IsDelta returns true for any ObjectTyoe that represents a delta (i.e.
  75. // REFDeltaObject or OFSDeltaObject).
  76. func (t ObjectType) IsDelta() bool {
  77. return t == REFDeltaObject || t == OFSDeltaObject
  78. }
  79. // ParseObjectType parses a string representation of ObjectType. It returns an
  80. // error on parse failure.
  81. func ParseObjectType(value string) (typ ObjectType, err error) {
  82. switch value {
  83. case "commit":
  84. typ = CommitObject
  85. case "tree":
  86. typ = TreeObject
  87. case "blob":
  88. typ = BlobObject
  89. case "tag":
  90. typ = TagObject
  91. case "ofs-delta":
  92. typ = OFSDeltaObject
  93. case "ref-delta":
  94. typ = REFDeltaObject
  95. default:
  96. err = ErrInvalidType
  97. }
  98. return
  99. }
上海开阖软件有限公司 沪ICP备12045867号-1