Help us shape the future of Go! Take the 2026 Go Developer Survey

git

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGitNotFound = errors.New("diff mode requires git, which was not found in PATH")
	ErrNotARepo    = errors.New("not a git repository")
	ErrUnknownRef  = errors.New("unknown ref")
	ErrNoMergeBase = errors.New("no merge-base")
)

Sentinel errors for diff-mode failures; match with errors.Is.

Functions

func TopLevel added in v1.5.0

func TopLevel(dir string) (string, error)

TopLevel returns the absolute, symlink-resolved root of the work tree containing dir. It returns an error wrapping ErrNotARepo when dir is not inside a git work tree (or git is unavailable).

Types

type ChangeSet added in v1.5.0

type ChangeSet struct {
	// Files changed (added, copied, modified, renamed), relative to the
	// directory passed to ChangedFiles, sorted. Deleted files are omitted.
	Files []string
	// Base is the resolved commit the working tree is compared against
	// (ModeSince: the ref; ModeBranch: the merge-base; ModeStaged: empty).
	Base string
	// PartiallyStaged lists staged files that also have unstaged changes
	// (ModeStaged only).
	PartiallyStaged []string
}

ChangeSet is the result of ChangedFiles.

func ChangedFiles added in v1.5.0

func ChangedFiles(dir string, spec Spec) (*ChangeSet, error)

ChangedFiles lists the files changed according to spec, relative to dir.

All git commands run inside dir with --relative, so when dir is a subdirectory of the repository, only changes under it are returned. Untracked files never appear (git diff does not list them).

type Info

type Info struct {
	// Current commit hash (full SHA)
	Commit string

	// Current branch name
	Branch string
}

Info contains Git repository metadata.

This type holds Git information extracted from a repository at the time of analysis. Both fields are optional - if the directory is not a Git repository or Git is not available, both will be empty strings.

Used to correlate analysis reports with specific code versions and improve comparison accuracy across runs.

func ExtractInfo

func ExtractInfo(projectPath string) Info

ExtractInfo attempts to extract Git metadata from a directory.

This function runs git commands to get the current commit hash and branch name. It gracefully handles non-Git directories and missing git installations by returning empty strings rather than errors.

The function executes:

  • `git rev-parse HEAD` for the commit hash
  • `git rev-parse --abbrev-ref HEAD` for the branch name

Both commands are run in the specified directory. If either fails (not a git repo, git not installed, etc.), that field will be empty.

Parameters:

  • projectPath: The directory to extract Git info from

Returns:

  • Info struct with commit and branch (may be empty if not a Git repo)

Example:

info := git.ExtractInfo("/path/to/project")
if info.Commit != "" {
    fmt.Printf("Analyzed commit: %s on branch %s\n", info.Commit, info.Branch)
}

type Mode added in v1.5.0

type Mode string

Mode selects which change set diff-based analysis looks at.

const (
	// ModeStaged compares the index with HEAD (pre-commit).
	ModeStaged Mode = "staged"
	// ModeSince compares a ref with the working tree.
	ModeSince Mode = "since"
	// ModeBranch compares merge-base(ref, HEAD) with the working tree (PR / CI).
	ModeBranch Mode = "branch"
)

type Spec added in v1.5.0

type Spec struct {
	Mode Mode
	Ref  string // Unused for ModeStaged
}

Spec describes the requested change set.

func (Spec) Flag added in v1.5.0

func (s Spec) Flag() string

Flag returns the CLI flag that selects this mode, for error messages.

Jump to

Keyboard shortcuts

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