THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

77 rindas
1.7KB

  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "fmt"
  9. "strings"
  10. )
  11. // EmptySHA defines empty git SHA
  12. const EmptySHA = "0000000000000000000000000000000000000000"
  13. // SHA1 a git commit name
  14. type SHA1 [20]byte
  15. // Equal returns true if s has the same SHA1 as caller.
  16. // Support 40-length-string, []byte, SHA1.
  17. func (id SHA1) Equal(s2 interface{}) bool {
  18. switch v := s2.(type) {
  19. case string:
  20. if len(v) != 40 {
  21. return false
  22. }
  23. return v == id.String()
  24. case []byte:
  25. return bytes.Equal(v, id[:])
  26. case SHA1:
  27. return v == id
  28. default:
  29. return false
  30. }
  31. }
  32. // String returns string (hex) representation of the Oid.
  33. func (id SHA1) String() string {
  34. return hex.EncodeToString(id[:])
  35. }
  36. // MustID always creates a new SHA1 from a [20]byte array with no validation of input.
  37. func MustID(b []byte) SHA1 {
  38. var id SHA1
  39. copy(id[:], b)
  40. return id
  41. }
  42. // NewID creates a new SHA1 from a [20]byte array.
  43. func NewID(b []byte) (SHA1, error) {
  44. if len(b) != 20 {
  45. return SHA1{}, fmt.Errorf("Length must be 20: %v", b)
  46. }
  47. return MustID(b), nil
  48. }
  49. // MustIDFromString always creates a new sha from a ID with no validation of input.
  50. func MustIDFromString(s string) SHA1 {
  51. b, _ := hex.DecodeString(s)
  52. return MustID(b)
  53. }
  54. // NewIDFromString creates a new SHA1 from a ID string of length 40.
  55. func NewIDFromString(s string) (SHA1, error) {
  56. var id SHA1
  57. s = strings.TrimSpace(s)
  58. if len(s) != 40 {
  59. return id, fmt.Errorf("Length must be 40: %s", s)
  60. }
  61. b, err := hex.DecodeString(s)
  62. if err != nil {
  63. return id, err
  64. }
  65. return NewID(b)
  66. }