64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
|
|
// Package recordstore owns private record location and worktree checks.
|
||
|
|
package recordstore
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"go.maximumdirect.net/releasekit/internal/git"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ResolveDir resolves an override relative to the repository or a directory in
|
||
|
|
// its shared Git metadata, returning both the record directory and checkout.
|
||
|
|
func ResolveDir(ctx context.Context, repositoryDir, override, category string) (string, string, error) {
|
||
|
|
root, err := git.RepositoryRoot(ctx, repositoryDir)
|
||
|
|
if err != nil {
|
||
|
|
return "", "", err
|
||
|
|
}
|
||
|
|
if override != "" {
|
||
|
|
if filepath.IsAbs(override) {
|
||
|
|
return filepath.Clean(override), root, nil
|
||
|
|
}
|
||
|
|
return filepath.Join(root, override), root, nil
|
||
|
|
}
|
||
|
|
common, err := git.CommonDirectory(ctx, repositoryDir)
|
||
|
|
if err != nil {
|
||
|
|
return "", "", err
|
||
|
|
}
|
||
|
|
return filepath.Join(common, "releasekit", category), root, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// CheckPath requires worktree record locations to be ignored, except for Git
|
||
|
|
// metadata directories. The record directory must already exist.
|
||
|
|
func CheckPath(ctx context.Context, repositoryDir, root, proposed string) error {
|
||
|
|
parent, err := filepath.EvalSymlinks(filepath.Dir(proposed))
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("resolve record directory %q: %w", filepath.Dir(proposed), err)
|
||
|
|
}
|
||
|
|
actual := filepath.Join(parent, filepath.Base(proposed))
|
||
|
|
rel, err := filepath.Rel(root, actual)
|
||
|
|
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
metadata, err := git.MetadataDirectories(ctx, repositoryDir)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
for _, directory := range metadata {
|
||
|
|
rel, err := filepath.Rel(directory, actual)
|
||
|
|
if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ignored, err := git.IsIgnored(ctx, repositoryDir, actual)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("check Git ignore rules for record path %q: %w", actual, err)
|
||
|
|
}
|
||
|
|
if !ignored {
|
||
|
|
return fmt.Errorf("record path %q is inside the worktree and is not ignored", actual)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|