| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 11 |
) |
| 12 |
|
| 13 |
// UploadPackRequest represents a upload-pack request. |
| 14 |
// Zero-value is not safe, use NewUploadPackRequest instead. |
| 15 |
type UploadPackRequest struct { |
| 16 |
UploadRequest |
| 17 |
UploadHaves |
| 18 |
} |
| 19 |
|
| 20 |
// NewUploadPackRequest creates a new UploadPackRequest and returns a pointer. |
| 21 |
func NewUploadPackRequest() *UploadPackRequest { |
| 22 |
ur := NewUploadRequest() |
| 23 |
return &UploadPackRequest{ |
| 24 |
UploadHaves: UploadHaves{}, |
| 25 |
UploadRequest: *ur, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// NewUploadPackRequestFromCapabilities creates a new UploadPackRequest and |
| 30 |
// returns a pointer. The request capabilities are filled with the most optimal |
| 31 |
// ones, based on the adv value (advertised capabilities), the UploadPackRequest |
| 32 |
// it has no wants, haves or shallows and an infinite depth |
| 33 |
func NewUploadPackRequestFromCapabilities(adv *capability.List) *UploadPackRequest { |
| 34 |
ur := NewUploadRequestFromCapabilities(adv) |
| 35 |
return &UploadPackRequest{ |
| 36 |
UploadHaves: UploadHaves{}, |
| 37 |
UploadRequest: *ur, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// IsEmpty returns whether a request is empty - it is empty if Haves are contained |
| 42 |
// in the Wants, or if Wants length is zero, and we don't have any shallows |
| 43 |
func (r *UploadPackRequest) IsEmpty() bool { |
| 44 |
return isSubset(r.Wants, r.Haves) && len(r.Shallows) == 0 |
| 45 |
} |
| 46 |
|
| 47 |
func isSubset(needle []plumbing.Hash, haystack []plumbing.Hash) bool { |
| 48 |
for _, h := range needle { |
| 49 |
found := false |
| 50 |
for _, oh := range haystack { |
| 51 |
if h == oh { |
| 52 |
found = true |
| 53 |
break |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if !found { |
| 58 |
return false |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return true |
| 63 |
} |
| 64 |
|
| 65 |
// UploadHaves is a message to signal the references that a client has in a |
| 66 |
// upload-pack. Do not use this directly. Use UploadPackRequest request instead. |
| 67 |
type UploadHaves struct { |
| 68 |
Haves []plumbing.Hash |
| 69 |
} |
| 70 |
|
| 71 |
// Encode encodes the UploadHaves into the Writer. If flush is true, a flush |
| 72 |
// command will be encoded at the end of the writer content. |
| 73 |
func (u *UploadHaves) Encode(w io.Writer, flush bool) error { |
| 74 |
e := pktline.NewEncoder(w) |
| 75 |
|
| 76 |
plumbing.HashesSort(u.Haves) |
| 77 |
|
| 78 |
var last plumbing.Hash |
| 79 |
for _, have := range u.Haves { |
| 80 |
if bytes.Equal(last[:], have[:]) { |
| 81 |
continue |
| 82 |
} |
| 83 |
|
| 84 |
if err := e.Encodef("have %s\n", have); err != nil { |
| 85 |
return fmt.Errorf("sending haves for %q: %s", have, err) |
| 86 |
} |
| 87 |
|
| 88 |
last = have |
| 89 |
} |
| 90 |
|
| 91 |
if flush && len(u.Haves) != 0 { |
| 92 |
if err := e.Flush(); err != nil { |
| 93 |
return fmt.Errorf("sending flush-pkt after haves: %s", err) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |