Skip to content

Commit e23222a

Browse files
committed
Use new git command
1 parent 7f1f190 commit e23222a

16 files changed

Lines changed: 268 additions & 232 deletions

gitclone/checkout.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package gitclone
55
import (
66
"fmt"
77

8-
"github.com/bitrise-io/go-utils/command/git"
98
"github.com/bitrise-io/go-utils/log"
9+
"github.com/bitrise-io/go-utils/v2/git"
1010
"github.com/bitrise-steplib/steps-git-clone/gitclone/bitriseapi"
1111
)
1212

@@ -65,7 +65,7 @@ func NewParameterValidationError(msg string) error {
6565

6666
// checkoutStrategy is the interface an actual checkout strategy implements
6767
type checkoutStrategy interface {
68-
do(gitCmd git.Git, fetchOptions fetchOptions, fallback fallbackRetry) error
68+
do(gitFactory git.Factory, fetchOptions fetchOptions, fallback fallbackRetry) error
6969

7070
// getBuildTriggerRef returns ref to the commit/branch/tag that triggered the build.
7171
// For simple checkout strategies the returned ref will be HEAD (after running 'do').
@@ -250,7 +250,7 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
250250

251251
return checkoutPRMergeRef{
252252
params: *params,
253-
fallbackCheckout: func(gitCmd git.Git) error {
253+
fallbackCheckout: func(gitFactory git.Factory) error {
254254
log.Warnf("Using manual merge strategy with PR source branch")
255255

256256
manualMergeFallbackFetchOpts := selectFetchOptions(CheckoutPRManualMergeMethod, cfg.CloneDepth, cfg.FetchTags, cfg.UpdateSubmodules, len(cfg.SparseDirectories) != 0)
@@ -267,7 +267,7 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
267267
}
268268

269269
// PR merge branch checkout falls back to PR manual merge strategy using the PR source branch
270-
return fallbackManualMergeWithSourceBranch.do(gitCmd, manualMergeFallbackFetchOpts, manualMergeFallbackFallback)
270+
return fallbackManualMergeWithSourceBranch.do(gitFactory, manualMergeFallbackFetchOpts, manualMergeFallbackFallback)
271271
},
272272
}, nil
273273
}
@@ -314,7 +314,7 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
314314

315315
return checkoutCommit{
316316
params: *params,
317-
fallbackCheckout: func(gitCmd git.Git) error {
317+
fallbackCheckout: func(gitFactory git.Factory) error {
318318
log.Warnf("Using commit checkout strategy with PR source branch")
319319

320320
if cfg.Branch == "" || cfg.Commit == "" {
@@ -339,7 +339,7 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
339339
params: *params,
340340
}
341341

342-
return commitCheckoutFallbackCheckoutMethod.do(gitCmd, commitCheckoutFallbackFetchOpts, commitCheckoutFallbackFallback)
342+
return commitCheckoutFallbackCheckoutMethod.do(gitFactory, commitCheckoutFallbackFetchOpts, commitCheckoutFallbackFallback)
343343
},
344344
}, nil
345345
}

gitclone/checkout_helper.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/bitrise-io/go-utils/command"
9-
"github.com/bitrise-io/go-utils/command/git"
108
"github.com/bitrise-io/go-utils/log"
9+
"github.com/bitrise-io/go-utils/v2/git"
1110
)
1211

