git

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 11 Imported by: 4 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Gitz

Write fluent interactions to Git using the gitz library. Programmatically crafting git commands becomes a breeze! Verify your code using the inbuilt testing framework.

Badges

Build status License MIT Go Report Card Go Version Coverage Quality Gate Status

Documentation

Check out the latest documentation

Documentation

Overview

Package git provides a simple lightweight library for performing fluent git operations against an existing cloned repository

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Trim added in v0.3.0

func Trim(strs ...string) []string

Trim iterates through a slice, trimming leading and trailing whitespace from each string. Empty strings are ignored and removed from the slice

func TrimAndPrefix added in v0.3.0

func TrimAndPrefix(prefix string, strs ...string) []string

TrimAndPrefix iterates through a slice, trimming leading and trailing whitespace from each string before appending the provided prefix. Empty strings are ignored and removed from the slice

Types

type Client

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

Client provides a way of performing fluent operations against git. Any git operation exposed by this client are effectively handed-off to an installed git client on the current OS. Git operations will be mapped as closely as possible to the official Git specification

func NewClient

func NewClient() (*Client, error)

NewClient returns a new instance of the git client

func (*Client) Checkout added in v0.3.0

func (c *Client) Checkout(branch string) (string, error)

Checkout will attempt to checkout a branch with the given name. If the branch does not exist, it is created at the current working tree reference (or commit), and then switched to. If the branch does exist, then switching to it restores all working tree files

func (*Client) Clone added in v0.2.0

func (c *Client) Clone(url string, opts ...CloneOption) (string, error)

Clone a repository by its provided URL into a newly created directory. A default clone will ensure remote tracking branches are created for each branch within the repository with only the default branch being checked out fully. The cloned directory will mirror that of the repository name within its URL. Options can be provided to customize the clone behavior

func (*Client) Commit

func (c *Client) Commit(msg string) (string, error)

Commit a snapshot of changes within the current repository (working directory) and describe those changes with a given log message

func (*Client) DeleteTag

func (c *Client) DeleteTag(tag string) (string, error)

DeleteTag a tag both locally and from the remote origin

func (*Client) Log

func (c *Client) Log(opts ...LogOption) (*Log, error)

Log retrieves the commit log of the current repository (working directory) in an easy-to-parse format. Options can be provided to customize log retrieval, creating a targeted snapshot. By default, the entire history from the repository HEAD (most recent commit) will be retrieved. The logs are generated using the default git options:

git log --pretty='format:> %H %B%-N' --no-color

func (*Client) Pull

func (c *Client) Pull() (string, error)

Pull all changes from a remote repository and immediately update the current repository (current working) directory with those changes. This ensures that your current repository keeps track of remote changes and stays in sync

func (*Client) Push

func (c *Client) Push(opts ...PushOption) (string, error)

Push (or upload) all local changes to the remote repository. By default, changes associated with the current branch will be pushed back to the remote. Options can be provided to configure branch and tag push semantics

func (*Client) PushRef added in v0.3.0

func (c *Client) PushRef(ref string) (string, error)

PushRef will push an individual reference to the remote repository

func (*Client) Repository added in v0.2.0

func (c *Client) Repository() (Repository, error)

Repository returns details about the current repository (working directory), by carrying out a series of checks. Answers to which are returned as a snapshot for querying

func (*Client) Stage

func (c *Client) Stage(opts ...StageOption) (string, error)

Stage changes to any file or folder within the current repository (working directory) ready for inclusion in the next commit. Options can be provided to further configure stage semantics. By default, all changes will be staged ready for the next commit.

func (*Client) Tag

func (c *Client) Tag(tag string, opts ...CreateTagOption) (string, error)

Tag a specific point within a repositories history and push it to the configured remote. Tagging comes in two flavours:

  • A lightweight tag, which points to a specific commit within the history and marks a specific point in time
  • An annotated tag, which is treated as a full object within git, and must include a tagging message (or annotation)

By default, a lightweight tag will be created, unless specific tag options are provided

func (*Client) Tags added in v0.3.0

func (c *Client) Tags(opts ...ListTagsOption) ([]string, error)

Tags retrieves all local tags from the current repository (working directory). By default, all tags are retrieved in ascending lexicographic order as implied through the RefName sort key. Options can be provided to customize retrieval

func (*Client) Version

func (c *Client) Version() string

Version of git used by the client

type CloneOption added in v0.3.0

type CloneOption func(*cloneOptions)

CloneOption provides a way for setting specific options during a clone operation. Each supported option can customize the way in which the repository is cloned onto the file system into a target working directory

func WithCheckoutRef added in v0.3.0

func WithCheckoutRef(ref string) CloneOption

