summaryrefslogtreecommitdiff
path: root/reference.go
diff options
context:
space:
mode:
authorSegev Finer <[email protected]>2020-11-14 02:50:59 +0200
committerGitHub <[email protected]>2020-11-13 16:50:59 -0800
commit2bd574b6bd75a044f526cf882161db08ac3cb633 (patch)
tree5e5060f5d6c75a3a6396222328ad2670743739f6 /reference.go
parent2d639d8e49470be188108eeae8e8f722d68aa910 (diff)
Add ReferenceNormalizeName (#681)
Diffstat (limited to 'reference.go')
-rw-r--r--reference.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/reference.go b/reference.go
index b5f5e47..7b5e3c2 100644
--- a/reference.go
+++ b/reference.go
@@ -488,3 +488,42 @@ func ReferenceIsValidName(name string) bool {
}
return false
}
+
+const (
+ // This should match GIT_REFNAME_MAX in src/refs.h
+ _refnameMaxLength = C.size_t(1024)
+)
+
+type ReferenceFormat uint
+
+const (
+ ReferenceFormatNormal ReferenceFormat = C.GIT_REFERENCE_FORMAT_NORMAL
+ ReferenceFormatAllowOnelevel ReferenceFormat = C.GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
+ ReferenceFormatRefspecPattern ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
+ ReferenceFormatRefspecShorthand ReferenceFormat = C.GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
+)
+
+// ReferenceNormalizeName normalizes the reference name and checks validity.
+//
+// This will normalize the reference name by removing any leading slash '/'
+// characters and collapsing runs of adjacent slashes between name components
+// into a single slash.
+//
+// See git_reference_symbolic_create() for rules about valid names.
+func ReferenceNormalizeName(name string, flags ReferenceFormat) (string, error) {
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+
+ buf := (*C.char)(C.malloc(_refnameMaxLength))
+ defer C.free(unsafe.Pointer(buf))
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ ecode := C.git_reference_normalize_name(buf, _refnameMaxLength, cname, C.uint(flags))
+ if ecode < 0 {
+ return "", MakeGitError(ecode)
+ }
+
+ return C.GoString(buf), nil
+}