gitinterface

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultSigningProgramGPG  string = "gpg"
	DefaultSigningProgramSSH  string = "ssh-keygen"
	DefaultSigningProgramX509 string = "gpgsm"
)
View Source
const (
	RefPrefix       = "refs/"
	BranchRefPrefix = "refs/heads/"
	TagRefPrefix    = "refs/tags/"
	RemoteRefPrefix = "refs/remotes/"
)
View Source
const DefaultRemoteName = "origin"

Variables

View Source
var (
	ErrSigningKeyNotSpecified     = errors.New("signing key not specified in git config")
	ErrUnknownSigningMethod       = errors.New("unknown signing method (not one of gpg, ssh, x509)")
	ErrUnableToSign               = errors.New("unable to sign Git object")
	ErrIncorrectVerificationKey   = errors.New("incorrect key provided to verify signature")
	ErrVerifyingSigstoreSignature = errors.New("unable to verify Sigstore signature")
	ErrVerifyingSSHSignature      = errors.New("unable to verify SSH signature")
	ErrInvalidSignature           = errors.New("unable to parse signature / signature has unexpected header")
)
View Source
var (
	ErrReferenceNotFound = plumbing.ErrReferenceNotFound
)
View Source
var (
	ErrTagAlreadyExists = errors.New("tag already exists")
)
View Source
var ErrWrittenBlobLengthMismatch = errors.New("length of blob written does not match length of contents")

Functions

func AbsoluteReference

func AbsoluteReference(repo *git.Repository, target string) (string, error)

AbsoluteReference returns the fully qualified reference path for the provided Git ref.

func ApplyCommit

func ApplyCommit(repo *git.Repository, commit *object.Commit, curRef *plumbing.Reference) (plumbing.Hash, error)

ApplyCommit writes a commit object in the repository and updates the specified reference to point to the commit.

func ApplyTag

func ApplyTag(repo *git.Repository, tag *object.Tag) (plumbing.Hash, error)

ApplyTag sets the tag reference after the tag object is written to the repository's object store.

func CloneAndFetch

func CloneAndFetch(ctx context.Context, remoteURL, dir, initialBranch string, refs []string) (*git.Repository, error)

CloneAndFetch clones a repository using the specified URL and additionally fetches the specified refs.

func CloneAndFetchToMemory

func CloneAndFetchToMemory(ctx context.Context, remoteURL, initialBranch string, refs []string) (*git.Repository, error)

CloneAndFetchToMemory clones an in-memory repository using the specified URL and additionally fetches the specified refs.

func Commit

func Commit(repo *git.Repository, treeHash plumbing.Hash, targetRef string, message string, sign bool) (plumbing.Hash, error)

Commit creates a new commit in the repo and sets targetRef's HEAD to the commit.

func CommitUsingSpecificKey added in v0.3.0

func CommitUsingSpecificKey(repo *git.Repository, treeHash plumbing.Hash, targetRef, message string, signingKeyPEMBytes []byte) (plumbing.Hash, error)

CommitUsingSpecificKey creates a new commit in the repository for the specified parameters. The commit is signed using the PEM encoded SSH or GPG private key. This function is expected for use in tests and gittuf's developer mode. In standard workflows, Commit() must be used instead which infers the signing key from the user's Git config.

func CreateCommitObject

func CreateCommitObject(gitConfig *config.Config, treeHash plumbing.Hash, parentHashes []plumbing.Hash, message string, clock clockwork.Clock) *object.Commit

CreateCommitObject returns a commit object using the specified parameters.

func CreateTagObject

func CreateTagObject(gitConfig *config.Config, targetObj object.Object, name, message string, clock clockwork.Clock) *object.Tag

CreateTagObject crafts and returns a new tag object using the specified parameters.

func EmptyBlob

func EmptyBlob() plumbing.Hash

EmptyBlob returns the hash of an empty blob in a Git repository. Note: it is generated on the fly rather than stored as a constant to support SHA-256 repositories in future.