WithCheckoutRef changes the default checkout behavior after a clone succeeds. A branch or tag reference is supported. Checking out a tag will result in a detached HEAD. An empty string will be ignored

func WithDepth added in v0.3.0

func WithDepth(depth int) CloneOption

WithDepth ensures the repository will be cloned at a specific depth, effectively truncating the history to the required number of commits. The result will be a shallow repository. Any depth less than one is ignored, resulting in a full clone of the history

func WithDirectory added in v0.3.0

func WithDirectory(dir string) CloneOption

WithDirectory provides a named directory for cloning the repository into. If the directory already exists, it must be empty for the clone to be successful. An empty string will be ignored

func WithNoTags added in v0.3.0

func WithNoTags() CloneOption

WithNoTags prevents any tags from being included during the clone

type CreateTagOption added in v0.3.0

type CreateTagOption func(*createTagOptions)

CreateTagOption provides a way for setting specific options during a tag creation operation. Each supported option can customize the way the tag is created against the current repository (working directory)

func WithAnnotation

func WithAnnotation(message string) CreateTagOption

WithAnnotation ensures the created tag is annotated with the provided message. This ultimately converts the standard lightweight tag into an annotated tag which is stored as a full object within the git database. Any leading and trailing whitespace will automatically be trimmed from the message. This allows empty messages to be ignored

type ErrGitExecCommand

type ErrGitExecCommand struct {
	// Cmd contains the command that caused the git client to error
	Cmd string

	// Out contains any raw output from the git client as a result
	// of the error
	Out string
}

ErrGitExecCommand is raised when a git command fails to execute

func (ErrGitExecCommand) Error

func (e ErrGitExecCommand) Error() string

Error returns a friendly formatted message of the current error

type ErrGitMissing

type ErrGitMissing struct {
	// PathEnv contains the value of the PATH environment variable
	PathEnv string
}

ErrGitMissing is raised when no git client was identified within the PATH environment variable on the current OS

func (ErrGitMissing) Error

func (e ErrGitMissing) Error() string

Error returns a friendly formatted message of the current error

type ListTagsOption added in v0.3.0

type ListTagsOption func(*listTagsOptions)

ListTagsOption provides a way for setting specific options during a list tags operation. Each supported option can customize the way in which the tags are queried and returned from the current repository (workng directory)

func WithShellGlob added in v0.3.0

func WithShellGlob(patterns ...string) ListTagsOption

WithShellGlob limits the number of tags that will be retrieved, by only returning tags that match a given Shell Glob pattern. If multiple patterns are provided, tags will be retrieved if they match against a single pattern. All leading and trailing whitespace will be trimmed, allowing empty patterns to be ignored

func WithSortBy added in v0.3.0

func WithSortBy(keys ...SortKey) ListTagsOption

WithSortBy allows the retrieved order of tags to be changed by sorting against a reserved field name. By default, sorting will always be in ascending order. To change this behaviour, prefix a field name with a hyphen (-<fieldname>). You can sort tags against multiple fields, but this does change the expected behavior. The last field name is treated as the primary key for the entire sort. All leading and trailing whitespace will be trimmed, allowing empty field names to be ignored

type Log

type Log struct {
	// Raw contains the raw commit log
	Raw string

	// Commits contains the optionally parsed commit log. By default
	// the parsed history will always be present, unless the
	// [WithRawOnly] option is provided during retrieval
	Commits []LogEntry
}

Log represents a snapshot of commit history from a repository

type LogEntry

type LogEntry struct {
	// Hash contains the unique identifier associated with the commit
	Hash string

	// AbbrevHash contains the seven character abbreviated commit hash
	AbbrevHash string

	// Message contains the message associated with the commit
	Message string
}

LogEntry represents a single parsed entry from within the commit history of a repository

type LogOption

type LogOption func(*logOptions)

LogOption provides a way for setting specific options during a log operation. Each supported option can customize the way the log history of the current repository (working directory) is processed before retrieval

func WithGrep added in v0.2.0

func WithGrep(matches ...string) LogOption

WithGrep limits the number of commits that will be output within the log history to any with a log message that contains one of the provided matches (regular expressions). All leading and trailing whitespace will be trimmed, allowing empty matches to be ignored

func WithInvertGrep added in v0.2.0

func WithInvertGrep(matches ...string) LogOption

WithInvertGrep limits the number of commits that will be output within the log history to any with a log message that does not contain one of the provided matches (regular expressions). All leading and trailing whitespace will be trimmed, allowing empty matches to be ignored

func WithMatchAll added in v0.2.0

func WithMatchAll() LogOption

WithMatchAll when used in combination with git.WithGrep will limit the number of returned commits to those whose log message contains all of the provided matches (regular expressions)

func WithPaths

func WithPaths(paths ...string) LogOption

