Skip to content

Commit

Permalink
Repo config for github remote and branch name
Browse files Browse the repository at this point in the history
Fixes: #88

commit-id:0d6fa5d9
  • Loading branch information
ejoffe committed Sep 26, 2021
1 parent 21b4136 commit ac0b650
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type RepoConfig struct {

RequireChecks bool `default:"true" yaml:"requireChecks"`
RequireApproval bool `default:"true" yaml:"requireApproval"`

GitHubRemote string `default:"origin" yaml:"githubRemote"`
GitHubBranch string `default:"master" yaml:"githubBranch"`
}

type UserConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ User specific configuration is saved to .spr.yml in the user home directory.
| githubRepoName | str | | name of the github repository (fetched from git remote config) |
| requireChecks | bool | true | require checks to pass in order to merge |
| requireApproval | bool | true | require pull request approval in order to merge |
| githubRemote | str | origin | github remote name to use |
| githubBranch | str | master | github branch for pull request target |

| User Config | Type | Default | Description |
| ------------------- | ---- | ------- | -------------------------------------------------------------- |
Expand Down
24 changes: 16 additions & 8 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (sd *stackediff) AmendCommit(ctx context.Context) {
commitIndex = commitIndex - 1
check(err)
sd.mustgit("commit --fixup "+localCommits[commitIndex].CommitHash, nil)
sd.mustgit("rebase origin/master -i --autosquash --autostash", nil)
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
sd.mustgit(rebaseCommand, nil)
}

// UpdatePullRequests implements a stacked diff workflow on top of github.
Expand Down Expand Up @@ -183,7 +185,7 @@ func (sd *stackediff) MergePullRequests(ctx context.Context) {
sd.profiletimer.Step("MergePullRequests::merge pr")

if sd.config.User.CleanupRemoteBranch {
sd.gitcmd.Git(fmt.Sprintf("push -d origin %s", prToMerge.FromBranch), nil)
sd.gitcmd.Git(fmt.Sprintf("push -d %s %s", sd.config.Repo.GitHubRemote, prToMerge.FromBranch), nil)
}

// Close all the pull requests in the stack below the merged pr
Expand All @@ -198,7 +200,7 @@ func (sd *stackediff) MergePullRequests(ctx context.Context) {
sd.github.ClosePullRequest(ctx, pr)

if sd.config.User.CleanupRemoteBranch {
sd.gitcmd.Git(fmt.Sprintf("push -d origin %s", pr.FromBranch), nil)
sd.gitcmd.Git(fmt.Sprintf("push -d %s %s", sd.config.Repo.GitHubRemote, pr.FromBranch), nil)
}
}
sd.profiletimer.Step("MergePullRequests::close prs")
Expand Down Expand Up @@ -247,13 +249,15 @@ func (sd *stackediff) ProfilingSummary() {
// getLocalCommitStack returns a list of unmerged commits
func (sd *stackediff) getLocalCommitStack() []git.Commit {
var commitLog string
sd.mustgit("log origin/master..HEAD", &commitLog)
logCommand := fmt.Sprintf("log %s/%s..HEAD",
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
sd.mustgit(logCommand, &commitLog)
commits, valid := sd.parseLocalCommitStack(commitLog)
if !valid {
// if not valid - it means commit hook was not installed
// install commit-hook and try again
hook.InstallCommitHook(sd.gitcmd)
sd.mustgit("log origin/master..HEAD", &commitLog)
sd.mustgit(logCommand, &commitLog)
commits, valid = sd.parseLocalCommitStack(commitLog)
if !valid {
panic("unable to fetch local commits")
Expand Down Expand Up @@ -356,7 +360,9 @@ func (sd *stackediff) fetchAndGetGitHubInfo(ctx context.Context) *github.GitHubI

fetch := func() {
sd.mustgit("fetch", nil)
sd.mustgit("rebase origin/master --autostash", nil)
rebaseCommand := fmt.Sprintf("rebase %s/%s --autostash",
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
sd.mustgit(rebaseCommand, nil)
waitgroup.Done()
}

Expand All @@ -368,7 +374,7 @@ func (sd *stackediff) fetchAndGetGitHubInfo(ctx context.Context) *github.GitHubI
}

// syncCommitStackToGitHub gets all the local commits in the given branch
// which are new (on top of origin/master) and creates a corresponding
// which are new (on top of remote branch) and creates a corresponding
// branch on github for each commit.
func (sd *stackediff) syncCommitStackToGitHub(ctx context.Context,
commits []git.Commit, info *github.GitHubInfo) {
Expand Down Expand Up @@ -406,7 +412,9 @@ func (sd *stackediff) syncCommitStackToGitHub(ctx context.Context,
commit.CommitHash+":refs/heads/"+branchName)
}
if len(updatedCommits) > 0 {
sd.mustgit("push --force --atomic origin "+strings.Join(refNames, " "), nil)
pushCommand := fmt.Sprintf("push --force --atomic %s ", sd.config.Repo.GitHubRemote)
pushCommand += strings.Join(refNames, " ")
sd.mustgit(pushCommand, nil)
}
sd.profiletimer.Step("SyncCommitStack::PushBranches")
}
Expand Down
6 changes: 6 additions & 0 deletions spr/spr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestSPRBasicFlowFourCommits(t *testing.T) {
cfg := config.EmptyConfig()
cfg.Repo.RequireChecks = true
cfg.Repo.RequireApproval = true
cfg.Repo.GitHubRemote = "origin"
cfg.Repo.GitHubBranch = "master"
gitmock := mockgit.NewMockGit(t)
githubmock := mockclient.NewMockClient(t)
githubmock.Info = &github.GitHubInfo{
Expand Down Expand Up @@ -128,6 +130,8 @@ func TestSPRAmendCommit(t *testing.T) {
cfg := config.EmptyConfig()
cfg.Repo.RequireChecks = true
cfg.Repo.RequireApproval = true
cfg.Repo.GitHubRemote = "origin"
cfg.Repo.GitHubBranch = "master"
gitmock := mockgit.NewMockGit(t)
githubmock := mockclient.NewMockClient(t)
githubmock.Info = &github.GitHubInfo{
Expand Down Expand Up @@ -227,6 +231,8 @@ func TestSPRReorderCommit(t *testing.T) {
cfg := config.EmptyConfig()
cfg.Repo.RequireChecks = true
cfg.Repo.RequireApproval = true
cfg.Repo.GitHubRemote = "origin"
cfg.Repo.GitHubBranch = "master"
gitmock := mockgit.NewMockGit(t)
githubmock := mockclient.NewMockClient(t)
githubmock.Info = &github.GitHubInfo{
Expand Down

0 comments on commit ac0b650

Please sign in to comment.