aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/read_resolve.go
blob: a998f970f5eba3491a5878ee4c5d5e981131e186 (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 (
	"errors"

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

// Resolve resolves one reference name from the files store visible namespace.
func (store *Store) Resolve(name string) (ref.Ref, error) { //nolint:ireturn
	if name == "" {
		return nil, refstore.ErrReferenceNotFound
	}

	resolved, err := store.readLooseRef(name)
	if err == nil {
		return resolved, nil
	}

	if !errors.Is(err, refstore.ErrReferenceNotFound) {
		refPath := store.loosePath(name)

		info, statErr := store.rootFor(refPath.root).Stat(refPath.path)
		if statErr != nil || !info.IsDir() {
			return nil, err
		}
	}

	packed, packedErr := store.readPackedRefs()
	if packedErr != nil {
		return nil, packedErr
	}

	detached, ok := packed.byName[name]
	if !ok {
		return nil, refstore.ErrReferenceNotFound
	}

	return detached, nil
}