diff options
| author | 2026-03-07 15:08:14 +0800 | |
|---|---|---|
| committer | 2026-03-07 15:56:39 +0800 | |
| commit | 9d08dc994d51298e2d8e75d8ed4ee477312ec53a (patch) | |
| tree | 28c2a53174dd319fbd330132d446ef9ead8cb7ee /ref/refname/branch.go | |
| parent | refstore: Remove Shorten for now (diff) | |
| signature | No signature | |
ref/refname: Add refname validation
Diffstat (limited to 'ref/refname/branch.go')
| -rw-r--r-- | ref/refname/branch.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/ref/refname/branch.go b/ref/refname/branch.go new file mode 100644 index 00000000..274a95e3 --- /dev/null +++ b/ref/refname/branch.go @@ -0,0 +1,25 @@ +package refname + +import "strings" + +// Branch checks one branch shorthand and returns its fully-qualified +// refs/heads/... name. +// +// Unlike Git in-repository branch parsing, this helper does not expand @{-n}. +func Branch(name string) (string, error) { + full := "refs/heads/" + name + if strings.HasPrefix(name, "-") || full == "refs/heads/HEAD" { + return "", &NameError{Name: name, Reason: "invalid branch name"} + } + + err := validate(full, 0) + if err != nil { + return "", err + } + + if strings.HasPrefix(name, "refs/") { + return name, nil + } + + return full, nil +} |
