aboutsummaryrefslogtreecommitdiff
path: root/ref/store/files/root_open_common.go
blob: cac98cbcb1569bc37c230f3bed3739336a509b26 (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
package files

import (
	"errors"
	"os"
	"path/filepath"
	"strings"
)

func openCommonRoot(gitRoot *os.Root) (*os.Root, error) {
	content, err := gitRoot.ReadFile("commondir")
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			return gitRoot.OpenRoot(".")
		}

		return nil, err
	}

	commonDir := strings.TrimSpace(string(content))
	if commonDir == "" {
		return nil, os.ErrNotExist
	}

	if filepath.IsAbs(commonDir) {
		return os.OpenRoot(commonDir)
	}

	// This is okay because that's how Git defines it anyway.
	return os.OpenRoot(filepath.Join(gitRoot.Name(), commonDir))
}