wsb 0.258.6441

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! What one `wsb update` run may look up more than once.
//!
//! `wsb update --recursive` is not one update. It is one update per repository
//! in the nesting tree — 34 of them in this workspace — and each of those,
//! today, starts from nothing: it walks the whole workspace for `.wsb/state.json`
//! files, walks it again for `.wstemplate` files, reads and parses every
//! template to ask whether it names this project, counts the commits of every
//! repository under it, and asks the network for the tags of every project a
//! template pins. Thirty-four times over, for answers that were the same every
//! time.
//!
//! This is where those answers are kept for the length of a run.
//!
//! # What may be cached, and what may not
//!
//! The rule is the whole design: **an answer may be cached only if nothing an
//! update does can change it.** An update writes files. So:
//!
//! | Answer | Cached | Why |
//! |---|---|---|
//! | `.wsb/state.json` alias map | yes | see below |
//! | `.wstemplate` file lists | yes | an update writes a template's OUTPUT, never a template |
//! | template text and its references | yes | same reason |
//! | a repository's submodules | yes | an update does not add or remove one |
//! | commit counts, tree-change counts | yes | an update does not commit |
//! | tags, `git describe`, HEAD | yes | an update does not move a ref — unless `--tag` says to, and then [`RunCache::forget_repo`] is called |
//! | remotes, remote URLs, remote tags | yes | an update does not push |
//! | **whether a worktree is clean** | **no** | an update writes into worktrees; this is the one fact it changes |
//! | **a project's `version.txt`** | **no** | an update writes it, and a later repository in the run has to read what the earlier one wrote |
//!
//! The last two rows are why `--recursive` renders a template once for its
//! owner and again for every project it names: a manifest is rendered before
//! the project it pins has been updated, and rendered again afterwards against
//! the version.txt that update wrote. Caching either of them would collapse
//! that into one answer and change what lands on disk.
//!
//! The alias map deserves its own sentence, because an update DOES rewrite the
//! state files it is read from. A project that registers an explicit alias and
//! scan root is written back with the same two, so the map it contributes to
//! cannot change. A project that registers neither has its alias derived, and
//! saving can change it — a repository that was MOVED still records the old
//! path until its own update rewrites it — but such a project's scan root is
//! itself, so the map it reads is keyed to it alone and is shared with nobody.
//! Either way, no repository in a run can be handed a map another repository's
//! update invalidated.
//!
//! The two claims that an update CAN break are not left as comments.
//! [`RunCache::forget_repo`] and [`RunCache::note_written`] drop what a claim
//! stops covering — so a workspace that breaks one gets the slow answer rather
//! than the wrong one.
//!
//! # Using one, or not
//!
//! A cache is not a mode. `RunCache::new()` handed to a single update is the
//! behaviour of a run that caches nothing, because nothing is ever asked twice;
//! that is exactly what the non-recursive `wsb update` does, and what
//! `--verify` re-runs each repository against to prove the shared one agrees.

use crate::prelude::*;
use crate::state::template_refs::TemplateReferences;
use std::collections::BTreeMap;
use std::collections::HashMap as Map;
use std::hash::Hash;
use std::sync::Mutex;

/// HEAD as one answer, so a repository is opened and read once per run.
///
/// The shape follows [`crate::state::project_facts`]'s use of it: whether HEAD
/// is detached decides whether there is a branch to name, and a repository with
/// no commits has no commit fields at all.
#[derive(Debug, Clone)]
pub struct HeadSnapshot {
    pub detached: bool,
    /// The branch name, when HEAD is attached to one.
    pub branch: Option<String>,
    /// `(full hex id, seconds since epoch, timezone offset in seconds)`, absent
    /// when the repository has no commits yet.
    pub commit: Option<(String, i64, i32)>,
}

