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

123 lines
2.7KB

  1. package packp
  2. import (
  3. "errors"
  4. "io"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
  7. "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
  8. )
  9. var (
  10. ErrEmptyCommands = errors.New("commands cannot be empty")
  11. ErrMalformedCommand = errors.New("malformed command")
  12. )
  13. // ReferenceUpdateRequest values represent reference upload requests.
  14. // Values from this type are not zero-value safe, use the New function instead.
  15. type ReferenceUpdateRequest struct {
  16. Capabilities *capability.List
  17. Commands []*Command
  18. Shallow *plumbing.Hash
  19. // Packfile contains an optional packfile reader.
  20. Packfile io.ReadCloser
  21. // Progress receives sideband progress messages from the server
  22. Progress sideband.Progress
  23. }
  24. // New returns a pointer to a new ReferenceUpdateRequest value.
  25. func NewReferenceUpdateRequest() *ReferenceUpdateRequest {
  26. return &ReferenceUpdateRequest{
  27. // TODO: Add support for push-cert
  28. Capabilities: capability.NewList(),
  29. Commands: nil,
  30. }
  31. }
  32. // NewReferenceUpdateRequestFromCapabilities returns a pointer to a new
  33. // ReferenceUpdateRequest value, the request capabilities are filled with the
  34. // most optimal ones, based on the adv value (advertised capabilities), the
  35. // ReferenceUpdateRequest contains no commands
  36. //
  37. // It does set the following capabilities:
  38. // - agent
  39. // - report-status
  40. // - ofs-delta
  41. // - ref-delta
  42. // - delete-refs
  43. // It leaves up to the user to add the following capabilities later:
  44. // - atomic
  45. // - ofs-delta
  46. // - side-band
  47. // - side-band-64k
  48. // - quiet
  49. // - push-cert
  50. func NewReferenceUpdateRequestFromCapabilities(adv *capability.List) *ReferenceUpdateRequest {
  51. r := NewReferenceUpdateRequest()
  52. if adv.Supports(capability.Agent) {
  53. r.Capabilities.Set(capability.Agent, capability.DefaultAgent)
  54. }
  55. if adv.Supports(capability.ReportStatus) {
  56. r.Capabilities.Set(capability.ReportStatus)
  57. }
  58. return r
  59. }
  60. func (r *ReferenceUpdateRequest) validate() error {
  61. if len(r.Commands) == 0 {
  62. return ErrEmptyCommands
  63. }
  64. for _, c := range r.Commands {
  65. if err := c.validate(); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. type Action string
  72. const (
  73. Create Action = "create"
  74. Update = "update"
  75. Delete = "delete"
  76. Invalid = "invalid"
  77. )
  78. type Command struct {
  79. Name plumbing.ReferenceName
  80. Old plumbing.Hash
  81. New plumbing.Hash
  82. }
  83. func (c *Command) Action() Action {
  84. if c.Old == plumbing.ZeroHash && c.New == plumbing.ZeroHash {
  85. return Invalid
  86. }
  87. if c.Old == plumbing.ZeroHash {
  88. return Create
  89. }
  90. if c.New == plumbing.ZeroHash {
  91. return Delete
  92. }
  93. return Update
  94. }
  95. func (c *Command) validate() error {
  96. if c.Action() == Invalid {
  97. return ErrMalformedCommand
  98. }
  99. return nil
  100. }
上海开阖软件有限公司 沪ICP备12045867号-1