gitdir

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotStarted happens if you try to operate on the gitDirectory before you have started
	// it with StartCheckoutLoop.
	ErrNotStarted = errors.New("the gitDirectory hasn't been started (and hence, cloned) yet")
	// ErrCannotWriteToReadOnly happens if you try to do a write operation for a non-authenticated Git repo.
	ErrCannotWriteToReadOnly = errors.New("the gitDirectory is read-only, cannot write")
)

Functions

This section is empty.

Types

type AuthMethod

type AuthMethod interface {
	// This AuthMethod is a superset of the go-git AuthMethod
	transport.AuthMethod
	// TransportType defines what transport type should be used with this method
	TransportType() gitprovider.TransportType
}

AuthMethod specifies the authentication method and related credentials for connecting to a Git repository.

func NewHTTPSAuthMethod

func NewHTTPSAuthMethod(username, password string) (AuthMethod, error)

func NewSSHAuthMethod

func NewSSHAuthMethod(identityFile, knownHostsFile []byte) (AuthMethod, error)

NewSSHAuthMethod creates a new AuthMethod for the Git SSH protocol, using a given identity and known_hosts file.

identityFile is the bytes of e.g. ~/.ssh/id_rsa, given that ~/.ssh/id_rsa.pub is registered with and trusted by the Git provider.

knownHostsFile should be the file content of the known_hosts file to use for remote (e.g. GitHub) public key verification. If you want to use the default git CLI behavior, populate this byte slice with contents from ioutil.ReadFile("~/.ssh/known_hosts").

type GitDirectory

type GitDirectory interface {
	// Dir returns the backing temporary directory of the git clone.
	Dir() string
	// MainBranch returns the configured main branch.
	MainBranch() string
	// RepositoryRef returns the repository reference.
	RepositoryRef() gitprovider.RepositoryRef

	// StartCheckoutLoop clones the repo synchronously, and then starts the checkout loop non-blocking.
	// If the checkout loop has been started already, this is a no-op.
	StartCheckoutLoop() error
	// Suspend waits for any pending transactions or operations, and then locks the internal mutex so that
	// no other operations can start. This means the periodic background checkout loop will momentarily stop.
	Suspend()
	// Resume unlocks the mutex locked in Suspend(), so that other Git operations, like the background checkout
	// loop can resume its operation.
	Resume()

	// Pull performs a pull & checkout to the latest revision.
	// ErrNotStarted is returned if the repo hasn't been cloned yet.
	Pull(ctx context.Context) error

	// CheckoutNewBranch creates a new branch and checks out to it.
	// ErrNotStarted is returned if the repo hasn't been cloned yet.
	CheckoutNewBranch(branchName string) error
	// CheckoutMainBranch goes back to the main branch.
	// ErrNotStarted is returned if the repo hasn't been cloned yet.
	CheckoutMainBranch() error

	// Commit creates a commit of all changes in the current worktree with the given parameters.
	// It also automatically pushes the branch after the commit.
	// ErrNotStarted is returned if the repo hasn't been cloned yet.
	// ErrCannotWriteToReadOnly is returned if opts.AuthMethod wasn't provided.
	Commit(ctx context.Context, authorName, authorEmail, msg string) error
	// CommitChannel is a channel to where new observed Git SHAs are written.
	CommitChannel() chan string

	// Cleanup terminates any pending operations, and removes the temporary directory.
	Cleanup() error
}

GitDirectory is an abstraction layer for a temporary Git clone. It pulls and checks out new changes periodically in the background. It also allows high-level access to write operations, like creating a new branch, committing, and pushing.

func NewGitDirectory

func NewGitDirectory(repoRef gitprovider.RepositoryRef, opts GitDirectoryOptions) (GitDirectory, error)

Create a new GitDirectory implementation. In order to start using this, run StartCheckoutLoop().

type GitDirectoryOptions

type GitDirectoryOptions struct {
	// Options
	Branch   string        // default "master"
	Interval time.Duration // default 30s
	Timeout  time.Duration // default 1m

	// Authentication
	AuthMethod AuthMethod
}

GitDirectoryOptions provides options for the gitDirectory. TODO: Refactor this into the controller-runtime Options factory pattern.

func (*GitDirectoryOptions) Default

func (o *GitDirectoryOptions) Default()

Jump to

Keyboard shortcuts

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