本站源代码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

56 lignes
1.3KB

  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "encoding/hex"
  8. "fmt"
  9. "strings"
  10. "gopkg.in/src-d/go-git.v4/plumbing"
  11. )
  12. // EmptySHA defines empty git SHA
  13. const EmptySHA = "0000000000000000000000000000000000000000"
  14. // SHA1 a git commit name
  15. type SHA1 = plumbing.Hash
  16. // MustID always creates a new SHA1 from a [20]byte array with no validation of input.
  17. func MustID(b []byte) SHA1 {
  18. var id SHA1
  19. copy(id[:], b)
  20. return id
  21. }
  22. // NewID creates a new SHA1 from a [20]byte array.
  23. func NewID(b []byte) (SHA1, error) {
  24. if len(b) != 20 {
  25. return SHA1{}, fmt.Errorf("Length must be 20: %v", b)
  26. }
  27. return MustID(b), nil
  28. }
  29. // MustIDFromString always creates a new sha from a ID with no validation of input.
  30. func MustIDFromString(s string) SHA1 {
  31. b, _ := hex.DecodeString(s)
  32. return MustID(b)
  33. }
  34. // NewIDFromString creates a new SHA1 from a ID string of length 40.
  35. func NewIDFromString(s string) (SHA1, error) {
  36. var id SHA1
  37. s = strings.TrimSpace(s)
  38. if len(s) != 40 {
  39. return id, fmt.Errorf("Length must be 40: %s", s)
  40. }
  41. b, err := hex.DecodeString(s)
  42. if err != nil {
  43. return id, err
  44. }
  45. return NewID(b)
  46. }
上海开阖软件有限公司 沪ICP备12045867号-1