/// Everything one run has already answered, and the variables it renders
/// under.
///
/// Every field but `environment` is a memo keyed by the thing it describes,
/// and none of those is authoritative: dropping them changes how long a run
/// takes and nothing else. `environment` is what the memos were answered
/// UNDER — a render with a variable set produces different text — which is
/// why a run with different variables is a different cache.
#[derive(Default)]
pub struct RunCache {
    /// Variables templates see set for this run, over the process's own.
    ///
    /// How a render is produced as if a variable were set without setting it
    /// for the whole process: the environment is shared by every thread, and
    /// a read-only query that set and restored a variable changed what every
    /// concurrent render saw while it ran.
    environment: BTreeMap<String, String>,
    /// `scan_root` → `alias → project root`.
    project_roots: Mutex<Map<PathBuf, Arc<HashMap<String, PathBuf>>>>,
    /// `root` → the `.wstemplate` files under it, as the ignore rules there see them.
    templates: Mutex<Map<PathBuf, Arc<Vec<PathBuf>>>>,
    /// `template path` → its text.
    template_text: Mutex<Map<PathBuf, Arc<String>>>,
    /// `template path` → what that text refers to.
    template_refs: Mutex<Map<PathBuf, Arc<TemplateReferences>>>,
    /// `project root` → the absolute paths of its submodules.
    submodule_roots: Mutex<Map<PathBuf, Arc<Vec<PathBuf>>>>,
    /// `(repo root, major)` → its part of a version: (commits, changes since
    /// its last release), counted for the commit that holds its tree.
    version_counts: Mutex<Map<(PathBuf, u32), (u32, u32)>>,
    /// The repository whose commit this run renders for — the pre-commit
    /// hook's `wsb update --git-add` — whose version is the one it will have
    /// once the commit lands, whatever the commit holds.
    committing: Mutex<Option<PathBuf>>,
    /// `repo root` → tags in the clone, newest first, as the repository OPENED
    /// at that path reports them.
    tags: Mutex<Map<PathBuf, Arc<Vec<String>>>>,
    /// The same, for the version calculation, which DISCOVERS the repository
    /// containing a path instead of insisting the path is one. A separate memo
    /// because it is a separate question: for a directory that is not a
    /// repository the two have different answers, and one key serving both
    /// would hand a project its parent's tags.
    discovered_tags: Mutex<Map<PathBuf, Arc<Vec<String>>>>,
    /// `repo root` → the newest tag reachable from HEAD.
    nearest_tags: Mutex<Map<PathBuf, Arc<Option<String>>>>,
    /// `repo root` → HEAD.
    heads: Mutex<Map<PathBuf, Arc<HeadSnapshot>>>,
    /// `repo root` → configured remote names, or the reason there are none.
    remotes: Mutex<Map<PathBuf, Arc<std::result::Result<Vec<String>, String>>>>,
    /// `(repo root, remote)` → that remote's fetch URL.
    remote_urls: Mutex<Map<(PathBuf, String), Arc<String>>>,
    /// `(repo root, remote)` → the tags on that remote. The only entry a
    /// network round trip pays for, and the reason a run went to GitHub 48
    /// times for 7 projects.
    remote_tags: Mutex<Map<(PathBuf, String), Arc<Vec<String>>>>,
}

impl RunCache {
    /// A cache that has answered nothing yet.
    pub fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    /// A cache that has answered nothing yet, for a run that renders with
    /// `variable` set to `value`.
    pub fn with_variable(variable: &str, value: &str) -> Arc<Self> {
        Arc::new(Self {
            environment: BTreeMap::from([(variable.to_string(), value.to_string())]),
            ..Self::default()
        })
    }

    /// The variables this run renders under, over the process's own.
    pub fn environment(&self) -> &BTreeMap<String, String> {
        &self.environment
    }

    // ── Reads ────────────────────────────────────────────────────────────────

    /// The `alias → project root` map for `scan_root`.
    pub fn project_roots(
        &self,
        scan_root: &Path,
        compute: impl FnOnce() -> Result<HashMap<String, PathBuf>>,
    ) -> Result<Arc<HashMap<String, PathBuf>>> {
        memo(&self.project_roots, scan_root.to_path_buf(), || {
            compute().map(Arc::new)
        })
    }

