git

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2019 License: MIT Imports: 6 Imported by: 0

README

GoDoc CircleCI Go Report Card

libgit2-api

This project aim to be an idiomatic and simple interface to libgit2 and eventually to a git repository. The main idea is to use it just as you use in command line.

The main reason of developing such library is to use it on my various git tools. For now basic functionalities will be implemented. These are:

  • Clone
  • Checkout
  • Pull
  • Log (kind of)

Using this API

First of all you should get this project by:

go get -d github.com/isacikoz/libgit2-api

After you downloaded it you will need a compiled libgit2 library on your operating system. If you are on macOS and using brew, you can install libgit2 via brew install libgit2, if you want to build it by yourself here you go:

  • make sure you have following libraries and tools installed:

    • cmake
    • pkg-config
    • libssl
    • libssh2
  • run the script file (Linux): scripts/install-libgit2.sh

After you install required library, you can use this API. Also, I am considering to supply a sample make file so that you can build your Go application with this project.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAuthenticationRequired as the name implies
	ErrAuthenticationRequired = errors.New("authentication required")
	// ErrAuthenticationType means that given credentials cannot be used for given repository url
	ErrAuthenticationType = errors.New("authentication method is not valid")
	// ErrClone is a generic clone error
	ErrClone = errors.New("cannot clone repo")
	// ErrCannotOpenRepo is returned when the repo couldn't be loaded from filesystem
	ErrCannotOpenRepo = errors.New("cannot load repository")
	// ErrCreateCallbackFail is reuterned when an error occurred while creating callbacks
	ErrCreateCallbackFail = errors.New("cannot create default callbacks")
	// ErrNoRemoteName if the remote name is empty while fetching
	ErrNoRemoteName = errors.New("remote name not specified")
	// ErrNotValidRemoteName is returned if the given remote name is not found
	ErrNotValidRemoteName = errors.New("not a valid remote name")
	// ErrAlreadyUpToDate if the repo is up-to-date
	ErrAlreadyUpToDate = errors.New("already up-to-date")
	// ErrFastForwardOnly if the merge can be made by fast-forward
	ErrFastForwardOnly = errors.New("fast-forward only")
	// ErrBranchNotFound is returned when the given ref can't found
	ErrBranchNotFound = errors.New("cannot locate remote-tracking branch")
	// ErrEntryNotIndexed is returned when the entry is not indexed
	ErrEntryNotIndexed = errors.New("entry is not indexed")
)

Functions

This section is empty.

Types

type Branch

type Branch struct {
	Name     string
	FullName string
	Hash     string

	Head     bool
	Ahead    int
	Behind   int
	Upstream *Branch
	// contains filtered or unexported fields
}

Branch is a wrapper of lib.Branch object

func (*Branch) IsRemote

func (b *Branch) IsRemote() bool

IsRemote returns false if it is a local branch

func (*Branch) String

func (b *Branch) String() string

func (*Branch) Target

func (b *Branch) Target() *Commit

Target is the hash of targeted commit

func (*Branch) Type

func (b *Branch) Type() RefType

Type is the reference type of this ref

type CloneOptions

type CloneOptions struct {
	Bare        bool
	Recursive   bool
	Depth       int
	Credentials Credential
}

CloneOptions are mostly used git clone options from a remote

type Commit

type Commit struct {
	Author  *Signature
	Message string
	Summary string
	Hash    string
	// contains filtered or unexported fields
}

Commit is the wrapper of actual lib.Commit object

func (*Commit) Amend

func (c *Commit) Amend(message string, author ...*Signature) (*Commit, error)

Amend updates the commit and returns NEW commit pointer

func (*Commit) Diff

func (c *Commit) Diff() (*Diff, error)

Diff has similar behavior to "git diff <commit>"

func (*Commit) ParentID

func (c *Commit) ParentID() (string, error)

ParentID returns the commits parent hash.

func (*Commit) String

func (c *Commit) String() string

type CredType

type CredType uint8

CredType defines the credentials type for authentication with remote

const (
	// CredTypeUserpassPlaintext is used for http, https schemes
	CredTypeUserpassPlaintext CredType = iota
	// CredTypeSSHKey is for authenticating over ssh
	CredTypeSSHKey
	// CredTypeSSHAgent is used when using an agent for ssh auth
	CredTypeSSHAgent
)

