Categorygithub.com/gookit/gitw
modulepackage
0.3.5
Repository: https://github.com/gookit/gitw.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Alias find.
AllVars get all git vars.
Branch command of git.
Branches list.
Cmd create instance with git cmd and args.
CommentChar find.
Config get git config by name.
ConfigAll get.
DataDir get .git dir name.
Editor returns program name of the editor.
EditText starts an editor to edit data, and returns the edited data.
EmptyBranchInfos instance.
FirstLine from command output.
GlobalConfig get git global config by name.
HasDotGitDir in the path.
HasFile check.
Head read current branch name.
IsDebug mode.
IsGitCmd check.
IsGitCommand check.
IsGitDir check.
Log command of git Usage: Log("-2").OutputLines().
MustString must return string, will panic on error.
MustStrings must return strings, will panic on error.
New create instance with args.
NewBranchInfo from branch line text.
NewBranchInfos create.
NewEmptyRemoteInfo only with URL string.
NewRange object.
NewRemoteInfo create.
NewRepo create Repo object.
NewStatusInfo from string.
NewWithArgs create instance with git cmd and args.
NewWithWorkdir create instance with workdir and args.
OutputLines split output to lines Deprecated: please use cmdr.OutputLines.
ParseBranchLine to BranchInfo data verbose: False - only branch name True - get by `git br -v --all` format: * BRANCH_NAME COMMIT_ID COMMIT_MSG.
ParseRemoteURL info to the RemoteInfo object.
PrintCmdline on exec.
Quiet run.
Ref get.
RefList for two sha.
Remote command of git.
Remotes list.
RestStd instance.
RevList command of git.
Run command with args.
SetDebug mode.
SetGlobalConfig by name.
SetWorkdir for the std.
Show command of git.
ShowDiff git log diff by a commit sha.
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").
Spawn run command with args.
Std instance get.
SymbolicFullName reads a branch name from a ref such as "@{upstream}".
SymbolicRef reads a branch name from a ref such as "HEAD".
Tag command of git Usage: Tag("-l").OutputLines().
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".
Var get by git var.
Version info git.
Workdir git workdir name.
WorkdirName get git workdir name.

# Constants

branch types.
branch types.
flags for search branches.
flags for search branches.
flags for search branches.
ConfFile in .git/.
enum type value constants for fetch tags.
DefaultBin name.
DefaultBranchName value.
DefaultRemoteName value.
enum type value constants for fetch tags.
GitDir name.
GitHubGit string.
GitHubHost name.
GitHubURL string.
HeadFile in .git/.
some consts for remote info.
some consts for remote info.
enum type value constants for fetch tags.
RemotePfxOnBranch prefix keywords.
remote type names.
remote type names.
some consts for remote info.
some consts for remote info.
some consts for remote info.
ShaHead keywords.
StatusPattern string.
some special keywords for match tag.
some special keywords for match tag.
some special keywords for match tag.
git host type.
git host type.
git host type.

# Variables

ErrInvalidBrLine error.
ErrRemoteInfoNil error.
GlobalFlags for run git command.

# Structs

BranchInfo for a git branch.
BranchInfos for a git repo.
GitWrap is a project-wide struct that represents a command to be run in the console.
Range struct.
RemoteInfo struct - http: "https://github.com/gookit/gitw.git" - git: "[email protected]:gookit/gitw.git".
Repo struct.
RepoConfig struct.
RepoInfo struct.
SearchOpt for search branches.
StatusInfo struct by run: git status -bs -u.

# Type aliases

RemoteInfos map.