Skip to content

Commit f62c12f

Browse files
committed
Fix remote origin branch config
fixes #349 commit-id:f9c27350
1 parent 23df1e2 commit f62c12f

File tree

9 files changed

+47
-61
lines changed

9 files changed

+47
-61
lines changed

config/config.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
)
1010

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

1817
// Config object to hold spr configuration
@@ -21,6 +20,9 @@ type RepoConfig struct {
2120
GitHubRepoName string `yaml:"githubRepoName"`
2221
GitHubHost string `default:"github.com" yaml:"githubHost"`
2322

23+
GitHubRemote string `default:"origin" yaml:"githubRemote"`
24+
GitHubBranch string `default:"main" yaml:"githubBranch"`
25+
2426
RequireChecks bool `default:"true" yaml:"requireChecks"`
2527
RequireApproval bool `default:"true" yaml:"requireApproval"`
2628

@@ -36,11 +38,6 @@ type RepoConfig struct {
3638
ForceFetchTags bool `default:"false" yaml:"forceFetchTags"`
3739
}
3840

39-
type InternalConfig struct {
40-
GitHubRemote string `default:"origin" yaml:"githubRemote"`
41-
GitHubBranch string `default:"main" yaml:"githubBranch"`
42-
}
43-
4441
type UserConfig struct {
4542
ShowPRLink bool `default:"true" yaml:"showPRLink"`
4643
LogGitCommands bool `default:"true" yaml:"logGitCommands"`
@@ -62,9 +59,8 @@ type InternalState struct {
6259

6360
func EmptyConfig() *Config {
6461
return &Config{
65-
Repo: &RepoConfig{},
66-
User: &UserConfig{},
67-
Internal: &InternalConfig{},
62+
Repo: &RepoConfig{},
63+
User: &UserConfig{},
6864
State: &InternalState{
6965
MergeCheckCommit: map[string]string{},
7066
},
@@ -79,9 +75,6 @@ func DefaultConfig() *Config {
7975
rake.LoadSources(cfg.User,
8076
rake.DefaultSource(),
8177
)
82-
rake.LoadSources(cfg.Internal,
83-
rake.DefaultSource(),
84-
)
8578

8679
cfg.User.LogGitCommands = false
8780
cfg.User.LogGitHubCalls = false

config/config_parser/config_parser.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
1818
rake.DefaultSource(),
1919
NewGitHubRemoteSource(cfg, gitcmd),
2020
rake.YamlFileSource(RepoConfigFilePath(gitcmd)),
21+
NewRemoteBranchSource(gitcmd),
2122
)
2223
if cfg.Repo.GitHubHost == "" {
2324
fmt.Println("unable to auto configure repository host - must be set manually in .spr.yml")
@@ -38,11 +39,6 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
3839
rake.YamlFileSource(UserConfigFilePath()),
3940
)
4041

41-
rake.LoadSources(cfg.Internal,
42-
rake.DefaultSource(),
43-
NewRemoteBranchSource(gitcmd),
44-
)
45-
4642
rake.LoadSources(cfg.State,
4743
rake.DefaultSource(),
4844
rake.YamlFileSource(InternalConfigFilePath()),

config/config_parser/remote_branch.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config_parser
22

33
import (
4-
"fmt"
54
"regexp"
65

76
"github.com/ejoffe/spr/config"
@@ -27,12 +26,11 @@ func (s *remoteBranch) Load(cfg interface{}) {
2726

2827
matches := _remoteBranchRegex.FindStringSubmatch(output)
2928
if matches == nil {
30-
fmt.Printf("error: unable to fetch remote branch info, using defaults")
3129
return
3230
}
3331

34-
internalCfg := cfg.(*config.InternalConfig)
32+
repoCfg := cfg.(*config.RepoConfig)
3533

36-
internalCfg.GitHubRemote = matches[2]
37-
internalCfg.GitHubBranch = matches[3]
34+
repoCfg.GitHubRemote = matches[2]
35+
repoCfg.GitHubBranch = matches[3]
3836
}

config/config_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99

1010
func TestEmptyConfig(t *testing.T) {
1111
expect := &Config{
12-
Repo: &RepoConfig{},
13-
User: &UserConfig{},
14-
Internal: &InternalConfig{},
12+
Repo: &RepoConfig{},
13+
User: &UserConfig{},
1514
State: &InternalState{
1615
MergeCheckCommit: map[string]string{},
1716
},
@@ -25,6 +24,8 @@ func TestDefaultConfig(t *testing.T) {
2524
Repo: &RepoConfig{
2625
GitHubRepoOwner: "",
2726
GitHubRepoName: "",
27+
GitHubRemote: "origin",
28+
GitHubBranch: "main",
2829
GitHubHost: "github.com",
2930
RequireChecks: true,
3031
RequireApproval: true,
@@ -40,10 +41,6 @@ func TestDefaultConfig(t *testing.T) {
4041
StatusBitsHeader: true,
4142
StatusBitsEmojis: true,
4243
},
43-
Internal: &InternalConfig{
44-
GitHubRemote: "origin",
45-
GitHubBranch: "main",
46-
},
4744
State: &InternalState{
4845
MergeCheckCommit: map[string]string{},
4946
},

git/helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func GetLocalBranchName(gitcmd GitInterface) string {
2525
}
2626

2727
func BranchNameFromCommit(cfg *config.Config, commit Commit) string {
28-
remoteBranchName := cfg.Internal.GitHubBranch
28+
remoteBranchName := cfg.Repo.GitHubBranch
2929
return "spr/" + remoteBranchName + "/" + commit.CommitID
3030
}
3131

@@ -48,15 +48,15 @@ func GetLocalTopCommit(cfg *config.Config, gitcmd GitInterface) *Commit {
4848
func GetLocalCommitStack(cfg *config.Config, gitcmd GitInterface) []Commit {
4949
var commitLog string
5050
logCommand := fmt.Sprintf("log --format=medium --no-color %s/%s..HEAD",
51-
cfg.Internal.GitHubRemote, cfg.Internal.GitHubBranch)
51+
cfg.Repo.GitHubRemote, cfg.Repo.GitHubBranch)
5252
gitcmd.MustGit(logCommand, &commitLog)
5353
commits, valid := parseLocalCommitStack(commitLog)
5454
if !valid {
5555
// if not valid - run rebase to add commit ids
5656
rewordPath, err := exec.LookPath("spr_reword_helper")
5757
check(err)
5858
rebaseCommand := fmt.Sprintf("rebase %s/%s -i --autosquash --autostash",
59-
cfg.Internal.GitHubRemote, cfg.Internal.GitHubBranch)
59+
cfg.Repo.GitHubRemote, cfg.Repo.GitHubBranch)
6060
gitcmd.GitWithEditor(rebaseCommand, nil, rewordPath)
6161

6262
gitcmd.MustGit(logCommand, &commitLog)

github/githubclient/client.go

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

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

188188
pullRequests := matchPullRequestStack(c.config.Repo, targetBranch, localCommitStack, resp.Viewer.PullRequests)
@@ -354,7 +354,7 @@ func (c *client) GetAssignableUsers(ctx context.Context) []github.RepoAssignee {
354354
func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
355355
info *github.GitHubInfo, commit git.Commit, prevCommit *git.Commit) *github.PullRequest {
356356

357-
baseRefName := c.config.Internal.GitHubBranch
357+
baseRefName := c.config.Repo.GitHubBranch
358358
if prevCommit != nil {
359359
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
360360
}
@@ -512,7 +512,7 @@ func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
512512
fmt.Printf("> github update %d : %s\n", pr.Number, pr.Title)
513513
}
514514

515-
baseRefName := c.config.Internal.GitHubBranch
515+
baseRefName := c.config.Repo.GitHubBranch
516516
if prevCommit != nil {
517517
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
518518
}

readme.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,33 @@ User specific configuration is saved to .spr.yml in the user home directory.
151151

152152
| Repository Config | Type | Default | Description |
153153
|-------------------------| ---- |------------|-----------------------------------------------------------------------------------|
154-
| requireChecks | bool | true | require checks to pass in order to merge |
155-
| requireApproval | bool | true | require pull request approval in order to merge |
156-
| githubRepoOwner | str | | name of the github owner (fetched from git remote config) |
157-
| githubRepoName | str | | name of the github repository (fetched from git remote config) |
158-
| githubHost | str | github.com | github host, can be updated for github enterprise use case |
159-
| mergeMethod | str | rebase | merge method, valid values: [rebase, squash, merge] |
154+
| requireChecks | bool | true | require checks to pass in order to merge |
155+
| requireApproval | bool | true | require pull request approval in order to merge |
156+
| githubRepoOwner | str | | name of the github owner (fetched from git remote config) |
157+
| 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 |
160+
| githubHost | str | github.com | github host, can be updated for github enterprise use case |
161+
| mergeMethod | str | rebase | merge method, valid values: [rebase, squash, merge] |
160162
| mergeQueue | bool | false | use GitHub merge queue to merge pull requests |
161163
| prTemplatePath | str | | path to PR template (e.g. .github/PULL_REQUEST_TEMPLATE/pull_request_template.md) |
162-
| prTemplateInsertStart | str | | text to search for in PR template that determines body insert start location |
163-
| prTemplateInsertEnd | str | | text to search for in PR template that determines body insert end location |
164+
| prTemplateInsertStart | str | | text to search for in PR template that determines body insert start location |
165+
| prTemplateInsertEnd | str | | text to search for in PR template that determines body insert end location |
164166
| mergeCheck | str | | enforce a pre-merge check using 'git spr check' |
165167
| forceFetchTags | bool | false | also fetch tags when running 'git spr update' |
166168
| branchNameIncludeTarget | bool | false | include target branch name in pull request branch name |
167169

168170

169-
| User Config | Type | Default | Description |
170-
| -------------------- | ---- | ------- | ----------------------------------------------------------------- |
171-
| showPRLink | bool | true | show full pull request http link |
172-
| logGitCommands | bool | true | logs all git commands to stdout |
173-
| logGitHubCalls | bool | true | logs all github api calls to stdout |
174-
| statusBitsHeader | bool | true | show status bits type headers |
175-
| statusBitsEmojis | bool | true | show status bits using fancy emojis |
176-
| createDraftPRs | bool | false | new pull requests are created as draft |
177-
| preserveTitleAndBody | bool | false | updating pull requests will not overwrite the pr title and body |
178-
| noRebase | bool | false | when true spr update will not rebase on top of origin |
171+
| User Config | Type | Default | Description |
172+
| -------------------- | ---- | ------- | --------------------------------------------------------------- |
173+
| showPRLink | bool | true | show full pull request http link |
174+
| logGitCommands | bool | true | logs all git commands to stdout |
175+
| logGitHubCalls | bool | true | logs all github api calls to stdout |
176+
| statusBitsHeader | bool | true | show status bits type headers |
177+
| statusBitsEmojis | bool | true | show status bits using fancy emojis |
178+
| createDraftPRs | bool | false | new pull requests are created as draft |
179+
| preserveTitleAndBody | bool | false | updating pull requests will not overwrite the pr title and body |
180+
| noRebase | bool | false | when true spr update will not rebase on top of origin |
179181

180182
Happy Coding!
181183
-------------

spr/spr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (sd *stackediff) AmendCommit(ctx context.Context) {
8181
sd.gitcmd.MustGit("commit --fixup "+localCommits[commitIndex].CommitHash, nil)
8282

8383
rebaseCmd := fmt.Sprintf("rebase -i --autosquash --autostash %s/%s",
84-
sd.config.Internal.GitHubRemote, sd.config.Internal.GitHubBranch)
84+
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
8585
sd.gitcmd.MustGit(rebaseCmd, nil)
8686
}
8787

@@ -482,7 +482,7 @@ func (sd *stackediff) fetchAndGetGitHubInfo(ctx context.Context) *github.GitHubI
482482
sd.gitcmd.MustGit("fetch", nil)
483483
}
484484
rebaseCommand := fmt.Sprintf("rebase %s/%s --autostash",
485-
sd.config.Internal.GitHubRemote, sd.config.Internal.GitHubBranch)
485+
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
486486
err := sd.gitcmd.Git(rebaseCommand, nil)
487487
if err != nil {
488488
return nil
@@ -544,7 +544,7 @@ func (sd *stackediff) syncCommitStackToGitHub(ctx context.Context,
544544
commit.CommitHash+":refs/heads/"+branchName)
545545
}
546546
if len(updatedCommits) > 0 {
547-
pushCommand := fmt.Sprintf("push --force --atomic %s ", sd.config.Internal.GitHubRemote)
547+
pushCommand := fmt.Sprintf("push --force --atomic %s ", sd.config.Repo.GitHubRemote)
548548
pushCommand += strings.Join(refNames, " ")
549549
sd.gitcmd.MustGit(pushCommand, nil)
550550
}

spr/spr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func makeTestObjects(t *testing.T) (
2222
cfg := config.EmptyConfig()
2323
cfg.Repo.RequireChecks = true
2424
cfg.Repo.RequireApproval = true
25-
cfg.Internal.GitHubRemote = "origin"
26-
cfg.Internal.GitHubBranch = "master"
25+
cfg.Repo.GitHubRemote = "origin"
26+
cfg.Repo.GitHubBranch = "master"
2727
cfg.Repo.MergeMethod = "rebase"
2828
gitmock = mockgit.NewMockGit(t)
2929
githubmock = mockclient.NewMockClient(t)

0 commit comments

Comments
 (0)