    /// The `.wstemplate` files under `root`.
    pub fn templates_under(
        &self,
        root: &Path,
        compute: impl FnOnce() -> Result<Vec<PathBuf>>,
    ) -> Result<Arc<Vec<PathBuf>>> {
        memo(&self.templates, root.to_path_buf(), || {
            compute().map(Arc::new)
        })
    }

    /// One template's text.
    pub fn template_text(
        &self,
        path: &Path,
        compute: impl FnOnce() -> Result<String>,
    ) -> Result<Arc<String>> {
        memo(&self.template_text, path.to_path_buf(), || {
            compute().map(Arc::new)
        })
    }

    /// What one template's text refers to.
    pub fn template_references(
        &self,
        path: &Path,
        compute: impl FnOnce() -> Result<TemplateReferences>,
    ) -> Result<Arc<TemplateReferences>> {
        memo(&self.template_refs, path.to_path_buf(), || {
            compute().map(Arc::new)
        })
    }

    /// The absolute paths of `project_root`'s submodules.
    ///
    /// Infallible at the call site — a directory that is not a repository has
    /// none — so this takes a plain closure.
    pub fn submodule_roots(
        &self,
        project_root: &Path,
        compute: impl FnOnce() -> Vec<PathBuf>,
    ) -> Arc<Vec<PathBuf>> {
        memo(&self.submodule_roots, project_root.to_path_buf(), || {
            Ok(Arc::new(compute()))
        })
        .expect("an infallible closure cannot fail")
    }

    /// Say that this run renders for a commit being made in `repo_root`.
    pub fn commit_is_being_made_in(&self, repo_root: &Path) {
        *self.committing.lock().expect("the run cache is unusable after a panic") = Some(place(repo_root));
    }

    /// Whether this run renders for a commit being made in `repo_root`.
    pub fn is_being_committed(&self, repo_root: &Path) -> bool {
        self.committing
            .lock()
            .expect("the run cache is unusable after a panic")
            .as_deref()
            .is_some_and(|committing| committing == place(repo_root))
    }

    /// A repository's part of a version: its commits and its changes since its
    /// last release, for the commit that holds its tree
    /// (`state_common::version_counts`).
    pub fn version_counts(
        &self,
        repo_root: &Path,
        major: u32,
        compute: impl FnOnce() -> Result<(u32, u32)>,
    ) -> Result<(u32, u32)> {
        memo(&self.version_counts, (repo_root.to_path_buf(), major), compute)
    }

    /// `repo_root`'s tags, newest first.
    pub fn tags(
        &self,
        repo_root: &Path,
        compute: impl FnOnce() -> Result<Vec<String>>,
    ) -> Result<Arc<Vec<String>>> {
        memo(&self.tags, repo_root.to_path_buf(), || compute().map(Arc::new))
    }

    /// The tags of the repository CONTAINING `path`, newest first.
    pub fn discovered_tags(
        &self,
        path: &Path,
        compute: impl FnOnce() -> Result<Vec<String>>,
    ) -> Result<Arc<Vec<String>>> {
        memo(&self.discovered_tags, path.to_path_buf(), || {
            compute().map(Arc::new)
        })
    }

    /// The newest tag reachable from `repo_root`'s HEAD.
    pub fn nearest_tag(
        &self,
        repo_root: &Path,
        compute: impl FnOnce() -> Result<Option<String>>,
    ) -> Result<Arc<Option<String>>> {
        memo(&self.nearest_tags, repo_root.to_path_buf(), || compute().map(Arc::new))
    }

    /// `repo_root`'s HEAD.
    pub fn head(
        &self,
        repo_root: &Path,
        compute: impl FnOnce() -> Result<HeadSnapshot>,
    ) -> Result<Arc<HeadSnapshot>> {
        memo(&self.heads, repo_root.to_path_buf(), || compute().map(Arc::new))
    }

    /// `repo_root`'s configured remotes, or the reason it has none usable.
    pub fn remotes(
        &self,
        repo_root: &Path,
        compute: impl FnOnce() -> std::result::Result<Vec<String>, String>,
    ) -> Arc<std::result::Result<Vec<String>, String>> {
        memo(&self.remotes, repo_root.to_path_buf(), || {
            Ok(Arc::new(compute()))
        })
        .expect("an infallible closure cannot fail")
    }

