git

package module
v0.0.0-...-7d35770 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package git provides utilities for working with git repositories.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRepo = errors.New("invalid git repository")

ErrInvalidRepo indicates that the provided string is not a valid git repository name or URL.

Functions

func RegisterHostHandler

func RegisterHostHandler(exactHost string, handler HostHandler)

RegisterHostHandler allows for the registration of a host handler that is triggered when the host of a URL exactly matches the given host, ignoring case.

This method is not thread-safe, and is expected to be called from init().

Types

type HostHandler

type HostHandler interface {
	// Validate checks if the URL is valid for this host.
	Validate(u *url.URL) error

	// Canon canonicalizes the URL for this host.
	Canon(u *url.URL) *url.URL
}

HostHandler defines the interface for host-specific validation and canonicalization of URLs.

type Repo

type Repo struct {
	// contains filtered or unexported fields
}

Repo represents a parsed and canonicalized git repository.

func ParseRepo

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

ParseRepo parses a git repository URL or SCP-like string and returns a Repo.

It validates the repository against registered host handlers and computes a canonical URL.

If the repository is not valid, an error is returned.

func (*Repo) Canon

func (r *Repo) Canon() *url.URL

Canon returns the canonicalized URL of the repository.

func (*Repo) ID

func (r *Repo) ID() string

ID returns a string that can be used to identify the repository.

The ID is based on the canonical URL, but is made up of the host and path only. The ID is unsuitable for interacting with the repository.

func (*Repo) Parsed

func (r *Repo) Parsed() *url.URL

Parsed returns the parsed URL of the repository (preserving case and structure from parsing).

func (*Repo) Raw

func (r *Repo) Raw() string

Raw returns the original raw string used to parse the repository.

type StandardHostHandler

type StandardHostHandler struct {
	// ForceScheme will replace any URL scheme with its value when set.
	ForceScheme string

	// StripUser will remove the user from the URL when true.
	StripUser bool

	// HasTrailingSlash will add a trailing slash to the URL if it doesn't have
	// one. If false any trailing slash will be removed.
	HasTrailingSlash bool

	// HasDotGitSuffix will add a .git suffix to the URL if it doesn't have one.
	// If false any .git suffix will be removed.
	HasDotGitSuffix bool

	// PathPrefix is the prefix that is required to be set for a valid git
	// repository for this host.
	PathPrefix string

	// MinPathSegments is the minimum number of path segments that are required for a
	// valid git repository for this host. If 0 there is no restriction. If PathPrefix
	// is not empty, then only segments after the prefix are considered.
	MinPathSegments int

	// MaxPathSegments is the maximum number of path segments that are required for a
	// valid git repository for this host. If 0 there is no restriction. If PathPrefix
	// is not empty, then only segments after the prefix are considered.
	MaxPathSegments int

	// LowerPathSegments is the number of path segments that should be lowercased
	// following any PathPrefix, during the canonicalization process. This is to
	// ensure that URLs for case-insensitive hosts are canonicalized correctly.
	//
	// A zero value means no path segements will be lowercased.
	// A positive value of N means the first N path segments following the
	// PathPrefix will be lowercased.
	// A negative value of -N means all but the last N path segments following
	// the PathPrefix will be lowercased.
	LowerPathSegments int
}

StandardHostHandler implements HostHandler with common validation and canonicalization settings.

func (*StandardHostHandler) Canon

func (h *StandardHostHandler) Canon(u *url.URL) *url.URL

Canon implements the HostHandler interface.

func (*StandardHostHandler) Validate

func (h *StandardHostHandler) Validate(u *url.URL) error

Validate implements the HostHandler interface.

Jump to

Keyboard shortcuts

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