CurrentBranch reads the checked-out branch for the git repository.
(ctx context.Context)
| 220 | |
| 221 | // CurrentBranch reads the checked-out branch for the git repository. |
| 222 | func (c *Client) CurrentBranch(ctx context.Context) (string, error) { |
| 223 | args := []string{"symbolic-ref", "--quiet", "HEAD"} |
| 224 | cmd, err := c.Command(ctx, args...) |
| 225 | if err != nil { |
| 226 | return "", err |
| 227 | } |
| 228 | out, err := cmd.Output() |
| 229 | if err != nil { |
| 230 | var gitErr *GitError |
| 231 | if ok := errors.As(err, &gitErr); ok && len(gitErr.Stderr) == 0 { |
| 232 | gitErr.err = ErrNotOnAnyBranch |
| 233 | gitErr.Stderr = "not on any branch" |
| 234 | return "", gitErr |
| 235 | } |
| 236 | return "", err |
| 237 | } |
| 238 | branch := firstLine(out) |
| 239 | return strings.TrimPrefix(branch, "refs/heads/"), nil |
| 240 | } |
| 241 | |
| 242 | // ShowRefs resolves fully-qualified refs to commit hashes. |
| 243 | func (c *Client) ShowRefs(ctx context.Context, refs []string) ([]Ref, error) { |