aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/update_verify_refnames.go
blob: 12d67c5f5a35583a0d476d84badbdb0253c73d86 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package files

import (
	"strings"

	"codeberg.org/lindenii/furgit/refstore"
)

func verifyRefnameAvailable(name string, existing map[string]struct{}, writes []string, deleted map[string]struct{}) error {
	for existingName := range existing {
		if existingName == name {
			continue
		}

		if _, skip := deleted[existingName]; skip {
			continue
		}

		if refnamesConflict(name, existingName) {
			return wrapUpdateError(name, &refstore.NameConflictError{Other: existingName})
		}
	}

	for _, other := range writes {
		if other == name {
			continue
		}

		if refnamesConflict(name, other) {
			return wrapUpdateError(name, &refstore.NameConflictError{Other: other})
		}
	}

	return nil
}

func refnamesConflict(left, right string) bool {
	return left == right ||
		strings.HasPrefix(left, right+"/") ||
		strings.HasPrefix(right, left+"/")
}