Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: add PushConfig to specify push config and add support for refspecs #550

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 28 additions & 20 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 @@ -217,27 +206,27 @@ func (g *Client) Init(ctx context.Context, url, branch string) error {
return nil
}

func (g *Client) Clone(ctx context.Context, url string, cloneOpts repository.CloneOptions) (*git.Commit, error) {
func (g *Client) Clone(ctx context.Context, url string, cfg repository.CloneConfig) (*git.Commit, error) {
if err := g.validateUrl(url); err != nil {
return nil, err
}

checkoutStrat := cloneOpts.CheckoutStrategy
checkoutStrat := cfg.CheckoutStrategy
switch {
case checkoutStrat.Commit != "":
return g.cloneCommit(ctx, url, checkoutStrat.Commit, cloneOpts)
return g.cloneCommit(ctx, url, checkoutStrat.Commit, cfg)
case checkoutStrat.RefName != "":
return g.cloneRefName(ctx, url, checkoutStrat.RefName, cloneOpts)
return g.cloneRefName(ctx, url, checkoutStrat.RefName, cfg)
case checkoutStrat.Tag != "":
return g.cloneTag(ctx, url, checkoutStrat.Tag, cloneOpts)
return g.cloneTag(ctx, url, checkoutStrat.Tag, cfg)
case checkoutStrat.SemVer != "":
return g.cloneSemVer(ctx, url, checkoutStrat.SemVer, cloneOpts)
return g.cloneSemVer(ctx, url, checkoutStrat.SemVer, cfg)
default:
branch := checkoutStrat.Branch
if branch == "" {
branch = git.DefaultBranch
}
return g.cloneBranch(ctx, url, branch, cloneOpts)
return g.cloneBranch(ctx, url, branch, cfg)
}
}

Expand Down Expand Up @@ -351,7 +340,7 @@ func (g *Client) Commit(info git.Commit, commitOpts ...repository.CommitOption)
return commit.String(), nil
}

func (g *Client) Push(ctx context.Context) error {
func (g *Client) Push(ctx context.Context, cfg repository.PushConfig) error {
if g.repository == nil {
return git.ErrNoGitRepository
}
Expand All @@ -361,8 +350,27 @@ func (g *Client) Push(ctx context.Context) error {
return fmt.Errorf("failed to construct auth method with options: %w", err)
}

var refspecs []config.RefSpec
for _, ref := range cfg.Refspecs {
refspecs = append(refspecs, config.RefSpec(ref))
}

// If no refspecs were provided, we need to push the current ref HEAD points to.
// The format of a refspec for a Git push is generally something like
// "refs/heads/branch:refs/heads/branch".
if len(refspecs) == 0 {
head, err := g.repository.Head()
if err != nil {
return err
}

headRefspec := config.RefSpec(fmt.Sprintf("%s:%[1]s", head.Name()))
refspecs = append(refspecs, headRefspec)
}

return g.repository.PushContext(ctx, &extgogit.PushOptions{
Force: g.forcePush,
RefSpecs: refspecs,
Force: cfg.Force,
RemoteName: extgogit.DefaultRemoteName,
Auth: authMethod,
Progress: nil,
Expand Down