Documentation
¶
Overview ¶
Package forge parses repository references into their coordinates. A reference can be a full URL in any of the forms git-clone(1) accepts (https, ssh, scp-like, git, bare hostname) or a shorthand (repo, owner/repo, owner/repo#42, owner/repo@rev), and parsing yields the host, owner, name, and any branch, tag, commit, pull request, or file path the reference carries. The URL shapes of GitHub, GitLab, Sourcehut, Codeberg, Bitbucket, and Azure DevOps are known; anything else falls back to a generic owner/repo reading, so self-hosted forges work out of the box. Everything here is pure and offline - no network calls; resolving what an ambiguous reference points at (e.g. Ref.Rev) is the caller's job.
Index ¶
Constants ¶
const ( ForgeAzureDevOps = "azuredevops" ForgeBitbucket = "bitbucket" ForgeCodeberg = "codeberg" ForgeGitHub = "github" ForgeGitLab = "gitlab" ForgeSourcehut = "sourcehut" )
Named forges recognized by Resolve and reported in Ref.Forge. Azure DevOps is deliberately not a Resolve name - its clone URLs embed an org/project path that owner/name construction cannot express - but dev.azure.com URLs are still recognized by ParseURL.
const ( HostAzureDevOps = "dev.azure.com" HostBitbucket = "bitbucket.org" HostCodeberg = "codeberg.org" HostGitHub = "github.com" HostGitLab = "gitlab.com" HostSourcehut = "git.sr.ht" )
Canonical hostnames of the built-in forges.
const ( SchemeGit = "git" SchemeHTTPS = "https" SchemeSSH = "ssh" )
Transports reported in Ref.Scheme. HTTP inputs are normalized to HTTPS.
const ( VCSGit = vcs.Git VCSJJ = vcs.JJ )
VCS names returned by DetectVCS, aliasing the vcs package's own so the two spellings cannot drift.
Variables ¶
This section is empty.
Functions ¶
func DetectVCS ¶
DetectVCS inspects an existing checkout to decide which VCS drives it, returning VCSJJ, VCSGit, or "" when neither marker is present. A `.jj` directory takes precedence over `.git`, which covers both jj layouts: colocated repos have a top-level `.git` alongside `.jj` and jj should own the working copy, while non-colocated repos have no top-level `.git` at all (their git store lives inside `.jj/repo/store/git`).
It delegates to vcs.Detect; reach for the vcs package directly when you also need vcs.Resolver to find the root a nested path belongs to.
func ExpandPRList ¶
ExpandPRList expands a PR list like "1,2,5-7" into []int{1, 2, 5, 6, 7}. Ranges are inclusive on both ends; duplicates are dropped; negative or zero numbers are rejected.
func IsValidRepoName ¶
IsValidRepoName reports whether name is a plausible repository name: ASCII letters, digits, '-', '_', and '.', at most 255 bytes (the common filesystem NAME_MAX for a single path component), and not "." or "..".
func Register ¶
func Register(f Forge)
Register adds a forge to the registry consulted by Resolve and ParseURL, replacing any existing entry with the same name or host. It lets consumers wire up self-hosted forges (e.g. an internal Gitea) whose URL shapes differ from the generic owner/repo fallback.
Types ¶
type Forge ¶
type Forge struct {
// GitSuffix reports whether the forge's clone URLs end in ".git".
// Sourcehut is the only built-in forge whose URLs do not.
GitSuffix bool
// Host is the forge's canonical hostname.
Host string
// Name is the identifier Resolve accepts and Ref.Forge reports.
Name string
// Parse extracts a Ref from a URL path already split from its scheme
// and host. It is a plain field rather than an interface method so that
// registering a self-hosted forge stays a one-liner.
Parse func(path, scheme, host string) (Ref, bool)
}
Forge describes how to recognize and construct URLs for a single forge.
func Resolve ¶
Resolve resolves a forge identifier - a registered name or a hostname, case-insensitively and with surrounding whitespace ignored - to its Forge. An empty value defaults to GitHub, and any unregistered value containing a dot is accepted as a self-hosted host with generic owner/repo parsing.
type Option ¶
type Option func(*options)
Option configures Parse and ParseShorthand.
func WithDefaultHost ¶
WithDefaultHost sets the Host recorded on shorthand results, so a GitLab-first consumer can flip the default away from GitHub. It does not affect URL parsing, and when unset shorthand results carry an empty Host.
func WithDefaultOwner ¶
WithDefaultOwner sets the owner assumed when a shorthand reference omits one ("repo" rather than "owner/repo").
type Ref ¶
type Ref struct {
// Branch is the branch name from URLs like .../tree/<branch> (GitHub,
// GitLab, Sourcehut) or .../src/branch/<branch> (Gitea). It is only
// set when the branch is unambiguous: with a trailing file path a
// slash-containing branch name cannot be told apart from the path, so
// forms like .../tree/<branch>/<path> leave Branch empty.
Branch string
// CloneURL is the clone URL derived from the reference, preserving the
// transport the reference used. Empty for shorthand references.
CloneURL string
// Commit is a full 40-hex commit hash from URLs like .../commit/<sha>
// or .../src/commit/<sha>, or from @<sha> shorthand.
Commit string
// ExplicitOwner reports whether the reference named its owner, as
// opposed to inheriting WithDefaultOwner.
ExplicitOwner bool
// FilePath is the sub-path from URLs like .../blob/<ref>/<path>,
// .../tree/<ref>/<path>, or .../src/<ref>/<path>, assuming a
// single-segment ref.
FilePath string
// Forge is the forge that recognized the reference: a registered name
// (github, gitlab, ...) or the bare hostname for unrecognized hosts.
// Empty for shorthand references.
Forge string
// Host is the canonical hostname, lowercased with any "www." prefix
// stripped. Empty for shorthand references unless WithDefaultHost is
// given.
Host string
// Name is the repository name with any ".git" suffix stripped.
Name string
// Owner is the user, organization, or (for GitLab) nested group path
// that owns the repository.
Owner string
// PullRequest is the pull/merge request number from URLs like
// .../pull/<n> or the #<n> shorthand. It is kept as a string because
// parsing does not validate it - consumers decide how strict to be.
PullRequest string
// Rev is an ambiguous revision from @<rev> shorthand or from URLs like
// Bitbucket's .../src/<ref>: it may name a branch or a tag, and only
// the remote can tell, so Branch and Tag are left empty. A 40-hex rev
// is classified as Commit instead.
Rev string
// Scheme is the transport the reference used: https, ssh, or git.
// Empty for shorthand references.
Scheme string
// Tag is the tag name from URLs like .../releases/tag/<tag> or
// .../src/tag/<tag>.
Tag string
}
Ref is the resolved coordinates of a repository reference.
func Parse ¶
Parse resolves a repository reference: a full URL in any form ParseURL accepts, or failing that a shorthand in any form ParseShorthand accepts. Destination suffixes (e.g. clone's "=<dir>") are not part of the grammar - strip them before calling.
func ParseShorthand ¶
ParseShorthand parses a shorthand repository reference:
repo owner/repo owner/repo#42 (pull request) owner/repo@main (branch or tag - see Ref.Rev) owner/repo@v1.2.3 owner/repo@<sha> (40-hex commit)
func ParseURL ¶
ParseURL attempts to parse a full repository URL. It is a probe rather than a validator - callers legitimately try it and fall through to shorthand parsing - so failure is reported as ok=false, not an error.
Supported URL forms (per git-clone(1)):
ssh://[user@]host[:port]/path [user@]host:path (scp-like) http[s]://host[:port]/path git://host[:port]/path host/path (bare hostname)
func (Ref) HTTPSURL ¶
HTTPSURL returns the HTTPS clone URL for the repository, or "" when it cannot be built - see [Ref.constructable]. Prefer CloneURL when the Ref came from ParseURL: it preserves the exact path the reference carried.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package vcs inspects an existing checkout: which version control system drives it, and where the repository containing a given path begins.
|
Package vcs inspects an existing checkout: which version control system drives it, and where the repository containing a given path begins. |