git

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const MergeBaseConfig = "gh-merge-base"

MergeBaseConfig is the configuration setting to keep track of the PR target branch.

Variables

View Source
var AllMatchingCredentialsPattern = CredentialPattern{/* contains filtered or unexported fields */}

AllMatchingCredentialsPattern allows for setting gh as credential helper for all hosts. However, we should endeavour to remove it as it's less secure.

View Source
var ErrNotOnAnyBranch = errors.New("git: not on any branch")

ErrNotOnAnyBranch indicates that the user is in detached HEAD state.

Functions

func IsURL

func IsURL(u string) bool

func ParseURL

func ParseURL(rawURL string) (*url.URL, error)

ParseURL normalizes git remote urls

Types

type BranchConfig

type BranchConfig struct {
	RemoteName     string   // .remote if string
	RemoteURL      *url.URL // .remote if url
	MergeRef       string   // .merge
	PushRemoteName string   // .pushremote if string
	PushRemoteURL  *url.URL // .pushremote if url

	// MergeBase is the optional base branch to target in a new PR if `--base` is not specified.
	MergeBase string
}

These are the keys we read from the git branch.<name> config.

type Client

type Client struct {
	GhPath  string
	RepoDir string
	GitPath string
	Stderr  io.Writer
	Stdin   io.Reader
	Stdout  io.Writer
	// contains filtered or unexported fields
}

func (*Client) AddRemote

func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBranches []string) (*Remote, error)

func (*Client) AuthenticatedCommand

func (c *Client) AuthenticatedCommand(ctx context.Context, credentialPattern CredentialPattern, args ...string) (*Command, error)

AuthenticatedCommand is a wrapper around Command that included configuration to use gh as the credential helper for git.

func (*Client) CheckoutBranch

func (c *Client) CheckoutBranch(ctx context.Context, branch string) error

func (*Client) CheckoutNewBranch

func (c *Client) CheckoutNewBranch(ctx context.Context, remoteName, branch string) error

func (*Client) Clone

func (c *Client) Clone(ctx context.Context, cloneURL string, args []string, mods ...CommandModifier) (string, error)

func (*Client) Command

func (c *Client) Command(ctx context.Context, args ...string) (*Command, error)

func (*Client) CommitBody

func (c *Client) CommitBody(ctx context.Context, sha string) (string, error)

func (*Client) Commits

func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commit, error)

func (*Client) Config

func (c *Client) Config(ctx context.Context, name string) (string, error)

func (*Client) Copy

func (c *Client) Copy() *Client

func (*Client) CurrentBranch

func (c *Client) CurrentBranch(ctx context.Context) (string, error)

CurrentBranch reads the checked-out branch for the git repository.

func (*Client) DeleteLocalBranch

func (c *Client) DeleteLocalBranch(ctx context.Context, branch string) error

func (*Client) DeleteLocalTag

func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, remote string, refspec string, mods ...CommandModifier) error

func (*Client) GitDir

func (c *Client) GitDir(ctx context.Context) (string, error)

func (*Client) HasLocalBranch

func (c *Client) HasLocalBranch(ctx context.Context, branch string) bool

func (*Client) IsLocalGitRepo

func (c *Client) IsLocalGitRepo(ctx context.Context) (bool, error)

func (*Client) LastCommit

func (c *Client) LastCommit(ctx context.Context) (*Commit, error)

func (*Client) PathFromRoot

func (c *Client) PathFromRoot(ctx context.Context) string

Show current directory relative to the top-level directory of repository.

func (*Client) Pull

func (c *Client) Pull(ctx context.Context, remote, branch string, mods ...CommandModifier) error

func (*Client) Push

func (c *Client) Push(ctx context.Context, remote string, ref string, mods ...CommandModifier) error

func (*Client) PushDefault

func (c *Client) PushDefault(ctx context.Context) (PushDefault, error)

PushDefault returns the value of push.default in the config. If the value is not set, it returns "simple" (the default git value). See https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault

func (*Client) PushRevision

func (c *Client) PushRevision(ctx context.Context, branch string) (RemoteTrackingRef, error)

PushRevision gets the value of the @{push} revision syntax An error here doesn't necessarily mean something is broken, but may mean that the @{push} revision syntax couldn't be resolved, such as in non-centralized workflows with push.default = simple. Downstream consumers should consider how to handle this error.

func (*Client) ReadBranchConfig

func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchConfig, error)

ReadBranchConfig parses the `branch.BRANCH.(remote|merge|pushremote|gh-merge-base)` part of git config. If no branch config is found or there is an error in the command, it returns an empty BranchConfig. Downstream consumers of ReadBranchConfig should consider the behavior they desire if this errors, as an empty config is not necessarily breaking.

func (*Client) RemotePushDefault

func (c *Client) RemotePushDefault(ctx context.Context) (string, error)

RemotePushDefault returns the value of remote.pushDefault in the config. If the value is not set, it returns an empty string.

func (*Client) Remotes

func (c *Client) Remotes(ctx context.Context) (RemoteSet, error)

func (*Client) SetBranchConfig

func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string) error

SetBranchConfig sets the named value on the given branch.

func (*Client) SetRemoteBranches

func (c *Client) SetRemoteBranches(ctx context.Context, remote string, refspec string) error

func (*Client) SetRemoteResolution

