Skip to content

Commit 93b66d4

Browse files
godreitothszabi
andauthored
Manual merge fallback (#230)
* Fallback to manual merge * Log warning when using fallback manual merge * Handle all merge ref errors * Add propoer fetch params and fallback * Force merge branch method * Force PR merge branch method * Fix unused var * Mock pr head fetch error * Use manual merge fallback with the pr head branch * Add fallback manual merge with pr source branch * Simplify fallback strategy creation * Add logging * Fix pr head branch based manual merge * Remove head branch manual checkout fallback * Remove mock errors * Code cleanup * Code cleanup * Code cleanup --------- Co-authored-by: Szabolcs Toth <[email protected]>
1 parent 6f23135 commit 93b66d4

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

gitclone/checkout.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
250250

251251
return checkoutPRMergeRef{
252252
params: *params,
253+
fallbackCheckout: func(gitCmd git.Git) error {
254+
log.Warnf("Using manual merge strategy with PR source branch")
255+
256+
manualMergeFallbackFetchOpts := selectFetchOptions(CheckoutPRManualMergeMethod, cfg.CloneDepth, cfg.FetchTags, cfg.UpdateSubmodules, len(cfg.SparseDirectories) != 0)
257+
manualMergeFallbackFallback := selectFallbacks(CheckoutPRManualMergeMethod, manualMergeFallbackFetchOpts)
258+
259+
prRepositoryURL := ""
260+
if isFork(cfg.RepositoryURL, cfg.PRSourceRepositoryURL) {
261+
prRepositoryURL = cfg.PRSourceRepositoryURL
262+
}
263+
264+
fallbackManualMergeWithSourceBranch, err := createManualMergeFallbackFunc(prRepositoryURL, cfg.Branch, cfg.Commit, cfg.PRDestBranch)
265+
if err != nil {
266+
return err
267+
}
268+
269+
// PR merge branch checkout falls back to PR manual merge strategy using the PR source branch
270+
return fallbackManualMergeWithSourceBranch.do(gitCmd, manualMergeFallbackFetchOpts, manualMergeFallbackFallback)
271+
},
253272
}, nil
254273
}
255274
case CheckoutPRDiffFileMethod:
@@ -429,3 +448,13 @@ func isPRCheckout(method CheckoutMethod) bool {
429448
panic(fmt.Sprintf("implementation missing for enum value %T", method))
430449
}
431450
}
451+
452+
func createManualMergeFallbackFunc(repositoryURL, branch, commit, prDestBranch string) (*checkoutPRManualMerge, error) {
453+
manualMergeFallbackParams, err := NewPRManualMergeParams(branch, commit, repositoryURL, prDestBranch)
454+
if err != nil {
455+
return nil, err
456+
}
457+
return &checkoutPRManualMerge{
458+
params: *manualMergeFallbackParams,
459+
}, nil
460+
}

gitclone/checkout_method_pr_auto_merge_branch.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/bitrise-io/go-utils/command/git"
8+
"github.com/bitrise-io/go-utils/log"
89
)
910

1011
// PRMergeRefParams are parameters to check out a Merge/Pull Request's merge ref (the result of merging the 2 branches)
@@ -33,10 +34,25 @@ func NewPRMergeRefParams(mergeRef, headRef string) (*PRMergeRefParams, error) {
3334
}
3435

3536
type checkoutPRMergeRef struct {
36-
params PRMergeRefParams
37+
params PRMergeRefParams
38+
fallbackCheckout fallbackCheckoutFunc
3739
}
3840

41+
type fallbackCheckoutFunc func(gitCmd git.Git) error
42+
3943
func (c checkoutPRMergeRef) do(gitCmd git.Git, fetchOpts fetchOptions, fallback fallbackRetry) error {
44+
if err := c.performCheckout(gitCmd, fetchOpts, fallback); err != nil {
45+
log.Warnf("Failed to checkout PR merge branch: %s", err)
46+
47+
if c.fallbackCheckout != nil {
48+
return c.fallbackCheckout(gitCmd)
49+
}
50+
return err
51+
}
52+
return nil
53+
}
54+
55+
func (c checkoutPRMergeRef) performCheckout(gitCmd git.Git, fetchOpts fetchOptions, fallback fallbackRetry) error {
4056
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
4157
refSpec := fmt.Sprintf("%s:%s", c.remoteMergeRef(), c.localMergeRef())
4258

gitclone/gitclone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (g GitCloner) CheckoutState(cfg Config) (CheckoutStateResult, error) {
138138
if !clean {
139139
g.logger.Println()
140140
g.logger.Warnf("Working tree is dirty, cleaning before checkout:")
141-
141+
142142
err = runner.Run(gitCmd.Clean("-fd"))
143143
if err != nil {
144144
g.logger.Warnf("Failed to clean untracked files: %s", err)

gitclone/gitclone_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ func Test_checkoutState(t *testing.T) {
507507
assert.Nil(t, actualErr)
508508
}
509509

510-
assert.Equal(t, tt.wantCmds, mockRunner.Cmds())
510+
gotCmds := mockRunner.Cmds()
511+
assert.Equal(t, tt.wantCmds, gotCmds)
511512
})
512513
}
513514
}

0 commit comments

Comments
 (0)