aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/transaction_verify_refnames.go
blob: 2efc872a68be098dad9feaa7b7f6def1627e2add (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
package files

import (
	"fmt"
	"strings"
)

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 fmt.Errorf("refstore/files: reference name conflict between %q and %q", name, existingName)
		}
	}

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

		if refnamesConflict(name, other) {
			return fmt.Errorf("refstore/files: reference name conflict between %q and %q", name, other)
		}
	}

	return nil
}

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