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.

object_id.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "bytes"
  6. "encoding/hex"
  7. "fmt"
  8. )
  9. type ObjectID interface {
  10. String() string
  11. IsZero() bool
  12. RawValue() []byte
  13. Type() ObjectFormat
  14. }
  15. /* SHA1 */
  16. type Sha1Hash [20]byte
  17. func (h *Sha1Hash) String() string {
  18. return hex.EncodeToString(h[:])
  19. }
  20. func (h *Sha1Hash) IsZero() bool {
  21. empty := Sha1Hash{}
  22. return bytes.Equal(empty[:], h[:])
  23. }
  24. func (h *Sha1Hash) RawValue() []byte { return h[:] }
  25. func (*Sha1Hash) Type() ObjectFormat { return Sha1ObjectFormat }
  26. var _ ObjectID = &Sha1Hash{}
  27. func MustIDFromString(hexHash string) ObjectID {
  28. id, err := NewIDFromString(hexHash)
  29. if err != nil {
  30. panic(err)
  31. }
  32. return id
  33. }
  34. /* SHA256 */
  35. type Sha256Hash [32]byte
  36. func (h *Sha256Hash) String() string {
  37. return hex.EncodeToString(h[:])
  38. }
  39. func (h *Sha256Hash) IsZero() bool {
  40. empty := Sha256Hash{}
  41. return bytes.Equal(empty[:], h[:])
  42. }
  43. func (h *Sha256Hash) RawValue() []byte { return h[:] }
  44. func (*Sha256Hash) Type() ObjectFormat { return Sha256ObjectFormat }
  45. /* utility */
  46. func NewIDFromString(hexHash string) (ObjectID, error) {
  47. var theObjectFormat ObjectFormat
  48. for _, objectFormat := range SupportedObjectFormats {
  49. if len(hexHash) == objectFormat.FullLength() {
  50. theObjectFormat = objectFormat
  51. break
  52. }
  53. }
  54. if theObjectFormat == nil {
  55. return nil, fmt.Errorf("length %d has no matched object format: %s", len(hexHash), hexHash)
  56. }
  57. b, err := hex.DecodeString(hexHash)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if len(b) != theObjectFormat.FullLength()/2 {
  62. return theObjectFormat.EmptyObjectID(), fmt.Errorf("length must be %d: %v", theObjectFormat.FullLength(), b)
  63. }
  64. return theObjectFormat.MustID(b), nil
  65. }
  66. func IsEmptyCommitID(commitID string) bool {
  67. if commitID == "" {
  68. return true
  69. }
  70. id, err := NewIDFromString(commitID)
  71. if err != nil {
  72. return false
  73. }
  74. return id.IsZero()
  75. }
  76. // ComputeBlobHash compute the hash for a given blob content
  77. func ComputeBlobHash(hashType ObjectFormat, content []byte) ObjectID {
  78. return hashType.ComputeHash(ObjectBlob, content)
  79. }
  80. type ErrInvalidSHA struct {
  81. SHA string
  82. }
  83. func (err ErrInvalidSHA) Error() string {
  84. return fmt.Sprintf("invalid sha: %s", err.SHA)
  85. }