    /// The fetch URL of one remote.
    pub fn remote_url(
        &self,
        repo_root: &Path,
        remote: &str,
        compute: impl FnOnce() -> Result<String>,
    ) -> Result<Arc<String>> {
        let key = (repo_root.to_path_buf(), remote.to_string());
        memo(&self.remote_urls, key, || compute().map(Arc::new))
    }

    /// The tags on one remote — the answer a network round trip pays for.
    pub fn remote_tags(
        &self,
        repo_root: &Path,
        remote: &str,
        compute: impl FnOnce() -> Result<Vec<String>>,
    ) -> Result<Arc<Vec<String>>> {
        let key = (repo_root.to_path_buf(), remote.to_string());
        memo(&self.remote_tags, key, || compute().map(Arc::new))
    }

    // ── What stops being true ────────────────────────────────────────────────

    /// Forget everything remembered about one repository.
    ///
    /// Called where a run moves a ref — `wsb update --tag` creates one, and
    /// every tag fact of that repository is a different answer from the moment
    /// it does. What is dropped here is asked again, and the answer kept is the
    /// one taken after the ref moved.
    pub fn forget_repo(&self, repo_root: &Path) {
        let root = repo_root.to_path_buf();
        drop_key(&self.tags, &root);
        drop_key(&self.discovered_tags, &root);
        drop_key(&self.nearest_tags, &root);
        drop_key(&self.heads, &root);
        drop_key(&self.remotes, &root);
        if let Ok(mut map) = self.version_counts.lock() {
            map.retain(|(path, _), _| path != &root);
        }
        if let Ok(mut map) = self.remote_urls.lock() {
            map.retain(|(path, _), _| path != &root);
        }
        if let Ok(mut map) = self.remote_tags.lock() {
            map.retain(|(path, _), _| path != &root);
        }
    }

    /// Take note that a render wrote `path`.
    ///
    /// One output can stop a claim from holding: a template that renders
    /// another template. Nothing in this workspace does it, and the answer to a
    /// workspace that does is to forget the template lists rather than to serve
    /// the ones read before the file existed.
    pub fn note_written(&self, path: &Path) {
        let is_template = path
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.ends_with(".wstemplate"));
        if !is_template {
            return;
        }
        clear(&self.templates);
        clear(&self.template_text);
        clear(&self.template_refs);
    }



}

/// Read a memo, or compute and keep the answer.
///
/// The lock is never held across `compute`: a closure here opens repositories
/// and talks to the network, and holding a cache lock through that is how a
/// cache becomes the thing that is slow.
///
/// An error is not kept. A run that fails to read a repository fails the same
/// way the next time it asks, which is what it did before there was a cache.
fn memo<K, V>(slot: &Mutex<Map<K, V>>, key: K, compute: impl FnOnce() -> Result<V>) -> Result<V>
where
    K: Eq + Hash + Clone,
    V: Clone,
{
    // A poisoned lock means a panic somewhere that held it. Nothing here is
    // authoritative, so the answer is to compute rather than to guess — and the
    // run is correct, just no faster than it was.
    if let Ok(map) = slot.lock() {
        if let Some(hit) = map.get(&key) {
            return Ok(hit.clone());
        }
    }
    let value = compute()?;
    if let Ok(mut map) = slot.lock() {
        map.insert(key, value.clone());
    }
    Ok(value)
}

fn drop_key<V>(slot: &Mutex<Map<PathBuf, V>>, key: &Path) {
    if let Ok(mut map) = slot.lock() {
        map.remove(key);
    }
}

fn clear<K, V>(slot: &Mutex<Map<K, V>>) {
    if let Ok(mut map) = slot.lock() {
        map.clear();
    }
}

/// A path as the place it names, so two spellings of one repository agree.
fn place(path: &Path) -> PathBuf {
    crate::platform::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}