git

package
v0.0.0-...-4ff3f89 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: BSD-3-Clause Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// MasterBranch is the name of the default branch for most repositories.
	MasterBranch = git_common.MasterBranch
	// MainBranch is the name of the default branch for some
	// repositories which don't use MasterBranch.
	// TODO(rmistry): Delete this after http://skbug.com/11842 is resolved.
	MainBranch = git_common.MainBranch
	// DefaultRef is the fully-qualified ref name of the default branch for most
	// repositories.
	DefaultRef = git_common.DefaultRef
	// DefaultRemote is the name of the default remote repository.
	DefaultRemote = git_common.DefaultRemote
	// DefaultRemoteBranch is the name of the default branch in the default
	// remote repository, for most repos.
	DefaultRemoteBranch = git_common.DefaultRemoteBranch
)

Variables

View Source
var ErrorNotFound = skerr.Fmt("file not found")
View Source
var ErrorNotSubmodule = skerr.Fmt("not a submodule")

Functions

func AddTrailer

func AddTrailer(commitMsg, trailer string) (string, error)

AddTrailer adds a trailer to the given commit message.

func BranchBaseName

func BranchBaseName(branch string) string

BranchBaseName ensures that the branch does not have the refs/heads/ prefix.

func Clone

func Clone(ctx context.Context, repoUrl, dest string, mirror bool) error

Clone runs "git clone" into the given destination directory. Most callers should use NewRepo or NewCheckout instead.

func DeleteLockFiles

func DeleteLockFiles(ctx context.Context, workdir string) error

DeleteLockFiles finds and deletes Git lock files within the given workdir.

func Executable

func Executable(ctx context.Context) (string, error)

Executable returns the path to Git.

func FullyQualifiedBranchName

func FullyQualifiedBranchName(branch string) string

FullyQualifiedBranchName ensures that the branch has the refs/heads/ prefix.

func GetBoolFooterVal

func GetBoolFooterVal(footersMap map[string]string, footer string, issue int64) bool

GetBoolFooterVal looks for the specified footer in the footersMap and returns it's boolean value. If the footer is not found then false is returned. If the value is not boolean then false is returned and an error is logged.

func GetFootersMap

func GetFootersMap(commitMsg string) map[string]string

GetFootersMap parses the specified commit msg and returns it's footers. Invalid footer formats are logged. Eg: commit msg: "test test\n\nBug: skia:123\nTested: true" will return {"skia": "123", "Tested": "true"}.

func GetStringFooterVal

func GetStringFooterVal(footersMap map[string]string, footer string) string

GetStringFooterVal looks for the specified footer in the footersMap and returns it's strings value. If the footer is not found then an empty string is returned.

func IsCommitHash

func IsCommitHash(s string) bool

IsCommitHash returns true if the given string looks like a valid (possibly shortened) Git commit hash.

func IsFullCommitHash

func IsFullCommitHash(s string) bool

IsFullCommitHash returns true if the given string looks like a full 40- character Git commit hash.

func JoinTrailers

func JoinTrailers(bodyLines, trailers []string) string

JoinTrailers joins a main commit message body with a trailers footer.

func LogFromTo

func LogFromTo(from, to string) string

LogFromTo returns a string which is used to log from one commit to another. It is important to note that:

  • The results may include the second commit but will not include the first.
  • The results include all commits reachable from the first commit which are not reachable from the second, ie. if there is a merge in the given range, the results will include that line of history and not just the commits which are descendants of the first commit. If you want only commits which are ancestors of the second commit AND descendants of the first, you should use LogLinear, but note that the results will be empty if the first commit is not an ancestor of the second, ie. they're on different branches.

func MakeFileInfo

func MakeFileInfo(name, mode string, typ ObjectType, size int) (fs.FileInfo, error)

MakeFileInfo returns an fs.FileInfo with the given information.

func NormalizeURL

func NormalizeURL(inputURL string) (string, error)

