git

package
v0.0.0-...-8bbc6b9 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2016 License: MIT, Apache-2.0 Imports: 19 Imported by: 0

README

Git Shell Build Status

Package git-module is a Go module for Git access through shell commands.

Limitations

  • Go version must be at least 1.3.
  • Git version must be no less than 1.7.1, and great than or equal to 1.8.0 is recommended.
  • For Windows users, try use as higher version as possible.

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const BRANCH_PREFIX = "refs/heads/"
View Source
const DEFAULT_TIMEOUT = 60 * time.Second
View Source
const (
	HOOK_PATH_UPDATE = "hooks/update"
)
View Source
const TAG_PREFIX = "refs/tags/"

Variables

View Source
var (
	// Debug enables verbose logging on everything.
	// This should be false in case Gogs starts in SSH mode.
	Debug  = false
	Prefix = "[git-module] "
)
View Source
var CommitsRangeSize = 50
View Source
var (
	ErrNotValidHook = errors.New("not a valid Git hook")
)

Functions

func AddChanges

func AddChanges(repoPath string, all bool, files ...string) error

AddAllChanges marks local changes to be ready for commit.

func BinVersion

func BinVersion() (string, error)

Version returns current Git version from shell.

func Clone

func Clone(from, to string, opts CloneRepoOptions) (err error)

Clone clones original repository to target path.

func CommitChanges

func CommitChanges(repoPath, message string, author *Signature) error

CommitChanges commits local changes with given message and author.

func CommitsCount

func CommitsCount(repoPath, revision string) (int64, error)

CommitsCount returns number of total commits of until given revision.

func Fsck

func Fsck(repoPath string, timeout time.Duration, args ...string) error

Fsck verifies the connectivity and validity of the objects in the database

func InitRepository

func InitRepository(repoPath string, bare bool) error

InitRepository initializes a new Git repository.

func IsBranchExist

func IsBranchExist(repoPath, name string) bool

IsBranchExist returns true if given branch exists in the repository.

func IsErrExecTimeout

func IsErrExecTimeout(err error) bool

func IsErrNotExist

func IsErrNotExist(err error) bool

func IsErrUnsupportedVersion

func IsErrUnsupportedVersion(err error) bool

func IsReferenceExist

func IsReferenceExist(repoPath, name string) bool

IsReferenceExist returns true if given reference exists in the repository.

func IsTagExist

func IsTagExist(repoPath, name string) bool

IsTagExist returns true if given tag exists in the repository.

func IsValidHookName

func IsValidHookName(name string) bool

IsValidHookName returns true if given name is a valid Git hook.

func MustID

func MustID(b []byte) sha1

MustID always creates a new sha1 from a [20]byte array with no validation of input.

func MustIDFromString

func MustIDFromString(s string) sha1

MustIDFromString always creates a new sha from a ID with no validation of input.

func NewID

func NewID(b []byte) (sha1, error)

NewID creates a new sha1 from a [20]byte array.

func NewIDFromString

func NewIDFromString(s string) (sha1, error)

NewIDFromString creates a new sha1 from a ID string of length 40.

func Pull

func Pull(repoPath string, opts PullRemoteOptions) error

Pull pulls changes from remotes.

func Push

func Push(repoPath, remote, branch string) error

Push pushs local commits to given remote branch.

func RefEndName

func RefEndName(refStr string) string

func ResetHEAD

func ResetHEAD(repoPath string, hard bool, revision string) error

ResetHEAD resets HEAD to given revision or head of branch.

func SetUpdateHook

func SetUpdateHook(repoPath, content string) error

SetUpdateHook writes given content to update hook of the reposiotry.

func UnescapeChars

func UnescapeChars(in []byte) []byte

UnescapeChars reverses escaped characters.

func Version

func Version() string

Types

type ArchiveType

type ArchiveType int
const (
	ZIP ArchiveType = iota + 1
	TARGZ
)

type Blob

type Blob struct {
	*TreeEntry
	// contains filtered or unexported fields
}

Blob represents a Git object.

func (*Blob) Data

func (b *Blob) Data() (io.Reader, error)

Data gets content of blob all at once and wrap it as io.Reader. This can be very slow and memory consuming for huge content.