1312
type fetchOptions struct {
@@ -38,7 +37,7 @@ const (
3837
refsHeadsPrefix = "refs/heads/"
3938
)
4039

41-
func fetch(gitCmd git.Git, remote string, ref string, options fetchOptions) error {
40+
func fetch(gitFactory git.Factory, remote string, ref string, options fetchOptions) error {
4241
var opts []string
4342
opts = append(opts, jobsFlag)
4443

@@ -67,11 +66,11 @@ func fetch(gitCmd git.Git, remote string, ref string, options fetchOptions) erro
6766
branch = strings.TrimPrefix(ref, refsHeadsPrefix)
6867
}
6968

70-
if err := runner.RunWithRetry(func() *command.Model {
71-
return gitCmd.Fetch(opts...)
69+
if err := runner.RunWithRetry(func() git.Template {
70+
return gitFactory.Fetch(opts...)
7271
}); err != nil {
7372
return handleCheckoutError(
74-
listBranches(gitCmd),
73+
listBranches(gitFactory),
7574
fetchFailedTag,
7675
err,
7776
"Fetching repository has failed",
@@ -82,15 +81,15 @@ func fetch(gitCmd git.Git, remote string, ref string, options fetchOptions) erro
8281
return nil
8382
}
8483

85-
func checkoutWithCustomRetry(gitCmd git.Git, arg string, retry fallbackRetry) error {
86-
if cErr := runner.Run(gitCmd.Checkout(arg)); cErr != nil {
84+
func checkoutWithCustomRetry(gitFactory git.Factory, arg string, retry fallbackRetry) error {
85+
if cErr := runner.Run(gitFactory.Checkout(arg)); cErr != nil {
8786
if retry != nil {
8887
log.Warnf("Checkout failed (%s): %v", arg, cErr)
89-
if err := retry.do(gitCmd); err != nil {
88+
if err := retry.do(gitFactory); err != nil {
9089
return err
9190
}
9291

93-
return runner.Run(gitCmd.Checkout(arg))
92+
return runner.Run(gitFactory.Checkout(arg))
9493
}
9594

9695
return fmt.Errorf("checkout failed (%s): %w", arg, cErr)
@@ -99,21 +98,21 @@ func checkoutWithCustomRetry(gitCmd git.Git, arg string, retry fallbackRetry) er
9998
return nil
10099
}
101100

102-
func forceCheckoutRemoteBranch(gitCmd git.Git, remote string, branchRef string, fetchTraits fetchOptions) error {
101+
func forceCheckoutRemoteBranch(gitFactory git.Factory, remote string, branchRef string, fetchTraits fetchOptions) error {
103102
branch := strings.TrimPrefix(branchRef, refsHeadsPrefix)
104-
if err := fetch(gitCmd, remote, branchRef, fetchTraits); err != nil {
103+
if err := fetch(gitFactory, remote, branchRef, fetchTraits); err != nil {
105104
wErr := fmt.Errorf("fetch branch %s: %w", branchRef, err)
106105
return fmt.Errorf("%v: %w", wErr, errors.New("please make sure the branch still exists"))
107106
}
108-
107+
109108
remoteBranch := fmt.Sprintf("%s/%s", remote, branch)
110109
// -B: create the branch if it doesn't exist, reset if it does
111110
// The latter is important in persistent environments because shallow-fetching only fetches 1 commit,
112111
// so the next run would see unrelated histories after shallow-fetching another single commit.
113-
err := runner.Run(gitCmd.Checkout("-B", branch, remoteBranch))
112+
err := runner.Run(gitFactory.Checkout("-B", branch, remoteBranch))
114113
if err != nil {
115114
return handleCheckoutError(
116-
listBranches(gitCmd),
115+
listBranches(gitFactory),
117116
checkoutFailedTag,
118117
err,
119118
"Checkout has failed",
@@ -124,15 +123,15 @@ func forceCheckoutRemoteBranch(gitCmd git.Git, remote string, branchRef string,
124123
return nil
125124
}
126125

127-
func mergeWithCustomRetry(gitCmd git.Git, arg string, retry fallbackRetry) error {
128-
if mErr := runner.Run(gitCmd.Merge(arg)); mErr != nil {
126+
func mergeWithCustomRetry(gitFactory git.Factory, arg string, retry fallbackRetry) error {
127+
if mErr := runner.Run(gitFactory.Merge(arg)); mErr != nil {
129128
if retry != nil {
130129
log.Warnf("Merge failed (%s): %v", arg, mErr)
131-
if err := retry.do(gitCmd); err != nil {
130+
if err := retry.do(gitFactory); err != nil {
132131
return err
133132
}
134133

135-
return runner.Run(gitCmd.Merge(arg))
134+
return runner.Run(gitFactory.Merge(arg))
136135
}
137136

138137
wErr := fmt.Errorf("merge failed (%s): %w", arg, mErr)
@@ -142,8 +141,8 @@ func mergeWithCustomRetry(gitCmd git.Git, arg string, retry fallbackRetry) error
142141
return nil
143142
}
144143

145-
func detachHead(gitCmd git.Git) error {
146-
if err := runner.Run(gitCmd.Checkout("--detach")); err != nil {
144+
func detachHead(gitFactory git.Factory) error {
145+
if err := runner.Run(gitFactory.Checkout("--detach")); err != nil {
147146
return newStepError(
148147
"detach_head_failed",
149148
fmt.Errorf("detaching head failed: %w", err),
@@ -154,6 +153,6 @@ func detachHead(gitCmd git.Git) error {
154153
return nil
155154
}
156155

157-
func deleteRef(gitCmd git.Git, ref string) error {
158-
return runner.Run(gitCmd.UpdateRef("-d", ref))
156+
func deleteRef(gitFactory git.Factory, ref string) error {
157+
return runner.Run(gitFactory.UpdateRef("-d", ref))
159158
}

gitclone/checkout_method_pr_auto_diff.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/bitrise-io/go-utils/command/git"
87
"github.com/bitrise-io/go-utils/log"
8+
"github.com/bitrise-io/go-utils/v2/git"
99
)
1010

1111
// PRDiffFileParams are parameters to check out a Merge/Pull Request (when a diff file is available)
@@ -35,28 +35,28 @@ type checkoutPRDiffFile struct {
3535
patchFile string
3636
}
3737

38-
func (c checkoutPRDiffFile) do(gitCmd git.Git, fetchOptions fetchOptions, fallback fallbackRetry) error {
38+
func (c checkoutPRDiffFile) do(gitFactory git.Factory, fetchOptions fetchOptions, fallback fallbackRetry) error {
3939
destBranchRef := refsHeadsPrefix + c.params.DestinationBranch
40-
if err := fetch(gitCmd, originRemoteName, destBranchRef, fetchOptions); err != nil {
40+
if err := fetch(gitFactory, originRemoteName, destBranchRef, fetchOptions); err != nil {
4141
return fmt.Errorf("failed to fetch base branch: %w", err)
4242
}
4343

44-
if err := checkoutWithCustomRetry(gitCmd, c.params.DestinationBranch, fallback); err != nil {
44+
if err := checkoutWithCustomRetry(gitFactory, c.params.DestinationBranch, fallback); err != nil {
4545
return err
4646
}
4747

48-
if err := runner.Run(gitCmd.Apply(c.patchFile)); err != nil {
48+
if err := runner.Run(gitFactory.Apply(c.patchFile)); err != nil {
4949
log.Warnf("Could not apply patch (%s): %v", c.patchFile, err)
5050
log.Warnf("Falling back to manual merge...")
5151

52-
if err := c.params.PRManualMergeStrategy.do(gitCmd, fetchOptions, fallback); err != nil {
52+
if err := c.params.PRManualMergeStrategy.do(gitFactory, fetchOptions, fallback); err != nil {
5353
return fmt.Errorf("fallback failed for applying patch (%s): %v", c.patchFile, err)
5454
}
5555

5656
return nil
5757
}
5858

59-
return detachHead(gitCmd)
59+
return detachHead(gitFactory)
6060
}
6161

6262
func (c checkoutPRDiffFile) getBuildTriggerRef() string {

gitclone/checkout_method_pr_auto_merge_branch.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package gitclone
33
import (
44
"fmt"
55
"strings"
6-
7-
"github.com/bitrise-io/go-utils/command/git"
6+
87
"github.com/bitrise-io/go-utils/log"
8+
"github.com/bitrise-io/go-utils/v2/git"
99
)
1010

1111
// PRMergeRefParams are parameters to check out a Merge/Pull Request's merge ref (the result of merging the 2 branches)
@@ -38,20 +38,20 @@ type checkoutPRMergeRef struct {
3838
fallbackCheckout fallbackCheckoutFunc
3939
}
4040

41-
type fallbackCheckoutFunc func(gitCmd git.Git) error
41+
type fallbackCheckoutFunc func(gitFactory git.Factory) error
4242

43-
func (c checkoutPRMergeRef) do(gitCmd git.Git, fetchOpts fetchOptions, fallback fallbackRetry) error {
44-
if err := c.performCheckout(gitCmd, fetchOpts, fallback); err != nil {
43+
func (c checkoutPRMergeRef) do(gitFactory git.Factory, fetchOpts fetchOptions, fallback fallbackRetry) error {
44+
if err := c.performCheckout(gitFactory, fetchOpts, fallback); err != nil {
4545
if c.fallbackCheckout != nil {
4646
log.Warnf("Failed to checkout PR merge branch: %s", err)
47-
return c.fallbackCheckout(gitCmd)
47+
return c.fallbackCheckout(gitFactory)
4848
}
4949
return err
5050
}
5151
return nil
5252
}
5353

54-
func (c checkoutPRMergeRef) performCheckout(gitCmd git.Git, fetchOpts fetchOptions, fallback fallbackRetry) error {
54+
func (c checkoutPRMergeRef) performCheckout(gitFactory git.Factory, fetchOpts fetchOptions, fallback fallbackRetry) error {
5555
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
5656
refSpec := fmt.Sprintf("%s:%s", c.remoteMergeRef(), c.localMergeRef())
5757

@@ -62,33 +62,33 @@ func (c checkoutPRMergeRef) performCheckout(gitCmd git.Git, fetchOpts fetchOptio
6262
// This is caused by the remote merge and head branches being "force-pushed" by GitHub.
6363
// To solve it we remove merge and head branch refs.
6464
// $ git update-ref -d refs/remotes/pull/7/merge
65-
err := deleteRef(gitCmd, c.localMergeRef())
65+
err := deleteRef(gitFactory, c.localMergeRef())
6666
if err != nil {
6767
return fmt.Errorf("failed to delete ref: %w", err)
6868
}
6969

7070
// $ git update-ref -d refs/remotes/pull/7/head
7171
// If the ref does not exist, the command still exits with 0 exit code.
72-
err = deleteRef(gitCmd, c.localHeadRef())
72+
err = deleteRef(gitFactory, c.localHeadRef())
7373
if err != nil {
7474
return fmt.Errorf("failed to delete ref: %w", err)
7575
}
7676

7777
//$ git fetch origin refs/remotes/pull/7/merge:refs/pull/7/merge
78-
err = fetch(gitCmd, originRemoteName, refSpec, fetchOpts)
78+
err = fetch(gitFactory, originRemoteName, refSpec, fetchOpts)
7979
if err != nil {
8080
return fmt.Errorf("failed to fetch merge ref: %w", err)
8181
}
8282

8383
// Also fetch the PR head ref because the step exports outputs based on the PR head commit (see output.go)
8484
// $ git fetch origin refs/remotes/pull/7/head:refs/pull/7/head
85-
err = c.fetchPRHeadRef(gitCmd, fetchOpts)
85+
err = c.fetchPRHeadRef(gitFactory, fetchOpts)
8686
if err != nil {
8787
return err
8888
}
8989

9090
// $ git checkout refs/remotes/pull/7/merge
91-
err = checkoutWithCustomRetry(gitCmd, c.localMergeRef(), nil)
91+
err = checkoutWithCustomRetry(gitFactory, c.localMergeRef(), nil)
9292
if err != nil {
9393
return err
9494
}
@@ -116,7 +116,7 @@ func (c checkoutPRMergeRef) remoteHeadRef() string {
116116
return fmt.Sprintf("refs/%s", c.params.HeadRef)
117117
}
118118

119-
func (c checkoutPRMergeRef) fetchPRHeadRef(gitCmd git.Git, fetchOpts fetchOptions) error {
119+
func (c checkoutPRMergeRef) fetchPRHeadRef(gitFactory git.Factory, fetchOpts fetchOptions) error {
120120
refSpec := fmt.Sprintf("%s:%s", c.remoteHeadRef(), c.localHeadRef())
121-
return fetch(gitCmd, originRemoteName, refSpec, fetchOpts)
121+
return fetch(gitFactory, originRemoteName, refSpec, fetchOpts)
122122
}

gitclone/checkout_method_pr_manual.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/bitrise-io/go-utils/command/git"
87
"github.com/bitrise-io/go-utils/log"
8+
"github.com/bitrise-io/go-utils/v2/git"
99
)
1010

1111
// PRManualMergeParams are parameters to check out a Merge Request using manual merge
@@ -58,14 +58,14 @@ type checkoutPRManualMerge struct {
5858
params PRManualMergeParams
5959
}
6060

61-
func (c checkoutPRManualMerge) do(gitCmd git.Git, fetchOptions fetchOptions, fallback fallbackRetry) error {
61+
func (c checkoutPRManualMerge) do(gitFactory git.Factory, fetchOptions fetchOptions, fallback fallbackRetry) error {
6262
// Fetch and checkout destinations branch
6363
destBranchRef := refsHeadsPrefix + c.params.DestinationBranch
64-
if err := forceCheckoutRemoteBranch(gitCmd, originRemoteName, destBranchRef, fetchOptions); err != nil {
64+
if err := forceCheckoutRemoteBranch(gitFactory, originRemoteName, destBranchRef, fetchOptions); err != nil {
6565
return fmt.Errorf("failed to fetch base branch: %w", err)
6666
}
6767

68-
commitHash, err := runner.RunForOutput(gitCmd.Log("%H"))
68+
commitHash, err := runner.RunForOutput(gitFactory.Log("%H"))
6969
if err != nil {
7070
log.Errorf("log commit hash: %v", err)
7171
}
@@ -76,7 +76,7 @@ func (c checkoutPRManualMerge) do(gitCmd git.Git, fetchOptions fetchOptions, fal
7676
remoteName = forkRemoteName
7777

7878
// Add fork remote
79-
if err := runner.Run(gitCmd.RemoteAdd(forkRemoteName, c.params.SourceRepoURL)); err != nil {
79+
if err := runner.Run(gitFactory.RemoteAdd(forkRemoteName, c.params.SourceRepoURL)); err != nil {
8080
return fmt.Errorf("adding remote fork repository failed (%s): %w", c.params.SourceRepoURL, err)
8181
}
8282

@@ -86,15 +86,15 @@ func (c checkoutPRManualMerge) do(gitCmd git.Git, fetchOptions fetchOptions, fal
8686

8787
// Fetch and merge
8888
sourceBranchRef := refsHeadsPrefix + c.params.SourceBranch
89-
if err := fetch(gitCmd, remoteName, sourceBranchRef, fetchOptions); err != nil {
89+
if err := fetch(gitFactory, remoteName, sourceBranchRef, fetchOptions); err != nil {
9090
return fmt.Errorf("failed to fetch compare branch: %w", err)
9191
}
9292

93-
if err := mergeWithCustomRetry(gitCmd, c.params.SourceMergeArg, fallback); err != nil {
93+
if err := mergeWithCustomRetry(gitFactory, c.params.SourceMergeArg, fallback); err != nil {
9494
return err
9595
}
9696

97-
return detachHead(gitCmd)
97+
return detachHead(gitFactory)
9898
}
9999

100100
func (c checkoutPRManualMerge) getBuildTriggerRef() string {

0 commit comments

Comments
 (0)