NormalizeURL strips everything from the URL except for the host and the path. A trailing ".git" is also stripped. The purpose is to allow for small variations in repo URL to be recognized as the same repo. The URL needs to contain a valid transport protocol, e.g. https, ssh. These URLs will all return 'github.com/skia-dev/textfiles':

"https://github.com/skia-dev/textfiles.git"
"ssh://git@github.com/skia-dev/textfiles"
"ssh://git@github.com:skia-dev/textfiles.git"

func ParseDir

func ParseDir(contents []byte) ([]fs.FileInfo, error)

ParseDir parses the contents of a directory. Expects the contents to be in the format used by git, ie. lines taking the form:

mode tree|blob hash name

func SplitTrailers

func SplitTrailers(commitMsg string) ([]string, []string)

SplitTrailers splits a commit message into a main commit message body and trailers footer. Assumes that the commit message is already well-formed with respect to trailers, ie. there is an empty line between the last body paragraph and the single trailers paragraph, which contains only lines in "key: value" format.

Types

type Branch

type Branch struct {
	// The human-readable name of the branch.
	Name string `json:"name"`

	// The commit hash pointed to by this branch.
	Head string `json:"head"`
}

Branch describes a Git branch.

type BranchList

type BranchList []*Branch

BranchList is a slice of Branch objects which implements sort.Interface.

func (BranchList) Len

func (bl BranchList) Len() int

func (BranchList) Less

func (bl BranchList) Less(a, b int) bool

func (BranchList) Swap

func (bl BranchList) Swap(a, b int)

type Checkout

type Checkout interface {
	GitDir

	// FetchRefFromRepo syncs the specified ref from the repo without modifying the
	// working copy.
	FetchRefFromRepo(ctx context.Context, repo, ref string) error

	// Fetch syncs refs from the remote without modifying the working copy.
	Fetch(ctx context.Context) error

	// AddRemote checks to see if a remote already exists in the checkout, if it
	// exists then the URL is matched with the repoURL. If the remote does not exist
	// then it is added.
	AddRemote(ctx context.Context, remote, repoUrl string) error

	// CleanupBranch forcibly resets all changes and checks out the given branch,
	// forcing it to match the same branch from origin. All local changes will be
	// lost.
	CleanupBranch(ctx context.Context, branch string) error

	// Cleanup forcibly resets all changes and checks out the main branch to match
	// that of the remote. All local changes will be lost.
	Cleanup(ctx context.Context) error

	// UpdateBranch syncs the Checkout from its remote. Forcibly resets and checks
	// out the given branch, forcing it to match the same branch from origin. All
	// local changes will be lost. Equivalent to c.Fetch() + c.CleanupBranch().
	UpdateBranch(ctx context.Context, branch string) error

	// Update syncs the Checkout from its remote. Forcibly resets and checks out
	// the main branch to match the remote. All local changes will be lost.
	// Equivalent to c.Fetch() + c.Cleanup().
	Update(ctx context.Context) error

	// IsDirty returns true if the Checkout is dirty, ie. any of the following are
	// true:
	// 1. There are unstaged changes.
	// 2. There are untracked files (not including .gitignore'd files).
	// 3. HEAD is not an ancestor of origin/main.
	//
	// Also returns the output of "git status", for human consumption if desired.
	IsDirty(ctx context.Context) (bool, string, error)
}

Checkout is used for managing a local git checkout.

type CheckoutDir

type CheckoutDir string

CheckoutDir implements Checkout.

func NewCheckout

func NewCheckout(ctx context.Context, repoUrl, workdir string) (CheckoutDir, error)

NewCheckout returns a Checkout instance based in the given working directory. Uses any existing checkout in the given directory, or clones one if necessary. In general, servers should use Repo instead of Checkout unless it is absolutely necessary to have a working copy.

func (CheckoutDir) AddRemote

func (c CheckoutDir) AddRemote(ctx context.Context, remote, repoUrl string) error