type Credential

type Credential interface {
	// Type returns the type of credential
	Type() CredType
}

Credential is an interface for specfying its type

type CredentialsAsPlainText

type CredentialsAsPlainText struct {
	UserName string
	Password string
}

CredentialsAsPlainText contains basic username and password information

func (*CredentialsAsPlainText) Type

func (c *CredentialsAsPlainText) Type() CredType

Type returns the type of credential

type CredentialsAsSSHAgent

type CredentialsAsSSHAgent struct {
	UserName string
}

CredentialsAsSSHAgent holds only usernmae if ssh daemon working

func (*CredentialsAsSSHAgent) Type

func (c *CredentialsAsSSHAgent) Type() CredType

Type returns the type of credential

type CredentialsAsSSHKey

type CredentialsAsSSHKey struct {
	UserName       string
	PublicKeyPath  string
	PrivateKeyPath string
	Passphrase     string
}

CredentialsAsSSHKey contains ssh file paths and related information

func (*CredentialsAsSSHKey) Type

func (c *CredentialsAsSSHKey) Type() CredType

Type returns the type of credential

type DeltaStatus

type DeltaStatus int

DeltaStatus ondicates a files status in a diff

const (
	DeltaUnmodified DeltaStatus = iota
	DeltaAdded
	DeltaDeleted
	DeltaModified
	DeltaRenamed
	DeltaCopied
	DeltaIgnored
	DeltaUntracked
	DeltaTypeChange
	DeltaUnreadable
	DeltaConflicted
)

Delta status of a file e.g. on a commit

type Diff

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

Diff is the wrapper for a diff content acquired from repo

func (*Diff) Deltas

func (d *Diff) Deltas() []*DiffDelta

Deltas returns the actual changes with file info

type DiffDelta

type DiffDelta struct {
	Status  DeltaStatus
	OldFile *DiffFile
	NewFile *DiffFile
	Patch   string
	Commit  *Commit
}

DiffDelta holds delta status, file changes and the actual patchs

func (*DiffDelta) DeltaStatusString

func (d *DiffDelta) DeltaStatusString() string

DeltaStatusString retruns delta status as string

func (*DiffDelta) String

func (d *DiffDelta) String() string

type DiffFile

type DiffFile struct {
	Path string
	Hash string
}

DiffFile the file that has been changed

type FetchOptions

type FetchOptions struct {
	Remote      string
	Credentials Credential
	Prune       bool
	All         bool
	Tags        bool
}

FetchOptions provides common options for fetch command

type IndexType

type IndexType int

IndexType describes the different stages a status entry can be in

const (
	IndexTypeStaged IndexType = iota
	IndexTypeUnstaged
	IndexTypeUntracked
	IndexTypeConflicted
)

The different status stages

type MergeOptions

type MergeOptions struct {
	Message               string
	NoFF                  bool
	FailOnConflict        bool
	IgnoreAlreadyUpToDate bool
}

MergeOptions defines common options for merge operation

type OptionsWithCreds

type OptionsWithCreds interface {
	// contains filtered or unexported methods
}

OptionsWithCreds provides an interface to get fetch callbacks

type Ref

type Ref interface {
	Type() RefType
	Target() *Commit
	String() string
}

Ref is the wrapper of lib.Ref

type RefType

type RefType uint8

RefType defines the ref types

const (
	RefTypeTag RefType = iota
	RefTypeBranch
	RefTypeHEAD
)

These types are used for mapping references

type Repository

type Repository struct {
	RefMap map[string][]Ref
	Head   *Branch
	// contains filtered or unexported fields
}

Repository is the wrapper and main interface to git repository

func Clone

func Clone(path string, url string, opts *CloneOptions) (*Repository, error)

Clone fetches a git repository from a given url

func Open

func Open(path string) (*Repository, error)

Open load the repository from the filesystem

func (*Repository) AddToIndex

func (r *Repository) AddToIndex(e *StatusEntry) error

AddToIndex is the wrapper of "git add /path/to/file" command

func (*Repository) AddToStash added in v0.1.5

