jj-fork 0.2.8

Maintain a fork as jj series and glues on top of upstream, and keep them current
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Configuration. Repository facts live in a committed `.jj-fork.toml` so every clone, CI job,
//! and agent sandbox sees them. Personal overrides live in jj config under `jj-fork.*` with the
//! same structure, for example `jj config set --user jj-fork.fork.remote mine`. They are read
//! from jj's effective configuration in-process, never by listing it. The whole effective config
//! serializes, so a saved plan can fingerprint it.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

pub const FILE_NAME: &str = ".jj-fork.toml";
/// Copy kept inside the jj repository, so checkouts of bookmarks without the committed file (a
/// series is rooted on upstream) still find the configuration.
const STORED: &str = ".jj/repo/jj-fork.toml";

/// Chooses the configuration file: an explicit path, else the stored copy of the committed file
/// (refreshed from it), else the committed file. Using the stored copy everywhere makes saved
/// plans independent of which checkout created or applies them; its content is fingerprinted.
pub fn resolve_path(root: &Path, explicit: Option<&Path>) -> PathBuf {
    if let Some(path) = explicit {
        return path.to_path_buf();
    }
    let tracked = root.join(FILE_NAME);
    let stored = root.join(STORED);
    if tracked.exists() {
        store_copy(root);
    }
    if !tracked.exists() && !stored.exists() {
        seed_from_refs(root);
    }
    if stored.exists() { stored } else { tracked }
}

/// A checkout without the committed file and without a stored copy (such as a series checkout in
/// a new clone) takes the file from a ref that contains it, preferring the fork branch.
fn seed_from_refs(root: &Path) {
    if !root.join(".jj/repo").is_dir() {
        return;
    }
    let Ok(refs) = crate::run::output(
        root,
        "git",
        &[
            "for-each-ref",
            "--format=%(refname)",
            "refs/remotes",
            "refs/heads",
        ],
    ) else {
        return;
    };
    let mut candidates: Vec<&str> = refs
        .lines()
        .filter(|r| {
            crate::run::succeeds(
                root,
                "git",
                &["cat-file", "-e", &format!("{r}:{FILE_NAME}")],
            )
            .unwrap_or(false)
        })
        .collect();
    candidates.sort_by_key(|r| (!r.ends_with("/fork/main"), r.to_string()));
    for r in candidates {
        if let Ok(text) =
            crate::run::output_all(root, "git", &["show", &format!("{r}:{FILE_NAME}")])
        {
            if std::fs::write(root.join(STORED), text).is_ok() {
                crate::progress(&format!(
                    "no {FILE_NAME} in this checkout; using the copy on {r}"
                ));
            }
            return;
        }
    }
}