func (*Blob) DataPipeline

func (b *Blob) DataPipeline(stdout, stderr io.Writer) error

type Branch

type Branch struct {
	Name string
	Path string
}

Branch represents a Git branch.

type CloneRepoOptions

type CloneRepoOptions struct {
	Mirror  bool
	Bare    bool
	Quiet   bool
	Timeout time.Duration
}

type Command

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

Command represents a command with its subcommands or arguments.

func NewCommand

func NewCommand(args ...string) *Command

NewCommand creates and returns a new Git Command based on given command and arguments.

func (*Command) AddArguments

func (c *Command) AddArguments(args ...string) *Command

AddArguments adds new argument(s) to the command.

func (*Command) Run

func (c *Command) Run() (string, error)

Run executes the command in defualt working directory and returns stdout in string and error (combined with stderr).

func (*Command) RunInDir

func (c *Command) RunInDir(dir string) (string, error)

RunInDir executes the command in given directory and returns stdout in string and error (combined with stderr).

func (*Command) RunInDirBytes

func (c *Command) RunInDirBytes(dir string) ([]byte, error)

RunInDir executes the command in given directory and returns stdout in []byte and error (combined with stderr).

func (*Command) RunInDirPipeline

func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error

RunInDirPipeline executes the command in given directory, it pipes stdout and stderr to given io.Writer.

func (*Command) RunInDirTimeout

func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error)

RunInDirTimeout executes the command in given directory with given timeout, and returns stdout in []byte and error (combined with stderr).

func (*Command) RunInDirTimeoutPipeline

func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error

RunInDirTimeoutPipeline executes the command in given directory with given timeout, it pipes stdout and stderr to given io.Writer.

func (*Command) RunTimeout

func (c *Command) RunTimeout(timeout time.Duration) (string, error)

RunTimeout executes the command in defualt working directory with given timeout, and returns stdout in string and error (combined with stderr).

func (*Command) String

func (c *Command) String() string

type Commit

type Commit struct {
	Tree
	ID            sha1 // The ID of this commit object
	Author        *Signature
	Committer     *Signature
	CommitMessage string
	// contains filtered or unexported fields
}

Commit represents a git commit.

func (*Commit) CommitsBefore

func (c *Commit) CommitsBefore() (*list.List, error)

func (*Commit) CommitsBeforeLimit

func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error)

func (*Commit) CommitsBeforeUntil

func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error)

func (*Commit) CommitsByRange

func (c *Commit) CommitsByRange(page int) (*list.List, error)

func (*Commit) CommitsCount

func (c *Commit) CommitsCount() (int64, error)

func (*Commit) CreateArchive

func (c *Commit) CreateArchive(path string, archiveType ArchiveType) error

func (*Commit) GetCommitByPath

func (c *Commit) GetCommitByPath(relpath string) (*Commit, error)

GetCommitByPath return the commit of relative path object.

func (*Commit) GetSubModule

func (c *Commit) GetSubModule(entryname string) (*SubModule, error)

func (*Commit) GetSubModules

func (c *Commit) GetSubModules() (*objectCache, error)

func (*Commit) IsImageFile

func (c *Commit) IsImageFile(name string) bool

func (*Commit) Message

func (c *Commit) Message() string

Message returns the commit message. Same as retrieving CommitMessage directly.

func (*Commit) Parent

func (c *Commit) Parent(n int) (*Commit, error)

Parent returns n-th parent (0-based index) of the commit.

func (*Commit) ParentCount

func (c *Commit) ParentCount() int

ParentCount returns number of parents of the commit. 0 if this is the root commit, otherwise 1,2, etc.

func (*Commit) ParentID

func (c *Commit) ParentID(n int) (sha1, error)

ParentID returns oid of n-th parent (0-based index). It returns nil if no such parent exists.

func (*Commit) SearchCommits

func (c *Commit) SearchCommits(keyword string) (*list.List, error)

func (*Commit) Summary

func (c *Commit) Summary() string

Summary returns first line of commit message.

type Entries

type Entries []*TreeEntry

func (Entries) GetCommitsInfo

func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error)

