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