git

package
v1.64.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const BaseBranchFile = "BASE_BRANCH"
View Source
const DefaultRemote = "origin"

Variables

View Source
var ErrNotOnAnyBranch = errors.New("you're not on any Git branch (a 'detached HEAD' state).")

ErrNotOnAnyBranch indicates that the user is in detached HEAD state

View Source
var GitCommand = func(args ...string) *exec.Cmd {
	return exec.Command("git", args...)
}
View Source
var SetRemoteResolution = func(name, resolution string) error {
	return SetRemoteConfig(name, "glab-resolved", resolution)
}
View Source
var StackLocation = filepath.Join(".git", "stacked")
View Source
var ToplevelDir = func() (string, error) {
	showCmd := exec.Command("git", "rev-parse", "--show-toplevel")
	output, err := run.PrepareCmd(showCmd).Output()
	return firstLine(output), err
}

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

Functions

func AddStackBaseBranch

func AddStackBaseBranch(title string, branch string) error

func AddStackRefDir

func AddStackRefDir(dir string) (string, error)

func AddStackRefFile

func AddStackRefFile(title string, stackRef StackRef) error

func AddUpstreamRemote

func AddUpstreamRemote(upstreamURL, cloneDir string) error

func CheckoutBranch

func CheckoutBranch(branch string, gr GitRunner) error

func CheckoutNewBranch

func CheckoutNewBranch(branch string) error

func CommitBody

func CommitBody(sha string) (string, error)

func Config

func Config(name string) (string, error)

func CreateBranches

func CreateBranches(t *testing.T, branches []string)

func CreateRefFiles

func CreateRefFiles(refs map[string]StackRef, title string) error

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch reads the checked-out branch for the git repository

func DeleteLocalBranch

func DeleteLocalBranch(branch string, gr GitRunner) error

func DeleteStackRefFile

func DeleteStackRefFile(title string, stackRef StackRef) error

func DescribeByTags

func DescribeByTags() (string, error)

DescribeByTags gives a description of the current object. Non-annotated tags are considered. Reference: https://git-scm.com/docs/git-describe

func GetAllConfig

func GetAllConfig(key string) ([]byte, error)

GetConfig returns the local config value associated with the provided key. If there are multiple values associated with the key, they are all returned.

func GetCurrentStackTitle

func GetCurrentStackTitle() (string, error)

func GetDefaultBranch

func GetDefaultBranch(remote string) (string, error)

GetDefaultBranch finds and returns the remote's default branch

func GetRemoteURL

func GetRemoteURL(remoteAlias string) (string, error)

func GitUserName

func GitUserName() ([]byte, error)

func InitGitRepo

func InitGitRepo(t *testing.T) string

func InitGitRepoWithCommit

func InitGitRepoWithCommit(t *testing.T) string

func IsValidURL

func IsValidURL(u string) bool

IsValidUrl tests a string to determine if it is a valid Git url or not.

func ListTags

func ListTags() ([]string, error)

ListTags gives a slice of tags from the current repository.

func ParseDefaultBranch

func ParseDefaultBranch(output []byte) (string, error)

func ParseURL

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

ParseURL normalizes git remote urls

func Push

func Push(remote string, ref string, cmdOut, cmdErr io.Writer) error

Push publishes a git ref to a remote

func RemoteBranchExists

func RemoteBranchExists(branch string, gr GitRunner) bool

func RunClone

func RunClone(cloneURL string, target string, args []string) (string, error)

func RunCmd

func RunCmd(args []string) error

func SetConfig

func SetConfig(key, value string) error

func SetLocalConfig

func SetLocalConfig(key, value string) error

func SetRemoteConfig

func SetRemoteConfig(remote, key, value string) error

func SetUpstream

func SetUpstream(remote string, branch string, cmdOut, cmdErr io.Writer) error

SetUpstream sets the upstream (tracking) of a branch

func StackRootDir

func StackRootDir(title string) (string, error)

func UncommittedChangeCount

func UncommittedChangeCount() (int, error)

func UpdateStackRefFile

func UpdateStackRefFile(title string, s StackRef) error

Types

type BranchConfig