GetCommitsInfo takes advantages of concurrey to speed up getting information of all commits that are corresponding to these entries. TODO: limit max goroutines number should be configurable

func (Entries) Len

func (tes Entries) Len() int

func (Entries) Less

func (tes Entries) Less(i, j int) bool

func (Entries) Sort

func (tes Entries) Sort()

func (Entries) Swap

func (tes Entries) Swap(i, j int)

type EntryMode

type EntryMode int
const (
	ENTRY_MODE_BLOB    EntryMode = 0100644
	ENTRY_MODE_EXEC    EntryMode = 0100755
	ENTRY_MODE_SYMLINK EntryMode = 0120000
	ENTRY_MODE_COMMIT  EntryMode = 0160000
	ENTRY_MODE_TREE    EntryMode = 0040000
)

There are only a few file modes in Git. They look like unix file modes, but they can only be one of these.

type ErrExecTimeout

type ErrExecTimeout struct {
	Duration time.Duration
}

func (ErrExecTimeout) Error

func (err ErrExecTimeout) Error() string

type ErrNotExist

type ErrNotExist struct {
	ID      string
	RelPath string
}

func (ErrNotExist) Error

func (err ErrNotExist) Error() string

type ErrUnsupportedVersion

type ErrUnsupportedVersion struct {
	Required string
}

func (ErrUnsupportedVersion) Error

func (err ErrUnsupportedVersion) Error() string

type Hook

type Hook struct {
	IsActive bool   // Indicates whether repository has this hook.
	Content  string // Content of hook if it's active.
	Sample   string // Sample content from Git.
	// contains filtered or unexported fields
}

Hook represents a Git hook.

func GetHook

func GetHook(repoPath, name string) (*Hook, error)

GetHook returns a Git hook by given name and repository.

func ListHooks

func ListHooks(repoPath string) (_ []*Hook, err error)

ListHooks returns a list of Git hooks of given repository.

func (*Hook) Name

func (h *Hook) Name() string

func (*Hook) Update

func (h *Hook) Update() error

Update updates hook settings.

type ObjectType

type ObjectType string
const (
	OBJECT_COMMIT ObjectType = "commit"
	OBJECT_TREE   ObjectType = "tree"
	OBJECT_BLOB   ObjectType = "blob"
	OBJECT_TAG    ObjectType = "tag"
)

type PullRemoteOptions

type PullRemoteOptions struct {
	All     bool
	Timeout time.Duration
}

type PullRequestInfo

type PullRequestInfo struct {
	MergeBase string
	Commits   *list.List
	NumFiles  int
}

PullRequestInfo represents needed information for a pull request.

type Repository

type Repository struct {
	Path string
	// contains filtered or unexported fields
}

Repository represents a Git repository.

func OpenRepository

func OpenRepository(repoPath string) (*Repository, error)

OpenRepository opens the repository at the given path.

func (*Repository) AddRemote

func (repo *Repository) AddRemote(name, url string, fetch bool) error

AddRemote adds a new remote to repository.

func (*Repository) CommitsBetween

func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error)

func (*Repository) CommitsBetweenIDs

func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, error)

func (*Repository) CommitsByFileAndRange

func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error)

func (*Repository) CommitsCountBetween

func (repo *Repository) CommitsCountBetween(start, end string) (int64, error)

func (*Repository) CreateTag

func (repo *Repository) CreateTag(name, revision string) error

func (*Repository) FileCommitsCount

func (repo *Repository) FileCommitsCount(revision, file string) (int64, error)

func (*Repository) FilesCountBetween

func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error)

func (*Repository) GetBranchCommit

func (repo *Repository) GetBranchCommit(name string) (*Commit, error)

GetBranchCommit returns the last commit of given branch.

func (*Repository) GetBranchCommitID

func (repo *Repository) GetBranchCommitID(name string) (string, error)

GetBranchCommitID returns last commit ID string of given branch.

func (*Repository) GetBranches

func (repo *Repository) GetBranches() ([]string, error)

GetBranches returns all branches of the repository.

func (*Repository) GetCommit

func (repo *Repository) GetCommit(commitID string) (*Commit, error)

GetCommit returns commit object of by ID string.

func (*Repository) GetCommitByPath

func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error)

