gunk-dev/the-valley

consult_snapshot.go

source6a6791929c36main's published tip history
4.3 KiB
1package cmd
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "the-valley/flow/internal/config"
13 "the-valley/flow/internal/consult"
14 "the-valley/flow/internal/git"
15 "the-valley/flow/internal/location"
16 "the-valley/flow/internal/project"
17)
18
19// repoSnapshot is the workspace a --repo consult reads: a detached worktree
20// of the project's registered clone at origin's default branch, never the
21// clone's own working tree.
22type repoSnapshot struct {
23 project, branch, dir, commit string
24}
25
26// workspaceLine heads the prompt, so the partner can cite what it read.
27func (s *repoSnapshot) workspaceLine() string {
28 return fmt.Sprintf("Workspace: %s at origin/%s %s", s.project, s.branch, s.commit)
29}
30
31// prepareRepoSnapshot fetches origin's default branch into the clone of the
32// registered project name and moves the snapshot at dir to it, as launch
33// starts a worktree: the target config's default_branch, else main. When the
34// fetch fails, it warns on errOut and reads the snapshot dir already holds;
35// with none there, it fails.
36func prepareRepoSnapshot(ctx context.Context, name, dir string, errOut io.Writer) (*repoSnapshot, error) {
37 reg, err := project.Load()
38 if err != nil {
39 return nil, err
40 }
41 _, clone := resolveRepoTarget(name, "", reg)
42 if clone == "" {
43 return nil, fmt.Errorf("project %q is not registered; use valley flow project add or --dir", name)
44 }
45 s := &repoSnapshot{project: name, branch: "main", dir: dir}
46 // A config that can't be loaded leaves main, unless a file under the
47 // flow names is what fails: that is never a fallback.
48 cfg, err := config.Load(clone)
49 var flowErr *config.FlowFileError
50 if errors.As(err, &flowErr) {
51 return nil, err
52 }
53 if err == nil && cfg.DefaultBranch != "" {
54 s.branch = cfg.DefaultBranch
55 }
56 if fetchErr := git.FetchRemoteBranch(ctx, clone, s.branch); fetchErr != nil {
57 s.commit = git.SnapshotCommit(ctx, dir)
58 if s.commit == "" {
59 return nil, fmt.Errorf("fetching origin/%s of project %q: %w; there is no earlier snapshot to read", s.branch, name, fetchErr)
60 }
61 fmt.Fprintf(errOut, "warning: fetching origin/%s of project %q failed: %v\nreading the last snapshot, at %s\n",
62 s.branch, name, strings.TrimSpace(fetchErr.Error()), s.commit)
63 return s, nil
64 }
65 s.commit = git.RefCommit(ctx, clone, "refs/remotes/origin/"+s.branch)
66 if s.commit == "" {
67 return nil, fmt.Errorf("project %q has no origin/%s", name, s.branch)
68 }
69 if err := git.SyncSnapshot(ctx, clone, dir, s.commit); err != nil {
70 return nil, fmt.Errorf("checking out origin/%s of project %q at %s: %w", s.branch, name, dir, err)
71 }
72 return s, nil
73}
74
75// oneShotSnapshotDir is a new, empty directory for the snapshot of an
76// unthreaded --repo consult, under location's ConsultSnapshots. The
77// snapshot is the project-named directory inside it, so remove both when
78// done.
79func oneShotSnapshotDir(name string) (parent, dir string, err error) {
80 l, err := location.Host()
81 if err != nil {
82 return "", "", err
83 }
84 root, err := l.ConsultSnapshots()
85 if err != nil {
86 return "", "", err
87 }
88 if err := os.MkdirAll(root, 0o700); err != nil {
89 return "", "", err
90 }
91 parent, err = os.MkdirTemp(root, "consult-*")
92 if err != nil {
93 return "", "", err
94 }
95 // A name such as ".." must not lead outside parent, which goes with it.
96 base := filepath.Base(name)
97 if base == "." || base == ".." || base == string(filepath.Separator) {
98 base = "workspace"
99 }
100 return parent, filepath.Join(parent, base), nil
101}
102
103// removeConsultSnapshots removes the snapshot of every consult thread in
104// store that no consult is using. Each thread makes a new one on its next
105// turn, at the same path.
106func removeConsultSnapshots(ctx context.Context, store consult.Store, out io.Writer) {
107 threads, err := store.List()
108 if err != nil {
109 fmt.Fprintf(out, " warning: listing consult threads: %v\n", err)
110 return
111 }
112 for name := range threads {
113 dir, err := store.WorkspaceDir(name)
114 if err != nil {
115 continue
116 }
117 if _, err := os.Lstat(dir); err != nil {
118 continue
119 }
120 unlock, err := store.Lock(name)
121 if err != nil {
122 fmt.Fprintf(out, "skipping consult thread %s's snapshot (in use)\n", name)
123 continue
124 }
125 if err := git.RemoveSnapshot(ctx, dir); err != nil {
126 fmt.Fprintf(out, " warning: %v\n", err)
127 } else {
128 fmt.Fprintf(out, "Removed consult thread %s's snapshot\n", name)
129 }
130 unlock()
131 }
132}