diff --git a/git/gogit/client.go b/git/gogit/client.go index ca03b105..9330a25f 100644 --- a/git/gogit/client.go +++ b/git/gogit/client.go @@ -52,7 +52,6 @@ type Client struct { authOpts *git.AuthOptions storer storage.Storer worktreeFS billy.Filesystem - forcePush bool credentialsOverHTTP bool useDefaultKnownHosts bool singleBranch bool @@ -148,16 +147,6 @@ func WithMemoryStorage() ClientOption { } } -// WithForcePush enables the use of force push for all push operations -// back to the Git repository. -// By default this is disabled. -func WithForcePush() ClientOption { - return func(c *Client) error { - c.forcePush = true - return nil - } -} - // WithInsecureCredentialsOverHTTP enables credentials being used over // HTTP. This is not recommended for production environments. func WithInsecureCredentialsOverHTTP() ClientOption { @@ -381,7 +370,7 @@ func (g *Client) Push(ctx context.Context, cfg repository.PushConfig) error { return g.repository.PushContext(ctx, &extgogit.PushOptions{ RefSpecs: refspecs, - Force: g.forcePush, + Force: cfg.Force, RemoteName: extgogit.DefaultRemoteName, Auth: authMethod, Progress: nil, diff --git a/git/gogit/client_test.go b/git/gogit/client_test.go index 57a9149b..4f0de373 100644 --- a/git/gogit/client_test.go +++ b/git/gogit/client_test.go @@ -360,7 +360,7 @@ func TestForcePush(t *testing.T) { cc2, err := commitFile(repo2, "test", "first push from second clone", time.Now()) g.Expect(err).ToNot(HaveOccurred()) - ggc2, err := NewClient(tmp2, nil, WithDiskStorage(), WithForcePush()) + ggc2, err := NewClient(tmp2, nil) g.Expect(err).ToNot(HaveOccurred()) ggc2.repository = repo2 @@ -369,7 +369,9 @@ func TestForcePush(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) // Force push from ggc2 should override ggc1. - err = ggc2.Push(context.TODO(), repository.PushConfig{}) + err = ggc2.Push(context.TODO(), repository.PushConfig{ + Force: true, + }) g.Expect(err).ToNot(HaveOccurred()) // Follow-up push from ggc1 errors. @@ -394,7 +396,6 @@ func TestSwitchBranch(t *testing.T) { setupFunc func(g *WithT, path string) string changeRepo func(g *WithT, c *Client) string branch string - forcePush bool singleBranch bool }{ { @@ -534,103 +535,6 @@ func TestSwitchBranch(t *testing.T) { setupFunc: nil, branch: "new", }, - { - name: "force push: switch to a branch ahead of the current branch", - setupFunc: func(g *WithT, repoURL string) string { - tmp := t.TempDir() - repo, err := extgogit.PlainClone(tmp, false, &extgogit.CloneOptions{ - URL: repoURL, - ReferenceName: plumbing.NewBranchReferenceName(git.DefaultBranch), - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - - err = createBranch(repo, "ahead") - g.Expect(err).ToNot(HaveOccurred()) - - cc, err := commitFile(repo, "test", "testing gogit switch ahead branch", time.Now()) - g.Expect(err).ToNot(HaveOccurred()) - err = repo.Push(&extgogit.PushOptions{ - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - return cc.String() - }, - branch: "ahead", - forcePush: true, - }, - { - name: "force push: switch to a branch behind the current branch", - setupFunc: func(g *WithT, repoURL string) string { - tmp := t.TempDir() - repo, err := extgogit.PlainClone(tmp, false, &extgogit.CloneOptions{ - URL: repoURL, - ReferenceName: plumbing.NewBranchReferenceName(git.DefaultBranch), - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - - err = createBranch(repo, "behind") - g.Expect(err).ToNot(HaveOccurred()) - ref, err := repo.Head() - g.Expect(err).ToNot(HaveOccurred()) - hash := ref.Hash().String() - - wt, err := repo.Worktree() - g.Expect(err).ToNot(HaveOccurred()) - err = wt.Checkout(&extgogit.CheckoutOptions{ - Branch: plumbing.ReferenceName("refs/heads/" + git.DefaultBranch), - }) - g.Expect(err).ToNot(HaveOccurred()) - - _, err = commitFile(repo, "test", "testing gogit switch behind branch", time.Now()) - g.Expect(err).ToNot(HaveOccurred()) - err = repo.Push(&extgogit.PushOptions{ - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - - return hash - }, - branch: "behind", - forcePush: true, - }, - { - name: "force push: switch to a branch that doesn't exist on remote", - setupFunc: nil, - branch: "new", - forcePush: true, - }, - { - name: "force: ignore a branch that exists in the remote", - setupFunc: func(g *WithT, repoURL string) string { - tmp := t.TempDir() - repo, err := extgogit.PlainClone(tmp, false, &extgogit.CloneOptions{ - URL: repoURL, - ReferenceName: plumbing.NewBranchReferenceName(git.DefaultBranch), - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - - head, err := repo.Head() - g.Expect(err).ToNot(HaveOccurred()) - - err = createBranch(repo, "singlebranch-ahead") - g.Expect(err).ToNot(HaveOccurred()) - - _, err = commitFile(repo, "test", "remote change that will be overwritten", time.Now()) - g.Expect(err).ToNot(HaveOccurred()) - err = repo.Push(&extgogit.PushOptions{ - RemoteName: git.DefaultRemote, - }) - g.Expect(err).ToNot(HaveOccurred()) - - return head.Hash().String() - }, - branch: "singlebranch-ahead", - singleBranch: true, - forcePush: true, - }, } for _, tt := range tests { @@ -665,7 +569,6 @@ func TestSwitchBranch(t *testing.T) { ggc, err := NewClient(tmp, nil) g.Expect(err).ToNot(HaveOccurred()) ggc.repository = repo - ggc.forcePush = tt.forcePush if tt.changeRepo != nil { expectedHash = tt.changeRepo(g, ggc) diff --git a/git/repository/options.go b/git/repository/options.go index 95b393a1..fea02721 100644 --- a/git/repository/options.go +++ b/git/repository/options.go @@ -59,6 +59,9 @@ type PushConfig struct { // For details about Git Refspecs, please see: // https://git-scm.com/book/en/v2/Git-Internals-The-Refspec Refspecs []string + + // Force, if set to true, will result in a force push. + Force bool } // CheckoutStrategy provides options to checkout a repository to a target.