MCPcopy Create free account
hub / github.com/entireio/git-sync / NormalizeMapping

Function NormalizeMapping

internal/validation/validation.go:125–160  ·  view source on GitHub ↗

NormalizeMapping validates a single mapping. allowOther accepts refs/* outside refs/heads/ and refs/tags/.

(m RefMapping, allowOther bool)

Source from the content-addressed store, hash-verified

123// NormalizeMapping validates a single mapping. allowOther accepts
124// refs/* outside refs/heads/ and refs/tags/.
125func NormalizeMapping(m RefMapping, allowOther bool) (NormalizedMapping, error) {
126 src := strings.TrimSpace(m.Source)
127 dst := strings.TrimSpace(m.Target)
128 if src == "" || dst == "" {
129 return NormalizedMapping{}, fmt.Errorf("invalid mapping %q:%q: source and target must be non-empty", m.Source, m.Target)
130 }
131
132 srcFQ := strings.HasPrefix(src, "refs/")
133 dstFQ := strings.HasPrefix(dst, "refs/")
134
135 if srcFQ && dstFQ {
136 sourceRef := plumbing.ReferenceName(src)
137 targetRef := plumbing.ReferenceName(dst)
138 srcKind := refKind(sourceRef)
139 dstKind := refKind(targetRef)
140 if !allowOther && srcKind == kindOther {
141 return NormalizedMapping{}, fmt.Errorf("unsupported source ref kind: %s (set --all-refs to allow arbitrary refs/* namespaces)", src)
142 }
143 if !allowOther && dstKind == kindOther {
144 return NormalizedMapping{}, fmt.Errorf("unsupported target ref kind: %s (set --all-refs to allow arbitrary refs/* namespaces)", dst)
145 }
146 if srcKind != dstKind {
147 return NormalizedMapping{}, fmt.Errorf("cross-kind mapping not allowed: %s (%s) -> %s (%s)", src, srcKind, dst, dstKind)
148 }
149 return NormalizedMapping{SourceRef: sourceRef, TargetRef: targetRef}, nil
150 }
151
152 if !srcFQ && !dstFQ {
153 return NormalizedMapping{
154 SourceRef: plumbing.NewBranchReferenceName(src),
155 TargetRef: plumbing.NewBranchReferenceName(dst),
156 }, nil
157 }
158
159 return NormalizedMapping{}, fmt.Errorf("ambiguous mapping: cannot mix fully-qualified and short ref names: %q -> %q", src, dst)
160}
161
162// ValidateMappings normalizes all mappings and rejects duplicate target refs.
163func ValidateMappings(mappings []RefMapping, allowOther bool) ([]NormalizedMapping, error) {

Callers 2

ValidateMappingsFunction · 0.85

Calls 1

refKindFunction · 0.85

Tested by 1