aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-27 14:13:25 +0200
committerGitHub <noreply@github.com>2017-04-27 14:13:25 +0200
commit026ce1b2cff0aae82b7f8d4f4413e2479479ea37 (patch)
tree4b559d70055114ee6fbfb0b3d87768e4e5694f10
parent64cd72debb2a94a49de5ffd3c3a6bfd626df7340 (diff)
parent38bd9e82fd3d83fd9a82678ec89227aeda2c3b78 (diff)
downloadgo-git-026ce1b2cff0aae82b7f8d4f4413e2479479ea37.tar.gz
Merge pull request #366 from smola/push-force
support force push (refspec with +)
-rw-r--r--remote.go86
-rw-r--r--remote_test.go87
2 files changed, 151 insertions, 22 deletions
diff --git a/remote.go b/remote.go
index ae8a544..1cb7ba7 100644
--- a/remote.go
+++ b/remote.go
@@ -8,6 +8,7 @@ import (
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
+ "gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
@@ -57,8 +58,7 @@ func (r *Remote) Fetch(o *FetchOptions) error {
func (r *Remote) Push(o *PushOptions) (err error) {
// TODO: Support deletes.
// TODO: Support pushing tags.
- // TODO: Check if force update is given, otherwise reject non-fast forward.
- // TODO: Sideband suppor
+ // TODO: Sideband support
if o.RemoteName == "" {
o.RemoteName = r.c.Name
@@ -265,37 +265,35 @@ func (r *Remote) addReferenceIfRefSpecMatches(rs config.RefSpec,
return nil
}
- dstName := rs.Dst(localRef.Name())
- oldHash := plumbing.ZeroHash
- newHash := localRef.Hash()
-
- iter, err := remoteRefs.IterReferences()
- if err != nil {
- return err
+ cmd := &packp.Command{
+ Name: rs.Dst(localRef.Name()),
+ Old: plumbing.ZeroHash,
+ New: localRef.Hash(),
}
- err = iter.ForEach(func(remoteRef *plumbing.Reference) error {
+ remoteRef, err := remoteRefs.Reference(cmd.Name)
+ if err == nil {
if remoteRef.Type() != plumbing.HashReference {
+ //TODO: check actual git behavior here
return nil
}
- if dstName != remoteRef.Name() {
- return nil
- }
+ cmd.Old = remoteRef.Hash()
+ } else if err != plumbing.ErrReferenceNotFound {
+ return err
+ }
- oldHash = remoteRef.Hash()
+ if cmd.Old == cmd.New {
return nil
- })
+ }
- if oldHash == newHash {
- return nil
+ if !rs.IsForceUpdate() {
+ if err := checkFastForwardUpdate(r.s, remoteRefs, cmd); err != nil {
+ return err
+ }
}
- req.Commands = append(req.Commands, &packp.Command{
- Name: dstName,
- Old: oldHash,
- New: newHash,
- })
+ req.Commands = append(req.Commands, cmd)
return nil
}
@@ -390,6 +388,50 @@ func objectExists(s storer.EncodedObjectStorer, h plumbing.Hash) (bool, error) {
return true, err
}
+func checkFastForwardUpdate(s storer.EncodedObjectStorer, remoteRefs storer.ReferenceStorer, cmd *packp.Command) error {
+ if cmd.Old == plumbing.ZeroHash {
+ _, err := remoteRefs.Reference(cmd.Name)
+ if err == plumbing.ErrReferenceNotFound {
+ return nil
+ }
+
+ if err != nil {
+ return err
+ }
+
+ return fmt.Errorf("non-fast-forward update: %s", cmd.Name.String())
+ }
+
+ ff, err := isFastForward(s, cmd.Old, cmd.New)
+ if err != nil {
+ return err
+ }
+
+ if !ff {
+ return fmt.Errorf("non-fast-forward update: %s", cmd.Name.String())
+ }
+
+ return nil
+}
+
+func isFastForward(s storer.EncodedObjectStorer, old, new plumbing.Hash) (bool, error) {
+ c, err := object.GetCommit(s, new)
+ if err != nil {
+ return false, err
+ }
+
+ found := false
+ iter := object.NewCommitPreIterator(c)
+ return found, iter.ForEach(func(c *object.Commit) error {
+ if c.Hash != old {
+ return nil
+ }
+
+ found = true
+ return storer.ErrStop
+ })
+}
+
func (r *Remote) newUploadPackRequest(o *FetchOptions,
ar *packp.AdvRefs) (*packp.UploadPackRequest, error) {
diff --git a/remote_test.go b/remote_test.go
index 2cd80cf..d48b6ca 100644
--- a/remote_test.go
+++ b/remote_test.go
@@ -266,6 +266,93 @@ func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
c.Assert(err, Equals, NoErrAlreadyUpToDate)
}
+func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) {
+ f := fixtures.Basic().One()
+ sto, err := filesystem.NewStorage(f.DotGit())
+ c.Assert(err, IsNil)
+
+ dstFs := f.DotGit()
+ dstSto, err := filesystem.NewStorage(dstFs)
+ c.Assert(err, IsNil)
+
+ url := fmt.Sprintf("file://%s", dstFs.Base())
+ r := newRemote(sto, &config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: url,
+ })
+
+ oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, IsNil)
+ c.Assert(oldRef, NotNil)
+
+ err = r.Push(&PushOptions{RefSpecs: []config.RefSpec{
+ config.RefSpec("refs/heads/master:refs/heads/branch"),
+ }})
+ c.Assert(err, ErrorMatches, "non-fast-forward update: refs/heads/branch")
+
+ newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, IsNil)
+ c.Assert(newRef, DeepEquals, oldRef)
+}
+
+func (s *RemoteSuite) TestPushForce(c *C) {
+ f := fixtures.Basic().One()
+ sto, err := filesystem.NewStorage(f.DotGit())
+ c.Assert(err, IsNil)
+
+ dstFs := f.DotGit()
+ dstSto, err := filesystem.NewStorage(dstFs)
+ c.Assert(err, IsNil)
+
+ url := fmt.Sprintf("file://%s", dstFs.Base())
+ r := newRemote(sto, &config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: url,
+ })
+
+ oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, IsNil)
+ c.Assert(oldRef, NotNil)
+
+ err = r.Push(&PushOptions{RefSpecs: []config.RefSpec{
+ config.RefSpec("+refs/heads/master:refs/heads/branch"),
+ }})
+ c.Assert(err, IsNil)
+
+ newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, IsNil)
+ c.Assert(newRef, Not(DeepEquals), oldRef)
+}
+
+func (s *RemoteSuite) TestPushNewReference(c *C) {
+ f := fixtures.Basic().One()
+ sto, err := filesystem.NewStorage(f.DotGit())
+ c.Assert(err, IsNil)
+
+ dstFs := f.DotGit()
+ dstSto, err := filesystem.NewStorage(dstFs)
+ c.Assert(err, IsNil)
+
+ url := fmt.Sprintf("file://%s", dstFs.Base())
+ r := newRemote(sto, &config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: url,
+ })
+
+ oldRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch"))
+ c.Assert(err, IsNil)
+ c.Assert(oldRef, NotNil)
+
+ err = r.Push(&PushOptions{RefSpecs: []config.RefSpec{
+ config.RefSpec("refs/heads/branch:refs/heads/branch2"),
+ }})
+ c.Assert(err, IsNil)
+
+ newRef, err := dstSto.Reference(plumbing.ReferenceName("refs/heads/branch2"))
+ c.Assert(err, IsNil)
+ c.Assert(newRef.Hash(), Equals, oldRef.Hash())
+}
+
func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
err := r.Push(&PushOptions{})