aboutsummaryrefslogtreecommitdiff
path: root/ref/refname/branch.go
diff options
context:
space:
mode:
Diffstat (limited to 'ref/refname/branch.go')
-rw-r--r--ref/refname/branch.go25
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
+}