1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
package packp
import (
"fmt"
"os"
"strings"
"time"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
. "gopkg.in/check.v1"
)
type UlReqSuite struct{}
var _ = Suite(&UlReqSuite{})
func (s *UlReqSuite) TestNewUploadRequestFromCapabilities(c *C) {
cap := capability.NewList()
cap.Set(capability.Sideband)
cap.Set(capability.Sideband64k)
cap.Set(capability.MultiACK)
cap.Set(capability.MultiACKDetailed)
cap.Set(capability.ThinPack)
cap.Set(capability.OFSDelta)
cap.Set(capability.Agent, "foo")
r := NewUploadRequestFromCapabilities(cap)
c.Assert(r.Capabilities.String(), Equals,
"multi_ack_detailed side-band-64k thin-pack ofs-delta agent=go-git/4.x",
)
}
func (s *UlReqSuite) TestValidateWants(c *C) {
r := NewUploadRequest()
err := r.Validate()
c.Assert(err, NotNil)
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
err = r.Validate()
c.Assert(err, IsNil)
}
func (s *UlReqSuite) TestValidateShallows(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Shallows = append(r.Shallows, plumbing.NewHash("2222222222222222222222222222222222222222"))
err := r.Validate()
c.Assert(err, NotNil)
r.Capabilities.Set(capability.Shallow)
err = r.Validate()
c.Assert(err, IsNil)
}
func (s *UlReqSuite) TestValidateDepthCommits(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Depth = DepthCommits(42)
err := r.Validate()
c.Assert(err, NotNil)
r.Capabilities.Set(capability.Shallow)
err = r.Validate()
c.Assert(err, IsNil)
}
func (s *UlReqSuite) TestValidateDepthReference(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Depth = DepthReference("1111111111111111111111111111111111111111")
err := r.Validate()
c.Assert(err, NotNil)
r.Capabilities.Set(capability.DeepenNot)
err = r.Validate()
c.Assert(err, IsNil)
}
func (s *UlReqSuite) TestValidateDepthSince(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Depth = DepthSince(time.Now())
err := r.Validate()
c.Assert(err, NotNil)
r.Capabilities.Set(capability.DeepenSince)
err = r.Validate()
c.Assert(err, IsNil)
}
func (s *UlReqSuite) TestValidateConflictSideband(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Capabilities.Set(capability.Sideband)
r.Capabilities.Set(capability.Sideband64k)
err := r.Validate()
c.Assert(err, NotNil)
}
func (s *UlReqSuite) TestValidateConflictMultiACK(c *C) {
r := NewUploadRequest()
r.Wants = append(r.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
r.Capabilities.Set(capability.MultiACK)
r.Capabilities.Set(capability.MultiACKDetailed)
err := r.Validate()
c.Assert(err, NotNil)
}
func ExampleUploadRequest_Encode() {
// Create an empty UlReq with the contents you want...
ur := NewUploadRequest()
// Add a couple of wants
ur.Wants = append(ur.Wants, plumbing.NewHash("3333333333333333333333333333333333333333"))
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111"))
ur.Wants = append(ur.Wants, plumbing.NewHash("2222222222222222222222222222222222222222"))
// And some capabilities you will like the server to use
ur.Capabilities.Add(capability.OFSDelta)
ur.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master")
// Add a couple of shallows
ur.Shallows = append(ur.Shallows, plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"))
ur.Shallows = append(ur.Shallows, plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
// And retrict the answer of the server to commits newer than "2015-01-02 03:04:05 UTC"
since := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC)
ur.Depth = DepthSince(since)
// Create a new Encode for the stdout...
e := newUlReqEncoder(os.Stdout)
// ...and encode the upload-request to it.
_ = e.Encode(ur) // ignoring errors for brevity
// Output:
// 005bwant 1111111111111111111111111111111111111111 ofs-delta symref=HEAD:/refs/heads/master
// 0032want 2222222222222222222222222222222222222222
// 0032want 3333333333333333333333333333333333333333
// 0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
// 0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
// 001cdeepen-since 1420167845
// 0000
}
func ExampleUploadRequest_Decode() {
// Here is a raw advertised-ref message.
raw := "" +
"005bwant 1111111111111111111111111111111111111111 ofs-delta symref=HEAD:/refs/heads/master\n" +
"0032want 2222222222222222222222222222222222222222\n" +
"0032want 3333333333333333333333333333333333333333\n" +
"0035shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" +
"0035shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" +
"001cdeepen-since 1420167845\n" + // 2015-01-02 03:04:05 +0000 UTC
pktline.FlushString
// Use the raw message as our input.
input := strings.NewReader(raw)
// Create the Decoder reading from our input.
d := newUlReqDecoder(input)
// Decode the input into a newly allocated UlReq value.
ur := NewUploadRequest()
_ = d.Decode(ur) // error check ignored for brevity
// Do something interesting with the UlReq, e.g. print its contents.
fmt.Println("capabilities =", ur.Capabilities.String())
fmt.Println("wants =", ur.Wants)
fmt.Println("shallows =", ur.Shallows)
switch depth := ur.Depth.(type) {
case DepthCommits:
fmt.Println("depth =", int(depth))
case DepthSince:
fmt.Println("depth =", time.Time(depth))
case DepthReference:
fmt.Println("depth =", string(depth))
}
// Output:
// capabilities = ofs-delta symref=HEAD:/refs/heads/master
// wants = [1111111111111111111111111111111111111111 2222222222222222222222222222222222222222 3333333333333333333333333333333333333333]
// shallows = [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
// depth = 2015-01-02 03:04:05 +0000 UTC
}
|