Skip to content

Commit

Permalink
fix hardcoded master branch references to use config.Repo.GitHubBranch
Browse files Browse the repository at this point in the history
fixes #110

commit-id:fb291bb8
  • Loading branch information
ejoffe committed Oct 15, 2021
1 parent 38576f6 commit 94190ea
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G
func (c *client) CreatePullRequest(ctx context.Context,
info *github.GitHubInfo, commit git.Commit, prevCommit *git.Commit) *github.PullRequest {

baseRefName := "master"
baseRefName := c.config.Repo.GitHubBranch
if prevCommit != nil {
baseRefName = branchNameFromCommit(info, *prevCommit)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func (c *client) UpdatePullRequest(ctx context.Context,
fmt.Printf("> github update %d - %s\n", pr.Number, pr.Title)
}

baseRefName := "master"
baseRefName := c.config.Repo.GitHubBranch
if prevCommit != nil {
baseRefName = branchNameFromCommit(info, *prevCommit)
}
Expand Down
4 changes: 2 additions & 2 deletions github/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type PullRequestMergeStatus struct {
}

// SortPullRequests sorts the pull requests so that the one that is on top of
// master will come first followed by the ones that are stacked on top.
// the target branch will come first followed by the ones that are stacked on top.
// The stack order is maintained so that multiple pull requests can be merged in
// the correct order.
func SortPullRequests(prs []*PullRequest, config *config.Config) []*PullRequest {
Expand All @@ -65,7 +65,7 @@ func SortPullRequests(prs []*PullRequest, config *config.Config) []*PullRequest
prs[j] = buf
}

targetBranch := "master"
targetBranch := config.Repo.GitHubBranch
j := 0
for i := 0; i < len(prs); i++ {
for j = i; j < len(prs); j++ {
Expand Down
4 changes: 2 additions & 2 deletions github/pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSortPullRequests(t *testing.T) {
},
}

config := config.EmptyConfig()
config := config.DefaultConfig()
prs = SortPullRequests(prs, config)
if prs[0].Number != 1 {
t.Fatalf("prs not sorted correctly %v\n", prs)
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestSortPullRequestsMixed(t *testing.T) {
},
}

config := config.EmptyConfig()
config := config.DefaultConfig()
prs = SortPullRequests(prs, config)
if prs[0].Number != 1 {
t.Fatalf("prs not sorted correctly %v\n", prs)
Expand Down
11 changes: 8 additions & 3 deletions hook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"os/exec"
"strings"

"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git"
)

const (
hookPath = ".git/hooks/commit-msg"
)

func InstallCommitHook(gitcmd git.GitInterface) {
func InstallCommitHook(cfg *config.Config, gitcmd git.GitInterface) {
var rootdir string
err := gitcmd.Git("rev-parse --show-toplevel", &rootdir)
check(err)
Expand All @@ -33,7 +34,9 @@ func InstallCommitHook(gitcmd git.GitInterface) {
// amend commit stack to add commit-id
rewordPath, err := exec.LookPath("spr_reword_helper")
check(err)
gitcmd.GitWithEditor("rebase origin/master -i --autosquash --autostash", nil, rewordPath)
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
cfg.Repo.GitHubRemote, cfg.Repo.GitHubBranch)
gitcmd.GitWithEditor(rebaseCommand, nil, rewordPath)
} else {
binPath, err := exec.LookPath("spr_commit_hook")
check(err)
Expand All @@ -43,6 +46,8 @@ func InstallCommitHook(gitcmd git.GitInterface) {
// amend commit stack to add commit-id
rewordPath, err := exec.LookPath("spr_reword_helper")
check(err)
gitcmd.GitWithEditor("rebase origin/master -i --autosquash --autostash", nil, rewordPath)
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
cfg.Repo.GitHubRemote, cfg.Repo.GitHubBranch)
gitcmd.GitWithEditor(rebaseCommand, nil, rewordPath)
}
}
11 changes: 7 additions & 4 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context) {
if commitsReordered(localCommits, githubInfo.PullRequests) {
reorder = true
// if commits have been reordered :
// first - rebase all pull requests to master
// first - rebase all pull requests to target branch
// then - update all pull requests
for _, pr := range githubInfo.PullRequests {
sd.github.UpdatePullRequest(ctx, githubInfo, pr, pr.Commit, nil)
Expand Down Expand Up @@ -176,7 +176,7 @@ func (sd *stackediff) MergePullRequests(ctx context.Context) {
}
prToMerge := githubInfo.PullRequests[prIndex]

// Update the base of the merging pr to master
// Update the base of the merging pr to target branch
sd.github.UpdatePullRequest(ctx, githubInfo, prToMerge, prToMerge.Commit, nil)
sd.profiletimer.Step("MergePullRequests::update pr base")

Expand Down Expand Up @@ -248,11 +248,14 @@ func (sd *stackediff) getLocalCommitStack() []git.Commit {
if !valid {
// if not valid - it means commit hook was not installed
// install commit-hook and try again
hook.InstallCommitHook(sd.gitcmd)
hook.InstallCommitHook(sd.config, sd.gitcmd)
sd.mustgit(logCommand, &commitLog)
commits, valid = sd.parseLocalCommitStack(commitLog)
if !valid {
panic("unable to fetch local commits")
errMsg := "unable to fetch local commits\n"
errMsg += " most likely this is an issue with missing commit-id in the commit body\n"
errMsg += " which is caused by the commit-msg hook not being installed propertly\n"
panic(errMsg)
}
}
return commits
Expand Down

0 comments on commit 94190ea

Please sign in to comment.