git

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LocalScope  ConfigScope = "--local"
	GlobalScope ConfigScope = "--global"
	System      ConfigScope = "--system"
	Traverse    ConfigScope = ""
	HEAD        string      = "HEAD"
)

Available ConfigScope's.

View Source
const (
	// GitCKInitTemplateDir is the Git template dir config key.
	GitCKInitTemplateDir = "init.templateDir"

	// GitCKCoreHooksPath is the Git global hooks path config key.
	GitCKCoreHooksPath = "core.hooksPath"

	GitCVTrue  = "true"
	GitCVFalse = "false"
)
View Source
const (
	// NullRef is the null reference used by git during certain hook execution.
	NullRef = "0000000000000000000000000000000000000000"
)

Variables

This section is empty.

Functions

func Clone

func Clone(repoPath string, url string, branch string, depth int) error

Clone an URL to a path `repoPath`.

func FetchOrClone

func FetchOrClone(
	repoPath string,
	url string, branch string,
	depth int,
	tagPattern string,
	repoCheck RepoCheck) (isNewClone bool, err error)

FetchOrClone either executes a fetch in `repoPath` or if not existing, clones to this path. The callback `repoCheck` before a fetch can trigger a reclone.

func FindGitDirs

func FindGitDirs(searchDir string) (all []string, err error)

FindGitDirs returns Git directories inside `searchDir`. Paths relative to `searchDir` containing `.dotfiles` (hidden files) will never be reported. Optionally the output can be sorted.

func GetCommitSHA

func GetCommitSHA(gitx *Context, ref string) (string, error)

GetCommitSHA gets the commit SHA1 of the ref.

func GetDefaultTemplateDir

func GetDefaultTemplateDir() string

GetDefaultTemplateDir gets the default Git template dir.

func GetLFSConfigFile

func GetLFSConfigFile(repoDir string) (string, bool)

GetLFSRequiredFile gets the LFS config file inside the repository and `true` if existing.

func GetTags

func GetTags(gitx *Context, commitSHA string) ([]string, error)

GetTags gets the tags at `commitSHA`.

func GetVersion

func GetVersion(gitx *Context, commitSHA string, matchPattern string) (v *version.Version, tag string, err error)

GetVersion gets the semantic version and its tag.

func GetVersionAt

func GetVersionAt(gitx *Context, commitSHA string) (*version.Version, string, error)

GetVersionAt gets the version & tag from the tags at `commitSHA`.

func IsCloneURLALocalPath

func IsCloneURLALocalPath(url string) bool

IsCloneURLALocalPath checks if the clone url is a local path. Thats the case if its not a URL Scheme, not a short SCP syntax and not a remote transport helper syntax. The problem arises on Windows with drive letters, since `C:/a/b` can technically be a short SCP syntax, we require at least 2 letters for the host name.

func IsCloneURLALocalURL

func IsCloneURLALocalURL(url string) bool

IsCloneURLALocalURL checks if the clone url is a url to a local directory. Thats the case only for `file://`.

func IsCloneURLANormalURL

func IsCloneURLANormalURL(url string) bool

IsCloneURLANormalURL checks if `url` is a normal url. Containing `<scheme>://` at the beginning.

func IsCloneURLARemoteHelperSyntax

func IsCloneURLARemoteHelperSyntax(url string) bool

IsRemoteHelperSyntax checks if `url` is a remote helper syntax. https://git-scm.com/docs/gitremote-helpers

func IsLFSAvailable

func IsLFSAvailable() bool

IsLFSAvailable tells if git-lfs is available in the path.

func IsRefReachable

func IsRefReachable(gitx *Context, startRef string, ref string) (bool, error)

IsRefReachable reports if `ref` (can be branch/tag/commit) is contained starting from `startRef`.

func ParseRemoteHelperSyntax

func ParseRemoteHelperSyntax(url string) []string

ParseRemoteHelperSyntax parses the url as a remote helper syntax and reporting the transport and address string if not nil. https://git-scm.com/docs/gitremote-helpers

func PullOrClone

func PullOrClone(
	repoPath string,
	url string,
	branch string,
	depth int,
	repoCheck func(*Context) error) (isNewClone bool, err error)

PullOrClone either executes a pull in `repoPath` or if not existing, clones to this path.

func SanitizeEnv

func SanitizeEnv(env []string) []string

SanitizeEnv santizes the environement from unwanted Git (possibly leaking) Git variables which might interfere with certain buggy Git commands.

Types

type ConfigScope

type ConfigScope string

ConfigScope Defines the scope of a config file, such as local, global or system.

type Context

type Context struct {
	cm.CmdContext
}

Context defines the context to execute it commands.

func Ctx

func Ctx() *Context

Ctx creates a git command execution context with current working dir.

func CtxC

func CtxC(cwd string) *Context

CtxC creates a git command execution context with working dir `cwd`.

func CtxCSanitized

func CtxCSanitized(cwd string) *Context

CtxCSanitized creates a git command execution context with working dir `cwd` and sanitized environement.

func CtxSanitized

func CtxSanitized() *Context

CtxSanitized creates a git command execution context with current working dir and sanitized environement.

func (*Context) AddConfig

func (c *Context) AddConfig(key string, value interface{}, scope ConfigScope) error

AddConfig adds a Git configuration values with key `key`.

func (*Context) FetchBranch

