Skip to content

Commit

Permalink
Handle remote specific branches (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsayer13 authored Oct 22, 2020
1 parent 13f23b1 commit 9876195
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
38 changes: 24 additions & 14 deletions gitclone/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ func autoMerge(gitCmd git.Git, mergeBranch, branchDest, buildURL, apiToken strin
if depth != 0 {
opts = append(opts, "--depth="+strconv.Itoa(depth))
}
opts = append(opts, "origin", "refs/heads/"+branchDest)
opts = append(opts, defaultRemoteName, "refs/heads/"+branchDest)
return gitCmd.Fetch(opts...)
}); err != nil {
return fmt.Errorf("Fetch failed, error: %v", err)
}

if mergeBranch != "" {
if err := runWithRetry(func() *command.Model {
return gitCmd.Fetch("origin", fetchArg(mergeBranch))
return gitCmd.Fetch(defaultRemoteName, fetchArg(mergeBranch))
}); err != nil {
return fmt.Errorf("fetch Pull Request branch failed (%s), error: %v",
mergeBranch, err)
Expand Down Expand Up @@ -255,7 +255,7 @@ func autoMerge(gitCmd git.Git, mergeBranch, branchDest, buildURL, apiToken strin
}

func manualMerge(gitCmd git.Git, repoURL, prRepoURL, branch, commit, branchDest string) error {
if err := runWithRetry(func() *command.Model { return gitCmd.Fetch("origin", "refs/heads/"+branchDest) }); err != nil {
if err := runWithRetry(func() *command.Model { return gitCmd.Fetch(defaultRemoteName, "refs/heads/"+branchDest) }); err != nil {
return fmt.Errorf("fetch failed, error: %v", err)
}
if err := pull(gitCmd, branchDest); err != nil {
Expand All @@ -278,7 +278,7 @@ func manualMerge(gitCmd git.Git, repoURL, prRepoURL, branch, commit, branchDest
return fmt.Errorf("merge failed (fork/%s), error: %v", branch, err)
}
} else {
if err := run(gitCmd.Fetch("origin", "refs/heads/"+branch)); err != nil {
if err := run(gitCmd.Fetch(defaultRemoteName, "refs/heads/"+branch)); err != nil {
return fmt.Errorf("fetch failed, error: %v", err)
}
if err := run(gitCmd.Merge(commit)); err != nil {
Expand All @@ -289,17 +289,26 @@ func manualMerge(gitCmd git.Git, repoURL, prRepoURL, branch, commit, branchDest
return nil
}

func parseListBranchesOutput(output string) []string {
split := strings.Split(output, "\n")
var branches []string
for _, branch := range split {
branch = strings.Trim(branch, " ")
branches = append(branches, branch)
func parseListBranchesOutput(output string) map[string][]string {
lines := strings.Split(output, "\n")
branchesByRemote := map[string][]string {}
for _, line := range lines {
line = strings.Trim(line, " ")
split := strings.Split(line, "/")

remote := split[0]
branch := ""
if (len(split) > 1) {
branch = strings.Join(split[1:], "/")
branches := branchesByRemote[remote]
branches = append(branches, branch)
branchesByRemote[remote] = branches
}
}
return branches
return branchesByRemote
}

func listBranches(gitCmd git.Git) ([]string, error) {
func listBranches(gitCmd git.Git) (map[string][]string, error) {
if err := run(gitCmd.Fetch()); err != nil {
return nil, err
}
Expand All @@ -321,12 +330,13 @@ func checkout(gitCmd git.Git, arg, branch string, depth int, isTag bool) *step.E
opts = append(opts, "--tags")
}
if branch != "" {
opts = append(opts, "origin", "refs/heads/"+branch)
opts = append(opts, defaultRemoteName, "refs/heads/"+branch)
}
return gitCmd.Fetch(opts...)
}); err != nil {
if branch != "" {
branches, branchesErr := listBranches(gitCmd)
branchesByRemote, branchesErr := listBranches(gitCmd)
branches := branchesByRemote[defaultRemoteName]
if branchesErr == nil && !sliceutil.IsStringInSlice(branch, branches) {
return newStepErrorWithRecommendations(
"fetch_failed",
Expand Down
27 changes: 17 additions & 10 deletions gitclone/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,29 @@ func Test_parseListBranchesOutput(t *testing.T) {
tests := []struct {
name string
args string
want []string
want map[string][]string
}{
{
name: "single branch",
args: "master",
want: []string{"master"},
args: "upstream/master",
want: map[string][]string{
"upstream": {
"master",
},
},
},
{
name: "multiple branches",
args: `origin/bitrise-bot-1
origin/bitrise-bot-2
origin/bitrise-bot-3`,
want: []string{
"origin/bitrise-bot-1",
"origin/bitrise-bot-2",
"origin/bitrise-bot-3"},
args: `upstream/bitrise-bot-1
upstream/bitrise-bot-2
upstream/bitrise-bot-3`,
want: map[string][]string{
"upstream": {
"bitrise-bot-1",
"bitrise-bot-2",
"bitrise-bot-3",
},
},
},
}
for _, tt := range tests {
Expand Down
5 changes: 3 additions & 2 deletions gitclone/gitclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type Config struct {
}

const (
trimEnding = "..."
trimEnding = "..."
defaultRemoteName = "origin"
)

func printLogAndExportEnv(gitCmd git.Git, format, env string, maxEnvLength int) error {
Expand Down Expand Up @@ -110,7 +111,7 @@ func Execute(cfg Config) *step.Error {
)
}
if !originPresent {
if err := run(gitCmd.RemoteAdd("origin", cfg.RepositoryURL)); err != nil {
if err := run(gitCmd.RemoteAdd(defaultRemoteName, cfg.RepositoryURL)); err != nil {
return newStepError(
"add_remote_failed",
fmt.Errorf("adding remote repository failed (%s): %v", cfg.RepositoryURL, err),
Expand Down

0 comments on commit 9876195

Please sign in to comment.