WithPaths allows the log history to be retrieved for any number of files and folders within the current repository (working directory). Only commits that have had a direct impact on those files and folders will be retrieved. Paths to files and folders are relative to the root of the repository. All leading and trailing whitespace will be trimmed from the file paths, allowing empty paths to be ignored

func WithRawOnly

func WithRawOnly() LogOption

WithRawOnly ensures only the raw output from the git log of the current repository (working directory) is retrieved. No post-processing is carried out, resulting in an empty Log.Commits slice

func WithRef

func WithRef(ref string) LogOption

WithRef provides a starting point other than HEAD (most recent commit) when retrieving the log history of the current repository (working directory). Typically a reference can be either a commit hash, branch name or tag. The output of this option will typically be a shorter, fine-tuned history. This option is mutually exclusive with WithRefRange. All leading and trailing whitespace are trimmed from the reference, allowing empty references to be ignored

func WithRefRange

func WithRefRange(fromRef string, toRef string) LogOption

WithRefRange provides both a start and end point when retrieving a focused snapshot of the log history from the current repository (working directory). Typically a reference can be either a commit hash, branch name or tag. The output of this option will be a shorter, fine-tuned history, for example, the history between two tags. This option is mutually exclusive with WithRef. All leading and trailing whitespace are trimmed from the references, allowing empty references to be ignored

func WithSkip added in v0.2.0

func WithSkip(n int) LogOption

WithSkip skips any number of most recent commits from within the log history. A positive number (greater than zero) is expected. Skipping more commits than exists, will result in no history being retrieved. Skipping zero commits, will retrieve the entire log. This option has a higher order of precedence than git.WithTake

func WithTake added in v0.2.0

func WithTake(n int) LogOption

WithTake limits the number of commits that will be output within the log history. A positive number (greater than zero) is expected. Taking more commits than exists, has the same effect as retrieving the entire log. Taking zero commits, will retrieve an empty log. This option has a lower order of precedence than git.WithSkip

type PushOption added in v0.3.0

type PushOption func(*pushOptions)

PushOption provides a way of setting specific options during a git push operation. Each supported option can customize the way in which references are pushed back to the remote

func WithAllBranches added in v0.3.0

func WithAllBranches() PushOption

WithAllBranches will push all locally created branch references back to the remote

func WithAllTags added in v0.3.0

func WithAllTags() PushOption

WithAllTags will push all locally created tag references back to the remote

func WithRefSpecs added in v0.3.0

func WithRefSpecs(refs ...string) PushOption

WithRefSpecs allows locally created references to be cherry-picked and pushed back to the remote. A reference (or refspec) can be as simple as a name, where git will automatically resolve any ambiguity, or as explicit as providing a source and destination for each local reference within the remote. Check out the official git documentation on how to write a more complex refspec

type Repository added in v0.2.0

type Repository struct {
	ShallowClone  bool
	DetachedHead  bool
	DefaultBranch string
}

Repository provides a snapshot of the current state of a repository (working directory)

type SortKey added in v0.3.0

type SortKey string

SortKey represents a structured field name that can be used as a sort key when analysing referenced objects such as tags

const (
	// CreatorDate sorts the reference in ascending order by the creation date
	// of the underlying commit
	CreatorDate SortKey = "creatordate"

	// CreatorDateDesc sorts the reference in descending order by the creation date
	// of the underlying commit
	CreatorDateDesc SortKey = "-creatordate"

	// RefName sorts the reference by its name in ascending lexicographic order
	RefName SortKey = "refname"

	// RefNameDesc sorts the reference by its name in descending lexicographic order
	RefNameDesc SortKey = "-refname"

	// TaggerDate sorts the reference in ascending order by its tag creation date
	TaggerDate SortKey = "taggerdate"

	// TaggerDateDesc sorts the reference in descending order by its tag
	// creation date
	TaggerDateDesc SortKey = "-taggerdate"

	// Version interpolates the references as a version number and sorts in
	// ascending order
	Version SortKey = "version:refname"

	// VersionDesc interpolates the references as a version number and sorts in
	// descending order
	VersionDesc SortKey = "-version:refname"
)

func (SortKey) String added in v0.3.0

func (k SortKey) String() string

String converts the sort key from an enum into its string counterpart

type StageOption added in v0.2.0

type StageOption func(*stageOptions)

StageOption provides a way for setting specific options during a stage operation. Each supported option can customize the way files are staged within the current repository (working directory)

func WithPathSpecs added in v0.2.0

func WithPathSpecs(specs ...string) StageOption

WithPathSpecs permits a series of PathSpecs (or globs) to be defined that will stage any matching files within the current repository (working directory). Paths to files and folders are relative to the root of the repository. All leading and trailing whitespace will be trimmed from the file paths, allowing empty paths to be ignored

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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