git

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package git provides Git operations for gitcode-cli

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDir = fmt.Errorf("invalid directory")

ErrInvalidDir is returned when a directory argument fails validation.

View Source
var ErrInvalidRef = fmt.Errorf("invalid git ref")

ErrInvalidRef is returned when a git ref fails validation.

Functions

func Clone

func Clone(repo *Repo, dir string, depth int) error

Clone clones a repository to the specified directory

func CloneWithProtocol

func CloneWithProtocol(repo *Repo, dir string, protocol string, depth int) error

CloneWithProtocol clones a repository using the specified protocol

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch returns the current branch name

func DefaultRemote

func DefaultRemote() (string, error)

DefaultRemote returns the default remote (origin or first available)

func HasLocalChanges

func HasLocalChanges() (bool, error)

HasLocalChanges returns true if there are uncommitted changes

func IsRepo

func IsRepo() bool

IsRepo returns true if current directory is a git repository

func RemoteURL

func RemoteURL(name string) (string, error)

RemoteURL returns the URL of a remote

func Remotes

func Remotes() ([]string, error)

Remotes returns the list of remote names

func RootDir

func RootDir() (string, error)

RootDir returns the root directory of the git repository

func Run

func Run(args ...string) (string, error)

Run executes a git command and returns the output

func RunInDir

func RunInDir(dir string, args ...string) (string, error)

RunInDir executes a git command in a specific directory

func RunInDirWithEnv added in v0.3.9

func RunInDirWithEnv(dir string, env map[string]string, args ...string) (string, error)

RunInDirWithEnv executes a git command in a specific directory with extra environment variables.

func RunWithEnv added in v0.3.9

func RunWithEnv(env map[string]string, args ...string) (string, error)

RunWithEnv executes a git command with extra environment variables.

func SafeCheckout added in v0.7.0

func SafeCheckout(branch string) error

SafeCheckout runs "git checkout <branch>" after validating the branch name. This prevents option-injection attacks where a branch name starts with "-".

func SafeCheckoutWithOutput added in v0.7.0

func SafeCheckoutWithOutput(stdout, stderr io.Writer, dir string, branch string) error

SafeCheckoutWithOutput runs "git checkout <branch>" after validating the branch name, streaming stdout/stderr to the provided writers.

func SafeFetch added in v0.7.0

func SafeFetch(remote, ref, localBranch string) error

SafeFetch runs "git fetch <remote> -- <ref>:<localBranch>" after validating both the remote ref and the local branch name. All parameters including "remote" are validated to prevent option-injection.

func SafeFetchFromURL added in v0.7.0

func SafeFetchFromURL(fetchURL, ref, localBranch string) error

SafeFetchFromURL runs "git fetch <fetchURL> -- <ref>:<localBranch>" after validating both the URL and the ref names. This is used when fetching from a fork or external repository.

func SafeFetchFromURLWithOutput added in v0.7.0

func SafeFetchFromURLWithOutput(stdout, stderr io.Writer, dir, fetchURL, ref, localBranch string) error

SafeFetchFromURLWithOutput runs a validated git fetch from a URL, streaming output.

func SafeFetchWithOutput added in v0.7.0

func SafeFetchWithOutput(stdout, stderr io.Writer, dir, remote, ref, localBranch string) error

SafeFetchWithOutput runs a validated git fetch, streaming output. All parameters including "remote" are validated to prevent option-injection.

func ValidateDir added in v0.7.0

func ValidateDir(dir string) error

ValidateDir validates that a directory argument is safe for use as a git command argument (e.g. the target directory of "git clone"). It rejects:

  • Empty strings
  • Strings starting with "-" (prevent option injection)
  • Strings containing control characters

func ValidateFetchURL added in v0.7.0

func ValidateFetchURL(rawURL string) error

ValidateFetchURL validates a git fetch/push URL. It rejects:

  • Empty strings
  • URLs starting with "-" (prevent option injection via dash-prefixed host)
  • URLs with invalid or unexpected schemes

func ValidateRef added in v0.7.0

func ValidateRef(ref string) error

ValidateRef validates that a git ref (branch, tag, or remote ref) is safe for use as a git command argument. It rejects:

  • Empty strings
  • Strings starting with "-" (prevent option injection)
  • Strings with control characters, spaces, or shell metacharacters
  • Strings that don't match the expected ref pattern

Types

type Repo

type Repo struct {
	Owner string
	Name  string
	Host  string
}

Repo represents a parsed repository reference

func CurrentRepo

func CurrentRepo() (*Repo, error)

CurrentRepo returns the current repository from git remote

func ParseRepo

func ParseRepo(ref string) (*Repo, error)

ParseRepo parses a repository reference Supports formats: - owner/repo - https://gitcode.com/owner/repo - git@gitcode.com:owner/repo.git

func (*Repo) GitURL

func (r *Repo) GitURL(protocol string) string

GitURL returns the git URL for the repository

func (*Repo) String

func (r *Repo) String() string

String returns the full repository path (owner/name)

func (*Repo) URL

func (r *Repo) URL() string

URL returns the full URL for the repository

Source Files

  • git.go
  • repo.go

Jump to

Keyboard shortcuts

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