gitrepo

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package gitrepo provides operations on git repos.

Index

Constants

View Source
const RootPath = "."

Variables

View Source
var ErrEmptyCommitMessage = errors.New("empty commit message")

ErrEmptyCommitMessage returns when the commit message is empty.

View Source
var ErrNoModificationsToCommit = errors.New("no modifications to commit")

ErrNoModificationsToCommit is returned when a commit is attempted on a clean worktree.

Functions

This section is empty.

Types

type Commit

type Commit struct {
	Hash    plumbing.Hash
	Message string
	When    time.Time
}

Commit represents a git commit.

type ConventionalCommit added in v0.4.0

type ConventionalCommit struct {
	// Type is the type of change (e.g., "feat", "fix", "docs").
	Type string `yaml:"type" json:"type"`
	// Subject is the short summary of the change.
	Subject string `yaml:"subject" json:"subject"`
	// Body is the long-form description of the change.
	Body string `yaml:"body" json:"body"`
	// LibraryID is the library ID the commit associated with.
	LibraryID string `yaml:"-" json:"-"`
	// Footers contain metadata (e.g,"BREAKING CHANGE", "Reviewed-by").
	// Repeated footer keys not supported, only first value is kept
	Footers map[string]string `yaml:"-" json:"-"`
	// IsBreaking indicates if the commit introduces a breaking change.
	IsBreaking bool `yaml:"-" json:"-"`
	// IsNested indicates if the commit is a nested commit.
	IsNested bool `yaml:"-" json:"-"`
	// CommitHash is the full commit hash.
	CommitHash string `yaml:"-" json:"commit_hash,omitempty"`
	// When is the timestamp of the commit.
	When time.Time `yaml:"-" json:"-"`
}

ConventionalCommit represents a parsed conventional commit message. See https://www.conventionalcommits.org/en/v1.0.0/ for details.

func ParseCommits added in v0.4.0

func ParseCommits(commit *Commit, libraryID string) ([]*ConventionalCommit, error)

ParseCommits parses a commit message into a slice of ConventionalCommit structs.

It supports a top-level commit wrapped in BEGIN_COMMIT and END_COMMIT (BEGIN_COMMIT_OVERRIDE and END_COMMIT_OVERRIDE are also supported for backward compatibility). If found, this block takes precedence, and only its content will be parsed.

The message can also contain multiple nested commits, each wrapped in BEGIN_NESTED_COMMIT and END_NESTED_COMMIT markers.

Malformed override or nested blocks (e.g., with a missing end marker) are ignored. Any commit part that is found but fails to parse as a valid conventional commit is logged and skipped.

func (*ConventionalCommit) MarshalJSON added in v0.4.0

func (c *ConventionalCommit) MarshalJSON() ([]byte, error)

MarshalJSON implements a custom JSON marshaler for ConventionalCommit.

type LocalRepository added in v0.1.1

type LocalRepository struct {
	Dir string
	// contains filtered or unexported fields
}

LocalRepository represents a git repository.

func NewRepository

func NewRepository(opts *RepositoryOptions) (*LocalRepository, error)

NewRepository provides access to a git repository based on the provided options.

If opts.Clone is CloneOptionNone, it opens an existing repository at opts.Dir. If opts.Clone is CloneOptionMaybe, it opens the repository if it exists, otherwise it clones from opts.RemoteURL. If opts.Clone is CloneOptionAlways, it always clones from opts.RemoteURL.

func (*LocalRepository) AddAll added in v0.1.1

func (r *LocalRepository) AddAll() error

AddAll adds all pending changes from the working tree to the index, so that the changes can later be committed.

func (*LocalRepository) ChangedFiles added in v0.4.0

func (r *LocalRepository) ChangedFiles() ([]string, error)

ChangedFiles returns a list of files that have been modified, added, or deleted in the working tree, including both staged and unstaged changes.

func (*LocalRepository) ChangedFilesInCommit added in v0.2.0

func (r *LocalRepository) ChangedFilesInCommit(commitHash string) ([]string, error)

ChangedFilesInCommit returns the files changed in the given commit.

func (*LocalRepository) Checkout added in v0.5.0

func (r *LocalRepository) Checkout(commitSha string) error

Checkout checks out the local repository at the provided git SHA.

func (*LocalRepository) CheckoutCommitAndCreateBranch added in v0.6.0

func (r *LocalRepository) CheckoutCommitAndCreateBranch(name, commitHash string) error

CheckoutCommitAndCreateBranch creates a new git branch from a specific commit hash and checks out the branch in the local git repository.

func (*LocalRepository) CleanUntracked added in v0.2.0

func (r *LocalRepository) CleanUntracked(paths []string) error

CleanUntracked removes untracked files within the given paths.

func (*LocalRepository) Commit added in v0.1.1

func (r *LocalRepository) Commit(msg string) error

Commit creates a new commit with the provided message and author information.

func (*LocalRepository) CreateBranchAndCheckout added in v0.2.0

func (r *LocalRepository) CreateBranchAndCheckout(name string) error

CreateBranchAndCheckout creates a new git branch and checks out the branch in the local git repository.

func (*LocalRepository) DeleteBranch added in v0.2.0

func (r *LocalRepository) DeleteBranch(branchName string) error

DeleteBranch deletes a branch on the origin remote.

func (*LocalRepository) DeleteLocalBranches added in v0.6.0

func (r *LocalRepository) DeleteLocalBranches(names []string) error

DeleteLocalBranches deletes a list of local branches. It returns an error if any branch deletion fails, or nil if all succeed.

