gunk-dev/the-valley

snapshot.go

source88a5a486b52fmain's published tip history
4.6 KiB
1package main
2
3import (
4 "errors"
5 "fmt"
6 "os/exec"
7 "strings"
8
9 "the-valley/approval"
10 "the-valley/composed"
11)
12
13// snapshot is one commit of another repository: the instance repository
14// a controller reads its floor and the instance identity from. A ref is
15// resolved to it once, by the ref's exact name, and everything read
16// afterwards is read at that commit, so a ref that moves meanwhile mixes
17// no two versions into one reading.
18//
19// Every read is git plumbing over refs and objects: show-ref, rev-parse,
20// cat-file and rev-list, and composed's ls-tree and cat-file for the
21// floor. Each runs with optional locks off and with lazy fetching off, so
22// in a partial clone an object the repository does not hold fails the
23// read rather than being fetched into it. A snapshot creates no lock
24// file, worktree, index or object there, and a repository the controller
25// may only read can be read.
26type snapshot struct {
27 repo string // the repository, as given
28 ref string // the full ref name resolved
29 commit string // the commit it held
30}
31
32// takeSnapshot resolves ref, a full ref name, in repo to the commit it
33// holds. A repository with no such ref, or one whose ref holds something
34// other than a commit, is refused.
35func takeSnapshot(repo, ref string) (snapshot, error) {
36 if !strings.HasPrefix(ref, "refs/") {
37 return snapshot{}, fmt.Errorf("%s is not a full ref name", ref)
38 }
39 // show-ref --exists exits 2 for a ref that is missing, and with
40 // another status for a failure to read.
41 _, err := readGit(repo, "show-ref", "--exists", "--end-of-options", ref)
42 var exit *exec.ExitError
43 if errors.As(err, &exit) && exit.ExitCode() == 2 {
44 return snapshot{}, fmt.Errorf("%s has no %s", repo, ref)
45 } else if err != nil {
46 return snapshot{}, err
47 }
48 oid, err := readGit(repo, "show-ref", "--verify", "--hash", "--end-of-options", ref)
49 if err != nil {
50 return snapshot{}, err
51 }
52 commit, err := readGit(repo, "rev-parse", "--verify", "--end-of-options", oid+"^{commit}")
53 if err != nil {
54 return snapshot{}, fmt.Errorf("%s in %s is not a commit: %w", ref, repo, err)
55 }
56 return snapshot{repo: repo, ref: ref, commit: commit}, nil
57}
58
59// read is the blob at path in the snapshot's commit.
60func (s snapshot) read(path string) ([]byte, error) {
61 kind, err := readGit(s.repo, "cat-file", "-t", s.commit+":"+path)
62 if err != nil {
63 return nil, fmt.Errorf("%s has no %s at %s: %w", s.repo, path, s.commit, err)
64 }
65 if kind != "blob" {
66 return nil, fmt.Errorf("%s at %s in %s is a %s, not a file", path, s.commit, s.repo, kind)
67 }
68 out, err := gitIn(s.repo, readOnly, "--no-optional-locks", "cat-file", "blob", s.commit+":"+path)
69 return []byte(out), err
70}
71
72// floor is the floor in dir as the snapshot's commit holds it. A Floor
73// whose Ref is an object ID is read at that commit (composed.Floor), so
74// the floor composed is the snapshot's whatever the ref holds by then.
75func (s snapshot) floor(dir string) composed.Floor {
76 return composed.Floor{Repo: s.repo, Ref: s.commit, Dir: dir, NoLazyFetch: true}
77}
78
79// instanceID is the instance identity (approval.InstanceID) of the
80// snapshot's commit: its root commits and the repository's object format.
81// A shallow repository is refused, since the roots it holds need not be
82// its history's, and so is one missing a commit of that history, which
83// rev-list cannot walk.
84func (s snapshot) instanceID() (string, error) {
85 shallow, err := readGit(s.repo, "rev-parse", "--is-shallow-repository")
86 if err != nil {
87 return "", err
88 }
89 if shallow != "false" {
90 return "", fmt.Errorf("the instance repository %s is shallow, so the root commits of its %s are not known", s.repo, s.ref)
91 }
92 format, err := readGit(s.repo, "rev-parse", "--show-object-format")
93 if err != nil {
94 return "", err
95 }
96 roots, err := readGit(s.repo, "rev-list", "--max-parents=0", "--end-of-options", s.commit)
97 if err != nil {
98 return "", fmt.Errorf("the history of %s in the instance repository does not read: %w", s.ref, err)
99 }
100 id, err := approval.InstanceID(format, strings.Fields(roots))
101 if err != nil {
102 return "", fmt.Errorf("the instance identity: %w", err)
103 }
104 return id, nil
105}
106
107// readOnly is the environment a snapshot's git runs in: lazy fetching
108// off, so a partial clone's missing object fails the read instead of
109// being fetched into the repository.
110var readOnly = []string{"GIT_NO_LAZY_FETCH=1"}
111
112// readGit runs a git command that only reads, and returns its output
113// trimmed. Optional locks are off, so no command takes the lock some take
114// to refresh an index, and lazy fetching is off.
115func readGit(repo string, args ...string) (string, error) {
116 out, err := gitIn(repo, readOnly, append([]string{"--no-optional-locks"}, args...)...)
117 return strings.TrimSpace(out), err
118}