33 lines
955 B
Go
33 lines
955 B
Go
// Package projectkey turns a project directory into the stable key
|
|
// patterm uses to name its scratchpad directory under $XDG_DATA_HOME.
|
|
// SPEC §3.
|
|
//
|
|
// Two invocations from the same realpath must produce the same key.
|
|
// The key is only used as a directory name on disk — there is no
|
|
// daemon to look up.
|
|
package projectkey
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Key derives the 16-char hex key from the realpath of dir.
|
|
func Key(dir string) (string, error) {
|
|
abs, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return "", fmt.Errorf("projectkey: abs %q: %w", dir, err)
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(abs)
|
|
if err != nil {
|
|
// Directory may not exist yet; fall back to the absolute path.
|
|
// The key stays stable; downstream code will fail later when it
|
|
// tries to chdir or write into the dir.
|
|
resolved = abs
|
|
}
|
|
sum := sha256.Sum256([]byte(resolved))
|
|
return hex.EncodeToString(sum[:8]), nil
|
|
}
|