Skip to content

Repository.Remote fails if the repository has branches that checked out from pull-request #331

@kyoh86

Description

@kyoh86

Summary

Using GitHub CLI gh ( https://github.com/cli/cli ), I often checkout pull-request.

gh pr checkout 330

But go-git fails to parse the config in the repository that has a branch created by gh.

To reproduce:

  1. Prepare a repository
$ git clone https://github.com/go-git/go-git
$ gh pr checkout 330
  1. Try to get the remote URLs from the repository with go-git
	repo, err := git.PlainOpen("./go-git")
	if err != nil {
		return nil, fmt.Errorf("open local repository: %w", err)
	}
	remote, err := repo.Remote("origin")
	if err != nil {
		return nil, fmt.Errorf("get remote %s: %w", name, err)
	}
	return remote.Config().URLs, nil

It fails.

Supplement

gh pr checkout xxx creates a branch like below.

[branch "foo/bar"]
	remote = origin
	merge = refs/pull/330/head

go-git says "branch config: invalid merge".

go-git/config/branch.go

Lines 39 to 41 in da81027

if b.Merge != "" && !b.Merge.IsBranch() {
return errBranchInvalidMerge
}

errBranchInvalidMerge = errors.New("branch config: invalid merge")

func (r ReferenceName) IsBranch() bool {
return strings.HasPrefix(string(r), refHeadPrefix)
}

refHeadPrefix = refPrefix + "heads/"

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleIssues/PRs that are marked for closure due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions