gitw

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 22 Imported by: 3

README

Gitw

GitHub go.mod Go version GitHub tag (latest SemVer) Go Reference Go Report Card Unit-Tests Coverage Status

gitw - Git command wrapper, generate git changelog, fetch repo information and some git tools.

  • Wrap local git commands
  • Quickly run git commands
  • Quickly query repository information
    • Quick fetch status, remote, branch ... details
  • Quickly generate version changelogs via git log
    • Allow custom build configuration
    • Allow custom build filtering , styles, etc
    • can be used directly in GitHub Actions
  • Support git-emoji code search and replace render

中文说明

Install

required: go 1.18+, git 2.x

go get github.com/gookit/gitw

Usage

package main

import (
	"fmt"

	"github.com/gookit/gitw"
)

func main() {
	// logTxt, err := gitw.ShowLogs("v1.0.2", "v1.0.3")
	logTxt := gitw.MustString(gitw.ShowLogs("v1.0.2", "v1.0.3"))
	fmt.Println(logTxt)

	// Local Branches
	brList := gitw.MustStrings(gitw.Branches())
	fmt.Println(brList)

	// custom create command

	logCmd := gitw.New("log", "-2")
	// git.Run()
	// txt, err := logCmd.Output()
	txt := logCmd.SafeOutput()

	fmt.Println(txt)
}
With more arguments

Examples, get commit logs between two sha versions via git log

	logCmd := gitw.Log("--reverse").
		Argf("--pretty=format:\"%s\"", c.cfg.LogFormat)

	if c.cfg.Verbose {
		logCmd.OnBeforeExec(gitw.PrintCmdline)
	}

	// add custom args. eg: "--no-merges"
	logCmd.AddArgs("--no-merges")

	// logCmd.Argf("%s...%s", "v0.1.0", "HEAD")
	if sha1 != "" && sha2 != "" {
		logCmd.Argf("%s...%s", sha1, sha2)
	}

	fmt.Println(logCmd.SafeOutput())

Repository

You can quickly get a git repository information at local.

repo := gitw.NewRepo("/path/to/my-repo")
Status Information
si := repo.StatusInfo()

dump.Println(si)

Output:

repo-status-info

Branch Information
brInfo := repo.CurBranchInfo()

dump.Println(brInfo)

Output:

one-remote-info

Remote Information
rt := repo.DefaultRemoteInfo()

dump.Println(rt)

Output:

one-remote-info

Repo Information
dump.Println(repo.Info())

Output:

simple-repo-info

Changelog

You can quickly generate changelog by gitw/chlog package.

  • Allows custom build configuration. see .github/changelog.yml
  • can set filtering, grouping, output styles, etc.
Install
go install github.com/gookit/gitw/cmd/chlog@latest
Usage

Please run chlog -h to see help:

chlog-help

Generate changelog:

chlog prev last
chlog last head
chlog -c .github/changelog.yml last head

Outputs:

chlog-demo

Use on action

Can use gitw/chlog on GitHub actions. It does not depend on the Go environment, just download the binary files of the corresponding system.

Example:

Full script please see .github/workflows/release.yml

# ...

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Generate changelog
        run: |
          curl https://github.com/gookit/gitw/releases/latest/download/chlog-linux-amd64 -L -o /usr/local/bin/chlog
          chmod a+x /usr/local/bin/chlog
          chlog -c .github/changelog.yml -o changelog.md prev last 

Use in code
package main

import (
	"fmt"

	"github.com/gookit/gitw/chlog"
	"github.com/gookit/goutil"
)

func main() {
	cl := chlog.New()
	cl.Formatter = &chlog.MarkdownFormatter{
		RepoURL: "https://github.com/gookit/gitw",
	}
	cl.WithConfig(func(c *chlog.Config) {
		// some settings ...
		c.Title = "## Change Log"
	})

	// fetch git log
	cl.FetchGitLog("v0.1.0", "HEAD", "--no-merges")

	// do generate
	goutil.PanicIfErr(cl.Generate())

	// dump
	fmt.Println(cl.Changelog())
}

Commands

Methods in GitWrap

Commands of git, more please see pkg.go.dev

func (gw *GitWrap) Add(args ...string) *GitWrap
func (gw *GitWrap) Annotate(args ...string) *GitWrap
func (gw *GitWrap) Apply(args ...string) *GitWrap
func (gw *GitWrap) Bisect(args ...string) *GitWrap
func (gw *GitWrap) Blame(args ...string) *GitWrap
func (gw *GitWrap) Branch(args ...string) *GitWrap
func (gw *GitWrap) Checkout(args ...string) *GitWrap
func (gw *GitWrap) CherryPick(args ...string) *GitWrap
func (gw *GitWrap) Clean(args ...string) *GitWrap
func (gw *GitWrap) Clone(args ...string) *GitWrap
func (gw *GitWrap) Commit(args ...string) *GitWrap
func (gw *GitWrap) Config(args ...string) *GitWrap
func (gw *GitWrap) Describe(args ...string) *GitWrap
func (gw *GitWrap) Diff(args ...string) *GitWrap
func (gw *GitWrap) Fetch(args ...string) *GitWrap
func (gw *GitWrap) Init(args ...string) *GitWrap
func (gw *GitWrap) Log(args ...string) *GitWrap
func (gw *GitWrap) Merge(args ...string) *GitWrap
func (gw *GitWrap) Pull(args ...string) *GitWrap
func (gw *GitWrap) Push(args ...string) *GitWrap
func (gw *GitWrap) Rebase(args ...string) *GitWrap
func (gw *GitWrap) Reflog(args ...string) *GitWrap
func (gw *GitWrap) Remote(args ...string) *GitWrap
// and more ...
Commonly git functions

Git command functions of std:

func Alias(name string) string
func AllVars() string
func Branches() ([]string, error)
func CommentChar(text string) (string, error)
func Config(name string) string
func ConfigAll(name string) ([]string, error)
func DataDir() (string, error)
func EditText(data string) string
func Editor() string
func GlobalConfig(name string) (string, error)
func HasDotGitDir(path string) bool
func HasFile(segments ...string) bool
func Head() (string, error)
func Quiet(args ...string) bool
func Ref(ref string) (string, error)
func RefList(a, b string) ([]string, error)
func Remotes() ([]string, error)
func SetGlobalConfig(name, value string) error
func SetWorkdir(dir string)
func ShowDiff(sha string) (string, error)
func ShowLogs(sha1, sha2 string) (string, error)
func Spawn(args ...string) error
func SymbolicFullName(name string) (string, error)
func SymbolicRef(ref string) (string, error)
func Tags(args ...string) ([]string, error)
func Var(name string) string
func Version() (string, error)
func Workdir() (string, error)
func WorkdirName() (string, error)
Util functions
func SetDebug()
func SetDebug(open bool)
func IsDebug() bool
func IsGitCmd(command string) bool
func IsGitCommand(command string) bool
func IsGitDir(dir string) bool
func ParseRemoteURL(URL string, r *RemoteInfo) (err error)
func MustString(s string, err error) string
func MustStrings(ss []string, err error) []string
func FirstLine(output string) string
func OutputLines(output string) []string
func EditText(data string) string
Remote info
// type RemoteInfo
func NewEmptyRemoteInfo(URL string) *RemoteInfo
func NewRemoteInfo(name, url, typ string) (*RemoteInfo, error)
func (r *RemoteInfo) GitURL() string
func (r *RemoteInfo) Invalid() bool
func (r *RemoteInfo) Path() string
func (r *RemoteInfo) RawURLOfHTTP() string
func (r *RemoteInfo) RepoPath() string
func (r *RemoteInfo) String() string
func (r *RemoteInfo) URLOfHTTP() string
func (r *RemoteInfo) URLOfHTTPS() string
func (r *RemoteInfo) Valid() bool

Refers

LICENSE

MIT

Documentation

Overview

Package gitw git command wrapper, git changelog, repo information and some git tools.

Index

Constants

View Source
const (
	// GitDir name
	GitDir = ".git"
	// HeadFile in .git/
	HeadFile = "HEAD"
	// ConfFile in .git/
	ConfFile = "config"
	// GitHubHost name
	GitHubHost = "github.com"
	// GitHubURL string
	GitHubURL = "https://github.com"
	// GitHubGit string
	GitHubGit = "git@github.com"
)
View Source
const (
	TypeGitHub  = "github"
	TypeGitlab  = "gitlab"
	TypeDefault = "git"
)

git host type

View Source
const (
	// DefaultBin name
	DefaultBin = "git"

	// DefaultBranchName value
	DefaultBranchName = "master"
	// DefaultRemoteName value
	DefaultRemoteName = "origin"
)
View Source
const (
	ProtoSSH  = "ssh"
	ProtoHTTP = "http"

	SchemeGIT   = "git"
	SchemeHTTP  = "http"
	SchemeHTTPS = "https"
)

some consts for remote info

View Source
const (
	BranchLocal  = "local"
	BranchRemote = "remote"
)

branch types

View Source
const (
	BrSearchLocal  uint8 = 1
	BrSearchRemote uint8 = 1 << 1
	BrSearchAll          = BrSearchLocal | BrSearchRemote
)

flags for search branches

View Source
const (
	RemoteTypePush  = "push"
	RemoteTypeFetch = "fetch"
)

remote type names

View Source
const (
	TagLast = "last"
	TagPrev = "prev"
	TagHead = "head"
)

some special keywords for match tag

View Source
const (
	RefNameTagType int = iota
	CreatorDateTagType
	DescribeTagType
)

enum type value constants for fetch tags

View Source
const RemotePfxOnBranch = "remotes/"

RemotePfxOnBranch prefix keywords

View Source
const ShaHead = "HEAD"

ShaHead keywords

View Source
const StatusPattern = `^([\w-]+)...([\w-]+)/(\w[\w/-]+)$`

StatusPattern string. eg: master...origin/master

Variables

View Source
var ErrInvalidBrLine = errorx.Raw("invalid git branch line text")

ErrInvalidBrLine error

View Source
var ErrRemoteInfoNil = errorx.Raw("the remote info data cannot be nil")

ErrRemoteInfoNil error

View Source
var GlobalFlags []string

GlobalFlags for run git command

Functions

func Alias

func Alias(name string) string

Alias find

func AllVars

func AllVars() string

AllVars get all git vars

func Branches

func Branches() ([]string, error)

Branches list

func CommentChar

func CommentChar(text string) (string, error)

CommentChar find

func Config

func Config(name string) string

Config get git config by name

func ConfigAll

func ConfigAll(name string) ([]string, error)

ConfigAll get

func DataDir

func DataDir() (string, error)

DataDir get .git dir name. eg: ".git"

func EditText

func EditText(data string) string

EditText starts an editor to edit data, and returns the edited data.

func Editor

func Editor() string

Editor returns program name of the editor. from https://github.com/alibaba/git-repo-go/blob/master/editor/editor.go

func FirstLine deprecated

func FirstLine(output string) string

FirstLine from command output.