type BranchConfig struct {
	RemoteName string
	RemoteURL  *url.URL
	MergeRef   string
}

func ReadBranchConfig

func ReadBranchConfig(branch string) BranchConfig

ReadBranchConfig parses the `branch.BRANCH.(remote|merge)` part of git config

type Commit

type Commit struct {
	Sha   string
	Title string
}

func Commits

func Commits(baseRef, headRef string) ([]*Commit, error)

type GitRunner

type GitRunner interface {
	Git(args ...string) (string, 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 AddRemote

func AddRemote(name, u string) (*Remote, error)

AddRemote adds a new git remote and auto-fetches objects from it

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 Remotes

func Remotes() (RemoteSet, error)

Remotes gets the git remotes set for the current repo

type SSHAliasMap

type SSHAliasMap map[string]string

SSHAliasMap encapsulates the translation of SSH hostname aliases

func ParseSSHConfig

func ParseSSHConfig() SSHAliasMap

ParseSSHConfig constructs a map of SSH hostname aliases based on user and system configuration files

func (SSHAliasMap) Translator

func (m SSHAliasMap) Translator() func(*url.URL) *url.URL

Translator returns a function that applies hostname aliases to URLs

type Stack

type Stack struct {
	Title string
	Refs  map[string]StackRef
}

Stack represents a stacked diff data structure. Refs are structured as a doubly linked list where the links are identified with the StackRef.Prev and StackRef.Next fields. The StackRef.SHA is the id that the former two fields can point to. All stacks must be created with GatherStackRefs which validates the stack for consistency.

func GatherStackRefs

func GatherStackRefs(title string) (Stack, error)

func GetStacks

func GetStacks() ([]Stack, error)

func (*Stack) BaseBranch

func (s *Stack) BaseBranch(gr GitRunner) (string, error)

func (*Stack) Branches

func (s *Stack) Branches() []string

func (Stack) Empty

func (s Stack) Empty() bool

func (*Stack) First

func (s *Stack) First() StackRef

func (*Stack) IndexAt

func (s *Stack) IndexAt(ref StackRef) int

func (*Stack) Iter

func (s *Stack) Iter() iter.Seq[StackRef]

Iter returns an iterator to range from the first to the last ref in the stack.

func (*Stack) Iter2

func (s *Stack) Iter2() iter.Seq2[int, StackRef]

Iter2 returns an iterator like Iter, but includes an index

func (*Stack) Last

func (s *Stack) Last() StackRef

func (Stack) RefFromBranch

func (s Stack) RefFromBranch(branch string) (StackRef, error)

func (*Stack) RemoveBranch

func (s *Stack) RemoveBranch(ref StackRef, gr GitRunner) error

func (*Stack) RemoveRef

func (s *Stack) RemoveRef(ref StackRef, gr GitRunner) error

type StackRef

type StackRef struct {
	Prev        string `json:"prev"`
	Branch      string `json:"branch"`
	SHA         string `json:"sha"`
	Next        string `json:"next"`
	MR          string `json:"mr"`
	Description string `json:"description"`
}

func CurrentStackRefFromCurrentBranch

func CurrentStackRefFromCurrentBranch(title string) (StackRef, error)

func (StackRef) Empty

func (r StackRef) Empty() bool

Empty returns true if the stack ref does not have an associated SHA (commit). This indicates that the StackRef is invalid.

func (StackRef) IsFirst

func (r StackRef) IsFirst() bool

IsFirst returns true if the stack ref is the first of the stack. A stack ref is considered the first if it does not reference any previous ref.

func (StackRef) IsLast

func (r StackRef) IsLast() bool

IsLast returns true if the stack ref is the last of the stack. A stack ref is considered the last if it does not reference any next ref.

func (StackRef) Subject

func (r StackRef) Subject() string

Subject returns the stack ref description suitable as commit Subject and for other in space limited places. It only takes the first line of the description into account and truncates it to 72 characters.

type StandardGitCommand

type StandardGitCommand struct{}

func (StandardGitCommand) Git

func (gitc StandardGitCommand) Git(args ...string) (string, error)

Directories

Path Synopsis
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.

Jump to

Keyboard shortcuts

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