| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 9 |
) |
| 10 |
|
| 11 |
// UploadRequest values represent the information transmitted on a |
| 12 |
// upload-request message. Values from this type are not zero-value |
| 13 |
// safe, use the New function instead. |
| 14 |
// This is a low level type, use UploadPackRequest instead. |
| 15 |
type UploadRequest struct { |
| 16 |
Capabilities *capability.List |
| 17 |
Wants []plumbing.Hash |
| 18 |
Shallows []plumbing.Hash |
| 19 |
Depth Depth |
| 20 |
Filter Filter |
| 21 |
} |
| 22 |
|
| 23 |
// Depth values stores the desired depth of the requested packfile: see |
| 24 |
// DepthCommit, DepthSince and DepthReference. |
| 25 |
type Depth interface { |
| 26 |
isDepth() |
| 27 |
IsZero() bool |
| 28 |
} |
| 29 |
|
| 30 |
// DepthCommits values stores the maximum number of requested commits in |
| 31 |
// the packfile. Zero means infinite. A negative value will have |
| 32 |
// undefined consequences. |
| 33 |
type DepthCommits int |
| 34 |
|
| 35 |
func (d DepthCommits) isDepth() {} |
| 36 |
|
| 37 |
func (d DepthCommits) IsZero() bool { |
| 38 |
return d == 0 |
| 39 |
} |
| 40 |
|
| 41 |
// DepthSince values requests only commits newer than the specified time. |
| 42 |
type DepthSince time.Time |
| 43 |
|
| 44 |
func (d DepthSince) isDepth() {} |
| 45 |
|
| 46 |
func (d DepthSince) IsZero() bool { |
| 47 |
return time.Time(d).IsZero() |
| 48 |
} |
| 49 |
|
| 50 |
// DepthReference requests only commits not to found in the specified reference. |
| 51 |
type DepthReference string |
| 52 |
|
| 53 |
func (d DepthReference) isDepth() {} |
| 54 |
|
| 55 |
func (d DepthReference) IsZero() bool { |
| 56 |
return string(d) == "" |
| 57 |
} |
| 58 |
|
| 59 |
// NewUploadRequest returns a pointer to a new UploadRequest value, ready to be |
| 60 |
// used. It has no capabilities, wants or shallows and an infinite depth. Please |
| 61 |
// note that to encode an upload-request it has to have at least one wanted hash. |
| 62 |
func NewUploadRequest() *UploadRequest { |
| 63 |
return &UploadRequest{ |
| 64 |
Capabilities: capability.NewList(), |
| 65 |
Wants: []plumbing.Hash{}, |
| 66 |
Shallows: []plumbing.Hash{}, |
| 67 |
Depth: DepthCommits(0), |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// NewUploadRequestFromCapabilities returns a pointer to a new UploadRequest |
| 72 |
// value, the request capabilities are filled with the most optimal ones, based |
| 73 |
// on the adv value (advertised capabilities), the UploadRequest generated it |
| 74 |
// has no wants or shallows and an infinite depth. |
| 75 |
func NewUploadRequestFromCapabilities(adv *capability.List) *UploadRequest { |
| 76 |
r := NewUploadRequest() |
| 77 |
|
| 78 |
if adv.Supports(capability.MultiACKDetailed) { |
| 79 |
r.Capabilities.Set(capability.MultiACKDetailed) |
| 80 |
} else if adv.Supports(capability.MultiACK) { |
| 81 |
r.Capabilities.Set(capability.MultiACK) |
| 82 |
} |
| 83 |
|
| 84 |
if adv.Supports(capability.Sideband64k) { |
| 85 |
r.Capabilities.Set(capability.Sideband64k) |
| 86 |
} else if adv.Supports(capability.Sideband) { |
| 87 |
r.Capabilities.Set(capability.Sideband) |
| 88 |
} |
| 89 |
|
| 90 |
if adv.Supports(capability.ThinPack) { |
| 91 |
r.Capabilities.Set(capability.ThinPack) |
| 92 |
} |
| 93 |
|
| 94 |
if adv.Supports(capability.OFSDelta) { |
| 95 |
r.Capabilities.Set(capability.OFSDelta) |
| 96 |
} |
| 97 |
|
| 98 |
if adv.Supports(capability.Agent) { |
| 99 |
r.Capabilities.Set(capability.Agent, capability.DefaultAgent()) |
| 100 |
} |
| 101 |
|
| 102 |
return r |
| 103 |
} |
| 104 |
|
| 105 |
// Validate validates the content of UploadRequest, following the next rules: |
| 106 |
// - Wants MUST have at least one reference |
| 107 |
// - capability.Shallow MUST be present if Shallows is not empty |
| 108 |
// - is a non-zero DepthCommits is given capability.Shallow MUST be present |
| 109 |
// - is a DepthSince is given capability.Shallow MUST be present |
| 110 |
// - is a DepthReference is given capability.DeepenNot MUST be present |
| 111 |
// - MUST contain only maximum of one of capability.Sideband and capability.Sideband64k |
| 112 |
// - MUST contain only maximum of one of capability.MultiACK and capability.MultiACKDetailed |
| 113 |
func (req *UploadRequest) Validate() error { |
| 114 |
if len(req.Wants) == 0 { |
| 115 |
return fmt.Errorf("want can't be empty") |
| 116 |
} |
| 117 |
|
| 118 |
if err := req.validateRequiredCapabilities(); err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
if err := req.validateConflictCapabilities(); err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
|
| 126 |
return nil |
| 127 |
} |
| 128 |
|
| 129 |
func (req *UploadRequest) validateRequiredCapabilities() error { |
| 130 |
msg := "missing capability %s" |
| 131 |
|
| 132 |
if len(req.Shallows) != 0 && !req.Capabilities.Supports(capability.Shallow) { |
| 133 |
return fmt.Errorf(msg, capability.Shallow) |
| 134 |
} |
| 135 |
|
| 136 |
switch req.Depth.(type) { |
| 137 |
case DepthCommits: |
| 138 |
if req.Depth != DepthCommits(0) { |
| 139 |
if !req.Capabilities.Supports(capability.Shallow) { |
| 140 |
return fmt.Errorf(msg, capability.Shallow) |
| 141 |
} |
| 142 |
} |
| 143 |
case DepthSince: |
| 144 |
if !req.Capabilities.Supports(capability.DeepenSince) { |
| 145 |
return fmt.Errorf(msg, capability.DeepenSince) |
| 146 |
} |
| 147 |
case DepthReference: |
| 148 |
if !req.Capabilities.Supports(capability.DeepenNot) { |
| 149 |
return fmt.Errorf(msg, capability.DeepenNot) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func (req *UploadRequest) validateConflictCapabilities() error { |
| 157 |
msg := "capabilities %s and %s are mutually exclusive" |
| 158 |
if req.Capabilities.Supports(capability.Sideband) && |
| 159 |
req.Capabilities.Supports(capability.Sideband64k) { |
| 160 |
return fmt.Errorf(msg, capability.Sideband, capability.Sideband64k) |
| 161 |
} |
| 162 |
|
| 163 |
if req.Capabilities.Supports(capability.MultiACK) && |
| 164 |
req.Capabilities.Supports(capability.MultiACKDetailed) { |
| 165 |
return fmt.Errorf(msg, capability.MultiACK, capability.MultiACKDetailed) |
| 166 |
} |
| 167 |
|
| 168 |
return nil |
| 169 |
} |