git

package module
v0.6.1 Latest Latest
Warning

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

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

README

About

Git sdk to support the gitops workflows of our internal tooling.

It uses the go-git library, sacrificing on overall flexibity to simplify our specific use-cases of that library as much as possible.

The functionality of this sdk may change in a backward-incompatible way as our needs evolve.

Features

Currently, the sdk focuses on the following use-cases:

  • Cloning and/or pulling on a repo depending on the current state of the target repository
  • Verifying that the top commit of a repository was signed by a key from a trusted list
  • Adding and commiting on a group of files if any are changed
  • Pushing to the origin of a repository returned by a function argument with retries if there are conflicts

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitFiles

func CommitFiles(repo *GitRepository, files []string, msg string, opts CommitOptions) (bool, error)

Commits the given list of files in the git repository. If not changes are detected in the files provided, a commit will not be attempted.

func MemCloneGitRepo added in v0.5.0

func MemCloneGitRepo(url string, ref string, depth int, sshCred *SshCredentials) (*GitRepository, *MemoryStore, error)

Clone the given reference of a given repo in a memory filesystem. A reference to the generated filesystem as well as the repository is returned.

func PushChanges

func PushChanges(hook PushPreHook, ref string, sshCred *SshCredentials, retries int64, retryInterval time.Duration) error

Takes a function argument that should return a git repository with changes to push if there are (and nil otherwise). From there, it will try to push the new commits in the repository to the given reference on origin. If there are conflicts during the push, it will keep retrying by re-invoking its function argument and push on the returned repository.

func VerifyTopCommit

func VerifyTopCommit(repo *GitRepository, armoredKeyrings []string) error

Verifies that the top commit of a given git repository was signed by one of the keys that are passed in the argument. Returns an error if it isn't.

Types

type CommitOptions added in v0.2.0

type CommitOptions struct {
	//Name of the commiter
	Name string
	//Email of the commiter
	Email string
	//Optional key used to signed the git commit
	SignatureKey *CommitSignatureKey
}

Optional parameters to pass to the CommitFiles command

type CommitSignatureKey added in v0.4.0

type CommitSignatureKey struct {
	Entity *openpgp.Entity
}

Structure abstracting away openpgp.Entity structure needed by go-git to sign keys

func GetSignatureKey added in v0.4.0

func GetSignatureKey(signKeyPath string, passphrasePath string) (*CommitSignatureKey, error)

Produces a commit signature needed to sign a commit. Arguments are file paths to an armored private pgp key and optionally a passphrase to decrypt it if it is encrypted

type GitRepository added in v0.4.0

type GitRepository struct {
	Repo *gogit.Repository
}

Structure abstracting away gogit.Repository structure needed by go-git to manipulate a git repository

func SyncGitRepo

func SyncGitRepo(dir string, url string, ref string, sshCred *SshCredentials) (*GitRepository, bool, error)

Clone or pull the given reference of a given repo at a given path on the filesystem. If the repo was previously cloned at the path, a pull will be done, else a clone.

type MemoryStore added in v0.5.0

type MemoryStore struct {
	Fs *billy.Filesystem
	// contains filtered or unexported fields
}

Container for a memory store. It used to keep a reference to the store and clear it as needed. The Fs property is a pointer to a billy.Filesystem that can be used to intereract with the filesystem in memory

func (*MemoryStore) Clear added in v0.5.0

func (mem *MemoryStore) Clear()

Frees the references to the memory store, allowing the garbage collector to collect it.

func (*MemoryStore) GetKeyVals added in v0.5.0

func (mem *MemoryStore) GetKeyVals(sourcePath string) (map[string]string, error)

Returns all the files in the memory filesystem thet fall under a given source path as a map where the keys are the relative path of each file (relative to the specified source path) and the value is their content. You can pass the empty string as a source path if you wish to return the entire content of the memory filesystem.

type PushPreHook

type PushPreHook func() (*GitRepository, error)

Function signature meant to be passed as an argument to the PushChanges function. It should return a git repository with changes to push if there are changes to push otherwise it should return nil. The function should be idempotent as it might be called repeatedly if there is a conflict during the push

type SshCredentials added in v0.4.0

type SshCredentials struct {
	Keys *ssh.PublicKeys
}

Structure abstracting away ssh.PublicKeys structure needed by go-git to authenticate with git server

func GetSshCredentials added in v0.4.0

func GetSshCredentials(sshKeyPath string, knownHostsPath string) (*SshCredentials, error)

Produces ssh credentials needed by go-git to clone/pull a remote repository and push to it. Arguments are file paths to the private ssh key of the user and ssh host key fingerprint of the git server.

Jump to

Keyboard shortcuts

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