git

package module
v0.0.0-...-638da55 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: MIT Imports: 14 Imported by: 0

README

git-module

git命令封装

Documentation

Index

Constants

View Source
const DefaultTimeout = time.Minute

DefaultTimeout is the default timeout duration for all commands.

View Source
const LogFormatHashOnly = `format:%H`

Variables

View Source
var (
	ErrParentNotExist    = errors.New("parent does not exist")
	ErrSubmoduleNotExist = errors.New("submodule does not exist")
	ErrRevisionNotExist  = errors.New("revision does not exist")
	ErrRemoteNotExist    = errors.New("remote does not exist")
	ErrExecTimeout       = errors.New("execution was timed out")
	ErrNoMergeBase       = errors.New("no merge based was found")
	ErrNotBlob           = errors.New("the entry is not a blob")
)

Functions

func BinVersion

func BinVersion() (string, error)

BinVersion returns current Git binary version that is used by this module.

func Clone

func Clone(url, dst string, opts ...CloneOptions) error

Clone clones the repository from remote URL to the destination.

func ExecCommandString

func ExecCommandString(dir, name, cmd string) (string, error)

func Init

func Init(path string, opts ...InitOptions) error

Init initializes a new Git repository.

func RepoAdd

func RepoAdd(repoPath string, opts ...AddOptions) error

RepoAdd adds local changes to index for the repository in given path.

func RepoCheckout

func RepoCheckout(repoPath, branch string, opts ...CheckoutOptions) error

Checkout checks out to given branch for the repository in given path.

func RepoFsck

func RepoFsck(repoPath string, opts ...FsckOptions) error

RepoFsck verifies the connectivity and validity of the objects in the database for the repository in given path.

func RepoMove

func RepoMove(repoPath, src, dst string, opts ...MoveOptions) error

RepoMove moves a file, a directory, or a symlink file or directory from source to destination for the repository in given path.

func RepoPush

func RepoPush(repoPath, remote, branch string, opts ...PushOptions) error

RepoPush pushs local changes to given remote and branch for the repository in given path.

func RepoReset

func RepoReset(repoPath, rev string, opts ...ResetOptions) error

RepoReset resets working tree to given revision for the repository in given path.

func SetOutput

func SetOutput(output io.Writer)

SetOutput sets the output writer for logs.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the prefix to be prepended to each log entry.

Types

type AddOptions