Deprecated: please use cmdr.FirstLine

func GlobalConfig

func GlobalConfig(name string) (string, error)

GlobalConfig get git global config by name

func HasDotGitDir

func HasDotGitDir(path string) bool

HasDotGitDir in the path

func HasFile

func HasFile(segments ...string) bool

HasFile check

func Head() (string, error)

Head read current branch name. return like: "refs/heads/main"

func IsDebug

func IsDebug() bool

IsDebug mode

func IsGitCmd

func IsGitCmd(command string) bool

IsGitCmd check

func IsGitCommand

func IsGitCommand(command string) bool

IsGitCommand check

func IsGitDir

func IsGitDir(dir string) bool

IsGitDir check

func MustString

func MustString(s string, err error) string

MustString must return string, will panic on error

func MustStrings

func MustStrings(ss []string, err error) []string

MustStrings must return strings, will panic on error

func OutputLines deprecated

func OutputLines(output string) []string

OutputLines split output to lines

Deprecated: please use cmdr.OutputLines

func ParseRemoteURL

func ParseRemoteURL(URL string, r *RemoteInfo) (err error)

ParseRemoteURL info to the RemoteInfo object.

func PrintCmdline

func PrintCmdline(gw *GitWrap)

PrintCmdline on exec

func Quiet

func Quiet(args ...string) bool

Quiet run

func Ref

func Ref(ref string) (string, error)

Ref get

func RefList

func RefList(a, b string) ([]string, error)

RefList for two sha

func Remotes

func Remotes() ([]string, error)

Remotes list

func RestStd

func RestStd()

RestStd instance

func Run

func Run(args ...string) error

Run command with args

func SetDebug

func SetDebug(open bool)

SetDebug mode

func SetGlobalConfig

func SetGlobalConfig(name, value string) error

SetGlobalConfig by name

func SetWorkdir

func SetWorkdir(dir string)

SetWorkdir for the std

func ShowDiff

func ShowDiff(sha string) (string, error)

ShowDiff git log diff by a commit sha

func ShowLogs

func ShowLogs(sha1, sha2 string) (string, error)

ShowLogs show git log between sha1 to sha2

Usage:

gitw.ShowLogs("v1.0.2", "v1.0.3")
gitw.ShowLogs("commit id 1", "commit id 2")

func Spawn

func Spawn(args ...string) error

Spawn run command with args

func SymbolicFullName

func SymbolicFullName(name string) (string, error)

SymbolicFullName reads a branch name from a ref such as "@{upstream}"

func SymbolicRef

func SymbolicRef(ref string) (string, error)

SymbolicRef reads a branch name from a ref such as "HEAD"

func Tags

func Tags(args ...string) ([]string, error)

Tags list

something:

`git tag -l` == `git tag --format '%(refname:strip=2)'`

more e.g:

 // refname - sorts in a lexicographic order
 // version:refname or v:refname - this sorts based on version
	git tag --sort=-version:refname
	git tag -l --sort version:refname
	git tag --format '%(refname:strip=2)' --sort=-taggerdate
	git tag --format '%(refname:strip=2) %(objectname)' --sort=-taggerdate
	git log --tags --simplify-by-decoration --pretty="format:%d - %cr"

func Var

func Var(name string) string

Var get by git var.

Example

all: git var -l
one: git var GIT_EDITOR

func Version

func Version() (string, error)

Version info git.

func Workdir

func Workdir() (string, error)

Workdir git workdir name. alias of WorkdirName()

func WorkdirName

func WorkdirName() (string, error)

WorkdirName get git workdir name

Types

type BranchInfo added in v0.2.3

type BranchInfo struct {
	// Current active branch
	Current bool
	// Name The full branch name. eg: fea_xx, remotes/origin/fea_xx
	Name string
	// Short only branch name. local branch is equals Name. eg: fea_xx
	Short string
	// Hash commit hash ID.
	Hash string
	// HashMsg commit hash message.
	HashMsg string
	// Alias name
	Alias string
	// Remote name. local branch is empty. eg: origin
	Remote string
}

BranchInfo for a git branch

func NewBranchInfo added in v0.2.3

func NewBranchInfo(line string) (*BranchInfo, error)

NewBranchInfo from branch line text

func ParseBranchLine added in v0.2.3

func ParseBranchLine(line string, verbose bool) (*BranchInfo, error)

ParseBranchLine to BranchInfo data

verbose:

False - only branch name
True  - get by `git br -v --all`
        format: * BRANCH_NAME  COMMIT_ID  COMMIT_MSG

func (*BranchInfo) IsRemoted added in v0.2.3

func (b *BranchInfo) IsRemoted() bool

IsRemoted branch check

func (*BranchInfo) IsValid added in v0.2.3

func (b *BranchInfo) IsValid() bool

IsValid branch check

func (*BranchInfo) ParseName added in v0.2.3

func (b *BranchInfo) ParseName() *BranchInfo

ParseName for get remote and short name.

func (*BranchInfo) SetName added in v0.2.3

func (b *BranchInfo) SetName(name string)

SetName for branch and parse

type BranchInfos added in v0.2.3

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

BranchInfos for a git repo

func EmptyBranchInfos added in v0.2.3

func EmptyBranchInfos() *BranchInfos

EmptyBranchInfos instance

func NewBranchInfos added in v0.2.3

func NewBranchInfos(gitOut string) *BranchInfos

NewBranchInfos create

func (*BranchInfos) All added in v0.3.5

func (bs *BranchInfos) All() []*BranchInfo

All branches list

func (*BranchInfos) BrLines added in v0.2.3