func EmptyTree

func EmptyTree() plumbing.Hash

EmptyTree returns the hash of an empty tree in a Git repository. Note: it is generated on the fly rather than stored as a constant to support SHA-256 repositories in future.

func Fetch

func Fetch(ctx context.Context, repo *git.Repository, remoteName string, refs []string, fastForwardOnly bool) error

Fetch constructs refspecs for the refs and fetches to the repo from the specified remote. For more information on the Git refspec, please consult: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.

The fastForwardOnly flag controls if the constructed refspec allows non-fast-forward fetches. The target of the refspec is the same as the requested ref. Also, the remote tracker for the ref is also always updated.

func FetchRefSpec

func FetchRefSpec(ctx context.Context, repo *git.Repository, remoteName string, refs []config.RefSpec) error

FetchRefSpec fetches to the repo from the specified remote using pre-constructed refspecs. For more information on the Git refspec, please consult: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.

func GetAllFilesInTree added in v0.2.0

func GetAllFilesInTree(tree *object.Tree) (map[string]plumbing.Hash, error)

GetAllFilesInTree returns all filepaths and the corresponding hash in the specified tree.

func GetBlob added in v0.2.0

func GetBlob(repo *git.Repository, blobID plumbing.Hash) (*object.Blob, error)

GetBlob returns the requested blob object.

func GetCommit added in v0.2.0

func GetCommit(repo *git.Repository, commitID plumbing.Hash) (*object.Commit, error)

GetCommit returns the requested commit object.

func GetCommitFilePaths

func GetCommitFilePaths(commit *object.Commit) ([]string, error)

GetCommitFilePaths returns all the file paths of the provided commit object. This strictly enumerates all the files recursively in the commit object's tree.

func GetCommitsBetweenRange

func GetCommitsBetweenRange(repo *git.Repository, commitNewID, commitOldID plumbing.Hash) ([]*object.Commit, error)

GetCommitsBetweenRange returns the commits (including the new commit, excluding the old) between the specified ranges. If the old commit ID is set to zero, all commits reachable from the new commit are returned.

The returned commits are sorted by commit IDs. Ideally, they should be ordered by occurrence but go-git introduces some randomness here. It might be an effect of walking the graph anyway, so the sort by ID ensures the returned commit slice is deterministic.

func GetDiffFilePaths

func GetDiffFilePaths(commitA, commitB *object.Commit) ([]string, error)

GetDiffFilePaths enumerates all the changed file paths between the two commits. If one of the commits is nil, the other commit's tree is enumerated.

func GetFilePathsChangedByCommit

func GetFilePathsChangedByCommit(repo *git.Repository, commit *object.Commit) ([]string, error)

GetFilePathsChangedByCommit returns the paths changed by the commit relative to its parent commit. If the commit is a merge commit, i.e., it has more than one parent, check if the commit is the same as at least one of its parents. If there is a matching parent, we return no changes. If there is no matching parent commit, we return the changes between the commit and each of its parents.

func GetSigningCommand

func GetSigningCommand() (string, []string, error)

func GetTag added in v0.2.0

func GetTag(repo *git.Repository, tagID plumbing.Hash) (*object.Tag, error)

GetTag returns the requested tag object.

func GetTip

func GetTip(repo *git.Repository, refName string) (plumbing.Hash, error)

GetTip returns the hash of the tip of the specified ref.

func GetTree added in v0.2.0

func GetTree(repo *git.Repository, treeID plumbing.Hash) (*object.Tree, error)

GetTree returns the requested tree object.

func IsTag

func IsTag(repo *git.Repository, target string) bool

IsTag returns true if the specified target is a tag in the repository.

func KnowsCommit

func KnowsCommit(repo *git.Repository, commitID plumbing.Hash, commit *object.Commit) (bool, error)

KnowsCommit indicates if the commit under test, identified by commitID, has a path to commit. If commit is the same as the commit under test or if commit is an ancestor of commit under test, KnowsCommit returns true.

func Push

