Skip to content

Commit

Permalink
Add Atomic to push options
Browse files Browse the repository at this point in the history
push --atomic allows a push to succeed or fail atomically. If one ref
fails, the whole push fails. This commit allows the user to set Atomic
as an option for a push.
  • Loading branch information
John Cai committed Nov 3, 2021
1 parent 3211a7a commit 589a41c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions options.go
Expand Up @@ -230,6 +230,8 @@ type PushOptions struct {
FollowTags bool
// PushOptions sets options to be transferred to the server during push.
Options map[string]string
// Atomic sets option to be an atomic push
Atomic bool
}

// Validate validates the fields and sets the default values.
Expand Down
19 changes: 19 additions & 0 deletions plumbing/protocol/packp/updreq_encode_test.go
Expand Up @@ -170,3 +170,22 @@ func (s *UpdReqEncodeSuite) TestPushOptions(c *C) {

s.testEncode(c, r, expected)
}

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

r := NewReferenceUpdateRequest()
r.Capabilities.Set(capability.Atomic)
r.Commands = []*Command{
{Name: name, Old: hash1, New: hash2},
}

expected := pktlines(c,
"1ecf0ef2c2dffb796033e5a02219af86ec6584e5 2ecf0ef2c2dffb796033e5a02219af86ec6584e5 myref\x00atomic",
pktline.FlushString,
)

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

if o.Atomic && ar.Capabilities.Supports(capability.Atomic) {
_ = req.Capabilities.Set(capability.Atomic)
}

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

0 comments on commit 589a41c

Please sign in to comment.