func (bs *BranchInfos) BrLines() []string

BrLines get

func (*BranchInfos) Current added in v0.2.3

func (bs *BranchInfos) Current() *BranchInfo

Current branch

func (*BranchInfos) GetByName added in v0.2.3

func (bs *BranchInfos) GetByName(branch string, remote ...string) *BranchInfo

GetByName find branch by name

func (*BranchInfos) HasLocal added in v0.3.2

func (bs *BranchInfos) HasLocal(branch string) bool

HasLocal branch check

func (*BranchInfos) HasRemote added in v0.3.2

func (bs *BranchInfos) HasRemote(branch, remote string) bool

HasRemote branch check

func (*BranchInfos) IsExists added in v0.3.2

func (bs *BranchInfos) IsExists(branch string, remote ...string) bool

IsExists branch check

func (*BranchInfos) LastErr added in v0.2.3

func (bs *BranchInfos) LastErr() error

LastErr get

func (*BranchInfos) Locales added in v0.2.3

func (bs *BranchInfos) Locales() []*BranchInfo

Locales branches

func (*BranchInfos) Parse added in v0.2.3

func (bs *BranchInfos) Parse() *BranchInfos

Parse given branch lines

func (*BranchInfos) Remotes added in v0.2.3

func (bs *BranchInfos) Remotes(remote string) []*BranchInfo

Remotes branch infos get

if remote="", will return all remote branches

func (*BranchInfos) Search added in v0.2.3

func (bs *BranchInfos) Search(name string, flag uint8) []*BranchInfo

Search branches by name.

TIP: recommend use `SearchV2()` for search branches.

Usage:

Search("fea", BrSearchLocal)
Search("fea", BrSearchAll)
// search on remotes
Search("fea", BrSearchRemote)
// search on remotes and remote name must be equals "origin"
Search("origin:fea", BrSearchRemote)

func (*BranchInfos) SearchV2 added in v0.3.5

func (bs *BranchInfos) SearchV2(matcher brinfo.BranchMatcher, opt *SearchOpt) []*BranchInfo

SearchV2 search branches by matcher and hook func.

Usage:

SearchV2(brinfo.NewContainsMatch("fea"), &SearchOpt{})
// use multi matcher
SearchV2(brinfo.QuickMulti("start:fea","glob:fea*"), &SearchOpt{})

func (*BranchInfos) SetBrLines added in v0.2.3

func (bs *BranchInfos) SetBrLines(brLines []string)

SetBrLines for parse.

type GitWrap

type GitWrap struct {
	// Workdir for run git
	Workdir string
	// Bin git bin name. default is "git"
	Bin string
	// Args for run git. contains git command name.
	Args []string
	// Stdin more settings
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	// DryRun if True, not real execute command
	DryRun bool
	// BeforeExec command hook.
	//
	// Usage: gw.BeforeExec = gitw.PrintCmdline
	BeforeExec func(gw *GitWrap)
}

GitWrap is a project-wide struct that represents a command to be run in the console.

func Branch

func Branch(args ...string) *GitWrap

Branch command of git

func Cmd

func Cmd(cmd string, args ...string) *GitWrap

Cmd create instance with git cmd and args

func Log

func Log(args ...string) *GitWrap

Log command of git

Usage: Log("-2").OutputLines()

func New

func New(args ...string) *GitWrap

New create instance with args

func NewWithArgs

func NewWithArgs(cmd string, args ...string) *GitWrap

NewWithArgs create instance with git cmd and args

func NewWithWorkdir

func NewWithWorkdir(workdir string, args ...string) *GitWrap

NewWithWorkdir create instance with workdir and args

func Remote

func Remote(args ...string) *GitWrap

Remote command of git

func RevList

func RevList(args ...string) *GitWrap

RevList command of git

func Show

func Show(args ...string) *GitWrap

Show command of git

func Std

func Std() *GitWrap

Std instance get

func Tag

func Tag(args ...string) *GitWrap

Tag command of git

Usage:

Tag("-l").OutputLines()

func (*GitWrap) Add

func (gw *GitWrap) Add(args ...string) *GitWrap

Add command for git

func (*GitWrap) AddArg

func (gw *GitWrap) AddArg(args ...string) *GitWrap

AddArg add args and returns the current object

func (*GitWrap) AddArgs

func (gw *GitWrap) AddArgs(args []string) *GitWrap

AddArgs for the git. alias of WithArgs()

func (*GitWrap) Annotate

func (gw *GitWrap) Annotate(args ...string) *GitWrap

Annotate command for git

func (*GitWrap) Apply

func (gw *GitWrap) Apply(args ...string) *GitWrap

Apply command for git

func (*GitWrap) ArgIf

func (gw *GitWrap) ArgIf(arg string, exprOk bool) *GitWrap

ArgIf add arg and returns the current object

func (*GitWrap) Argf

func (gw *GitWrap) Argf(format string, args ...interface{}) *GitWrap

Argf add arg and returns the current object.

func (*GitWrap) Bisect

func (gw *GitWrap) Bisect(args ...string) *GitWrap

Bisect command for git

func (*GitWrap) Blame

func (gw *GitWrap) Blame(args ...string) *GitWrap

Blame command for git

func (*GitWrap) Branch

func (gw *GitWrap) Branch(args ...string) *GitWrap

Branch command for git

func (*GitWrap) Checkout

func (gw *GitWrap) Checkout(args ...string) *GitWrap

Checkout command for git

func (*GitWrap) CherryPick

func (gw *GitWrap) CherryPick(args ...string) *GitWrap

CherryPick command for git