func Push(ctx context.Context, repo *git.Repository, remoteName string, refs []string) error

Push constructs refspecs for the specified Git refs and pushes from the repo to the remote. For more information on the Git refspec, please consult: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.

The refspecs are constructed to be fast-forward only.

func PushRefSpec

func PushRefSpec(ctx context.Context, repo *git.Repository, remoteName string, refs []config.RefSpec) error

PushRefSpec pushes from repo to the specified remote using pre-constructed refspecs. For more information on the Git refspec, please consult: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.

All pushes are set to be atomic as the intent of using multiple refs is to sync the RSL.

func ReadBlob

func ReadBlob(repo *git.Repository, blobID plumbing.Hash) ([]byte, error)

ReadBlob returns the contents of a the blob referenced by blobID.

func RefSpec

func RefSpec(repo *git.Repository, refName, remoteName string, fastForwardOnly bool) (config.RefSpec, error)

RefSpec creates a Git refspec for the specified ref. For more information on the Git refspec, please consult: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.

func RemoteRef

func RemoteRef(refName, remoteName string) string

func ResetCommit

func ResetCommit(repo *git.Repository, refName string, commitID plumbing.Hash) error

ResetCommit sets a Git reference with the name refName to the commit specified by its hash as commitID. Note that the commit must already be in the repository's object store.

func ResetDueToError

func ResetDueToError(cause error, repo *git.Repository, refName string, commitID plumbing.Hash) error

ResetDueToError is a helper used to reverse a change applied to a ref due to an error encountered after the change but part of the same operation. This ensures that gittuf operations are atomic. Otherwise, a repository may enter a violation state where a ref is updated without accompanying RSL entries or other metadata changes.

func Tag

func Tag(repo *git.Repository, target plumbing.Hash, name, message string, sign bool) (plumbing.Hash, error)

Tag creates a new tag in the repository pointing to the specified target.

func VerifyCommitSignature

func VerifyCommitSignature(ctx context.Context, commit *object.Commit, key *tuf.Key) error

VerifyCommitSignature is used to verify a cryptographic signature associated with commit using TUF public keys.

func VerifyTagSignature

func VerifyTagSignature(ctx context.Context, tag *object.Tag, key *tuf.Key) error

VerifyTagSignature is used to verify a cryptographic signature associated with tag using TUF public keys.

func WriteBlob

func WriteBlob(repo *git.Repository, contents []byte) (plumbing.Hash, error)

WriteBlob creates a blob object with the specified contents and returns the ID of the resultant blob.

func WriteCommit

func WriteCommit(repo *git.Repository, commit *object.Commit) (plumbing.Hash, error)

WriteCommit stores the commit object in the repository's object store, returning the new commit's ID.

func WriteTag

func WriteTag(repo *git.Repository, tag *object.Tag) (plumbing.Hash, error)

WriteTag writes the tag to the repository's object store.

func WriteTree

func WriteTree(repo *git.Repository, entries []object.TreeEntry) (plumbing.Hash, error)

WriteTree creates a Git tree with the specified entries. It sorts the entries prior to creating the tree.

Types

type SigningMethod

type SigningMethod int
const (
	SigningMethodGPG SigningMethod = iota
	SigningMethodSSH
	SigningMethodX509
)

type TreeBuilder added in v0.2.0

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

TreeBuilder is used to create multi-level trees in a repository. Based on `buildTreeHelper` in go-git.

func NewTreeBuilder added in v0.2.0

func NewTreeBuilder(repo *git.Repository) *TreeBuilder

NewTreeBuilder returns a TreeBuilder instance for the repository.

func (*TreeBuilder) WriteRootTreeFromBlobIDs added in v0.2.0

func (t *TreeBuilder) WriteRootTreeFromBlobIDs(files map[string]plumbing.Hash) (plumbing.Hash, error)

WriteRootTreeFromBlobIDs accepts a map of paths to their blob IDs and returns the root tree ID that contains these files.

Jump to

Keyboard shortcuts

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