From 929b8cc620abca70b3444b09be5249f6c6cb7812 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 26 Mar 2026 09:19:01 +0000 Subject: ref/name: Rename from ref/refname --- ref/name/normalize.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 ref/name/normalize.go (limited to 'ref/name/normalize.go') diff --git a/ref/name/normalize.go b/ref/name/normalize.go new file mode 100644 index 00000000..9cbe7126 --- /dev/null +++ b/ref/name/normalize.go @@ -0,0 +1,53 @@ +package refname + +import "strings" + +// Normalize collapses slashes according to what Git wants +// then validates the normalized name. +func Normalize(name string, options Options) (string, error) { + normalized := collapseSlashes(name) + + err := validate(normalized, options.flags()) + if err != nil { + return "", err + } + + return normalized, nil +} + +func normalizeRefPath(path string) (string, bool) { + components := make([]string, 0, strings.Count(path, "/")+1) + i := 0 + + for i < len(path) { + for i < len(path) && path[i] == '/' { + i++ + } + + if i == len(path) { + break + } + + j := i + for j < len(path) && path[j] != '/' { + j++ + } + + component := path[i:j] + switch component { + case ".": + case "..": + if len(components) == 0 { + return "", false + } + + components = components[:len(components)-1] + default: + components = append(components, component) + } + + i = j + } + + return strings.Join(components, "/"), true +} -- cgit v1.3.1-10-gc9f91