AddRemote checks to see if a remote already exists in the checkout, if it exists then the URL is matched with the repoURL. If the remote does not exist then it is added.

func (CheckoutDir) Branches

func (c CheckoutDir) Branches(ctx context.Context) ([]*Branch, error)

Branches runs "git branch" and returns a slice of Branch instances.

func (CheckoutDir) CatFile

func (c CheckoutDir) CatFile(ctx context.Context, ref, path string) ([]byte, error)

CatFile runs "git cat-file -p <ref>:<path>".

func (CheckoutDir) Cleanup

func (c CheckoutDir) Cleanup(ctx context.Context) error

Cleanup forcibly resets all changes and checks out the main branch to match that of the remote. All local changes will be lost.

func (CheckoutDir) CleanupBranch

func (c CheckoutDir) CleanupBranch(ctx context.Context, branch string) error

CleanupBranch forcibly resets all changes and checks out the given branch, forcing it to match the same branch from origin. All local changes will be lost.

func (CheckoutDir) Details

func (c CheckoutDir) Details(ctx context.Context, name string) (*vcsinfo.LongCommit, error)

Details returns a vcsinfo.LongCommit instance representing the given commit.

func (CheckoutDir) Dir

func (c CheckoutDir) Dir() string

Dir returns the working directory of the GitDir.

func (CheckoutDir) Fetch

func (c CheckoutDir) Fetch(ctx context.Context) error

Fetch syncs refs from the remote without modifying the working copy.

func (CheckoutDir) FetchRefFromRepo

func (c CheckoutDir) FetchRefFromRepo(ctx context.Context, repo, ref string) error

FetchRefFromRepo syncs the specified ref from the repo without modifying the working copy.

func (CheckoutDir) FullHash

func (c CheckoutDir) FullHash(ctx context.Context, ref string) (string, error)

FullHash gives the full commit hash for the given ref.

func (CheckoutDir) GetBranchHead

func (c CheckoutDir) GetBranchHead(ctx context.Context, branchName string) (string, error)

GetBranchHead returns the commit hash at the HEAD of the given branch.

func (CheckoutDir) GetFile

func (c CheckoutDir) GetFile(ctx context.Context, fileName, commit string) (string, error)

GetFile returns the contents of the given file at the given commit.

func (CheckoutDir) GetRemotes

func (c CheckoutDir) GetRemotes(ctx context.Context) (map[string]string, error)

GetRemotes returns a mapping of remote repo name to URL.

func (CheckoutDir) Git

func (c CheckoutDir) Git(ctx context.Context, cmd ...string) (string, error)

Git runs the given git command in the Checkout.

func (CheckoutDir) IsAncestor

func (c CheckoutDir) IsAncestor(ctx context.Context, a, b string) (bool, error)

IsAncestor returns true iff A is an ancestor of B.

func (CheckoutDir) IsDirty

func (c CheckoutDir) IsDirty(ctx context.Context) (bool, string, error)