func (*GitWrap) Clean

func (gw *GitWrap) Clean(args ...string) *GitWrap

Clean command for git

func (*GitWrap) Clone

func (gw *GitWrap) Clone(args ...string) *GitWrap

Clone command for git

func (*GitWrap) Cmd

func (gw *GitWrap) Cmd(cmd string, args ...string) *GitWrap

Cmd new git wrap from current instance, can with args

func (*GitWrap) Cmdline

func (gw *GitWrap) Cmdline() string

Cmdline to command line

func (*GitWrap) CombinedOutput

func (gw *GitWrap) CombinedOutput() (string, error)

CombinedOutput run and return output, will combine stderr and stdout output

func (*GitWrap) Commit

func (gw *GitWrap) Commit(args ...string) *GitWrap

Commit command for git

func (*GitWrap) Config

func (gw *GitWrap) Config(args ...string) *GitWrap

Config command for git

func (*GitWrap) Describe

func (gw *GitWrap) Describe(args ...string) *GitWrap

Describe command for git

func (*GitWrap) Diff

func (gw *GitWrap) Diff(args ...string) *GitWrap

Diff command for git

func (*GitWrap) Exec

func (gw *GitWrap) Exec() error

Exec runs command with exec(3) Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339

func (*GitWrap) Fetch

func (gw *GitWrap) Fetch(args ...string) *GitWrap

Fetch command for git

func (*GitWrap) GitDir

func (gw *GitWrap) GitDir() string

GitDir return .git data dir

func (*GitWrap) Grep

func (gw *GitWrap) Grep(args ...string) *GitWrap

Grep command for git

func (*GitWrap) Init

func (gw *GitWrap) Init(args ...string) *GitWrap

Init command for git

func (*GitWrap) IsGitRepo

func (gw *GitWrap) IsGitRepo() bool

IsGitRepo return the work dir is a git repo.

func (*GitWrap) Log

func (gw *GitWrap) Log(args ...string) *GitWrap

Log command for git

func (*GitWrap) Merge

func (gw *GitWrap) Merge(args ...string) *GitWrap

Merge command for git

func (*GitWrap) MustRun

func (gw *GitWrap) MustRun()

MustRun a command. will panic on error

func (*GitWrap) Mv

func (gw *GitWrap) Mv(args ...string) *GitWrap

Mv command for git

func (*GitWrap) New

func (gw *GitWrap) New(args ...string) *GitWrap

New git wrap from current instance, can with args

func (*GitWrap) NewExecCmd

func (gw *GitWrap) NewExecCmd() *exec.Cmd

NewExecCmd create exec.Cmd from current cmd

func (*GitWrap) OnBeforeExec

func (gw *GitWrap) OnBeforeExec(fn func(gw *GitWrap)) *GitWrap

OnBeforeExec add hook

func (*GitWrap) Output

func (gw *GitWrap) Output() (string, error)

Output run and return output

func (*GitWrap) OutputLines

func (gw *GitWrap) OutputLines() ([]string, error)

OutputLines run and return output as lines

func (*GitWrap) PrintCmdline added in v0.3.3

func (gw *GitWrap) PrintCmdline() *GitWrap

PrintCmdline on exec command

func (*GitWrap) Pull

func (gw *GitWrap) Pull(args ...string) *GitWrap

Pull command for git

func (*GitWrap) Push

func (gw *GitWrap) Push(args ...string) *GitWrap

Push command for git

func (*GitWrap) Rebase

func (gw *GitWrap) Rebase(args ...string) *GitWrap

Rebase command for git

func (*GitWrap) Reflog

func (gw *GitWrap) Reflog(args ...string) *GitWrap

Reflog command for git

func (*GitWrap) Remote

func (gw *GitWrap) Remote(args ...string) *GitWrap

Remote command for git

func (*GitWrap) Reset

func (gw *GitWrap) Reset(args ...string) *GitWrap

Reset command for git

func (*GitWrap) ResetArgs

func (gw *GitWrap) ResetArgs()

ResetArgs for git

func (*GitWrap) Restore

func (gw *GitWrap) Restore(args ...string) *GitWrap

Restore command for git

func (*GitWrap) RevList

func (gw *GitWrap) RevList(args ...string) *GitWrap

RevList command for git

func (*GitWrap) RevParse

func (gw *GitWrap) RevParse(args ...string) *GitWrap

RevParse command for git

rev-parse usage:

git rev-parse --show-toplevel // get git workdir, repo dir.
git rev-parse -q --git-dir // get git data dir name. eg: .git

func (*GitWrap) Revert

func (gw *GitWrap) Revert(args ...string) *GitWrap

Revert command for git

func (*GitWrap) Rm

func (gw *GitWrap) Rm(args ...string) *GitWrap

Rm command for git

func (*GitWrap) Run

func (gw *GitWrap) Run() error

Run runs command with `Exec` on platforms except Windows which only supports `Spawn`

func (*GitWrap) SafeLines

func (gw *GitWrap) SafeLines() []string

SafeLines run and return output as lines

func (*GitWrap) SafeOutput

func (gw *GitWrap) SafeOutput() string

SafeOutput run and return output

func (*GitWrap) ShortLog

func (gw *GitWrap) ShortLog(args ...string) *GitWrap

ShortLog command for git

func (*GitWrap) Show

func (gw *GitWrap) Show(args ...string) *GitWrap

Show command for git

func (*GitWrap) Spawn

func (gw *GitWrap) Spawn() error

Spawn runs command with spawn(3)

func (*GitWrap) Stash

func (gw *GitWrap) Stash(args ...string) *GitWrap

