gitwrap

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 15 Imported by: 0

README

GitWrap

Git command wrapper, and some extra git tools package.

Github https://github.com/gookit/gitwrap

Install

required: go 1.14+, git 2.x

go get github.com/gookit/gitwrap

Usage

package main

import (
	"fmt"

	"github.com/gookit/gitwrap"
)

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

	// LocalBranches
	brList := gitwrap.MustStrings(gitwrap.LocalBranches())
	fmt.Println(brList)
	
	// custom create command

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

	fmt.Println(txt)
}

Gen changelog

You can quickly generate changelog by gitwrap/chlog package.

package main

import (
	"fmt"

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

func main() {
	cl := chlog.New()
	cl.WithConfig(func(c *chlog.Changelog) {
		// some settings ...
		c.Formatter = &chlog.MarkdownFormatter{
			RepoURL: "https://github.com/gookit/gitwrap",
		}
	})

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

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

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

Outputs:

## Change Log

### Update

- update: update some logic for git command run [96147fb](https://github.com/gookit/gitwrap/commit/96147fba43caf462a50bc97d7ed078dd0059e797)
- update: move RepoUrl config to MarkdownFormatter [8c861bf](https://github.com/gookit/gitwrap/commit/8c861bf05ae3576aba401692124df63372ae9ed7)

### Fixed

- fix: gen changelog error [1636761](https://github.com/gookit/gitwrap/commit/16367617bc364ce1022097e89313c7b09983981a)

### Other

- style: update some code logic [4a9f146](https://github.com/gookit/gitwrap/commit/4a9f14656b26a08b0cdd9c4f9cec9ae3bf5938b1)
- build(deps): bump github.com/gookit/color from 1.4.2 to 1.5.0 [037fa47](https://github.com/gookit/gitwrap/commit/037fa477954b630fe34ff7ceab51e6132db645cb)
- style: update examples and readme [8277389](https://github.com/gookit/gitwrap/commit/8277389817917e6b0cb97f3e5629f2c5034075e4)

Commands

Git command functions

func Alias(name string) string
func CommentChar(text string) (string, error)
func Config(name string) string
func ConfigAll(name string) ([]string, error)
func Dir() (string, error)
func Editor() string
func GlobalConfig(name string) (string, error)
func HasFile(segments ...string) bool
func Head() (string, error)
func LocalBranches() ([]string, error)
func Log(sha1, sha2 string) (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 Run(args ...string) error
func SetGlobalConfig(name, value string) error
func Show(sha string) (string, error)
func Spawn(args ...string) error
func SymbolicFullName(name string) (string, error)
func SymbolicRef(ref string) (string, error)
func Var(name string) string
func Version() (string, error)
func WorkdirName() (string, error)

Util functions:

func SetDebug()
func ParseURL(rawURL string) (u *url.URL, err error)
func IsGitCommand(command string) bool
func IsGitDir(dir string) bool
func MustString(s string, err error) string
func MustStrings(ss []string, err error) []string
func EditText(data string) string

Refer

Documentation

Overview

Package gitwrap is library warp git commands. some code is refer from github/hub

Index

Constants

View Source
const (
	ProtoSsh  = "ssh"
	ProtoHttp = "http"

	DefaultRemoteName = "origin"
)

Variables

View Source
var (
	DefaultBin = "git"
	GitDir     = ".git"
)
View Source
var GlobalFlags []string

GlobalFlags for run git command

Functions

func Alias

func Alias(name string) string

func AllVars

func AllVars() string

AllVars get all git vars

func CommentChar

func CommentChar(text string) (string, error)

func Config

func Config(name string) string

Config get git config by name

func ConfigAll

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

func Dir

func Dir() (string, error)

Dir get git dir. 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 GlobalConfig

func GlobalConfig(name string) (string, error)

GlobalConfig get git global config by name

func HasFile

func HasFile(segments ...string) bool

HasFile check

func Head() (string, error)

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

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

func LocalBranches

func LocalBranches() ([]string, error)

LocalBranches list

func Log

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

Log show git log between sha1 to sha2

Usage:

gitwrap.Log("v1.0.2", "v1.0.3")
gitwrap.Log("commit id 1", "commit id 2")

func MustString added in v0.1.1

func MustString(s string, err error) string

MustString must return string, will panic on error

func MustStrings added in v0.1.1

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

MustStrings must return strings, will panic on error

func PrintCmdline added in v0.1.2

func PrintCmdline(gw *GitWrap)

PrintCmdline on exec

func Quiet

func Quiet(args ...string) bool

func Ref

func Ref(ref string) (string, error)

Ref get

func RefList

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

RefList get

func Remotes

func Remotes() ([]string, error)

Remotes list

func Run

func Run(args ...string) error

func SetDebug

func SetDebug()

SetDebug mode

func SetGlobalConfig

func SetGlobalConfig(name, value string) error

SetGlobalConfig by name

func Show

func Show(sha string) (string, error)

Show git log diff by an commit sha

func Spawn

func Spawn(args ...string) error

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 Var

func Var(name string) string

Var get by git var. all: git var -l one: git var GIT_EDITOR

func Version

func Version() (string, error)

func Workdir

func Workdir() (string, error)

Workdir git workdir name. alias of WorkdirName()

func WorkdirName

func WorkdirName() (string, error)

WorkdirName git workdir name

Types

type CmdBuilder

type CmdBuilder struct {
	Dir string
}

CmdBuilder struct

type GitWrap

type GitWrap struct {
	// Bin git bin name. default is "git"
	Bin string
	// Cmd sub command name of git
	// Cmd  string
	Args []string
	// extra
	WorkDir string
	Stdin   *os.File
	Stdout  *os.File
	Stderr  *os.File
	// BeforeExec command
	BeforeExec func(gw *GitWrap)
	// contains filtered or unexported fields
}

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

func Cmd added in v0.1.2

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

Cmd create instance with git cmd and args

func New

func New(args ...string) *GitWrap

New create instance with args

func NewWithArgs added in v0.1.1

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

NewWithArgs create instance with git cmd and args

func (*GitWrap) Add added in v0.1.1

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

Add returns the current object

func (*GitWrap) AddIf added in v0.1.1

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

AddIf add arg and returns the current object

func (*GitWrap) Addf added in v0.1.1

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

Addf add arg and returns the current object.

func (*GitWrap) Cmdline added in v0.1.1

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) CurrentBranch

func (gw *GitWrap) CurrentBranch() string

CurrentBranch return current branch name

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) GitDir

func (gw *GitWrap) GitDir() string

GitDir return git data dir

func (*GitWrap) IsGitRepo

func (gw *GitWrap) IsGitRepo() bool

IsGitRepo return the work dir is an git repo.

func (*GitWrap) MustRun

func (gw *GitWrap) MustRun()

MustRun an command. will panic on error

func (*GitWrap) NewExecCmd

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

NewExecCmd create exec.Cmd from current cmd

func (*GitWrap) OnBeforeExec added in v0.1.2

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) Run

func (gw *GitWrap) Run() error

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

func (*GitWrap) SafeOutput

func (gw *GitWrap) SafeOutput() string

SafeOutput run and return output

func (*GitWrap) Spawn

func (gw *GitWrap) Spawn() error

Spawn runs command with spawn(3)

func (*GitWrap) String

func (gw *GitWrap) String() string

String to command line

func (*GitWrap) SubCmd

func (gw *GitWrap) SubCmd(cmd string) *GitWrap

SubCmd returns the current object

func (*GitWrap) Success

func (gw *GitWrap) Success() bool

Success run and return whether success

func (*GitWrap) WithArg

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

WithArg returns the current object. alias of the Add()

func (*GitWrap) WithArgIf added in v0.1.1

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

WithArgIf add arg and returns the current object

func (*GitWrap) WithArgf added in v0.1.1

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

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

func (*GitWrap) WithArgs

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

WithArgs for the git

func (*GitWrap) WithOutput

func (gw *GitWrap) WithOutput(out *os.File, errOut *os.File) *GitWrap

WithOutput returns the current argument

func (*GitWrap) WithStdin

func (gw *GitWrap) WithStdin(in *os.File) *GitWrap

WithStdin returns the current argument

func (*GitWrap) WithWorkDir

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

WithWorkDir returns the current object

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

func (*Range) IsIdentical

func (r *Range) IsIdentical() bool

type RemoteInfo

type RemoteInfo struct {
	// the repo remote name and URL address
	Name, 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/gitwrap.git" - git: "git@github.com:gookit/gitwrap.git"

func NewRemoteInfo

func NewRemoteInfo(name, url string) *RemoteInfo

NewRemoteInfo create

func (*RemoteInfo) GitUrl

func (r *RemoteInfo) GitUrl() string

func (*RemoteInfo) HttpUrl

func (r *RemoteInfo) HttpUrl() string

func (*RemoteInfo) HttpsUrl

func (r *RemoteInfo) HttpsUrl() string

func (*RemoteInfo) Invalid

func (r *RemoteInfo) Invalid() bool

func (*RemoteInfo) Path

func (r *RemoteInfo) Path() string

func (*RemoteInfo) String

func (r *RemoteInfo) String() string

func (*RemoteInfo) Valid

func (r *RemoteInfo) Valid() bool

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) DefaultRemoteInfo

func (r *Repo) DefaultRemoteInfo() *RemoteInfo

func (*Repo) Dir

func (r *Repo) Dir() string

func (*Repo) Git

func (r *Repo) Git() *GitWrap

func (*Repo) Info

func (r *Repo) Info()

func (*Repo) Init

func (r *Repo) Init() error

Init run init for the repo dir.

func (*Repo) RemoteInfo

func (r *Repo) RemoteInfo(name string) *RemoteInfo

func (*Repo) RemoteInfos

func (r *Repo) RemoteInfos()

type RepoConfig

type RepoConfig struct {
	DefaultBranch string
	DefaultRemote string
}

RepoConfig struct

type RepoInfo

type RepoInfo struct {
	Name string
	Path string

	Dir string
	URL string
}

RepoInfo struct

Directories

Path Synopsis
_examples
chlog command

Jump to

Keyboard shortcuts

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