Skip to content

Commit 3211a7a

Browse files
authored
Merge pull request go-git#399 from S-Bohn/add-push-options
Remote: PushOptions add push-options
2 parents ed3b10c + e729edb commit 3211a7a

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

Diff for: options.go

+2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ type PushOptions struct {
228228
// FollowTags will send any annotated tags with a commit target reachable from
229229
// the refs already being pushed
230230
FollowTags bool
231+
// PushOptions sets options to be transferred to the server during push.
232+
Options map[string]string
231233
}
232234

233235
// Validate validates the fields and sets the default values.

Diff for: plumbing/protocol/packp/updreq.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
type ReferenceUpdateRequest struct {
2020
Capabilities *capability.List
2121
Commands []*Command
22+
Options []*Option
2223
Shallow *plumbing.Hash
2324
// Packfile contains an optional packfile reader.
2425
Packfile io.ReadCloser
@@ -120,3 +121,8 @@ func (c *Command) validate() error {
120121

121122
return nil
122123
}
124+
125+
type Option struct {
126+
Key string
127+
Value string
128+
}

Diff for: plumbing/protocol/packp/updreq_encode.go

+18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func (req *ReferenceUpdateRequest) Encode(w io.Writer) error {
2929
return err
3030
}
3131

32+
if req.Capabilities.Supports(capability.PushOptions) {
33+
if err := req.encodeOptions(e, req.Options); err != nil {
34+
return err
35+
}
36+
}
37+
3238
if req.Packfile != nil {
3339
if _, err := io.Copy(w, req.Packfile); err != nil {
3440
return err
@@ -73,3 +79,15 @@ func formatCommand(cmd *Command) string {
7379
n := cmd.New.String()
7480
return fmt.Sprintf("%s %s %s", o, n, cmd.Name)
7581
}
82+
83+
func (req *ReferenceUpdateRequest) encodeOptions(e *pktline.Encoder,
84+
opts []*Option) error {
85+
86+
for _, opt := range opts {
87+
if err := e.Encodef("%s=%s", opt.Key, opt.Value); err != nil {
88+
return err
89+
}
90+
}
91+
92+
return e.Flush()
93+
}

Diff for: plumbing/protocol/packp/updreq_encode_test.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55

66
"github.com/go-git/go-git/v5/plumbing"
77
"github.com/go-git/go-git/v5/plumbing/format/pktline"
8+
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
89

9-
. "gopkg.in/check.v1"
1010
"io/ioutil"
11+
12+
. "gopkg.in/check.v1"
1113
)
1214

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

143145
s.testEncode(c, r, expected)
144146
}
147+
148+
func (s *UpdReqEncodeSuite) TestPushOptions(c *C) {
149+
hash1 := plumbing.NewHash("1ecf0ef2c2dffb796033e5a02219af86ec6584e5")
150+
hash2 := plumbing.NewHash("2ecf0ef2c2dffb796033e5a02219af86ec6584e5")
151+
name := plumbing.ReferenceName("myref")
152+
153+
r := NewReferenceUpdateRequest()
154+
r.Capabilities.Set(capability.PushOptions)
155+
r.Commands = []*Command{
156+
{Name: name, Old: hash1, New: hash2},
157+
}
158+
r.Options = []*Option{
159+
{Key: "SomeKey", Value: "SomeValue"},
160+
{Key: "AnotherKey", Value: "AnotherValue"},
161+
}
162+
163+
expected := pktlines(c,
164+
"1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00push-options",
165+
pktline.FlushString,
166+
"SomeKey=SomeValue",
167+
"AnotherKey=AnotherValue",
168+
pktline.FlushString,
169+
)
170+
171+
s.testEncode(c, r, expected)
172+
}

Diff for: remote.go

+7
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ func (r *Remote) newReferenceUpdateRequest(
319319
}
320320
}
321321

322+
if ar.Capabilities.Supports(capability.PushOptions) {
323+
_ = req.Capabilities.Set(capability.PushOptions)
324+
for k, v := range o.Options {
325+
req.Options = append(req.Options, &packp.Option{Key: k, Value: v})
326+
}
327+
}
328+
322329
if err := r.addReferencesToUpdate(o.RefSpecs, localRefs, remoteRefs, req, o.Prune); err != nil {
323330
return nil, err
324331
}

0 commit comments

Comments
 (0)