Stash command for git

func (*GitWrap) Status

func (gw *GitWrap) Status(args ...string) *GitWrap

Status command for git

func (*GitWrap) String

func (gw *GitWrap) String() string

String to command line

func (*GitWrap) Sub

func (gw *GitWrap) Sub(cmd string, args ...string) *GitWrap

Sub new sub git cmd from current instance, can with args

func (*GitWrap) Success

func (gw *GitWrap) Success() bool

Success run and return whether success

func (*GitWrap) Switch

func (gw *GitWrap) Switch(args ...string) *GitWrap

Switch command for git

func (*GitWrap) Tag

func (gw *GitWrap) Tag(args ...string) *GitWrap

Tag command for git

func (*GitWrap) Var

func (gw *GitWrap) Var(args ...string) *GitWrap

Var command for git

func (*GitWrap) WithArg

func (gw *GitWrap) WithArg(args ...string) *GitWrap

WithArg add args and returns the current object. alias of the WithArg()

func (*GitWrap) WithArgIf

func (gw *GitWrap) WithArgIf(arg string, exprOk bool) *GitWrap

WithArgIf add arg and returns the current object

func (*GitWrap) WithArgf

func (gw *GitWrap) WithArgf(format string, args ...interface{}) *GitWrap

WithArgf add arg and returns the current object. alias of the Argf()

func (*GitWrap) WithArgs

func (gw *GitWrap) WithArgs(args []string) *GitWrap

WithArgs for the git

func (*GitWrap) WithArgsIf

func (gw *GitWrap) WithArgsIf(args []string, exprOk bool) *GitWrap

WithArgsIf add arg and returns the current object

func (*GitWrap) WithDryRun added in v0.3.3

func (gw *GitWrap) WithDryRun(dryRun bool) *GitWrap

WithDryRun on exec command

func (*GitWrap) WithFn

func (gw *GitWrap) WithFn(fn func(gw *GitWrap)) *GitWrap

WithFn for setting gw

func (*GitWrap) WithOutput

func (gw *GitWrap) WithOutput(out, errOut io.Writer) *GitWrap

WithOutput returns the current argument

func (*GitWrap) WithStdin

func (gw *GitWrap) WithStdin(in io.Reader) *GitWrap

WithStdin returns the current argument

func (*GitWrap) WithWorkDir

func (gw *GitWrap) WithWorkDir(dir string) *GitWrap

WithWorkDir returns the current object

func (*GitWrap) Worktree

func (gw *GitWrap) Worktree(args ...string) *GitWrap

Worktree command for git

type Range

type Range struct {
	A string
	B string
}

Range struct

func NewRange

func NewRange(a, b string) (*Range, error)

NewRange object

func (*Range) IsAncestor

func (r *Range) IsAncestor() bool

IsAncestor check

func (*Range) IsIdentical

func (r *Range) IsIdentical() bool

IsIdentical check

type RemoteInfo

type RemoteInfo struct {
	// Name the repo remote name, default see DefaultRemoteName
	Name string
	// Type remote type. allow: push, fetch
	Type string
	// URL full git remote URL string.
	//
	// eg:
	// - http: "https://github.com/gookit/gitw.git"
	// - git: "git@github.com:gookit/gitw.git"
	URL string

	// Scheme the url scheme. eg: git, http, https
	Scheme string
	// Host name. eg: "github.com"
	Host string
	// the group, repo name
	Group, Repo string

	// Proto the type 'ssh' OR 'http'
	Proto string
}

RemoteInfo struct

- http: "https://github.com/gookit/gitw.git" - git: "git@github.com:gookit/gitw.git"

func NewEmptyRemoteInfo

func NewEmptyRemoteInfo(URL string) *RemoteInfo

NewEmptyRemoteInfo only with URL string.

func NewRemoteInfo

func NewRemoteInfo(name, url, typ string) (*RemoteInfo, error)

NewRemoteInfo create

func (*RemoteInfo) GitURL

func (r *RemoteInfo) GitURL() string

GitURL build. eg: "git@github.com:gookit/gitw.git"

func (*RemoteInfo) HTTPHost added in v0.3.2

func (r *RemoteInfo) HTTPHost() string

HTTPHost URL build. return like: https://github.com

func (*RemoteInfo) Invalid

func (r *RemoteInfo) Invalid() bool

Invalid check

func (*RemoteInfo) Path

func (r *RemoteInfo) Path() string

Path string

func (*RemoteInfo) RawURLOfHTTP

func (r *RemoteInfo) RawURLOfHTTP() string

RawURLOfHTTP get remote url, if RemoteInfo.URL is git proto, build an HTTPS url.

func (*RemoteInfo) RepoPath

func (r *RemoteInfo) RepoPath() string

RepoPath string

func (*RemoteInfo) String

func (r *RemoteInfo) String() string

String remote info to string.

func (*RemoteInfo) URLOfHTTP

func (r *RemoteInfo) URLOfHTTP() string

URLOfHTTP build an HTTP url.

func (*RemoteInfo) URLOfHTTPS

func (r *RemoteInfo) URLOfHTTPS() string

URLOfHTTPS build an HTTPS url.

func (*RemoteInfo) URLOrBuild added in v0.3.2

func (r *RemoteInfo) URLOrBuild() string

URLOrBuild get remote HTTP url, if RemoteInfo.URL is git proto, build an HTTPS url.

func (*RemoteInfo) Valid

func (r *RemoteInfo) Valid() bool

Valid check

type RemoteInfos

type RemoteInfos map[string]*RemoteInfo

RemoteInfos map. key is type name(see RemoteTypePush)

