gitexec

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: MIT Imports: 8 Imported by: 4

README

go-gitexec

Thin wrapper to execute git commands in Go.

Go Reference Go Report Card CI CodeQL

Usage

Basic Usage
import (
	"fmt"
	"strings"

	"github.com/thombashi/go-gitexec"
)

func ExampleRunGit() {
	executor, err := gitexec.New(&gitexec.Params{})
	if err != nil {
		panic(err)
	}

	result, err := executor.RunGit("status")
	if err != nil {
		panic(err)
	}

	fmt.Printf(result.Stdout.String())
}
Usage: with environment variables
import (
    "fmt"
    "os"
    "strings"

    "github.com/thombashi/go-gitexec"
)

func main() {
	executor, err := gitexec.New(&gitexec.Params{
		Env: []string{
			"GIT_AUTHOR_NAME=John Doe",
			"GIT_AUTHOR_EMAIL=johndoe@example.com",
		},
	})
	if err != nil {
		panic(err)
	}

	result, err := executor.RunGit("var", "GIT_AUTHOR_IDENT")
	if err != nil {
		panic(err)
	}

	fmt.Printf(result.Stdout.String())
}

Documentation

Overview

Package go-git-exec provides thin wrappers for executing Git commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdResult

type CmdResult struct {
	// Stdout contains the standard output of the Git command (if any).
	Stdout *bytes.Buffer

	// Stderr contains the standard error of the Git command (if any).
	Stderr *bytes.Buffer

	// ExitCode is the exit code of the Git command.
	ExitCode int
}

CmdResult represents the result of a Git command execution.

type GitExecutor

type GitExecutor interface {
	// RunGit executes a Git command with the specified arguments.
	RunGit(args ...string) (*CmdResult, error)

	// RunGitContext executes a Git command with the specified context and arguments.
	RunGitContext(ctx context.Context, args ...string) (*CmdResult, error)

	// WithLogger sets the logger to use for logging.
	WithLogger(logger *slog.Logger) GitExecutor

	// GetLogger returns the logger instance.
	GetLogger() *slog.Logger
}

GitExecutor is an interface to execute Git commands.

func New

func New(params *Params) (GitExecutor, error)

New creates a new GitExecutor instance.

type Params

type Params struct {
	// Logger is the logger to use for logging.
	Logger *slog.Logger

	// GitPath is the path to the Git executable. If empty, the Git executable is searched in the PATH.
	GitPath string

	// Env specifies the environment of the process.
	// Each entry is of the form "key=value".
	// If Env is nil, the new process uses the current process's
	// environment.
	// If Env contains duplicate environment keys, only the last
	// value in the slice for each duplicate key is used.
	// As a special case on Windows, SYSTEMROOT is always added if
	// missing and not explicitly set to the empty string.
	Env []string
}

Params is the parameters for creating a new GitExecutor instance.

Jump to

Keyboard shortcuts

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