git

package
v0.0.0-...-65468f9 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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 struct {
	GitDir
}

Checkout is a struct used for managing a local git checkout.

func NewCheckout

func NewCheckout(ctx context.Context, repoUrl, workdir string) (*Checkout, 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 (*Checkout) AddRemote

func (c *Checkout) 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 (*Checkout) Cleanup

func (c *Checkout) 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 (*Checkout) CleanupBranch

func (c *Checkout) 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 (*Checkout) Fetch

func (c *Checkout) Fetch(ctx context.Context) error

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

func (*Checkout) FetchRefFromRepo

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

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

func (*Checkout) IsDirty

func (c *Checkout) 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 (*Checkout) Update

func (c *Checkout) 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 (*Checkout) UpdateBranch

func (c *Checkout) 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().

type FS

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

FS implements vfs.FS using Git for a particular 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 string

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

func (GitDir) Branches

func (g GitDir) Branches(ctx context.Context) ([]*Branch, error)

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

func (GitDir) CatFile

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

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

func (GitDir) Details

func (g GitDir) Details(ctx context.Context, name string) (*vcsinfo.LongCommit, error)

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

func (GitDir) Dir

func (g GitDir) Dir() string

Dir returns the working directory of the GitDir.

func (GitDir) FullHash

func (g GitDir) FullHash(ctx context.Context, ref string) (string, error)

FullHash gives the full commit hash for the given ref.

func (GitDir) GetBranchHead

func (g GitDir) GetBranchHead(ctx context.Context, branchName string) (string, error)

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

func (GitDir) GetFile

func (g GitDir) GetFile(ctx context.Context, fileName, commit string) (string, error)

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

func (GitDir) GetRemotes

func (g GitDir) GetRemotes(ctx context.Context) (map[string]string, error)

GetRemotes returns a mapping of remote repo name to URL.

func (GitDir) Git

func (g GitDir) Git(ctx context.Context, cmd ...string) (string, error)

Git runs the given git command in the GitDir.

func (GitDir) IsAncestor

func (g GitDir) IsAncestor(ctx context.Context, a, b string) (bool, error)

IsAncestor returns true iff A is an ancestor of B.

func (GitDir) IsSubmodule

func (g GitDir) IsSubmodule(ctx context.Context, path, commit string) (bool, error)

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

func (GitDir) NumCommits

func (g GitDir) NumCommits(ctx context.Context) (int64, error)

NumCommits returns the number of commits in the repo.

func (GitDir) ReadDir

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

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

func (GitDir) ReadSubmodule

func (g GitDir) 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 (GitDir) RevList

func (g GitDir) RevList(ctx context.Context, args ...string) ([]string, error)

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

func (GitDir) RevParse

func (g GitDir) RevParse(ctx context.Context, args ...string) (string, error)

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

func (GitDir) UpdateSubmodule

func (g GitDir) 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 (GitDir) VFS

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

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

func (GitDir) Version

func (g GitDir) Version(ctx context.Context) (int, int, error)

Version returns the Git version.

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 struct {
	GitDir
}

Repo is a struct used for managing a local git repo.

func NewRepo

func NewRepo(ctx context.Context, repoUrl, workdir string) (*Repo, 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 (*Repo) Checkout

func (r *Repo) Checkout(ctx context.Context, workdir string) (*Checkout, error)

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

func (*Repo) TempCheckout

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

TempCheckout returns a TempCheckout of the repo.

func (*Repo) Update

func (r *Repo) Update(ctx context.Context) error

Update syncs the Repo from its remote.

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