func (c *Context) FetchBranch(remote string, branch string, tagPattern string) error

FetchBranch executes a fetch of a `branch` from the `remote` in `repoPath`. This command sadly does not automatically (git 2.30) fetch the tags on this branch automatically. Use `tagPattern` to specify explicitly which tags to fetch.

func (*Context) GetAllWorktrees

func (c *Context) GetAllWorktrees() (list []string, err error)

GetAllWorktrees returns all worktrees based on the current context's working directory.

func (*Context) GetCommitLog

func (c *Context) GetCommitLog(commitSHA string, format string) (string, error)

GetCommitLog gets all commits in the ancestry path starting from `firstSHA` (excluded in the result) up to and including `lastSHA`.

func (*Context) GetCommits

func (c *Context) GetCommits(firstSHA string, lastSHA string) ([]string, error)

GetCommits gets all commits in the ancestry path starting from `firstSHA` (excluded in the result) up to and including `lastSHA`.

func (*Context) GetConfig

func (c *Context) GetConfig(key string, scope ConfigScope) string

GetConfig gets a Git configuration value for key `key`.

func (*Context) GetConfigAll

func (c *Context) GetConfigAll(key string, scope ConfigScope) []string

GetConfigAll gets a all Git configuration values for key `key`.

func (*Context) GetConfigAllU

func (c *Context) GetConfigAllU(key string, scope ConfigScope) string

GetConfigAllU gets a all Git configuration values unsplitted for key `key`.

func (*Context) GetConfigRegex

func (c *Context) GetConfigRegex(regex string, scope ConfigScope) (res [][]string)

GetConfigRegex gets all Git configuration values for regex `regex`. Returns a list of pairs.

func (*Context) GetCurrentBranch

func (c *Context) GetCurrentBranch() (string, error)

GetCurrentBranch gets the current branch in repository.

func (*Context) GetGitDirCommon

func (c *Context) GetGitDirCommon() (gitDir string, err error)

GetGitDirCommon returns the common Git directory. For normal repos this points to the `.git` directory. For worktrees this points to the main worktrees git dir. The env. variable GIT_COMMON_DIR has especiall be introduced for multiple worktrees, see: https://github.com/git/git/commit/c7b3a3d2fe2688a30ddb8d516ed000eeda13c24e

func (*Context) GetGitDirWorktree

func (c *Context) GetGitDirWorktree() (gitDir string, err error)

GetGitDirWorktree returns the Git directory. For normal repos this points to the `.git` directory. For worktrees this points to the actual worktrees git dir `.git/worktrees/<....>/`.

func (*Context) GetMainWorktree

func (c *Context) GetMainWorktree() (string, error)

GetMainWorktree returns the main worktree based on the current context's working directory.

func (*Context) GetRemoteURLAndBranch

func (c *Context) GetRemoteURLAndBranch(remote string) (currentURL string, currentBranch string, err error)

GetRemoteURLAndBranch reports the `remote`s `url` and the current `branch` of HEAD.

func (*Context) GetRepoRoot

func (c *Context) GetRepoRoot() (topLevel string, gitDir string, gitDirWorktree string, err error)

GetRepoRoot returns the top level directory in a non-bare repository or the absolute Git directory in a bare repository for `topLevel`. This is the root level for Githooks. The `gitDir` is the common Git directory (main Git dir for worktrees).

func (*Context) IsBareRepo

func (c *Context) IsBareRepo() bool

IsBareRepo returns `true` if `c.Cwd` is a bare repository.

func (*Context) IsConfigSet

func (c *Context) IsConfigSet(key string, scope ConfigScope) bool

IsConfigSet tells if a git config is set.

func (*Context) IsGitDir

func (c *Context) IsGitDir() bool

IsGitDir returns `true` if `c.Cwd` is a git repository (bare or non-bare).

func (*Context) IsGitRepo

func (c *Context) IsGitRepo() bool

IsGitRepo returns `true` if `path` is a git repository (bare or non-bare).

func (*Context) LookupConfig

func (c *Context) LookupConfig(key string, scope ConfigScope) (string, bool)

LookupConfig gets a Git configuration value and reports if it exists or not.

func (*Context) Pull

func (c *Context) Pull(remote string) error

Pull executes a pull in `repoPath`.

func (*Context) SetConfig

func (c *Context) SetConfig(key string, value interface{}, scope ConfigScope) error

SetConfig sets a Git configuration values with key `key`.

func (*Context) UnsetConfig

func (c *Context) UnsetConfig(key string, scope ConfigScope) (err error)

UnsetConfig unsets all Git configuration values with key `key`.

type RepoCheck

type RepoCheck = func(Context, string, string) (bool, error)

RepoCheck is the function which is executed before a fetch. Arguments 1 and 2 are `url`, `branch`. Return an error to abort the action. Return `true` to trigger a complete reclone. Available ConfigScope's.

type ShortSCP

type ShortSCP []string

ShortSCP represents a short SCP syntax and corresponds to regex `reShortSCPSyntax`.

func ParseSCPSyntax

func ParseSCPSyntax(url string) ShortSCP

ParseSCPSyntax parses the url as a short SCP syntax and reporting the user, host and path if not nil.

func (ShortSCP) String

func (scp ShortSCP) String() string

String returns the whole short scp syntax as string.

Jump to

Keyboard shortcuts

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