gunk-dev/the-valley

consult_snapshot.go

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