type AddOptions struct {
	// Indicates whether to add all changes to index.
	All bool
	// The specific pathspecs to be added to index.
	Pathsepcs []string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

AddOptions contains optional arguments for adding local changes. Docs: https://git-scm.com/docs/git-add

type CheckoutOptions

type CheckoutOptions struct {
	// The base branch if checks out to a new branch.
	BaseBranch string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

CheckoutOptions contains optional arguments for checking out to a branch. Docs: https://git-scm.com/docs/git-checkout

type CloneOptions

type CloneOptions struct {
	// Indicates whether the repository should be cloned as a mirror.
	Mirror bool
	// Indicates whether the repository should be cloned in bare format.
	Bare bool
	// Indicates whether to suppress the log output.
	Quiet bool
	// The branch to checkout for the working tree when Bare=false.
	Branch string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

CloneOptions contains optional arguments for cloning a repository. Docs: https://git-scm.com/docs/git-clone

type Command

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

Command contains the name, arguments and environment variables of a command.

func NewCommand

func NewCommand(args ...string) *Command

NewCommand creates and returns a new Command with given arguments for "git".

func (*Command) AddArgs

func (c *Command) AddArgs(args ...string) *Command

AddArgs appends given arguments to the command.

func (*Command) AddEnvs

func (c *Command) AddEnvs(envs ...string) *Command

AddEnvs appends given environment variables to the command.

func (*Command) Run

func (c *Command) Run() ([]byte, error)

Run executes the command in working directory and default timeout duration. It returns stdout in string and error (combined with stderr).

func (*Command) RunCmd

func (c *Command) RunCmd(dir string) (string, error)

func (*Command) RunInDir

func (c *Command) RunInDir(dir string) ([]byte, error)

RunInDir executes the command in given directory and default timeout duration. It returns stdout and error (combined with stderr).

func (*Command) RunInDirPipeline

func (c *Command) RunInDirPipeline(stdout, stderr io.Writer, dir string) error

RunInDirPipeline executes the command in given directory and default timeout duration. It pipes stdout and stderr to supplied io.Writer.

func (*Command) RunInDirPipelineWithTimeout

func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, stderr io.Writer, dir string) (err error)

RunInDirPipelineWithTimeout executes the command in given directory and timeout duration. It pipes stdout and stderr to supplied io.Writer. DefaultTimeout will be used if the timeout duration is less than time.Nanosecond (i.e. less than or equal to 0). It returns an ErrExecTimeout if the execution was timed out.

func (*Command) RunInDirWithTimeout

func (c *Command) RunInDirWithTimeout(timeout time.Duration, dir string) ([]byte, error)

RunInDirWithTimeout executes the command in given directory and timeout duration. It returns stdout in []byte and error (combined with stderr).

func (*Command) RunWithTimeout

func (c *Command) RunWithTimeout(timeout time.Duration) ([]byte, error)

RunWithTimeout executes the command in working directory and given timeout duration. It returns stdout in string and error (combined with stderr).

func (*Command) String

func (c *Command) String() string

String returns the string representation of the command.

type CountObject

type CountObject struct {
	Count         int64
	Size          int64
	InPack        int64
	Packs         int64
	SizePack      int64
	PrunePackable int64
	Garbage       int64
	SizeGarbage   int64
}

CountObject contains disk usage report of a repository.

func RepoCountObjects

func RepoCountObjects(repoPath string, opts ...CountObjectsOptions) (*CountObject, error)

RepoCountObjects returns disk usage report of the repository in given path.

type CountObjectsOptions

type CountObjectsOptions struct {
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

CountObjectsOptions contains optional arguments for counting objects. Docs: https://git-scm.com/docs/git-count-objects

type Entry

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

func NewEntry

func NewEntry(data string) *Entry

git ls-tree -l --full-tree HEAD cmd/|while read filename;do echo "$filename $(git log -1 --format="|%h|%s|%cr|%ai|%an" -- `echo $filename|awk '{print $5}'`)" ;done 100644 blob 206c042ada4cb14d8f068c364b4b623a45038425 1799 cmd/bolt.go |c25d9ea|boltapi|10 months ago|2020-03-20 19:17:37 +0800|yckj0834 100644|blob|8923a8f720912df0810262bfbe74a0ee65f07d1d|3496|cmd/tty.go |c9e64fd|remove local tty|9 months ago|2020-04-08 03:30:17 +0800|yckj0834

type FetchOptions

type FetchOptions struct {
	// Indicates whether to prune during fetching.
	Prune bool
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

FetchOptions contains optional arguments for fetching repository updates. Docs: https://git-scm.com/docs/git-fetch

type FsckOptions

type FsckOptions struct {
	// The additional arguments to be applied.
	Args []string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

FsckOptions contains optional arguments for verifying the objects. Docs: https://git-scm.com/docs/git-fsck

type InitOptions

type InitOptions struct {
	// Indicates whether the repository should be initialized in bare format.
	Bare bool
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

InitOptions contains optional arguments for initializing a repository. Docs: https://git-scm.com/docs/git-init

type MoveOptions

type MoveOptions struct {
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

MoveOptions contains optional arguments for moving a file, a directory, or a symlink. Docs: https://git-scm.com/docs/git-mv

type NameStatus

type NameStatus struct {
	Added    []string
	Removed  []string
	Modified []string
}

NameStatus contains name status of a commit.

func RepoShowNameStatus

func RepoShowNameStatus(repoPath, rev string, opts ...ShowNameStatusOptions) (*NameStatus, error)

RepoShowNameStatus returns name status of given revision of the repository in given path.

type ObjectType

type ObjectType string

ObjectType is the type of a Git objet.

const (
	ObjectCommit ObjectType = "commit"
	ObjectTree   ObjectType = "tree"
	ObjectBlob   ObjectType = "blob"
	ObjectTag    ObjectType = "tag"
)

A list of object types.

type PullOptions

type PullOptions struct {
	// Indicates whether to rebased during pulling.
	Rebase bool
	// Indicates whether to pull from all remotes.
	All bool
	// The remote to pull updates from when All=false.
	Remote string
	// The branch to pull updates from when All=false and Remote is supplied.
	Branch string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

PullOptions contains optional arguments for pulling repository updates. Docs: https://git-scm.com/docs/git-pull

type PushOptions

type PushOptions struct {
	// The environment variables set for the push.
	Envs []string
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

PushOptions contains optional arguments for pushing repository changes. Docs: https://git-scm.com/docs/git-push

type Repository

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

func Open

func Open(repoPath string) (*Repository, error)

Open opens the repository at the given path. It returns an os.ErrNotExist if the path does not exist.

func (*Repository) Add

func (r *Repository) Add(opts ...AddOptions) error

Add adds local changes to index for the repository.

func (*Repository) Checkout

func (r *Repository) Checkout(branch string, opts ...CheckoutOptions) error

Checkout checks out to given branch for the repository.

func (*Repository) CountObjects

func (r *Repository) CountObjects(opts ...CountObjectsOptions) (*CountObject, error)

CountObjects returns disk usage report of the repository.

func (*Repository) Fetch

func (r *Repository) Fetch(opts ...FetchOptions) error

Fetch fetches updates for the repository.

func (*Repository) Fsck

func (r *Repository) Fsck(opts ...FsckOptions) error

Fsck verifies the connectivity and validity of the objects in the database for the repository.

func (*Repository) Move

func (r *Repository) Move(src, dst string, opts ...MoveOptions) error

Move moves a file, a directory, or a symlink file or directory from source to destination for the repository.

func (*Repository) Pull

func (r *Repository) Pull(opts ...PullOptions) error

Pull pulls updates for the repository.

func (*Repository) Push

func (r *Repository) Push(remote, branch string, opts ...PushOptions) error

Push pushs local changes to given remote and branch for the repository.

func (*Repository) Reset

func (r *Repository) Reset(rev string, opts ...ResetOptions) error

Reset resets working tree to given revision for the repository.

func (*Repository) RevParse

func (r *Repository) RevParse(rev string, opts ...RevParseOptions) (string, error)

RevParse returns full length (40) commit ID by given revision in the repository.

func (*Repository) ShowNameStatus

func (r *Repository) ShowNameStatus(rev string, opts ...ShowNameStatusOptions) (*NameStatus, error)

ShowNameStatus returns name status of given revision of the repository.

func (*Repository) String

func (r *Repository) String() string

type ResetOptions

type ResetOptions struct {
	// Indicates whether to perform a hard reset.
	Hard bool
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

ResetOptions contains optional arguments for resetting a branch. Docs: https://git-scm.com/docs/git-reset

type RevParseOptions

type RevParseOptions struct {
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

RevParseOptions contains optional arguments for parsing revision. Docs: https://git-scm.com/docs/git-rev-parse

type ShowNameStatusOptions

type ShowNameStatusOptions struct {
	// The timeout duration before giving up for each shell command execution.
	// The default timeout duration will be used when not supplied.
	Timeout time.Duration
}

ShowNameStatusOptions contains optional arguments for showing name status. Docs: https://git-scm.com/docs/git-show#Documentation/git-show.txt---name-status

type Tree

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

func NewTree

func NewTree(dir, path, branch string) *Tree

func (*Tree) LsTree

func (t *Tree) LsTree() error

git ls-tree -l --full-tree HEAD cmd/|while read filename;do echo "$(echo $filename|awk '{print $1"|"$2"|"$3"|"$4"|"$5}') $(git log -1 --format="|%h|%s|%cr|%ai|%an" -- `echo $filename|awk '{print $5}'`)" ;done

Jump to

Keyboard shortcuts

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