git

package
v0.67.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package git provide a wrapper for git command line interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckoutRevision

func CheckoutRevision(repoDir, remoteName, branch, revision string) error

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

func Clone(remoteURL, dest string) (err error)

Clone the repository into destination directory.

If destination directory is not empty it will return an error.

func FetchAll

func FetchAll(repoDir string) (err error)

FetchAll will fetch the latest commits and tags from remote.

func FetchTags

func FetchTags(repoDir string) (err error)

FetchTags will fetch all tags from remote.

func GetRemoteURL

func GetRemoteURL(repoDir, remoteName string) (url string, err error)

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

func GetTag(repoDir, revision string) (tag string, err error)

GetTag get the tag from revision. If revision is empty it's default to "HEAD".

func LatestCommit

func LatestCommit(repoDir, ref string) (commit string, err error)

LatestCommit get the latest commit hash in short format from "ref". If ref is empty, its default to "origin/master".

func LatestTag

func LatestTag(repoDir string) (tag string, err error)

LatestTag get latest tag.

func LatestVersion

func LatestVersion(repoDir string) (version string, err error)

LatestVersion will try to get latest tag from repository. If it's fail get the latest commit hash.

func ListTags

func ListTags(repoDir string) (tags []string, err error)

ListTags get all tags from repository.

func LogRevisions

func LogRevisions(repoDir, prevRevision, nextRevision string) (err error)

LogRevisions get commits between two revisions.

func RemoteBranches

func RemoteBranches(repoDir string) ([]string, error)

RemoteBranches return list of remote branches.

func RemoteChange

func RemoteChange(repoDir, oldName, newName, newURL string) (err error)

RemoteChange change current repository remote name (e.g. "origin") to new remote name and URL.

Types

type Git

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

Git is a type for working with single git repository.

func New

func New(dir string) (git *Git, err error)

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

func (git *Git) Equal(v any) bool

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

func (git *Git) IsIgnored(path string) (b bool)

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

func (git *Git) LogFollow(path, format string) (logs []string, err error)

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.

func (*Git) String

func (git *Git) String() string

String return the working directory with prefix "git+file://".

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

func LoadGitignore(dir string) (ign *Gitignore, err error)

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

func (ign *Gitignore) IsIgnored(path string) bool

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

func (ign *Gitignore) Parse(dir string, content []byte)

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

Jump to

Keyboard shortcuts

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