aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/update_validate.go
blob: cfbaca1d6983b063235dbf3d1ab644bb9807db0e (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package files

import (
	"fmt"
	"strings"

	objectid "codeberg.org/lindenii/furgit/object/id"
	"codeberg.org/lindenii/furgit/ref/name"
	refstore "codeberg.org/lindenii/furgit/ref/store"
)

func (executor *refUpdateExecutor) validateQueuedUpdate(op queuedUpdate) error {
	if op.name == "" {
		return wrapUpdateError(op.name, &refstore.InvalidNameError{Err: fmt.Errorf("empty reference name")})
	}

	switch op.kind {
	case updateCreate, updateReplace:
		err := refname.ValidateUpdateName(op.name, true)
		if err != nil {
			return wrapUpdateError(op.name, &refstore.InvalidNameError{Err: err})
		}

		if op.newID.Size() == 0 {
			return wrapUpdateError(op.name, &refstore.InvalidValueError{Err: objectid.ErrInvalidAlgorithm})
		}
	case updateDelete, updateVerify:
		err := refname.ValidateUpdateName(op.name, false)
		if err != nil {
			return wrapUpdateError(op.name, &refstore.InvalidNameError{Err: err})
		}

		if op.oldID.Size() == 0 {
			return wrapUpdateError(op.name, &refstore.InvalidValueError{Err: objectid.ErrInvalidAlgorithm})
		}
	case updateCreateSymbolic, updateReplaceSymbolic:
		err := refname.ValidateUpdateName(op.name, true)
		if err != nil {
			return wrapUpdateError(op.name, &refstore.InvalidNameError{Err: err})
		}

		if strings.TrimSpace(op.newTarget) == "" {
			return wrapUpdateError(op.name, &refstore.InvalidValueError{Err: fmt.Errorf("empty symbolic target")})
		}

		err = refname.ValidateSymbolicTarget(op.name, strings.TrimSpace(op.newTarget))
		if err != nil {
			return wrapUpdateError(op.name, &refstore.InvalidValueError{Err: err})
		}
	case updateDeleteSymbolic, updateVerifySymbolic:
		err := refname.ValidateUpdateName(op.name, false)
		if err != nil {
			return wrapUpdateError(op.name, &refstore.InvalidNameError{Err: err})
		}
	default:
		return fmt.Errorf("refstore/files: unsupported update operation %d", op.kind)
	}

	if op.kind == updateReplaceSymbolic || op.kind == updateDeleteSymbolic || op.kind == updateVerifySymbolic {
		if strings.TrimSpace(op.oldTarget) == "" {
			return wrapUpdateError(op.name, &refstore.InvalidValueError{Err: fmt.Errorf("empty symbolic old target")})
		}
	}

	return nil
}