git

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrTypeNotFound         = "REPO_NOT_FOUND"
	ErrTypePrivateRepo      = "PRIVATE_REPO"
	ErrTypeTimeout          = "CLONE_TIMEOUT"
	ErrTypeUnsafeProtocol   = "UNSAFE_PROTOCOL"
	ErrTypeUnsupportedVCS   = "UNSUPPORTED_VCS"
	ErrTypeNetworkTransient = "NETWORK_TRANSIENT"
	ErrTypeOther            = "GIT_ERROR"
)
View Source
const (
	MaxCloneTime = 30 * time.Second
)

Variables

View Source
var CIProviders = map[string]CIProvider{
	"GitHub Actions": {
		Paths:          []string{".github/workflows"},
		SparsePatterns: []string{".github/workflows/*.yml", ".github/workflows/*.yaml"},
	},
	"GitLab CI":           {Paths: []string{".gitlab-ci.yml", ".gitlab-ci.yaml"}},
	"CircleCI":            {Paths: []string{".circleci/config.yml", ".circleci/config.yaml"}},
	"Travis CI":           {Paths: []string{".travis.yml"}},
	"Jenkins":             {Paths: []string{"Jenkinsfile"}},
	"Azure Pipelines":     {Paths: []string{"azure-pipelines.yml"}},
	"Bitbucket Pipelines": {Paths: []string{"bitbucket-pipelines.yml"}},
	"AppVeyor":            {Paths: []string{"appveyor.yml"}},
}

CIProviders is the single source of truth for all supported CI services. Key is the human-readable service name used in reporting.

View Source
var SecurityPolicyPaths = []string{
	"SECURITY.md",
	"SECURITY.txt",
	"SECURITY.rst",
	".github/SECURITY.md",
	"docs/SECURITY.md",
}

SecurityPolicyPaths lists candidate locations for a security policy file. Used by the sparse checkout (to fetch these files) and the SOURCE_NO_SECURITY_POLICY check.

View Source
var SparseCheckoutPatterns = append(append([]string{
	".risk-guard.yml",
	".riskguardignore",
}, SecurityPolicyPaths...), ciSparsePatterns()...)

SparseCheckoutPatterns collects all file patterns needed by source checks for sparse checkout.

Functions

func AnalyzeRepository

func AnalyzeRepository(ctx context.Context, repoPath string) (*models.GitMetadata, error)

func CacheKey

func CacheKey(normalizedURL string) string

CacheKey returns a sha256 hex digest of a normalized URL, suitable for use as a path component.

func CleanupTarFile

func CleanupTarFile(path string)

CleanupTarFile removes a temp tar file, ignoring errors.

func CloneContentOnly

func CloneContentOnly(ctx context.Context, sourceURL, destDir, commitSHA string, sparseCheckoutPatterns []string) error

func CloneMetadataOnly

func CloneMetadataOnly(ctx context.Context, sourceURL, destDir string) error

CloneMetadataOnly uses --filter=tree:0 --no-checkout to avoid downloading file content, reducing clone size for repos where only commit history is needed.

func ExtractCachedRepo

func ExtractCachedRepo(tarPath, destDir string) error

ExtractCachedRepo extracts a cached tar.gz into destDir.

func GetCachedClone

func GetCachedClone(ctx context.Context, backend cache.Backend, patternsHash, normalizedURL, commitSHA string) (string, bool, error)

GetCachedClone retrieves a cached repo tar.gz, writing it to a temp file. Returns ("", false, nil) on cache miss.

func GetHeadCommit

func GetHeadCommit(ctx context.Context, repoPath string) (string, error)

func GetRemoteHeadSHA

func GetRemoteHeadSHA(ctx context.Context, sourceURL string) (string, error)

GetRemoteHeadSHA returns the commit SHA that HEAD points to on the remote, without cloning. Returns ("", nil) for empty repos that have no HEAD ref.

func GetRemoteRefSHA

func GetRemoteRefSHA(ctx context.Context, sourceURL, ref string) (string, error)

GetRemoteRefSHA resolves an arbitrary git ref (branch, tag, commit prefix) to a full SHA via ls-remote.

func GetVCSName

func GetVCSName(vcsType VCSType) string

GetVCSName returns a human-readable name for the VCS type

func IsFullSHA

func IsFullSHA(s string) bool

IsFullSHA returns true if s is a 40-character lowercase hex SHA.

func IsPrivateRequest

func IsPrivateRequest(ctx context.Context, sourceURL string) (bool, error)

IsPrivateRequest returns true if the request has authentication that indicates a private repo.

func NormalizeCacheURL

func NormalizeCacheURL(rawURL string) (string, error)

NormalizeCacheURL normalizes a source URL for use as a cache key component. Composes on NormalizeSourceURL (semantic normalization: known host upgrades, owner/repo extraction), then applies mechanical normalization (case folding, credential stripping, suffix cleanup).

func PutCachedClone

func PutCachedClone(ctx context.Context, backend cache.Backend, patternsHash, normalizedURL, commitSHA, repoDir string) error

PutCachedClone creates a tar.gz of repoDir and stores it in the cache backend.

func ValidateGitRef

func ValidateGitRef(ref string) error

ValidateGitRef validates that a git ref (commit, tag, branch) contains only safe characters.

func ValidateGitRepo

func ValidateGitRepo(path string) (string, error)

Types

type CIProvider

type CIProvider struct {
	Paths          []string // file/dir paths to check for existence
	SparsePatterns []string // sparse checkout globs; nil means same as Paths
}

CIProvider defines the file paths and sparse checkout patterns for a CI service.

type CloneError

type CloneError struct {
	URL       string
	Type      string
	Message   string
	GitOutput string
	Err       error
}

func (*CloneError) Error

func (e *CloneError) Error() string

func (*CloneError) Unwrap

func (e *CloneError) Unwrap() error

type CommitInfo

type CommitInfo struct {
	AuthorEmail string
	Timestamp   time.Time
}

type CommitMetrics

type CommitMetrics struct {
	RecentAuthorsCount     int
	MaxMonthlyAuthorsCount int
	MaxMonthlyWindowStart  time.Time
	MaxMonthlyWindowEnd    time.Time
	FirstCommit            time.Time
	LatestHumanCommit      time.Time
	CommitCount            int
	HumanCommitCount       int
}

type RollingWindowResult

type RollingWindowResult struct {
	MaxAuthors  int
	WindowStart time.Time
	WindowEnd   time.Time
}

type VCSType

type VCSType string

VCSType represents the type of version control system

const (
	VCSTypeGit     VCSType = "git"
	VCSTypeBazaar  VCSType = "bazaar"
	VCSTypeSVN     VCSType = "svn"
	VCSTypeMercur  VCSType = "mercurial"
	VCSTypeUnknown VCSType = "unknown"
)

func DetectVCSFromURL

func DetectVCSFromURL(urlStr string) (vcsType VCSType, supported bool)

DetectVCSFromURL attempts to determine the VCS type from a repository URL Returns the detected VCS type and whether it's supported by Risk Guard Assumes Git unless a known non-Git alternative is detected

Jump to

Keyboard shortcuts

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