func (*LocalRepository) GetCommit added in v0.2.0

func (r *LocalRepository) GetCommit(commitHash string) (*Commit, error)

GetCommit returns a commit for the given commit hash.

func (*LocalRepository) GetCommitsForPathsSinceCommit added in v0.2.0

func (r *LocalRepository) GetCommitsForPathsSinceCommit(paths []string, sinceCommit string) ([]*Commit, error)

GetCommitsForPathsSinceCommit returns the commits affecting any of the given paths, stopping looking at the given commit (which is not included in the results). The returned commits are ordered such that the most recent commit is first.

If sinceCommit is not provided, all commits are searched; otherwise, if no commit matching sinceCommit is found, an error is returned.

func (*LocalRepository) GetCommitsForPathsSinceTag added in v0.2.0

func (r *LocalRepository) GetCommitsForPathsSinceTag(paths []string, tagName string) ([]*Commit, error)

GetCommitsForPathsSinceTag returns all commits since tagName that contains files in paths.

If tagName empty, all commits for the given paths are returned.

func (*LocalRepository) GetDir added in v0.1.1

func (r *LocalRepository) GetDir() string

GetDir returns the directory of the repository.

func (*LocalRepository) GetHashForPath added in v0.5.0

func (r *LocalRepository) GetHashForPath(commitHash, path string) (string, error)

GetHashForPath returns a tree hash for the specified path, at the given commit in this repository. If the path does not exist at the commit, an empty string is returned rather than an error, as the purpose of this function is to allow callers to determine changes in the tree. (A path going from missing to anything else, or vice versa, indicates a change. A path being missing at two different commits is not a change.)

func (*LocalRepository) GetLatestCommit added in v0.4.0

func (r *LocalRepository) GetLatestCommit(path string) (*Commit, error)

GetLatestCommit returns the latest commit of the given path in the repository.

func (*LocalRepository) HeadHash added in v0.2.0

func (r *LocalRepository) HeadHash() (string, error)

HeadHash returns hash of the commit for the repository's HEAD.

func (*LocalRepository) IsClean added in v0.1.1

func (r *LocalRepository) IsClean() (bool, error)

IsClean reports whether the working tree has no uncommitted changes.

func (*LocalRepository) NewAndDeletedFiles added in v0.6.0

func (r *LocalRepository) NewAndDeletedFiles() ([]string, error)

NewAndDeletedFiles returns a list of files that are new or deleted.

func (*LocalRepository) Push added in v0.2.0

func (r *LocalRepository) Push(branchName string) error

Push pushes the local branch to the origin remote.

func (*LocalRepository) Remotes added in v0.1.1

func (r *LocalRepository) Remotes() ([]*Remote, error)

Remotes returns the remotes within the repository.

func (*LocalRepository) ResetHard added in v0.6.0

func (r *LocalRepository) ResetHard() error

ResetHard resets the repository to HEAD, discarding all local changes.

func (*LocalRepository) ResetSoft added in v0.6.0

func (r *LocalRepository) ResetSoft(commit string) error

ResetSoft resets the current branch head to a specific commit but leaves the working tree and index untouched.

func (*LocalRepository) Restore added in v0.2.0

func (r *LocalRepository) Restore(paths []string) error

Restore restores changes in the working tree, leaving staged area untouched. Note that untracked files, if any, are not touched.

Wrap git operations in exec, because git.Worktree.Restore does not support this operation.

type Remote added in v0.3.0

type Remote struct {
	Name string
	URLs []string
}

Remote represent a git remote.

type Repository

type Repository interface {
	AddAll() error
	Commit(msg string) error
	IsClean() (bool, error)
	Remotes() ([]*Remote, error)
	GetDir() string
	HeadHash() (string, error)
	ChangedFilesInCommit(commitHash string) ([]string, error)
	ChangedFiles() ([]string, error)
	GetCommit(commitHash string) (*Commit, error)
	GetLatestCommit(path string) (*Commit, error)
	GetCommitsForPathsSinceTag(paths []string, tagName string) ([]*Commit, error)
	GetCommitsForPathsSinceCommit(paths []string, sinceCommit string) ([]*Commit, error)
	CreateBranchAndCheckout(name string) error
	CheckoutCommitAndCreateBranch(name, commitHash string) error
	NewAndDeletedFiles() ([]string, error)
	Push(branchName string) error
	Restore(paths []string) error
	CleanUntracked(paths []string) error

	Checkout(commitHash string) error
	GetHashForPath(commitHash, path string) (string, error)
	ResetHard() error
	DeleteLocalBranches(names []string) error
	ResetSoft(commit string) error
	// contains filtered or unexported methods
}

Repository defines the interface for git repository operations.

type RepositoryOptions

type RepositoryOptions struct {
	// Dir is the directory where the repository will reside locally. Required.
	Dir string
	// MaybeClone will try to clone the repository if it does not exist locally.
	// If set to true, RemoteURL and RemoteBranch must also be set. Optional.
	MaybeClone bool
	// RemoteURL is the URL of the remote repository to clone from. Required if MaybeClone is set to true.
	RemoteURL string
	// RemoteBranch is the remote branch to clone. Required if MaybeClone is set to true.
	RemoteBranch string
	// CI is the type of Continuous Integration (CI) environment in which
	// the tool is executing.
	CI string
	// GitPassword is used for HTTP basic auth.
	GitPassword string
	// Depth controls the cloning depth if the repository needs to be cloned.
	Depth int
}

RepositoryOptions are used to configure a LocalRepository.

Jump to

Keyboard shortcuts

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