func (RemoteInfos) FetchInfo added in v0.3.2

func (rs RemoteInfos) FetchInfo() *RemoteInfo

FetchInfo fetch remote info

func (RemoteInfos) PushInfo added in v0.3.2

func (rs RemoteInfos) PushInfo() *RemoteInfo

PushInfo push remote info

type Repo

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

Repo struct

func NewRepo

func NewRepo(dir string) *Repo

NewRepo create Repo object

func (*Repo) AllRemoteInfos

func (r *Repo) AllRemoteInfos() map[string]RemoteInfos

AllRemoteInfos get

func (*Repo) AutoMatchTag added in v0.2.3

func (r *Repo) AutoMatchTag(sha string) string

AutoMatchTag by given sha or tag name

func (*Repo) AutoMatchTagByType added in v0.2.5

func (r *Repo) AutoMatchTagByType(sha string, tagType int) string

AutoMatchTagByType by given sha or tag name.

func (*Repo) BranchDelete added in v0.3.5

func (r *Repo) BranchDelete(name string, remote string) error

BranchDelete handle

func (*Repo) BranchInfo added in v0.2.3

func (r *Repo) BranchInfo(branch string, remote ...string) *BranchInfo

BranchInfo find branch info by name, if remote is empty, find local branch

func (*Repo) BranchInfos added in v0.2.3

func (r *Repo) BranchInfos() *BranchInfos

BranchInfos get branch infos of the repo

func (*Repo) Cmd

func (r *Repo) Cmd(name string, args ...string) *GitWrap

Cmd new git command wrapper

func (*Repo) CurBranchInfo added in v0.2.3

func (r *Repo) CurBranchInfo() *BranchInfo

CurBranchInfo get current branch info of the repo

func (*Repo) CurBranchName added in v0.2.3

func (r *Repo) CurBranchName() string

CurBranchName return current branch name

func (*Repo) DefaultRemoteInfo

func (r *Repo) DefaultRemoteInfo(typ ...string) *RemoteInfo

DefaultRemoteInfo get

func (*Repo) Dir

func (r *Repo) Dir() string

Dir get repo dir

func (*Repo) Err

func (r *Repo) Err() error

Err get last error

func (*Repo) FetchAll added in v0.3.5

func (r *Repo) FetchAll(args ...string) error

FetchAll fetch all remote branches

func (*Repo) FirstRemoteInfo added in v0.3.2

func (r *Repo) FirstRemoteInfo(typ ...string) *RemoteInfo

FirstRemoteInfo get

func (*Repo) Git

func (r *Repo) Git() *GitWrap

Git get git wrapper

func (*Repo) HasBranch added in v0.3.2

func (r *Repo) HasBranch(branch string, remote ...string) bool

func (*Repo) HasLocalBranch added in v0.3.2

func (r *Repo) HasLocalBranch(branch string) bool

func (*Repo) HasRemote

func (r *Repo) HasRemote(name string) bool

HasRemote check

func (*Repo) HasRemoteBranch added in v0.3.2

func (r *Repo) HasRemoteBranch(branch, remote string) bool

func (*Repo) HeadBranchName added in v0.3.3

func (r *Repo) HeadBranchName() string

HeadBranchName return current branch name

func (*Repo) Info

func (r *Repo) Info() *RepoInfo

Info get repo information

func (*Repo) Init

func (r *Repo) Init() error

Init run git init for the repo dir.

func (*Repo) IsInited

func (r *Repo) IsInited() bool

IsInited is init git repo dir

func (*Repo) LargestTag

func (r *Repo) LargestTag() string

LargestTag get max tag version of the repo

func (*Repo) LargestTagByTagType added in v0.2.5

func (r *Repo) LargestTagByTagType(tagType int) string

LargestTagByTagType get max tag version of the repo by tag_type

func (*Repo) LastAbbrevID

func (r *Repo) LastAbbrevID() string

LastAbbrevID get last abbrev commit ID, len is 7

func (*Repo) LastCommitID

func (r *Repo) LastCommitID() string

LastCommitID value

func (*Repo) MaxTag

func (r *Repo) MaxTag() string

MaxTag get max tag version of the repo

func (*Repo) PrevMaxTag

func (r *Repo) PrevMaxTag() string

PrevMaxTag get second-largest tag of the repo

func (*Repo) PrintCmdOnExec added in v0.3.2

func (r *Repo) PrintCmdOnExec() *Repo

PrintCmdOnExec settings.

func (*Repo) QuickRun added in v0.3.4

func (r *Repo) QuickRun(cmd string, args ...string) error

QuickRun git command

func (*Repo) RandomRemoteInfo

func (r *Repo) RandomRemoteInfo(typ ...string) *RemoteInfo

RandomRemoteInfo get

func (*Repo) ReadConfig added in v0.3.2

func (r *Repo) ReadConfig() []byte

ReadConfig contents from REPO/.git/config

func (*Repo) ReadHEAD added in v0.3.2

func (r *Repo) ReadHEAD() []byte

ReadHEAD contents from REPO/.git/HEAD

func (*Repo) ReloadBranches added in v0.3.5

func (r *Repo) ReloadBranches() *BranchInfos

ReloadBranches reload branch infos of the repo

func (*Repo) RemoteInfo

func (r *Repo) RemoteInfo(remote string, typ ...string) *RemoteInfo

RemoteInfo get by remote name and type.

- If remote is empty, will return default remote - If typ is empty, will return random type info.

Usage:

ri := RemoteInfo("origin")
ri = RemoteInfo("origin", "push")

