diff --git a/github/githubclient/client.go b/github/githubclient/client.go index c2e2e98..e353d47 100644 --- a/github/githubclient/client.go +++ b/github/githubclient/client.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "strings" "github.com/ejoffe/spr/config" "github.com/ejoffe/spr/git" @@ -87,9 +88,7 @@ func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.G err := c.api.Query(ctx, &query, variables) check(err) - var branchname string - err = gitcmd.Git("branch --show-current", &branchname) - check(err) + branchname := getLocalBranchName(gitcmd) var requests []*github.PullRequest for _, node := range query.Viewer.PullRequests.Nodes { @@ -325,6 +324,19 @@ func (c *client) ClosePullRequest(ctx context.Context, pr *github.PullRequest) { } } +func getLocalBranchName(gitcmd git.GitInterface) string { + var output string + err := gitcmd.Git("branch", &output) + check(err) + lines := strings.Split(output, "\n") + for _, line := range lines { + if strings.HasPrefix(line, "* ") { + return line[2:] + } + } + panic("cannot determine local git branch name") +} + func branchNameFromCommit(info *github.GitHubInfo, commit git.Commit) string { return "pr/" + info.UserName + "/" + info.LocalBranch + "/" + commit.CommitID }