Documentation
¶
Overview ¶
Package git provide a wrapper for git command line interface.
Index ¶
- func CheckoutRevision(repoDir, remoteName, branch, revision string) error
- func Clone(remoteURL, dest string) (err error)
- func FetchAll(repoDir string) (err error)
- func FetchTags(repoDir string) (err error)
- func GetRemoteURL(repoDir, remoteName string) (url string, err error)
- func GetTag(repoDir, revision string) (tag string, err error)
- func LatestCommit(repoDir, ref string) (commit string, err error)
- func LatestTag(repoDir string) (tag string, err error)
- func LatestVersion(repoDir string) (version string, err error)
- func ListTags(repoDir string) (tags []string, err error)
- func LogRevisions(repoDir, prevRevision, nextRevision string) (err error)
- func RemoteBranches(repoDir string) ([]string, error)
- func RemoteChange(repoDir, oldName, newName, newURL string) (err error)
- type Git
- type Gitignore
- type IgnorePattern
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckoutRevision ¶
CheckoutRevision will set the HEAD to specific revision on specific branch. Any untracked files and directories will be removed before checking out existing branch or creating new branch. If remoteName is empty, it will use default reference "origin". If branch is empty, it will use default branch "master". If revision is empty, it will do nothing.
This function assume that repository is up-to-date with remote. Client may call FetchAll() before, to prevent checking out revision that may not exist.
func Clone ¶
Clone the repository into destination directory.
If destination directory is not empty it will return an error.
func FetchAll ¶
FetchAll will fetch the latest commits and tags from remote.
func FetchTags ¶
FetchTags will fetch all tags from remote.
func GetRemoteURL ¶
GetRemoteURL return remote URL or error if repository is not git or url is empty. If remoteName is empty, it will be set to default ("origin").
func GetTag ¶
GetTag get the tag from revision. If revision is empty it's default to "HEAD".
func LatestCommit ¶
LatestCommit get the latest commit hash in short format from "ref". If ref is empty, its default to "origin/master".
func LatestVersion ¶
LatestVersion will try to get latest tag from repository. If it's fail get the latest commit hash.
func ListTags ¶
ListTags get all tags from repository.
func LogRevisions ¶
LogRevisions get commits between two revisions.
func RemoteBranches ¶
RemoteBranches return list of remote branches.
Types ¶
type Git ¶
type Git struct {
// contains filtered or unexported fields
}
Git is a type for working with single git repository.
func New ¶
New start working on repository in directory `dir`. If the `dir` does not contains ".git" directory it will return an error, even if the parent directory may contains the ".git" directory.
func (*Git) Equal ¶
Equal return true if `v` is instance of `*Git` and has the same working directory. This implement kilabit.info/pakakeh.go/lib/reflect.Equaler interface.
Example ¶
package main
import (
"fmt"
"log"
"kilabit.info/pakakeh.go/lib/git"
)
func main() {
var agit *git.Git
var err error
agit, err = git.New(`testdata/Equal`)
if err != nil {
log.Fatal(err)
}
var vgit *git.Git
var dir = `testdata/IsIgnored`
vgit, err = git.New(dir)
if err != nil {
log.Fatal(err)
}
var got = agit.Equal(vgit)
fmt.Printf("On git %s: Equal is %t\n", dir, got)
dir = `testdata/Equal`
vgit, err = git.New(dir)
if err != nil {
log.Fatal(err)
}
got = agit.Equal(vgit)
fmt.Printf("On git %s: Equal is %t\n", dir, got)
}
Output: On git testdata/IsIgnored: Equal is false On git testdata/Equal: Equal is true
func (*Git) IsIgnored ¶
IsIgnored return true if the `path` is ignored by git. This is processed by matching it with all of the patterns in the ".gitignore" file inside the path directory and its parent, until the root of Git repository.
Example ¶
package main
import (
"fmt"
"log"
"kilabit.info/pakakeh.go/lib/git"
)
func main() {
var agit *git.Git
var err error
agit, err = git.New(`testdata/IsIgnored`)
if err != nil {
log.Fatal(err)
}
var listPath = []string{
``,
`vendor`,
`vendor/dummy`,
`hello.html`,
`hello.go`,
`foo/hello.go`,
}
var path string
var got bool
for _, path = range listPath {
got = agit.IsIgnored(path)
fmt.Printf("%q: %t\n", path, got)
}
}
Output: "": true "vendor": true "vendor/dummy": true "hello.html": true "hello.go": false "foo/hello.go": false
func (*Git) LogFollow ¶
LogFollow return history of single file `path`, following rename.
The format parameter set the output format, default to '%h,%at,%an,%ae,%s' which print short hash, author commit timestamp, author name, author email, and subject; respectively separated by comma.
type Gitignore ¶
type Gitignore struct {
// contains filtered or unexported fields
}
Gitignore is a type that represent ".gitignore" file. The content of Gitignore can be populated from LoadGitignore function or Gitignore.Parse method.
func LoadGitignore ¶
LoadGitignore load the gitignore file inside directory `dir`. It will return nil without error if the ".gitignore" file is not exists.
Any invalid pattern will be ignored.
func (*Gitignore) IsIgnored ¶
IsIgnored return true if the `path` is ignored by this Gitignore content. The `path` is relative to Gitignore directory.
Example ¶
package main
import (
"fmt"
"kilabit.info/pakakeh.go/lib/git"
)
func main() {
var ign = git.Gitignore{}
ign.Parse(`testdata/IsIgnored/`, []byte(`# comment
# comment
vendor/ # Ignore vendor directory, but not vendor file.
/hello.* # Ignore hello at root, but not foo/hello.go.
!hello.go`))
var listPath = []string{
``,
`vendor`,
`vendor/dummy`,
`hello.html`,
`hello.go`,
`foo/hello.go`,
`foo/vendor`,
}
for _, path := range listPath {
fmt.Printf("%q: %t\n", path, ign.IsIgnored(path))
}
}
Output: "": true "vendor": true "vendor/dummy": true "hello.html": true "hello.go": false "foo/hello.go": false "foo/vendor": false
func (*Gitignore) Parse ¶
Parse the raw content of ".gitignore" file that located inside the `dir` directory. This is an alternative to populate Gitignore content beside LoadGitignore. Any invalid pattern inside the `content` will be ignored.
type IgnorePattern ¶
type IgnorePattern struct {
// contains filtered or unexported fields
}
IgnorePattern is a type that store the parsed ignore pattern line from gitignore file.
func ParseIgnorePattern ¶
func ParseIgnorePattern(line []byte) (ign IgnorePattern)
ParseIgnorePattern parse the line from gitignore. At this point, the line must be not empty and not a comment. If the pattern is invalid it will be ignored and [IsMatch] will always return false.
func (*IgnorePattern) IsMatch ¶
func (pat *IgnorePattern) IsMatch(path string) bool
IsMatch return true if the `path` match with the pattern.
Source Files
¶
- git.go
- gitignore.go
- ignore_pattern.go