Skip to content

Commit

Permalink
git: add PushConfig.Force for force pushing
Browse files Browse the repository at this point in the history
Add `PushConfig.Force` for force pushing and remove
`gogit.Client.forcePush` in favor of the former.

Remove some stale test cases from `TestSwitchBranch` related to force
push since we don't check if force pushing is enabled while switching
branches anymore. Ref: #433

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
  • Loading branch information
aryan9600 committed May 15, 2023
1 parent 39b978a commit adda160
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 113 deletions.
13 changes: 1 addition & 12 deletions git/gogit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type Client struct {
authOpts *git.AuthOptions
storer storage.Storer
worktreeFS billy.Filesystem
forcePush bool
credentialsOverHTTP bool
useDefaultKnownHosts bool
singleBranch bool
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
105 changes: 4 additions & 101 deletions git/gogit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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
}{
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions git/repository/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit adda160

Please sign in to comment.