Skip to content

Commit 5c50650

Browse files
committed
Remove GitHubBranch and GitHubOrigin from config - now fetched automatically using git status
fixes #320 commit-id:3f943623
1 parent 1d6b6a4 commit 5c50650

12 files changed

+99
-75
lines changed

config/config.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type Config struct {
12-
Repo *RepoConfig
13-
User *UserConfig
14-
State *InternalState
12+
Repo *RepoConfig
13+
User *UserConfig
14+
Internal *InternalConfig
15+
State *InternalState
1516
}
1617

1718
// Config object to hold spr configuration
@@ -23,10 +24,6 @@ type RepoConfig struct {
2324
RequireChecks bool `default:"true" yaml:"requireChecks"`
2425
RequireApproval bool `default:"true" yaml:"requireApproval"`
2526

26-
GitHubRemote string `default:"origin" yaml:"githubRemote"`
27-
GitHubBranch string `default:"master" yaml:"githubBranch"`
28-
RemoteBranches []string `yaml:"remoteBranches"`
29-
3027
MergeMethod string `default:"rebase" yaml:"mergeMethod"`
3128
MergeQueue bool `default:"false" yaml:"mergeQueue"`
3229

@@ -41,6 +38,11 @@ type RepoConfig struct {
4138
BranchNameIncludeTarget bool `default:"false" yaml:"branchNameIncludeTarget"`
4239
}
4340

41+
type InternalConfig struct {
42+
GitHubRemote string `default:"origin" yaml:"githubRemote"`
43+
GitHubBranch string `default:"main" yaml:"githubBranch"`
44+
}
45+
4446
type UserConfig struct {
4547
ShowPRLink bool `default:"true" yaml:"showPRLink"`
4648
LogGitCommands bool `default:"true" yaml:"logGitCommands"`
@@ -62,8 +64,9 @@ type InternalState struct {
6264

6365
func EmptyConfig() *Config {
6466
return &Config{
65-
Repo: &RepoConfig{},
66-
User: &UserConfig{},
67+
Repo: &RepoConfig{},
68+
User: &UserConfig{},
69+
Internal: &InternalConfig{},
6770
State: &InternalState{
6871
MergeCheckCommit: map[string]string{},
6972
},
@@ -78,6 +81,9 @@ func DefaultConfig() *Config {
7881
rake.LoadSources(cfg.User,
7982
rake.DefaultSource(),
8083
)
84+
rake.LoadSources(cfg.Internal,
85+
rake.DefaultSource(),
86+
)
8187

8288
cfg.User.LogGitCommands = false
8389
cfg.User.LogGitHubCalls = false
@@ -102,9 +108,3 @@ func (c Config) MergeMethod() (genclient.PullRequestMergeMethod, error) {
102108
}
103109
return mergeMethod, err
104110
}
105-
106-
func check(err error) {
107-
if err != nil {
108-
panic(err)
109-
}
110-
}

config/config_parser/config_parser.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
1616

1717
rake.LoadSources(cfg.Repo,
1818
rake.DefaultSource(),
19-
GitHubRemoteSource(cfg, gitcmd),
19+
NewGitHubRemoteSource(cfg, gitcmd),
2020
rake.YamlFileSource(RepoConfigFilePath(gitcmd)),
2121
rake.YamlFileWriter(RepoConfigFilePath(gitcmd)),
2222
)
@@ -39,6 +39,11 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
3939
rake.YamlFileSource(UserConfigFilePath()),
4040
)
4141

42+
rake.LoadSources(cfg.Internal,
43+
rake.DefaultSource(),
44+
NewRemoteBranchSource(gitcmd),
45+
)
46+
4247
rake.LoadSources(cfg.State,
4348
rake.DefaultSource(),
4449
rake.YamlFileSource(InternalConfigFilePath()),
@@ -74,10 +79,3 @@ func InternalConfigFilePath() string {
7479
filepath := filepath.Clean(path.Join(rootdir, ".spr.state"))
7580
return filepath
7681
}
77-
78-
func GitHubRemoteSource(config *config.Config, gitcmd git.GitInterface) *remoteSource {
79-
return &remoteSource{
80-
gitcmd: gitcmd,
81-
config: config,
82-
}
83-
}

config/config_parser/config_parser_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ func TestGitHubRemoteSource(t *testing.T) {
6565
GitHubHost: "github.com",
6666
RequireChecks: false,
6767
RequireApproval: false,
68-
GitHubRemote: "",
69-
GitHubBranch: "",
7068
MergeMethod: "",
7169
},
7270
User: &config.UserConfig{
@@ -81,7 +79,7 @@ func TestGitHubRemoteSource(t *testing.T) {
8179
Repo: &config.RepoConfig{},
8280
User: &config.UserConfig{},
8381
}
84-
source := GitHubRemoteSource(&actual, mock)
82+
source := NewGitHubRemoteSource(&actual, mock)
8583
source.Load(nil)
8684
assert.Equal(t, expect, actual)
8785
}

config/config_parser/remote_branch.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package config_parser
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
"github.com/ejoffe/spr/config"
8+
"github.com/ejoffe/spr/git"
9+
)
10+
11+
type remoteBranch struct {
12+
gitcmd git.GitInterface
13+
}
14+
15+
func NewRemoteBranchSource(gitcmd git.GitInterface) *remoteBranch {
16+
return &remoteBranch{
17+
gitcmd: gitcmd,
18+
}
19+
}
20+
21+
var _remoteBranchRegex = regexp.MustCompile(`^## ([a-zA-Z0-9_\-/\.]+)\.\.\.([a-zA-Z0-9_\-/\.]+)/([a-zA-Z0-9_\-/\.]+)`)
22+
23+
func (s *remoteBranch) Load(cfg interface{}) {
24+
var output string
25+
err := s.gitcmd.Git("status -b --porcelain -u no", &output)
26+
check(err)
27+
28+
matches := _remoteBranchRegex.FindStringSubmatch(output)
29+
if matches == nil {
30+
fmt.Printf("error: unable to fetch remote branch info, using defaults")
31+
return
32+
}
33+
34+
internalCfg := cfg.(*config.InternalConfig)
35+
36+
internalCfg.GitHubRemote = matches[2]
37+
internalCfg.GitHubBranch = matches[3]
38+
}

config/config_parser/remote_source.go

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ type remoteSource struct {
1414
config *config.Config
1515
}
1616

17+
func NewGitHubRemoteSource(config *config.Config, gitcmd git.GitInterface) *remoteSource {
18+
return &remoteSource{
19+
gitcmd: gitcmd,
20+
config: config,
21+
}
22+
}
23+
1724
func (s *remoteSource) Load(_ interface{}) {
1825
var output string
1926
err := s.gitcmd.Git("remote -v", &output)

config/config_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99

1010
func TestEmptyConfig(t *testing.T) {
1111
expect := &Config{
12-
Repo: &RepoConfig{},
13-
User: &UserConfig{},
12+
Repo: &RepoConfig{},
13+
User: &UserConfig{},
14+
Internal: &InternalConfig{},
1415
State: &InternalState{
1516
MergeCheckCommit: map[string]string{},
1617
},
@@ -27,8 +28,6 @@ func TestDefaultConfig(t *testing.T) {
2728
GitHubHost: "github.com",
2829
RequireChecks: true,
2930
RequireApproval: true,
30-
GitHubRemote: "origin",
31-
GitHubBranch: "master",
3231
MergeMethod: "rebase",
3332
PRTemplatePath: "",
3433
PRTemplateInsertStart: "",
@@ -41,6 +40,10 @@ func TestDefaultConfig(t *testing.T) {
4140
StatusBitsHeader: true,
4241
StatusBitsEmojis: true,
4342
},
43+
Internal: &InternalConfig{
44+
GitHubRemote: "origin",
45+
GitHubBranch: "main",
46+
},
4447
State: &InternalState{
4548
MergeCheckCommit: map[string]string{},
4649
},

git/helpers.go

+8-21
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func GetLocalBranchName(gitcmd GitInterface) string {
2424
panic("cannot determine local git branch name")
2525
}
2626

27-
func BranchNameFromCommit(repoConfig *config.RepoConfig, gitcmd GitInterface, commit Commit) string {
28-
if repoConfig.BranchNameIncludeTarget {
29-
remoteBranchName := GetRemoteBranchName(repoConfig, gitcmd)
27+
func BranchNameFromCommit(cfg *config.Config, gitcmd GitInterface, commit Commit) string {
28+
if cfg.Repo.BranchNameIncludeTarget {
29+
remoteBranchName := cfg.Internal.GitHubBranch
3030
return "spr/" + remoteBranchName + "/" + commit.CommitID
3131
}
3232

@@ -44,23 +44,11 @@ func BranchNameRegex(repoConfig *config.RepoConfig) *regexp.Regexp {
4444
return _branchNameRegex
4545
}
4646

47-
// GetRemoteBranchName
48-
func GetRemoteBranchName(repoConfig *config.RepoConfig, gitcmd GitInterface) string {
49-
localBranchName := GetLocalBranchName(gitcmd)
50-
51-
for _, remoteBranchName := range repoConfig.RemoteBranches {
52-
if localBranchName == remoteBranchName {
53-
return remoteBranchName
54-
}
55-
}
56-
return repoConfig.GitHubBranch
57-
}
58-
5947
// GetLocalTopCommit returns the top unmerged commit in the stack
6048
//
6149
// return nil if there are no unmerged commits in the stack
62-
func GetLocalTopCommit(repoConfig *config.RepoConfig, gitcmd GitInterface) *Commit {
63-
commits := GetLocalCommitStack(repoConfig, gitcmd)
50+
func GetLocalTopCommit(cfg *config.Config, gitcmd GitInterface) *Commit {
51+
commits := GetLocalCommitStack(cfg, gitcmd)
6452
if len(commits) == 0 {
6553
return nil
6654
}
@@ -70,19 +58,18 @@ func GetLocalTopCommit(repoConfig *config.RepoConfig, gitcmd GitInterface) *Comm
7058
// GetLocalCommitStack returns a list of unmerged commits
7159
//
7260
// the list is ordered with the bottom commit in the stack first
73-
func GetLocalCommitStack(repoConfig *config.RepoConfig, gitcmd GitInterface) []Commit {
61+
func GetLocalCommitStack(cfg *config.Config, gitcmd GitInterface) []Commit {
7462
var commitLog string
75-
targetBranch := GetRemoteBranchName(repoConfig, gitcmd)
7663
logCommand := fmt.Sprintf("log --format=medium --no-color %s/%s..HEAD",
77-
repoConfig.GitHubRemote, targetBranch)
64+
cfg.Internal.GitHubRemote, cfg.Internal.GitHubBranch)
7865
gitcmd.MustGit(logCommand, &commitLog)
7966
commits, valid := parseLocalCommitStack(commitLog)
8067
if !valid {
8168
// if not valid - run rebase to add commit ids
8269
rewordPath, err := exec.LookPath("spr_reword_helper")
8370
check(err)
8471
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
85-
repoConfig.GitHubRemote, targetBranch)
72+
cfg.Internal.GitHubRemote, cfg.Internal.GitHubBranch)
8673
gitcmd.GitWithEditor(rebaseCommand, nil, rewordPath)
8774

8875
gitcmd.MustGit(logCommand, &commitLog)

git/mockgit/mockgit.go

-3
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,10 @@ type responder interface {
6666

6767
func (m *Mock) ExpectFetch() {
6868
m.expect("git fetch")
69-
m.expect("git branch --no-color").respond("* master")
7069
m.expect("git rebase origin/master --autostash")
7170
}
7271

7372
func (m *Mock) ExpectLogAndRespond(commits []*git.Commit) {
74-
m.expect("git branch --no-color").respond("* master")
7573
m.expect("git log --format=medium --no-color origin/master..HEAD").commitRespond(commits)
7674
}
7775

@@ -94,7 +92,6 @@ func (m *Mock) ExpectRemote(remote string) {
9492

9593
func (m *Mock) ExpectFixup(commitHash string) {
9694
m.expect("git commit --fixup " + commitHash)
97-
m.expect("git branch --no-color").respond("* master")
9895
m.expect("git rebase -i --autosquash --autostash origin/master")
9996
}
10097

github/githubclient/client.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G
182182
c.config.Repo.GitHubRepoName)
183183
check(err)
184184

185-
targetBranch := git.GetRemoteBranchName(c.config.Repo, gitcmd)
186-
localCommitStack := git.GetLocalCommitStack(c.config.Repo, gitcmd)
185+
targetBranch := c.config.Internal.GitHubBranch
186+
localCommitStack := git.GetLocalCommitStack(c.config, gitcmd)
187187

188188
pullRequests := matchPullRequestStack(c.config.Repo, targetBranch, localCommitStack, resp.Repository.PullRequests)
189189
for _, pr := range pullRequests {
@@ -340,11 +340,11 @@ func (c *client) GetAssignableUsers(ctx context.Context) []github.RepoAssignee {
340340
func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
341341
info *github.GitHubInfo, commit git.Commit, prevCommit *git.Commit) *github.PullRequest {
342342

343-
baseRefName := git.GetRemoteBranchName(c.config.Repo, gitcmd)
343+
baseRefName := c.config.Internal.GitHubBranch
344344
if prevCommit != nil {
345-
baseRefName = git.BranchNameFromCommit(c.config.Repo, gitcmd, *prevCommit)
345+
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
346346
}
347-
headRefName := git.BranchNameFromCommit(c.config.Repo, gitcmd, commit)
347+
headRefName := git.BranchNameFromCommit(c.config, gitcmd, commit)
348348

349349
log.Debug().Interface("Commit", commit).
350350
Str("FromBranch", headRefName).Str("ToBranch", baseRefName).
@@ -498,9 +498,9 @@ func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
498498
fmt.Printf("> github update %d : %s\n", pr.Number, pr.Title)
499499
}
500500

501-
baseRefName := git.GetRemoteBranchName(c.config.Repo, gitcmd)
501+
baseRefName := c.config.Internal.GitHubBranch
502502
if prevCommit != nil {
503-
baseRefName = git.BranchNameFromCommit(c.config.Repo, gitcmd, *prevCommit)
503+
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
504504
}
505505

506506
log.Debug().Interface("Commit", commit).

readme.md

-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ User specific configuration is saved to .spr.yml in the user home directory.
155155
| requireApproval | bool | true | require pull request approval in order to merge |
156156
| githubRepoOwner | str | | name of the github owner (fetched from git remote config) |
157157
| githubRepoName | str | | name of the github repository (fetched from git remote config) |
158-
| githubRemote | str | origin | github remote name to use |
159-
| githubBranch | str | master | github branch for pull request target |
160158
| githubHost | str | github.com | github host, can be updated for github enterprise use case |
161159
| mergeMethod | str | rebase | merge method, valid values: [rebase, squash, merge] |
162160
| mergeQueue | bool | false | use GitHub merge queue to merge pull requests |

0 commit comments

Comments
 (0)