func (c *Client) SetRemoteResolution(ctx context.Context, name, resolution string) error

func (*Client) ShowRefs

func (c *Client) ShowRefs(ctx context.Context, refs []string) ([]Ref, error)

ShowRefs resolves fully-qualified refs to commit hashes.

func (*Client) ToplevelDir

func (c *Client) ToplevelDir(ctx context.Context) (string, error)

ToplevelDir returns the top-level directory path of the current repository.

func (*Client) TrackingBranchNames

func (c *Client) TrackingBranchNames(ctx context.Context, prefix string) []string

func (*Client) UncommittedChangeCount

func (c *Client) UncommittedChangeCount(ctx context.Context) (int, error)

func (*Client) UnsetRemoteResolution

func (c *Client) UnsetRemoteResolution(ctx context.Context, name string) error

func (*Client) UpdateRemoteURL

func (c *Client) UpdateRemoteURL(ctx context.Context, name, url string) error

type Command

type Command struct {
	*exec.Cmd
}

func (*Command) Output

func (gc *Command) Output() ([]byte, error)

func (*Command) Run

func (gc *Command) Run() error

type CommandModifier

type CommandModifier func(*Command)

Allow individual commands to be modified from the default client options.

func WithRepoDir

func WithRepoDir(repoDir string) CommandModifier

func WithStderr

func WithStderr(stderr io.Writer) CommandModifier

func WithStdin

func WithStdin(stdin io.Reader) CommandModifier

func WithStdout

func WithStdout(stdout io.Writer) CommandModifier

type Commit

type Commit struct {
	Sha   string
	Title string
	Body  string
}

type CredentialPattern

type CredentialPattern struct {
	// contains filtered or unexported fields
}

CredentialPattern is used to inform AuthenticatedCommand which patterns Git should match against when trying to find credentials. It is a little over-engineered as a type because we want AuthenticatedCommand to have a clear compilation error when this is not provided, as opposed to using a string which might compile with `client.AuthenticatedCommand(ctx, "fetch")`.

It is only usable when constructed by another function in the package because the empty pattern, without allMatching set to true, will result in an error in AuthenticatedCommand.

Callers can currently opt-in to an slightly less secure mode for backwards compatibility by using AllMatchingCredentialsPattern.

func CredentialPatternFromGitURL

func CredentialPatternFromGitURL(gitURL string) (CredentialPattern, error)

CredentialPatternFromGitURL takes a git remote URL e.g. "https://github.com/cli/cli.git" or "git@github.com:cli/cli.git" and returns the credential pattern that should be used for it.

func CredentialPatternFromHost

func CredentialPatternFromHost(host string) CredentialPattern

CredentialPatternFromHost expects host to be in the form "github.com" and returns the credential pattern that should be used for it. It does not perform any canonicalisation e.g. "api.github.com" will not work as expected.

type GitError

type GitError struct {
	ExitCode int
	Stderr   string
	// contains filtered or unexported fields
}

func (*GitError) Error

func (ge *GitError) Error() string

func (*GitError) Unwrap

func (ge *GitError) Unwrap() error

type NotInstalled

type NotInstalled struct {
	// contains filtered or unexported fields
}

func (*NotInstalled) Error

func (e *NotInstalled) Error() string

func (*NotInstalled) Unwrap

func (e *NotInstalled) Unwrap() error

type PushDefault

type PushDefault string

PushDefault defines the action git push should take if no refspec is given. See: https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault

const (
	PushDefaultNothing  PushDefault = "nothing"
	PushDefaultCurrent  PushDefault = "current"
	PushDefaultUpstream PushDefault = "upstream"
	PushDefaultTracking PushDefault = "tracking"
	PushDefaultSimple   PushDefault = "simple"
	PushDefaultMatching PushDefault = "matching"
)

func ParsePushDefault

func ParsePushDefault(s string) (PushDefault, error)

type Ref

type Ref struct {
	Hash string
	Name string
}

Ref represents a git commit reference.

type Remote

type Remote struct {
	Name     string
	Resolved string
	FetchURL *url.URL
	PushURL  *url.URL
}

Remote is a parsed git remote.

func NewRemote

func NewRemote(name string, u string) *Remote

func (*Remote) String

func (r *Remote) String() string

type RemoteSet

type RemoteSet []*Remote

RemoteSet is a slice of git remotes.

func (RemoteSet) Len

func (r RemoteSet) Len() int

func (RemoteSet) Less

func (r RemoteSet) Less(i, j int) bool

func (RemoteSet) Swap

func (r RemoteSet) Swap(i, j int)

type RemoteTrackingRef

type RemoteTrackingRef struct {
	Remote string
	Branch string
}

RemoteTrackingRef is the structured form of the string "refs/remotes/<remote>/<branch>". For example, the @{push} revision syntax could report "refs/remotes/origin/main" which would be parsed into RemoteTrackingRef{Remote: "origin", Branch: "main"}.

func ParseRemoteTrackingRef

func ParseRemoteTrackingRef(s string) (RemoteTrackingRef, error)

➜ git rev-parse --symbolic-full-name bar/baz@{push} refs/remotes/foo/bar/baz ```

When using this ref, git assumes it means `remote: foo` `branch: bar/baz`.

func (RemoteTrackingRef) String

func (r RemoteTrackingRef) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL