gitutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gitutil provides shared utilities for cloning Git repositories and copying their contents to a target directory.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedHost is returned for a non-GitHub/non-GitLab host. It is a
	// permanent condition: callers can errors.Is it to avoid retrying hosts that
	// are not supported.
	ErrUnsupportedHost = errors.New("unsupported git host")
	// ErrRefNotFound is returned when a ref resolves to no commit on the remote
	// (deleted branch/tag, typo, or a short/non-existent SHA). Terminal:
	// retrying the same ref will not find it.
	ErrRefNotFound = errors.New("git ref not found")
)

Functions

func CloneAndCopyContext added in v0.4.0

func CloneAndCopyContext(ctx context.Context, repoURL, branch, commit, subPath, targetDir string, verbose bool) error

CloneAndCopyContext clones a Git repository URL and copies its contents to targetDir. It handles parsing the URL, shallow cloning, navigating to subpaths, and cleanup.

branch, commit, and subPath are explicit overrides. When branch and subPath are empty, the values parsed from the URL (e.g. https://github.com/o/r/tree/<branch>/<sub>) are used. The URL-derived ref is always treated as a branch; callers wanting to pin a commit SHA must set the commit argument explicitly. branch is passed to `git clone --branch`; commit triggers a fetch + checkout after the clone.

Every git invocation runs under ctx, so a caller can bound clone/fetch/checkout time (and disk/CPU runaway) by passing a context.WithTimeout. ctx cancellation kills the git child process.

func CopyDir

func CopyDir(src, dst string) error

CopyDir recursively copies a directory tree, skipping symlinks.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a single regular file. The caller must ensure src is not a symlink.

func CopyRepoContents

func CopyRepoContents(repoDir, subPath, targetDir string) error

CopyRepoContents copies files from a cloned repository to the output directory. It navigates to the subPath if specified and skips the .git directory. If the subPath points to a file (for example a GitLab /-/blob/.../SKILL.md URL), the file is copied into targetDir using its basename. Symlinks are skipped to prevent symlink traversal attacks from untrusted repos.

func ParseGitURL added in v0.4.0

func ParseGitURL(rawURL string) (cloneURL, branch, subPath string, err error)

ParseGitURL parses a Git web URL into its clone URL, branch, and subdirectory path. It supports GitHub URLs and GitLab-style URLs, including self-hosted GitLab instances that use /-/tree/ and /-/blob/ routes. Branch names containing slashes (e.g. feature/my-branch) are supported when encoded as %2F in the URL. The raw (escaped) path is used for splitting so the encoded branch segment is preserved, then unescaped for the return value.

func RepoNameFromCloneURL

func RepoNameFromCloneURL(cloneURL string) string

RepoNameFromCloneURL extracts the repository name from a clone URL (e.g., "https://github.com/org/my-repo.git" -> "my-repo").

func ResolveRefContext added in v0.4.0

func ResolveRefContext(ctx context.Context, repoURL, ref string) (string, error)

ResolveRefContext resolves a branch, tag, or HEAD to a concrete commit SHA on the remote WITHOUT cloning, using `git ls-remote`. A ref that is already a full 40-char commit SHA is returned unchanged (lowercased). An empty ref (after the URL-embedded branch is considered) resolves the remote's default branch (HEAD). ctx bounds the ls-remote call. A ref that resolves to no commit returns ErrRefNotFound (terminal).

Types

This section is empty.

Jump to

Keyboard shortcuts

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