func (r *Repository) AddToStash(s *Signature, message string, flags StashFlag) (*StashedItem, error)

AddToStash saves the modifications to stash

func (*Repository) Branches

func (r *Repository) Branches() ([]*Branch, error)

Branches loads branches with the lib's branch iterator loads both remote and local branches

func (*Repository) Commit

func (r *Repository) Commit(message string, author ...*Signature) (*Commit, error)

Commit adds a new commit onject to repository warning: this function does not check if the changes are indexed

func (*Repository) Commits

func (r *Repository) Commits() ([]*Commit, error)

Commits returns all of the commits of the repository

func (*Repository) Fetch

func (r *Repository) Fetch(opts *FetchOptions) error

Fetch downloads refs from given remote

func (*Repository) LoadHead added in v0.1.5

func (r *Repository) LoadHead() error

LoadHead can be used to refresh HEAD ref

func (*Repository) LoadStatus

func (r *Repository) LoadStatus() (*Status, error)

LoadStatus simply emulates a "git status" and returns the result

func (*Repository) Merge

func (r *Repository) Merge(ref string, opts *MergeOptions) error

Merge incorporates changes from the given branch into the current branch

func (*Repository) Path

func (r *Repository) Path() string

Path returns the filesystem location of the repository

func (*Repository) RemoveFromIndex

func (r *Repository) RemoveFromIndex(e *StatusEntry) error

RemoveFromIndex is the wrapper of "git reset path/to/file" command

func (*Repository) Stashes added in v0.1.5

func (r *Repository) Stashes() ([]*StashedItem, error)

Stashes returns the stashed items of the repository

func (*Repository) Tags

func (r *Repository) Tags() ([]*Tag, error)

Tags loads tags from the refs

type Signature added in v0.1.5

type Signature struct {
	Name  string
	Email string
	When  time.Time
}

Signature is the person who signs a commit

type StashFlag added in v0.1.5

type StashFlag int

StashFlag is the flag that affect the stash save operation.

const (
	StashDefault StashFlag = iota
	StashKeepIndex
	StashIncludeUntracked
	StashIncludeIgnored
)

See https://godoc.org/github.com/libgit2/git2go#StashFlag

type StashedItem added in v0.1.5

type StashedItem struct {
	Index   int
	Hash    string
	Message string
	// contains filtered or unexported fields
}

StashedItem is a change that stashed into the repository

type State

type State int

State is the current state of the repository

const (
	StateUnknown State = iota
	StateNone
	StateMerge
	StateRevert
	StateCherrypick
	StateBisect
	StateRebase
	StateRebaseInteractive
	StateRebaseMerge
	StateApplyMailbox
	StateApplyMailboxOrRebase
)

The different states for a repo

type Status

type Status struct {
	State    State
	Entities []*StatusEntry
}

Status contains all git status data

type StatusEntry

type StatusEntry struct {
	EntryType StatusEntryType
	// contains filtered or unexported fields
}

StatusEntry contains data for a single status entry

func (*StatusEntry) Indexed

func (e *StatusEntry) Indexed() bool

Indexed true if entry added to index

func (*StatusEntry) StatusEntryString

func (e *StatusEntry) StatusEntryString() string

StatusEntryString returns entry status in pretty format

func (*StatusEntry) String

func (e *StatusEntry) String() string

Indexed true if entry added to index

type StatusEntryType

type StatusEntryType int

StatusEntryType describes the type of change a status entry has undergone

const (
	StatusEntryTypeNew StatusEntryType = iota
	StatusEntryTypeModified
	StatusEntryTypeDeleted
	StatusEntryTypeRenamed
	StatusEntryTypeUntracked
	StatusEntryTypeTypeChange
	StatusEntryTypeConflicted
)

The set of supported StatusEntryTypes

type Tag

type Tag struct {
	Hash      string
	Shorthand string
	Name      string
	// contains filtered or unexported fields
}

Tag is used to label and mark a specific commit in the history.

func (*Tag) String added in v0.1.1

func (t *Tag) String() string

func (*Tag) Target

func (t *Tag) Target() *Commit

Target is the hash of targeted commit

func (*Tag) Type

func (t *Tag) Type() RefType

Type is the reference type of this ref

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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