func (*Repo) RemoteInfos

func (r *Repo) RemoteInfos(remote string) RemoteInfos

RemoteInfos get by remote name

func (*Repo) RemoteLines added in v0.3.3

func (r *Repo) RemoteLines() map[string]string

RemoteLines get like: {origin: url, other: url}

func (*Repo) RemoteNames

func (r *Repo) RemoteNames() []string

RemoteNames get

func (*Repo) SearchBranchV2 added in v0.3.5

func (r *Repo) SearchBranchV2(m brinfo.BranchMatcher, opt *SearchOpt) []*BranchInfo

SearchBranchV2 search branch infos by keywords

func (*Repo) SearchBranches added in v0.2.3

func (r *Repo) SearchBranches(name string, flag uint8) []*BranchInfo

SearchBranches search branch infos by name

func (*Repo) SetDryRun added in v0.3.4

func (r *Repo) SetDryRun(dr bool) *Repo

SetDryRun settings.

func (*Repo) SetUpstreamTo added in v0.3.2

func (r *Repo) SetUpstreamTo(remote, branch string, localBranch ...string) error

SetUpstreamTo set the branch upstream remote branch. If `localBranch` is empty, will use `branch` as `localBranch`

CMD:

git branch --set-upstream-to=<remote>/<branch> <local_branch>

func (*Repo) StatusInfo added in v0.2.7

func (r *Repo) StatusInfo() *StatusInfo

StatusInfo get status info of the repo

func (*Repo) TagByDescribe added in v0.2.5

func (r *Repo) TagByDescribe(current string) (ver string)

TagByDescribe get tag by describe command. if current not empty, will exclude it.

func (*Repo) TagSecondMax

func (r *Repo) TagSecondMax() string

TagSecondMax get second-largest tag of the repo

func (*Repo) TagSecondMaxByTagType added in v0.2.5

func (r *Repo) TagSecondMaxByTagType(tagType int) string

TagSecondMaxByTagType get second-largest tag of the repo by tag_type

func (*Repo) Tags

func (r *Repo) Tags() []string

Tags get repo tags list

func (*Repo) TagsSortedByCreatorDate added in v0.2.5

func (r *Repo) TagsSortedByCreatorDate() []string

TagsSortedByCreatorDate get repo tags list by creator date sort

func (*Repo) TagsSortedByRefName

func (r *Repo) TagsSortedByRefName() []string

TagsSortedByRefName get repo tags list

func (*Repo) UpstreamBranch added in v0.3.2

func (r *Repo) UpstreamBranch() string

UpstreamBranch get current upstream branch name.

func (*Repo) UpstreamPath added in v0.3.2

func (r *Repo) UpstreamPath() string

UpstreamPath get current upstream remote and branch. Returns like: origin/main

CMD:

git rev-parse --abbrev-ref @{u}

func (*Repo) UpstreamRemote added in v0.3.2

func (r *Repo) UpstreamRemote() string

UpstreamRemote get current upstream remote name.

func (*Repo) WithConfig

func (r *Repo) WithConfig(cfg *RepoConfig) *Repo

WithConfig new repo config

func (*Repo) WithConfigFn

func (r *Repo) WithConfigFn(fn func(cfg *RepoConfig)) *Repo

WithConfigFn new repo config func

func (*Repo) WithFn

func (r *Repo) WithFn(fn func(r *Repo)) *Repo

WithFn new repo self config func

type RepoConfig

type RepoConfig struct {
	// DefaultBranch name, default is DefaultBranchName
	DefaultBranch string
	// DefaultRemote name, default is DefaultRemoteName
	DefaultRemote string
}

RepoConfig struct

type RepoInfo

type RepoInfo struct {
	Name string
	Path string
	Dir  string
	URL  string
	// LastHash last commit hash value
	LastHash string
	Branch   string
	Version  string
	// Upstream remote name
	Upstream string
	// Remotes name and url mapping.
	Remotes map[string]string
}

RepoInfo struct

type SearchOpt added in v0.3.5

type SearchOpt struct {
	// Flag search flag, default is BrSearchLocal.
	Flag  uint8
	Limit int
	// Remote name, on which remote to search.
	Remote string
	// Before search callback, return false to skip.
	Before func(bi *BranchInfo) bool
}

SearchOpt for search branches

type StatusInfo added in v0.2.7

type StatusInfo struct {
	// Branch current branch name.
	Branch string
	// UpRemote current upstream remote name.
	UpRemote string
	// UpBranch current upstream remote branch name.
	UpBranch string

	// Deleted files
	Deleted []string
	// Renamed files, contains RM(rename and modify) files
	Renamed []string
	// Modified files
	Modified []string
	// Unstacked new created files.
	Unstacked []string
	// contains filtered or unexported fields
}

StatusInfo struct

by run: git status -bs -u

func NewStatusInfo added in v0.2.7

func NewStatusInfo(str string) *StatusInfo

NewStatusInfo from string.

func (*StatusInfo) FileNum added in v0.2.7

func (si *StatusInfo) FileNum() int

FileNum in git status

func (*StatusInfo) FromLines added in v0.2.7

func (si *StatusInfo) FromLines(lines []string) *StatusInfo

FromLines parse and load info

func (*StatusInfo) FromString added in v0.2.7

func (si *StatusInfo) FromString(str string) *StatusInfo

FromString parse and load info

func (*StatusInfo) IsCleaned added in v0.2.7

func (si *StatusInfo) IsCleaned() bool

IsCleaned status in workspace

Directories

Path Synopsis
_examples
cmd

Jump to

Keyboard shortcuts

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