git

package
v3.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: GPL-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package git provides functionality to interact with a Git repository.

Index

Constants

View Source
const (

	// H tracked file that is not either unmerged or skip-worktree
	ObjectStatusCached byte = 'H'
	// S tracked file that is skip-worktree
	ObjectStatusSkipWorktree byte = 'S'
	// M tracked file that is unmerged
	ObjectStatusUnmerged byte = 'M'
	// R tracked file with unstaged removal/deletion
	ObjectStatusRemoved byte = 'R'
	// C tracked file with unstaged modification/change
	ObjectStatusChanged byte = 'C'
	// K untracked paths which are part of file/directory conflicts which
	// prevent checking out tracked files
	ObjectStatusToBeKilled byte = 'K'
	// ? untracked file
	ObjectStatusUntracked byte = '?'
	// U file with resolve-undo information
	ObjectStatusWithResolveUndoInfo byte = 'U'
)
View Source
const Name = "git"

Variables

View Source
var ErrObjectNotFound = errors.New("git object id not found, file might not exist, untracked or modified")
View Source
var ErrRepositoryNotFound = errors.New("git repository not found")

ErrRepositoryNotFound is returned when a directory is not part of a git repository.

Functions

func CommandIsInstalled

func CommandIsInstalled() bool

CommandIsInstalled returns true if an executable called "git" is found in the directories listed in the PATH environment variable.

func CommitID

func CommitID(dir string) (string, error)

CommitID return the commit id of HEAD by running git rev-parse in the passed directory

func IsGitDir

func IsGitDir(dir string) (bool, error)

IsGitDir checks if the passed directory is part of a git repository. It returns true if dir or any of its parent directory containing a directory named ".git".

func LsFiles added in v3.3.0

func LsFiles(ctx context.Context, dir string, skipUntracked bool, ch chan<- *Object) error

LsFiles retrieves information about files in the index and working tree. The information is sent to ch. For a path multiple objects might be sent, if the status in the index and working tree differs. Listing files in git submodules is not supported. The function closes ch when it terminates.

func ObjectID added in v3.3.0

func ObjectID(ctx context.Context, absFilePath, repoRelFilePath string) (string, error)

ObjectID calculates the git ID (hash) of a fille.

func UntrackedFiles

func UntrackedFiles(dir string) ([]string, error)

UntrackedFiles returns a list of untracked and modified files in the git repository. Files that exist and are in a .gitignore file are included.

func WorktreeIsDirty

func WorktreeIsDirty(dir string) (bool, error)

WorktreeIsDirty returns true if the repository contains modified files, untracked files are considered, files in .gitignore are ignored

Types

type Mode added in v3.4.0

type Mode uint32

Mode is the file mode from git (https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) It is either: 100644 - normal file, 100755 - executable file, 120000 symbolic link,

const (
	ObjectTypeSymlink    Mode = 0o120000
	ObjectTypeFile       Mode = 0o100000
	ObjectTypeExectuable Mode = 0o100755
)

func (Mode) IsRegularFile added in v3.4.0

func (m Mode) IsRegularFile() bool

IsRegularFile returns true if m is ObjectTypeFile or ObjectTypeExectuable.

func (m Mode) IsSymlink() bool

IsSymlink returns true if m is ObjectTypeSymlink.

type Object added in v3.3.0

type Object struct {
	Status byte
	Mode   Mode
	// Name is the hash of the object
	Name    string
	RelPath string
}

func (*Object) String added in v3.3.0

func (o *Object) String() string

type PrintfFn added in v3.4.0

type PrintfFn func(format string, a ...any)

type Repository

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

Repository reads information from a Git repository.

func NewRepository

func NewRepository(dir string) *Repository

NewRepository returns a new Repository object that interacts with the git repository in dir.

func NewRepositoryWithCheck added in v3.4.2

func NewRepositoryWithCheck(dir string) (*Repository, error)

NewRepositoryWithCheck returns a new Repository object for the git repository at dir. If dir is not part of a git repository, the "git" command is not installed or can not be located in $PATH an error is returned.

func (*Repository) CommitID

func (g *Repository) CommitID() (string, error)

CommitID calls git.CommitID() for the repository. After the first successful call the commit ID is stored and the stored value is returned on successive calls.

func (*Repository) Name added in v3.2.0

func (g *Repository) Name() string

func (*Repository) UntrackedFiles added in v3.2.0

func (g *Repository) UntrackedFiles() ([]string, error)

UntrackedFiles returns a list of untracked and modified files in the git repository. Files that exist and are in a .gitignore file are included.

func (*Repository) WithoutUntracked

func (g *Repository) WithoutUntracked(paths ...string) ([]string, error)

func (*Repository) WorktreeIsDirty

func (g *Repository) WorktreeIsDirty() (bool, error)

WorktreeIsDirty calls git.WorktreeIsDirty. After the first successful call the result is stored and the stored value is returned on successive calls.

type TrackedObject added in v3.4.0

type TrackedObject struct {
	ObjectID string
	Mode     Mode
}

type TrackedObjects added in v3.4.0

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

TrackedObjects stores information about tracked and unmodified files in a git repository.

func NewTrackedObjects added in v3.4.0

func NewTrackedObjects(gitRepositoryDir string, debugLogf PrintfFn) *TrackedObjects

func (*TrackedObjects) Get added in v3.4.0

func (h *TrackedObjects) Get(ctx context.Context, absPath string) (*TrackedObject, error)

Get returns a TrackedObject for the file at absPath in the git repository. If the file does not exist, is untracked or modified ErrObjectNotFound is returned.

On the first call, the objects are read from the Git repository. If it fails, the call and following runs are returning the occurred error.

Jump to

Keyboard shortcuts

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