GetCommitByPath returns the last commit of relative path.

func (*Repository) GetHEADBranch

func (repo *Repository) GetHEADBranch() (*Branch, error)

GetHEADBranch returns corresponding branch of HEAD.

func (*Repository) GetHook

func (repo *Repository) GetHook(name string) (*Hook, error)

func (*Repository) GetMergeBase

func (repo *Repository) GetMergeBase(base, head string) (string, error)

GetMergeBase checks and returns merge base of two branches.

func (*Repository) GetPatch

func (repo *Repository) GetPatch(base, head string) ([]byte, error)

GetPatch generates and returns patch data between given revisions.

func (*Repository) GetPullRequestInfo

func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error)

GetPullRequestInfo generates and returns pull request information between base and head branches of repositories.

func (*Repository) GetTag

func (repo *Repository) GetTag(name string) (*Tag, error)

GetTag returns a Git tag by given name.

func (*Repository) GetTagCommit

func (repo *Repository) GetTagCommit(name string) (*Commit, error)

func (*Repository) GetTagCommitID

func (repo *Repository) GetTagCommitID(name string) (string, error)

GetTagCommitID returns last commit ID string of given tag.

func (*Repository) GetTags

func (repo *Repository) GetTags() ([]string, error)

GetTags returns all tags of the repository.

func (*Repository) GetTree

func (repo *Repository) GetTree(idStr string) (*Tree, error)

Find the tree object in the repository.

func (*Repository) Hooks

func (repo *Repository) Hooks() ([]*Hook, error)

func (*Repository) IsBranchExist

func (repo *Repository) IsBranchExist(name string) bool

func (*Repository) IsTagExist

func (repo *Repository) IsTagExist(name string) bool

func (*Repository) RemoveRemote

func (repo *Repository) RemoveRemote(name string) error

RemoveRemote removes a remote from repository.

func (*Repository) SetDefaultBranch

func (repo *Repository) SetDefaultBranch(name string) error

SetDefaultBranch sets default branch of repository.

type Signature

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

Signature represents the Author or Committer information.

type SubModule

type SubModule struct {
	Name string
	Url  string
}

type SubModuleFile

type SubModuleFile struct {
	*Commit
	// contains filtered or unexported fields
}

SubModuleFile represents a file with submodule type.

func NewSubModuleFile

func NewSubModuleFile(c *Commit, refUrl, refId string) *SubModuleFile

func (*SubModuleFile) RefId

func (sf *SubModuleFile) RefId() string

RefId returns reference ID.

func (*SubModuleFile) RefUrl

func (sf *SubModuleFile) RefUrl(urlPrefix string) string

FIXME: remove import of setting RefUrl guesses and returns reference URL.

type Tag

type Tag struct {
	Name string
	ID   sha1

	Object  sha1 // The id of this commit object
	Type    string
	Tagger  *Signature
	Message string
	// contains filtered or unexported fields
}

Tag represents a Git tag.

func (*Tag) Commit

func (tag *Tag) Commit() (*Commit, error)

type Tree

type Tree struct {
	ID sha1
	// contains filtered or unexported fields
}

Tree represents a flat directory listing.

func NewTree

func NewTree(repo *Repository, id sha1) *Tree

func (*Tree) GetBlobByPath

func (t *Tree) GetBlobByPath(relpath string) (*Blob, error)

func (*Tree) GetTreeEntryByPath

func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error)

func (*Tree) ListEntries

func (t *Tree) ListEntries() (Entries, error)

ListEntries returns all entries of current tree.

func (*Tree) SubTree

func (t *Tree) SubTree(rpath string) (*Tree, error)

type TreeEntry

type TreeEntry struct {
	ID   sha1
	Type ObjectType
	// contains filtered or unexported fields
}

func (*TreeEntry) Blob

func (te *TreeEntry) Blob() *Blob

func (*TreeEntry) IsDir

func (te *TreeEntry) IsDir() bool

func (*TreeEntry) IsSubModule

func (te *TreeEntry) IsSubModule() bool

func (*TreeEntry) Name

func (te *TreeEntry) Name() string

func (*TreeEntry) Size

func (te *TreeEntry) Size() int64

Jump to

Keyboard shortcuts

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