Documentation ¶
Overview ¶
Package git provides a wrapper for the git command line program. The helper methods avoids porcelain commands as much as possible and return a parsed output whenever possible.
Users of this package have access to the low-level Exec() function for the methods not yet implemented.
Index ¶
- func NewCmdError(cmd string, stdout, stderr []byte) error
- type CmdError
- type CommitMetadata
- type Config
- type Error
- type Git
- func (git *Git) Add(files ...string) error
- func (git *Git) AddSubmodule(name string, url string) (string, error)
- func (git *Git) Checkout(rev string, create bool) error
- func (git *Git) Clone(repoURL, dir string) error
- func (git *Git) Commit(msg string, args ...string) error
- func (git *Git) CurrentBranch() (string, error)
- func (git *Git) DeleteBranch(name string) error
- func (git *Git) DiffNames(from, to string) ([]string, error)
- func (git *Git) DiffTree(from, to string, relative, nameOnly, recurse bool) (string, error)
- func (git *Git) Exec(command string, args ...string) (string, error)
- func (git *Git) FetchRemoteRev(remote, ref string) (Ref, error)
- func (git *Git) GetConfigValue(key string) (string, error)
- func (git *Git) HasRemotes() (bool, error)
- func (git *Git) Init(dir string, defaultBranch string, bare bool) error
- func (git *Git) IsRepository() bool
- func (git *Git) ListUncommitted(dirs ...string) ([]string, error)
- func (git *Git) ListUntracked(dirs ...string) ([]string, error)
- func (git *Git) LogSummary(revs ...string) ([]LogLine, error)
- func (git *Git) Merge(branch string) error
- func (git *Git) MergeBase(commit1, commit2 string) (string, error)
- func (git *Git) NewBranch(name string) error
- func (git *Git) Pull(remote, branch string) error
- func (git *Git) Push(remote, branch string) error
- func (git *Git) RemoteAdd(name string, url string) error
- func (git *Git) Remotes() ([]Remote, error)
- func (git *Git) RevParse(rev string) (string, error)
- func (git *Git) Root() (string, error)
- func (git *Git) SetRemoteURL(remote, url string) error
- func (git *Git) ShowCommitMetadata(objectName string) (*CommitMetadata, error)
- func (git *Git) Status() (string, error)
- func (git *Git) URL(remote string) (string, error)
- func (git *Git) Version() (string, error)
- func (git *Git) With() *Options
- type LogLine
- type Options
- type Ref
- type Remote
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCmdError ¶
NewCmdError returns a new command line error.
Types ¶
type CmdError ¶
type CmdError struct {
// contains filtered or unexported fields
}
CmdError is the error for failed commands.
type CommitMetadata ¶ added in v0.4.3
type CommitMetadata struct { Author string Email string Time *time.Time Subject string Body string }
CommitMetadata is metadata associated with a Git commit.
type Config ¶
type Config struct { Username string // Username used in commits. Email string // Email used in commits. ProgramPath string // WorkingDir sets the directory where the commands will be applied. WorkingDir string // Env is the environment variables to be passed over to git. // If it is nil it means no environment variables should be passed. // To inherit all env vars from the parent process os.Getenv() needs // to be passed explicitly. Env []string // Isolated tells if the wrapper should run with isolated // configurations, which means setting it to true will make the wrapper // not rely on the global/system configuration. It's useful for // reproducibility of scripts. Isolated bool // AllowPorcelain tells if the wrapper is allowed to execute porcelain // commands. It's useful if the user is not sure if all commands used by // their program are plumbing. AllowPorcelain bool // Global arguments that are automatically added when executing git commands. // This is useful for setting config overrides or other common flags. GlobalArgs []string }
Config configures the wrapper.
type Error ¶
type Error string
Error is the sentinel error type.
const ( // ErrGitNotFound is the error that tells if git was not found. ErrGitNotFound Error = "git program not found" // ErrInvalidConfig is the error that tells if the configuration is invalid. ErrInvalidConfig Error = "invalid configuration" // ErrDenyPorcelain is the error that tells if a porcelain method was called // when AllowPorcelain is false. ErrDenyPorcelain Error = "porcelain commands are not allowed by the configuration" )
type Git ¶
type Git struct {
// contains filtered or unexported fields
}
Git is the wrapper object.
func WithConfig ¶
WithConfig creates a new git wrapper by providing the config.
func (*Git) AddSubmodule ¶
AddSubmodule adds the submodule name from url into this repository. For security reasons, this method should only be used in tests.
func (*Git) Checkout ¶
Checkout switches branches or change to specific revisions in the tree. When switching branches, the create flag can be set to automatically create the new branch before changing into it. Beware: Checkout is a porcelain method.
func (*Git) Clone ¶
Clone will clone the given repo inside the given dir. Beware: Clone is a porcelain method.
func (*Git) Commit ¶
Commit the current staged changes. The args are extra flags and/or arguments to git commit command line. Beware: Commit is a porcelain method.
func (*Git) CurrentBranch ¶
CurrentBranch returns the short branch name that HEAD points to.
func (*Git) DeleteBranch ¶
DeleteBranch deletes the branch.
func (*Git) DiffNames ¶
DiffNames recursively walks the git tree objects computing the from and to commit ids differences and return all the file names containing differences relative to configuration WorkingDir.
func (*Git) DiffTree ¶
DiffTree compares the from and to commit ids and returns the differences. If nameOnly is set then only the file names of changed files are show. If recurse is set, then it walks into child trees as well. If relative is set, then only show local changes of current dir.
func (*Git) Exec ¶
Exec executes any provided git command. We don't allow Exec if AllowPorcelain is set to false.
func (*Git) FetchRemoteRev ¶
FetchRemoteRev will fetch from the remote repo the commit id and ref name for the given remote and reference. This will make use of the network to fetch data from the remote configured on the git repo.
func (*Git) GetConfigValue ¶ added in v0.4.3
GetConfigValue returns the value mapped to given config key, or an error if they doesn't exist.
func (*Git) HasRemotes ¶ added in v0.4.4
HasRemotes returns if a there are any remotes configured.
func (*Git) Init ¶
Init initializes a git repository. If bare is true, it initializes a "bare repository", in other words, a repository not intended for work but just store revisions. Beware: Init is a porcelain method.
func (*Git) IsRepository ¶
IsRepository tell if the git wrapper setup is operating in a valid git repository.
func (*Git) ListUncommitted ¶
ListUncommitted lists uncommitted files in the directories provided in dirs.
func (*Git) ListUntracked ¶
ListUntracked lists untracked files in the directories provided in dirs.
func (*Git) LogSummary ¶
LogSummary returns a list of commit log summary in reverse chronological order from the revs set operation. It expects the same revision list as the `git rev-list` command.
It returns only the first line of the commit message.
func (*Git) Merge ¶
Merge branch into current branch using the non fast-forward strategy. Beware: Merge is a porcelain method.
func (*Git) Remotes ¶
Remotes returns a list of all configured remotes and their respective branches. The result slice is ordered lexicographically by the remote name.
Returns an empty list if no remote is found.
func (*Git) RevParse ¶
RevParse parses the rev name and returns the commit id it points to. The rev name follows the [git revisions](https://git-scm.com/docs/gitrevisions) documentation.
func (*Git) SetRemoteURL ¶
SetRemoteURL sets the remote url.
func (*Git) ShowCommitMetadata ¶ added in v0.4.3
func (git *Git) ShowCommitMetadata(objectName string) (*CommitMetadata, error)
ShowCommitMetadata returns common metadata associated with the given object. An object name can be a commit SHA or a symbolic name, i.e. HEAD, branch-name, etc.
func (*Git) Status ¶
Status returns the git status of the current branch. Beware: Status is a porcelain method.
type Options ¶ added in v0.4.4
type Options struct {
// contains filtered or unexported fields
}
Options is a customizable options object to be used with the Git wrapper.
func (*Options) WorkingDir ¶ added in v0.4.4
WorkingDir sets the wrapper working directory.
type Ref ¶
Ref is a git reference.
func (Ref) ShortCommitID ¶
ShortCommitID returns the short version of the commit ID. If the reference doesn't have a valid commit id it returns empty.