Skip to content

Commit

Permalink
plumbing: packp, Add encoding for push-options. Fixes #268.
Browse files Browse the repository at this point in the history
go-git: Add field `Options` to `PushOptions`, wire functionality.
  • Loading branch information
pprosbohn authored and S-Bohn committed Oct 26, 2021
1 parent 99457e5 commit e729edb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions options.go
Expand Up @@ -222,6 +222,8 @@ type PushOptions struct {
// FollowTags will send any annotated tags with a commit target reachable from
// the refs already being pushed
FollowTags bool
// PushOptions sets options to be transferred to the server during push.
Options map[string]string
}

// Validate validates the fields and sets the default values.
Expand Down
6 changes: 6 additions & 0 deletions plumbing/protocol/packp/updreq.go
Expand Up @@ -19,6 +19,7 @@ var (
type ReferenceUpdateRequest struct {
Capabilities *capability.List
Commands []*Command
Options []*Option
Shallow *plumbing.Hash
// Packfile contains an optional packfile reader.
Packfile io.ReadCloser
Expand Down Expand Up @@ -120,3 +121,8 @@ func (c *Command) validate() error {

return nil
}

type Option struct {
Key string
Value string
}
18 changes: 18 additions & 0 deletions plumbing/protocol/packp/updreq_encode.go
Expand Up @@ -29,6 +29,12 @@ func (req *ReferenceUpdateRequest) Encode(w io.Writer) error {
return err
}

if req.Capabilities.Supports(capability.PushOptions) {
if err := req.encodeOptions(e, req.Options); err != nil {
return err
}
}

if req.Packfile != nil {
if _, err := io.Copy(w, req.Packfile); err != nil {
return err
Expand Down Expand Up @@ -73,3 +79,15 @@ func formatCommand(cmd *Command) string {
n := cmd.New.String()
return fmt.Sprintf("%s %s %s", o, n, cmd.Name)
}

func (req *ReferenceUpdateRequest) encodeOptions(e *pktline.Encoder,
opts []*Option) error {

for _, opt := range opts {
if err := e.Encodef("%s=%s", opt.Key, opt.Value); err != nil {
return err
}
}

return e.Flush()
}
30 changes: 29 additions & 1 deletion plumbing/protocol/packp/updreq_encode_test.go
Expand Up @@ -5,9 +5,11 @@ import (

"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/pktline"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"

. "gopkg.in/check.v1"
"io/ioutil"

. "gopkg.in/check.v1"
)

type UpdReqEncodeSuite struct{}
Expand Down Expand Up @@ -142,3 +144,29 @@ func (s *UpdReqEncodeSuite) TestWithPackfile(c *C) {

s.testEncode(c, r, expected)
}

func (s *UpdReqEncodeSuite) TestPushOptions(c *C) {
hash1 := plumbing.NewHash("1ecf0ef2c2dffb796033e5a02219af86ec6584e5")
hash2 := plumbing.NewHash("2ecf0ef2c2dffb796033e5a02219af86ec6584e5")
name := plumbing.ReferenceName("myref")

r := NewReferenceUpdateRequest()
r.Capabilities.Set(capability.PushOptions)
r.Commands = []*Command{
{Name: name, Old: hash1, New: hash2},
}
r.Options = []*Option{
{Key: "SomeKey", Value: "SomeValue"},
{Key: "AnotherKey", Value: "AnotherValue"},
}

expected := pktlines(c,
"1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00push-options",
pktline.FlushString,
"SomeKey=SomeValue",
"AnotherKey=AnotherValue",
pktline.FlushString,
)

s.testEncode(c, r, expected)
}
7 changes: 7 additions & 0 deletions remote.go
Expand Up @@ -319,6 +319,13 @@ func (r *Remote) newReferenceUpdateRequest(
}
}

if ar.Capabilities.Supports(capability.PushOptions) {
_ = req.Capabilities.Set(capability.PushOptions)
for k, v := range o.Options {
req.Options = append(req.Options, &packp.Option{Key: k, Value: v})
}
}

if err := r.addReferencesToUpdate(o.RefSpecs, localRefs, remoteRefs, req, o.Prune); err != nil {
return nil, err
}
Expand Down

0 comments on commit e729edb

Please sign in to comment.