IsDirty returns true if the Checkout is dirty, ie. any of the following are true: 1. There are unstaged changes. 2. There are untracked files (not including .gitignore'd files). 3. HEAD is not an ancestor of origin/main.

Also returns the output of "git status", for human consumption if desired.

func (CheckoutDir) IsSubmodule

func (c CheckoutDir) IsSubmodule(ctx context.Context, path, commit string) (bool, error)

IsSubmodule returns true if the given path is submodule, ie contains gitlink.

func (CheckoutDir) NumCommits

func (c CheckoutDir) NumCommits(ctx context.Context) (int64, error)

NumCommits returns the number of commits in the repo.

func (CheckoutDir) ReadDir

func (c CheckoutDir) ReadDir(ctx context.Context, ref, path string) ([]os.FileInfo, error)

ReadDir is analogous to os.File.Readdir for a particular ref.

func (CheckoutDir) ReadSubmodule

func (c CheckoutDir) ReadSubmodule(ctx context.Context, path, commit string) (string, error)

ReadSubmodule returns commit hash of the given path, if the path is git submodule. ErrorNotFound is returned if path is not found in the git worktree. ErrorNotSubmodule is returned if path exists, but it's not a submodule.

func (CheckoutDir) RevList

func (c CheckoutDir) RevList(ctx context.Context, args ...string) ([]string, error)

RevList runs "git rev-list <name>" and returns a slice of commit hashes.

func (CheckoutDir) RevParse

func (c CheckoutDir) RevParse(ctx context.Context, args ...string) (string, error)

RevParse runs "git rev-parse <name>" and returns the result.

func (CheckoutDir) Update

func (c CheckoutDir) Update(ctx context.Context) error

Update syncs the Checkout from its remote. Forcibly resets and checks out the main branch to match the remote. All local changes will be lost. Equivalent to c.Fetch() + c.Cleanup().

func (CheckoutDir) UpdateBranch

func (c CheckoutDir) UpdateBranch(ctx context.Context, branch string) error

UpdateBranch syncs the Checkout from its remote. Forcibly resets and checks out the given branch, forcing it to match the same branch from origin. All local changes will be lost. Equivalent to c.Fetch() + c.CleanupBranch().

func (CheckoutDir) UpdateSubmodule

func (c CheckoutDir) UpdateSubmodule(ctx context.Context, path, commit string) error

UpdateSubmodule updates git submodule of the given path to the given commit. If submodule doesn't exist, it returns ErrorNotFound since it doesn't have all necessary information to create a valid submodule (requires an entry in .gitmodules).

func (CheckoutDir) VFS

func (c CheckoutDir) VFS(ctx context.Context, ref string) (*FS, error)

VFS returns a vfs.FS using Git for the given revision.

func (CheckoutDir) Version

func (c CheckoutDir) Version(ctx context.Context) (int, int, error)

Version returns the Git version.

type FS

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

FS implements vfs.FS using Git for a particular revision.

func VFS

func VFS(ctx context.Context, g GitDir, ref string) (*FS, error)

VFS returns a vfs.FS using Git for the given revision.

func (*FS) Close

func (fs *FS) Close(_ context.Context) error

Close implements vfs.FS.

func (*FS) Create

func (fs *FS) Create(_ context.Context, name string) (vfs.File, error)

Create implements vfs.FS.

func (*FS) Open

func (fs *FS) Open(_ context.Context, name string) (vfs.File, error)

Open implements vfs.FS.

type File

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

File implements vfs.File using Git for a particular revision.

func (*File) Close

func (f *File) Close(_ context.Context) error

Close implements vfs.File.

func (*File) Read

func (f *File) Read(ctx context.Context, buf []byte) (int, error)

Read implements vfs.File.

func (*File) ReadDir

func (f *File) ReadDir(ctx context.Context, n int) ([]fs.FileInfo, error)

ReadDir implements vfs.File.

func (*File) Stat

func (f *File) Stat(ctx context.Context) (fs.FileInfo, error)

Stat implements vfs.File.

func (*File) Write

func (f *File) Write(_ context.Context, _ []byte) (int, error)

Write implements vfs.File.

type GitDir

type GitDir interface {
	// Dir returns the working directory of the GitDir.
	Dir() string

	// Git runs the given git command in the GitDir.
	Git(ctx context.Context, cmd ...string) (string, error)

	// Details returns a vcsinfo.LongCommit instance representing the given commit.
	Details(ctx context.Context, name string) (*vcsinfo.LongCommit, error)

	// RevParse runs "git rev-parse <name>" and returns the result.
	RevParse(ctx context.Context, args ...string) (string, error)

	// RevList runs "git rev-list <name>" and returns a slice of commit hashes.
	RevList(ctx context.Context, args ...string) ([]string, error)

	// GetBranchHead returns the commit hash at the HEAD of the given branch.
	GetBranchHead(ctx context.Context, branchName string) (string, error)

	// Branches runs "git branch" and returns a slice of Branch instances.
	Branches(ctx context.Context) ([]*Branch, error)

	// GetFile returns the contents of the given file at the given commit.
	GetFile(ctx context.Context, fileName, commit string) (string, error)

	// IsSubmodule returns true if the given path is submodule, ie contains gitlink.
	IsSubmodule(ctx context.Context, path, commit string) (bool, error)

	// ReadSubmodule returns commit hash of the given path, if the path is git
	// submodule. ErrorNotFound is returned if path is not found in the git
	// worktree. ErrorNotSubmodule is returned if path exists, but it's not a
	// submodule.
	ReadSubmodule(ctx context.Context, path, commit string) (string, error)

	// UpdateSubmodule updates git submodule of the given path to the given commit.
	// If submodule doesn't exist, it returns ErrorNotFound since it doesn't have
	// all necessary information to create a valid submodule (requires an entry in
	// .gitmodules).
	UpdateSubmodule(ctx context.Context, path, commit string) error

	// NumCommits returns the number of commits in the repo.
	NumCommits(ctx context.Context) (int64, error)

	// IsAncestor returns true iff A is an ancestor of B.
	IsAncestor(ctx context.Context, a, b string) (bool, error)

	// Version returns the Git version.
	Version(ctx context.Context) (int, int, error)

	// FullHash gives the full commit hash for the given ref.
	FullHash(ctx context.Context, ref string) (string, error)

	// CatFile runs "git cat-file -p <ref>:<path>".
	CatFile(ctx context.Context, ref, path string) ([]byte, error)

	// ReadDir is analogous to os.File.Readdir for a particular ref.
	ReadDir(ctx context.Context, ref, path string) ([]os.FileInfo, error)

	// GetRemotes returns a mapping of remote repo name to URL.
	GetRemotes(ctx context.Context) (map[string]string, error)

	// VFS returns a vfs.FS using Git for the given revision.
	VFS(ctx context.Context, ref string) (*FS, error)
}

GitDir is a directory in which one may run Git commands.

type ObjectType

type ObjectType string

ObjectType represents a Git object type.

const (
	ObjectTypeBlob   ObjectType = "blob"
	ObjectTypeCommit ObjectType = "commit"
	ObjectTypeTree   ObjectType = "tree"
)

Types of git objects.

type Repo

type Repo interface {
	GitDir

	// Update syncs the Repo from its remote.
	Update(ctx context.Context) error

	// Checkout returns a Checkout of the Repo in the given working directory.
	Checkout(ctx context.Context, workdir string) (CheckoutDir, error)

	// TempCheckout returns a TempCheckout of the repo.
	TempCheckout(ctx context.Context) (*TempCheckout, error)
}

Repo is used for managing a local git repo, without a working copy.

type RepoDir

type RepoDir string

RepoDir implements Repo.

func NewRepo

func NewRepo(ctx context.Context, repoUrl, workdir string) (RepoDir, error)

NewRepo returns a Repo instance based in the given working directory. Uses any existing repo in the given directory, or clones one if necessary. Only creates bare clones; Repo does not maintain a checkout.

func (RepoDir) Branches

func (r RepoDir) Branches(ctx context.Context) ([]*Branch, error)

Branches runs "git branch" and returns a slice of Branch instances.

func (RepoDir) CatFile

func (r RepoDir) CatFile(ctx context.Context, ref, path string) ([]byte, error)

CatFile runs "git cat-file -p <ref>:<path>".

func (RepoDir) Checkout

func (r RepoDir) Checkout(ctx context.Context, workdir string) (CheckoutDir, error)

Checkout returns a Checkout of the Repo in the given working directory.

func (RepoDir) Details

func (r RepoDir) Details(ctx context.Context, name string) (*vcsinfo.LongCommit, error)

Details returns a vcsinfo.LongCommit instance representing the given commit.

func (RepoDir) Dir

func (r RepoDir) Dir() string

Dir returns the working directory of the Repo.

func (RepoDir) FullHash

func (r RepoDir) FullHash(ctx context.Context, ref string) (string, error)

FullHash gives the full commit hash for the given ref.

func (RepoDir) GetBranchHead

func (r RepoDir) GetBranchHead(ctx context.Context, branchName string) (string, error)

GetBranchHead returns the commit hash at the HEAD of the given branch.

func (RepoDir) GetFile

func (r RepoDir) GetFile(ctx context.Context, fileName, commit string) (string, error)

GetFile returns the contents of the given file at the given commit.

func (RepoDir) GetRemotes

func (r RepoDir) GetRemotes(ctx context.Context) (map[string]string, error)

GetRemotes returns a mapping of remote repo name to URL.

func (RepoDir) Git

func (r RepoDir) Git(ctx context.Context, cmd ...string) (string, error)

Git runs the given git command in the Repo.

func (RepoDir) IsAncestor

func (r RepoDir) IsAncestor(ctx context.Context, a, b string) (bool, error)

IsAncestor returns true iff A is an ancestor of B.

func (RepoDir) IsSubmodule

func (r RepoDir) IsSubmodule(ctx context.Context, path, commit string) (bool, error)

IsSubmodule returns true if the given path is submodule, ie contains gitlink.

func (RepoDir) NumCommits

func (r RepoDir) NumCommits(ctx context.Context) (int64, error)

NumCommits returns the number of commits in the repo.

func (RepoDir) ReadDir

func (r RepoDir) ReadDir(ctx context.Context, ref, path string) ([]os.FileInfo, error)

ReadDir is analogous to os.File.Readdir for a particular ref.

func (RepoDir) ReadSubmodule

func (r RepoDir) ReadSubmodule(ctx context.Context, path, commit string) (string, error)

ReadSubmodule returns commit hash of the given path, if the path is git submodule. ErrorNotFound is returned if path is not found in the git worktree. ErrorNotSubmodule is returned if path exists, but it's not a submodule.

func (RepoDir) RevList

func (r RepoDir) RevList(ctx context.Context, args ...string) ([]string, error)

RevList runs "git rev-list <name>" and returns a slice of commit hashes.

func (RepoDir) RevParse

func (r RepoDir) RevParse(ctx context.Context, args ...string) (string, error)

RevParse runs "git rev-parse <name>" and returns the result.

func (RepoDir) TempCheckout

func (r RepoDir) TempCheckout(ctx context.Context) (*TempCheckout, error)

TempCheckout returns a TempCheckout of the repo.

func (RepoDir) Update

func (r RepoDir) Update(ctx context.Context) error

Update syncs the Repo from its remote.

func (RepoDir) UpdateSubmodule

func (r RepoDir) UpdateSubmodule(ctx context.Context, path, commit string) error

UpdateSubmodule updates git submodule of the given path to the given commit. If submodule doesn't exist, it returns ErrorNotFound since it doesn't have all necessary information to create a valid submodule (requires an entry in .gitmodules).

func (RepoDir) VFS

func (r RepoDir) VFS(ctx context.Context, ref string) (*FS, error)

VFS returns a vfs.FS using Git for the given revision.

func (RepoDir) Version

func (r RepoDir) Version(ctx context.Context) (int, int, error)

Version returns the Git version.

type TempCheckout

type TempCheckout struct {
	Checkout
}

TempCheckout is a temporary Git Checkout.

func NewTempCheckout

func NewTempCheckout(ctx context.Context, repoUrl string) (*TempCheckout, error)

NewTempCheckout returns a TempCheckout instance. Creates a temporary directory and then clones the repoUrl into a subdirectory, based on default "git clone" behavior.

func (*TempCheckout) Delete

func (c *TempCheckout) Delete()

Delete removes the TempCheckout's working directory.

Directories

Path Synopsis
Package gitinfo enables querying info from Git repository using git and a local checkout.
Package gitinfo enables querying info from Git repository using git and a local checkout.

Jump to

Keyboard shortcuts

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