gitctx

package
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package gitctx reads local git context (current branch, remotes, parsed GitLab project path) so that "auto" composite commands like `gitlab-cli mr create --auto` can derive sensible defaults from cwd.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoGitLabRemote = errors.New("no GitLab remote found")

ErrNoGitLabRemote is returned when no remote URL points at a GitLab-style host.

View Source
var ErrNotAGitRepo = errors.New("not a git repository")

ErrNotAGitRepo is returned when the current working directory is not a git repository.

Functions

func CurrentBranch

func CurrentBranch(dir string) (string, error)

CurrentBranch returns the current branch name, or empty string if HEAD is detached.

func IsGitRepo

func IsGitRepo(dir string) bool

IsGitRepo returns true if dir (or cwd if empty) is inside a git working tree.

func RemoteURL

func RemoteURL(dir, remote string) (string, error)

RemoteURL returns the URL of the named remote.

func Remotes

func Remotes(dir string) (map[string]string, error)

Remotes returns a map from remote name → fetch URL.

func RepoRoot

func RepoRoot(dir string) (string, error)

RepoRoot returns the absolute path to the git repository root.

Types

type Context

type Context struct {
	Repo          string        // absolute path to git toplevel; empty if not a repo
	CurrentBranch string        // empty if detached HEAD or not a repo
	Remote        *GitLabRemote // nil if no GitLab remote found
}

Context bundles the most useful git context for composite commands.

func Detect

func Detect(dir string) (*Context, error)

Detect returns the context for dir (or cwd if empty). Returns a populated Context with whatever fields could be determined; never returns ErrNotAGitRepo.

type GitLabRemote

type GitLabRemote struct {
	Host        string // e.g. "gitlab.example.com"
	ProjectPath string // e.g. "group/subgroup/project" (no .git suffix, no leading slash)
	URL         string // original URL
}

GitLabRemote describes a parsed remote URL pointing at a GitLab host.

func DetectGitLabRemote

func DetectGitLabRemote(dir string) (*GitLabRemote, error)

DetectGitLabRemote scans the working directory's remotes (in priority order: "origin", "upstream", then any other) and returns the first parsable GitLab remote.

func ParseGitLabRemote

func ParseGitLabRemote(remoteURL string) (*GitLabRemote, error)

ParseGitLabRemote parses an SSH or HTTPS git remote URL into a GitLabRemote. Accepts:

git@gitlab.example.com:group/subgroup/project.git
ssh://git@gitlab.example.com:22/group/subgroup/project.git
https://gitlab.example.com/group/subgroup/project.git
https://oauth2:token@gitlab.example.com/group/subgroup/project.git
http://gitlab.example.com/group/project (rare; allowed for local dev)

Returns ErrNoGitLabRemote if the URL is empty or unrecognised.

Jump to

Keyboard shortcuts

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