@@ -488,3 +488,42 @@ func ReferenceIsValidName(name string) bool {
488488 }
489489 return false
490490}
491+
492+ const (
493+ // This should match GIT_REFNAME_MAX in src/refs.h
494+ _refnameMaxLength = C .size_t (1024 )
495+ )
496+
497+ type ReferenceFormat uint
498+
499+ const (
500+ ReferenceFormatNormal ReferenceFormat = C .GIT_REFERENCE_FORMAT_NORMAL
501+ ReferenceFormatAllowOnelevel ReferenceFormat = C .GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL
502+ ReferenceFormatRefspecPattern ReferenceFormat = C .GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
503+ ReferenceFormatRefspecShorthand ReferenceFormat = C .GIT_REFERENCE_FORMAT_REFSPEC_SHORTHAND
504+ )
505+
506+ // ReferenceNormalizeName normalizes the reference name and checks validity.
507+ //
508+ // This will normalize the reference name by removing any leading slash '/'
509+ // characters and collapsing runs of adjacent slashes between name components
510+ // into a single slash.
511+ //
512+ // See git_reference_symbolic_create() for rules about valid names.
513+ func ReferenceNormalizeName (name string , flags ReferenceFormat ) (string , error ) {
514+ cname := C .CString (name )
515+ defer C .free (unsafe .Pointer (cname ))
516+
517+ buf := (* C .char )(C .malloc (_refnameMaxLength ))
518+ defer C .free (unsafe .Pointer (buf ))
519+
520+ runtime .LockOSThread ()
521+ defer runtime .UnlockOSThread ()
522+
523+ ecode := C .git_reference_normalize_name (buf , _refnameMaxLength , cname , C .uint (flags ))
524+ if ecode < 0 {
525+ return "" , MakeGitError (ecode )
526+ }
527+
528+ return C .GoString (buf ), nil
529+ }
0 commit comments