/// Best effort: a read-only repository directory only loses the fallback.
pub fn store_copy(root: &Path) {
    let tracked = root.join(FILE_NAME);
    let stored = root.join(STORED);
    if !root.join(".jj/repo").is_dir() {
        return;
    }
    if let Ok(text) = std::fs::read(&tracked)
        && std::fs::read(&stored).ok().as_deref() != Some(text.as_slice())
    {
        let _ = std::fs::write(&stored, text);
    }
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub upstream: Upstream,
    #[serde(default)]
    pub fork: Fork,
    #[serde(default)]
    pub checks: Checks,
    #[serde(default)]
    pub generated: Option<Generated>,
    #[serde(default)]
    pub tiers: Tiers,
    #[serde(default)]
    pub low_memory: LowMemory,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Upstream {
    #[serde(default = "default_upstream_remote")]
    pub remote: String,
    pub url: String,
    #[serde(default = "default_main")]
    pub branch: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Fork {
    #[serde(default = "default_origin")]
    pub remote: String,
    /// The generated branch that merges upstream with every patch.
    #[serde(default = "default_fork_branch")]
    pub branch: String,
    /// Bookmark prefixes of the series rooted on upstream, for example `patch/` for features
    /// meant for upstream and `tooling/` for fork-only tooling.
    #[serde(default = "default_series_prefixes")]
    pub series_prefixes: Vec<String>,
    /// Bookmark prefix of glues: merges of two or more series that hold only their resolution.
    #[serde(default = "default_glue_prefix")]
    pub glue_prefix: String,
    /// A fork branch that mirrors upstream and is fast-forwarded on sync, if any.
    #[serde(default)]
    pub mirror_branch: Option<String>,
    #[serde(default = "default_merge_message")]
    pub merge_message: String,
}

impl Default for Fork {
    fn default() -> Self {
        Fork {
            remote: default_origin(),
            branch: default_fork_branch(),
            series_prefixes: default_series_prefixes(),
            glue_prefix: default_glue_prefix(),
            mirror_branch: None,
            merge_message: default_merge_message(),
        }
    }
}

#[derive(Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Checks {
    /// Checks for each stale patch, run on its replay onto the upstream target.
    #[serde(default)]
    pub patch: Vec<Check>,
    /// Checks for a fork-branch candidate, run before the branch moves.
    #[serde(default)]
    pub fork: Vec<Check>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Check {
    pub name: String,
    /// Shell command. Placeholders: `{go_packages}` (Go package directories the patch changes),
    /// `{jobs}` (parallelism, lower on small machines).
    pub run: String,
    /// Skip the check when this placeholder expands to nothing.
    #[serde(default)]
    pub when: Option<String>,
    #[serde(default)]
    pub kind: CheckKind,
    /// Difficulty tier of a fixer when this check fails on a patch.
    #[serde(default = "default_tier")]
    pub tier: String,
    /// Lower the tier to `low` when the log has at most this many compiler errors.
    #[serde(default)]
    pub low_if_errors_at_most: Option<usize>,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum CheckKind {
    /// Any failure fails the check.
    #[default]
    Command,
    /// `go test` output: failing tests are retried, then compared with upstream. Tests that
    /// also fail on upstream are reported and ignored.
    GoTest,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Generated {
    /// Globs of generated files. They are regenerated instead of merged and do not count
    /// toward conflict difficulty.
    pub paths: Vec<String>,
    /// Command that regenerates every generated file.
    pub regenerate: String,
    /// Patch checks regenerate only when the patch changes files matching these globs.
    #[serde(default)]
    pub inputs: Vec<String>,
    /// License header to restore on regenerated files whose generator drops it.
    #[serde(default)]
    pub header: Option<Header>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Header {
    pub text: String,
    /// Comment prefix by file extension, for example `{ go = "//", yaml = "#" }`.
    pub comments: BTreeMap<String, String>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Tiers {
    #[serde(default = "default_low_max")]
    pub low_max: Limits,
    #[serde(default = "default_medium_max")]
    pub medium_max: Limits,
}

impl Default for Tiers {
    fn default() -> Self {
        Tiers {
            low_max: default_low_max(),
            medium_max: default_medium_max(),
        }
    }
}

/// Upper bounds for a conflict tier, measured at the first conflicting commit.
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Limits {
    pub files: usize,
    pub hunks: usize,
    pub lines: usize,
    pub commits: usize,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct LowMemory {
    /// Machines with less memory than this run with reduced parallelism.
    #[serde(default = "default_low_memory_mib")]
    pub below_mib: u64,
    /// Environment for check commands on small machines. Placeholders: `{jobs}`,
    /// `{memory_limit_mib}` (60% of physical memory).
    #[serde(default)]
    pub env: BTreeMap<String, String>,
}

impl Default for LowMemory {
    fn default() -> Self {
        LowMemory {
            below_mib: default_low_memory_mib(),
            env: BTreeMap::new(),
        }
    }
}

fn default_upstream_remote() -> String {
    "upstream".into()
}
fn default_origin() -> String {
    "origin".into()
}
fn default_main() -> String {
    "main".into()
}
fn default_fork_branch() -> String {
    "fork/main".into()
}
fn default_series_prefixes() -> Vec<String> {
    vec!["patch/".into()]
}
fn default_glue_prefix() -> String {
    "glue/".into()
}
fn default_merge_message() -> String {
    "internal: assemble fork patch set".into()
}
fn default_tier() -> String {
    "medium".into()
}
fn default_low_max() -> Limits {
    Limits {
        files: 2,
        hunks: 3,
        lines: 60,
        commits: 3,
    }
}
fn default_medium_max() -> Limits {
    Limits {
        files: 12,
        hunks: 20,
        lines: 400,
        commits: 10,
    }
}
fn default_low_memory_mib() -> u64 {
    8192
}

impl Config {
    /// Loads the committed config, then applies `jj-fork.*` overrides from the effective jj
    /// config (user, repo, workspace, and environment layers, as jj resolves them), and checks
    /// that the bookmark roles are unambiguous.
    pub fn load(root: &Path, path: Option<&Path>) -> Result<Config> {
        let path = resolve_path(root, path);
        let text = std::fs::read_to_string(&path).with_context(|| {
            format!(
                "failed to read {}; run `jj fork init` first",
                path.display()
            )
        })?;
        let mut table: toml::Table =
            toml::from_str(&text).with_context(|| format!("invalid {}", path.display()))?;
        let jj = crate::native::jj_config::load(root, crate::native::CommandContext::Fork)?;
        if let Some(overrides) = crate::native::jj_config::fork_overrides(&jj)? {
            merge(&mut table, overrides);
        }
        let config: Config = toml::Value::Table(table)
            .try_into()
            .with_context(|| format!("invalid configuration in {} or jj config", path.display()))?;
        config.validate()?;
        Ok(config)
    }

    /// Every bookmark must have exactly one role: fork branch, mirror, series, or glue.
    pub fn validate(&self) -> Result<()> {
        let fork = &self.fork;
        if fork.series_prefixes.is_empty() {
            bail!("fork.series_prefixes must name at least one prefix");
        }
        if fork.series_prefixes.iter().any(String::is_empty) || fork.glue_prefix.is_empty() {
            bail!("series and glue prefixes must not be empty");
        }
        let mut seen = std::collections::BTreeSet::new();
        for prefix in &fork.series_prefixes {
            if !seen.insert(prefix) {
                bail!("fork.series_prefixes lists {prefix} twice");
            }
            if prefix.starts_with(&fork.glue_prefix)
                || fork.glue_prefix.starts_with(prefix.as_str())
            {
                bail!(
                    "series prefix {prefix} and glue prefix {} overlap, so a bookmark could be both",
                    fork.glue_prefix
                );
            }
        }
        let roles: Vec<&str> = fork
            .series_prefixes
            .iter()
            .chain(std::iter::once(&fork.glue_prefix))
            .map(String::as_str)
            .collect();
        let mut named = vec![("fork.branch", fork.branch.as_str())];
        named.extend(
            fork.mirror_branch
                .as_deref()
                .map(|m| ("fork.mirror_branch", m)),
        );
        for (key, name) in &named {
            if name.is_empty() {
                bail!("{key} must not be empty");
            }
            if let Some(prefix) = roles.iter().find(|p| name.starts_with(**p)) {
                bail!("{key} {name} is inside the series or glue prefix {prefix}");
            }
        }
        if fork.mirror_branch.as_deref() == Some(fork.branch.as_str()) {
            bail!("fork.mirror_branch must differ from fork.branch");
        }
        if fork.remote == self.upstream.remote {
            bail!(
                "fork.remote and upstream.remote are both {}; they must be different remotes",
                fork.remote
            );
        }
        if self.upstream.branch.is_empty() {
            bail!("upstream.branch must not be empty");
        }
        Ok(())
    }

    pub fn upstream_ref(&self) -> String {
        format!("{}@{}", self.upstream.branch, self.upstream.remote)
    }
}

fn merge(base: &mut toml::Table, overrides: toml::Table) {
    for (key, value) in overrides {
        match (base.get_mut(&key), value) {
            (Some(toml::Value::Table(base)), toml::Value::Table(value)) => merge(base, value),
            (_, value) => {
                base.insert(key, value);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(text: &str) -> Config {
        toml::from_str(text).unwrap()
    }

    #[test]
    fn overrides_merge_into_nested_tables() {
        let mut base: toml::Table = toml::from_str(
            "[upstream]\nurl = \"a\"\nbranch = \"main\"\n[fork]\nremote = \"origin\"\n",
        )
        .unwrap();
        let overrides: toml::Table = toml::from_str(
            "fork.remote = \"mine\"\nupstream.branch = \"trunk\"\nchecks.fork = [{ name = \"t\", run = \"true\" }]\n",
        )
        .unwrap();
        merge(&mut base, overrides);
        let config: Config = toml::Value::Table(base).try_into().unwrap();
        assert_eq!(config.fork.remote, "mine");
        assert_eq!(config.upstream.branch, "trunk");
        assert_eq!(config.upstream.url, "a");
        assert_eq!(config.fork.branch, "fork/main");
        assert_eq!(config.checks.fork.len(), 1);
    }

    #[test]
    fn roles_must_not_overlap() {
        assert!(parse("[upstream]\nurl = 'u'\n").validate().is_ok());
        let cases = [
            ("[fork]\nseries_prefixes = []", "at least one"),
            ("[fork]\nseries_prefixes = ['']", "not be empty"),
            ("[fork]\nglue_prefix = ''", "not be empty"),
            ("[fork]\nseries_prefixes = ['patch/', 'patch/']", "twice"),
            (
                "[fork]\nseries_prefixes = ['g']\nglue_prefix = 'glue/'",
                "overlap",
            ),
            ("[fork]\nseries_prefixes = ['glue/x/']", "overlap"),
            ("[fork]\nbranch = 'patch/fork'", "inside"),
            ("[fork]\nmirror_branch = 'glue/main'", "inside"),
            ("[fork]\nmirror_branch = 'fork/main'", "differ"),
            ("[fork]\nremote = 'upstream'", "different remotes"),
        ];
        for (text, message) in cases {
            let err = parse(&format!("[upstream]\nurl = 'u'\n{text}\n"))
                .validate()
                .unwrap_err()
                .to_string();
            assert!(err.contains(message), "{text}: {err}");
        }
    }

    #[test]
    fn effective_config_serializes() {
        let config = parse(
            "[upstream]\nurl = 'u'\n[checks]\nfork = [{ name = 't', run = 'true', kind = 'go-test' }]\n",
        );
        let json = serde_json::to_value(&config).unwrap();
        assert_eq!(json["fork"]["branch"], "fork/main");
        assert_eq!(json["checks"]["fork"][0]["kind"], "go-test");
    }

    #[test]
    fn unknown_keys_are_rejected() {
        let result: Result<Config, _> = toml::from_str("[upstream]\nurl = \"a\"\nbrnach = \"x\"\n");
        assert!(result.is_err());
    }
}