Skip to content

Commit

Permalink
Add warning when running spr on remote pr branches
Browse files Browse the repository at this point in the history
Fixes #96

commit-id:9a1dd589
  • Loading branch information
ejoffe authored and Eitan Joffe committed Dec 6, 2021
1 parent 0b38aa3 commit cf48807
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type client struct {
api *githubv4.Client
}

var pullRequestRegex = regexp.MustCompile(`pr/[a-zA-Z0-9_\-]+/([a-zA-Z0-9_\-/]+)/([a-f0-9]{8})$`)
var BranchNameRegex = regexp.MustCompile(`pr/[a-zA-Z0-9_\-]+/([a-zA-Z0-9_\-/]+)/([a-f0-9]{8})$`)

func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.GitHubInfo {
if c.config.User.LogGitHubCalls {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G
ToBranch: node.BaseRefName,
}

matches := pullRequestRegex.FindStringSubmatch(node.HeadRefName)
matches := BranchNameRegex.FindStringSubmatch(node.HeadRefName)
if matches != nil && matches[1] == branchname {
pullRequest.Commit = git.Commit{
CommitID: matches[2],
Expand Down
2 changes: 1 addition & 1 deletion github/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestPullRequestRegex(t *testing.T) {
}

for _, tc := range tests {
matches := pullRequestRegex.FindStringSubmatch(tc.input)
matches := BranchNameRegex.FindStringSubmatch(tc.input)
if tc.branch != matches[1] {
t.Fatalf("expected: '%v', actual: '%v'", tc.branch, matches[1])
}
Expand Down
12 changes: 10 additions & 2 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git"
"github.com/ejoffe/spr/github"
"github.com/ejoffe/spr/github/githubclient"
"github.com/ejoffe/spr/hook"
)

Expand Down Expand Up @@ -381,13 +382,20 @@ func (sd *stackediff) fetchAndGetGitHubInfo(ctx context.Context) *github.GitHubI
sd.mustgit("fetch", nil)
rebaseCommand := fmt.Sprintf("rebase %s/%s --autostash",
sd.config.Repo.GitHubRemote, sd.config.Repo.GitHubBranch)
//var output string
err := sd.gitcmd.Git(rebaseCommand, nil)
if err != nil {
return nil
}

info := sd.github.GetInfo(ctx, sd.gitcmd)
if githubclient.BranchNameRegex.FindString(info.LocalBranch) != "" {
fmt.Printf("error: don't run spr in a remote pr branch\n")
fmt.Printf(" this could lead to weird duplicate pull requests getting created\n")
fmt.Printf(" in general there is no need to checkout remote branches used for prs\n")
fmt.Printf(" instead use local branches and run spr update to sync your commit stack\n")
fmt.Printf(" with your pull requests on github\n")
fmt.Printf("branch name: %s\n", info.LocalBranch)
return nil
}

return info
}
Expand Down

0 comments on commit cf48807

Please sign in to comment.