differential-stack 0.13.0

Shadow-branch renderer for differential: the diff as a synthetic commit stack
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
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
//! The shadow-branch renderer: the diff rewritten as a synthetic commit stack
//! on a `refs/review/…/stack` ref, one commit per group in reading order —
//! `git log --oneline` alone shows the shape of the change, and skim
//! remainders are skippable on their subject line.
//!
//! Plumbing only (ADR 0011): temp index, hash-object, update-index,
//! write-tree, commit-tree, update-ref. No checkout, no branch switch, no
//! contact with the user's worktree.
//!
//! Every commit's content is computed BY APPLYING HUNKS cumulatively — the
//! final tip tree equalling the head tree is therefore a real assertion that
//! every hunk was carried (invariant 3), backed by an independent per-commit
//! `@@` recount (invariant 4). The exceptions are the same documented ones as
//! the core tree builder: zero-hunk files (binary, mode-only, empty) are
//! staged from recorded oids in a trailing `[meta]` commit.

use std::collections::HashMap;

use differential_engine::schema;

use differential_engine::EngineError;
use differential_engine::apply::apply_hunks;
use differential_engine::invariants::dumb_hunk_count;
use differential_engine::model::DiffView;
use differential_engine::plan::{self, Deferral, Fold, HunkId, reading_split};
use differential_engine::ports::{
    AttributeSource, CommitIdentity, CommitWriter, DiffSource, IndexEntry, IndexSession,
    ObjectReader, ObjectWriter, RangeResolver, RecountSource, RefWriter, TreeBuilder, TreeResolver,
};

/// Ref-name component width (`spec/stack.md`).
///
/// Narrower than `plan::short_oid` on purpose: this one ends up in a ref a
/// human types back, so it is a documented part of the CLI contract rather
/// than a display convenience.
const REF_ABBREV: usize = 7;

/// Who the synthetic commits belong to. Fixed, so a re-run of the same range
/// produces the same shas.
const IDENTITY: CommitIdentity<'static> = CommitIdentity {
    name: "differential",
    email: "differential@localhost",
};

#[derive(Default)]
pub struct StackOptions<'a> {
    /// Ref to land the stack on. Default: `refs/review/<base7>-<head7>/stack`.
    pub ref_name: Option<&'a str>,
}

#[derive(Debug, Clone)]
pub struct StackCommit {
    pub sha: String,
    pub subject: String,
    pub hunks: usize,
}

#[derive(Debug, Clone)]
pub struct StackResult {
    pub ref_name: String,
    pub tip: String,
    pub commits: Vec<StackCommit>,
    pub hunks_carried: usize,
    /// Independent per-commit `@@` recount, summed (invariant 4).
    pub recount: usize,
}

struct PlannedCommit {
    subject: String,
    body: String,
    /// Canonical hunks carried by this commit.
    hunks: Vec<HunkId>,
    /// Zero-hunk file indices carried by this commit (the `[meta]` commit).
    meta_files: Vec<usize>,
}

/// Build and land the stack. Errors (and leaves the ref untouched) if any
/// stack invariant fails.
pub fn build_stack<G>(
    git: &G,
    doc: &schema::PlanDocument,
    view: &DiffView,
    opts: &StackOptions,
) -> Result<StackResult, EngineError>
where
    G: ObjectReader
        + ObjectWriter
        + TreeBuilder
        + CommitWriter
        + TreeResolver
        + RecountSource
        + RefWriter,
{
    let base = &doc.source.base;
    let head = &doc.source.head;
    let mut plan = commit_plan(doc)?;

    // Zero-hunk files (binary, mode-only, empty add/delete) belong to no class
    // and therefore no group; without this commit the tree assertion cannot
    // hold. Staged from recorded oids — the documented tautology.
    let meta_files: Vec<usize> = (0..view.files.len())
        .filter(|&i| view.files[i].hunks.is_empty())
        .collect();
    if !meta_files.is_empty() {
        plan.push(PlannedCommit {
            subject: format!(
                "[meta] {} binary, mode or empty-file changes",
                meta_files.len()
            ),
            body: "Changes that carry no text hunks: binary content, mode-only flips and \
                   empty files. Staged from recorded object ids."
                .to_string(),
            hunks: Vec::new(),
            meta_files,
        });
    }

    // Invariant 2 over the plan: every canonical hunk in exactly one commit.
    let mut seen = vec![false; view.hunks.len()];
    for c in &plan {
        for &h in &c.hunks {
            if seen[h.index()] {
                return Err(EngineError::Invariant(format!(
                    "hunk {h} carried by two commits"
                )));
            }
            seen[h.index()] = true;
        }
    }
    let hunks_carried = seen.iter().filter(|s| **s).count();
    if hunks_carried != view.hunks.len() {
        return Err(EngineError::Invariant(format!(
            "stack plan carries {hunks_carried} hunks, {} exist",
            view.hunks.len()
        )));
    }

    let (commits, tip) = emit(git, base, head, view, &plan)?;

    // Invariant 3: the tip tree, built from applied hunks, equals head's tree.
    let tip_tree = git.tree_of(&tip)?;
    let head_tree = git.tree_of(head)?;
    if tip_tree != head_tree {
        return Err(EngineError::Invariant(format!(
            "stack tip tree {tip_tree} != head tree {head_tree} — a hunk was not carried"
        )));
    }

    // Invariant 4: independent recount over the built commits.
    let mut recount = 0usize;
    let mut parent = base.clone();
    for c in &commits {
        let patch = git.recount_patch(&parent, &c.sha)?;
        recount += dumb_hunk_count(&patch);
        parent = c.sha.clone();
    }
    if recount != view.hunks.len() {
        return Err(EngineError::Invariant(format!(
            "stack recount {recount} != canonical {}",
            view.hunks.len()
        )));
    }

    let ref_name = opts.ref_name.map(str::to_string).unwrap_or_else(|| {
        format!(
            "refs/review/{}-{}/stack",
            &base[..REF_ABBREV.min(base.len())],
            &head[..REF_ABBREV.min(head.len())]
        )
    });
    git.update_ref(&ref_name, &tip)?;

    Ok(StackResult {
        ref_name,
        tip,
        commits,
        hunks_carried,
        recount,
    })
}

/// One commit per group in rank order; skim groups split into exemplars (one
/// hunk per shape class) and a remainder skippable on its subject line.
fn commit_plan(doc: &schema::PlanDocument) -> Result<Vec<PlannedCommit>, EngineError> {
    if doc.groups.is_none() {
        return Err(EngineError::Invariant(
            "stack rendering needs a grouped document (groups is null)".into(),
        ));
    }
    // Which group is the audit's back-fill, and what token names a tier, are
    // both domain answers. They used to be re-derived here — the back-fill
    // test character for character, the token by composing `effort_name`
    // itself — which is exactly the drift `plan::ReviewView` exists to stop.
    //
    // The projection is now the ONLY handle: it carries the prose too, so the
    // raw `schema::Group` and the `PlanIndex` beside it are both gone.
    let review = plan::ReviewView::project(doc)?;
    let mut plan = Vec::new();

    for g in &review.groups {
        // Always folded: the stack's way of unfolding a skim group is the
        // [skim 2/2] commit that follows it.
        let split = reading_split(&review, g, Fold::Folded);
        let body = format!("{}\n\n{}", g.description, g.reason);
        // `unclassified` for the back-fill, the tier's own name otherwise.
        let tier = review.tier_name(g);

        match split.deferral {
            Deferral::None if g.unclassified => plan.push(PlannedCommit {
                subject: format!("[{tier}] {} hunks carried by no group", split.shown.len()),
                body,
                hunks: split.shown,
                meta_files: Vec::new(),
            }),
            Deferral::None if g.effort == schema::Effort::Skim => plan.push(PlannedCommit {
                subject: format!("[{tier}] {} — {} exemplars", g.label, split.shown.len()),
                body: format!("{body}\n\nEvery shape class in this group is a singleton."),
                hunks: split.shown,
                meta_files: Vec::new(),
            }),
            Deferral::None => plan.push(PlannedCommit {
                subject: format!("[{tier}] {}", g.label),
                body,
                hunks: split.shown,
                meta_files: Vec::new(),
            }),
            Deferral::FoldedNoise => plan.push(PlannedCommit {
                subject: format!(
                    "[noise] {} — folded, {} hunks",
                    g.label,
                    split.deferred.len()
                ),
                body,
                // A folded group still carries every hunk: what a reviewer is
                // asked to read never decides what the commit contains.
                hunks: split.all(),
                meta_files: Vec::new(),
            }),
            Deferral::SkimRemainder => {
                plan.push(PlannedCommit {
                    subject: format!("[skim 1/2] {} — {} exemplars", g.label, split.shown.len()),
                    body: format!(
                        "{body}\n\nOne hunk per shape class. {} further hunks follow in \
                         [skim 2/2].",
                        split.deferred.len()
                    ),
                    hunks: split.shown,
                    meta_files: Vec::new(),
                });
                plan.push(PlannedCommit {
                    subject: format!(
                        "[skim 2/2] {} — {} further hunks, same shapes",
                        g.label,
                        split.deferred.len()
                    ),
                    body: "Remaining members of the shapes verified in [skim 1/2]. \
                           Skippable on this subject line."
                        .to_string(),
                    hunks: split.deferred,
                    meta_files: Vec::new(),
                });
            }
        }
    }
    Ok(plan)
}

/// Cumulative emission over a temporary index.
fn emit<G>(
    git: &G,
    base: &str,
    head: &str,
    view: &DiffView,
    plan: &[PlannedCommit],
) -> Result<(Vec<StackCommit>, String), EngineError>
where
    G: ObjectReader + ObjectWriter + TreeBuilder + CommitWriter,
{
    let mut session = git.begin_from_tree(base)?;

    let mut applied: HashMap<usize, Vec<usize>> = HashMap::new();
    let mut base_blobs: HashMap<usize, Option<Vec<u8>>> = HashMap::new();
    let mut parent = base.to_string();
    let mut commits = Vec::with_capacity(plan.len());
    let trailer = format!(
        "Review-Synthetic: {}..{}",
        plan::short_oid(base),
        plan::short_oid(head)
    );

    for c in plan {
        let mut touched: Vec<usize> = c
            .hunks
            .iter()
            .map(|&h| view.hunks[h.index()].file)
            .collect();
        touched.sort_unstable();
        touched.dedup();
        for &h in &c.hunks {
            applied
                .entry(view.hunks[h.index()].file)
                .or_default()
                .push(h.index());
        }

        let mut entries: Vec<IndexEntry> = Vec::new();
        for &fi in &touched {
            entries.push(stage_file(git, base, view, fi, &applied, &mut base_blobs)?);
        }
        for &fi in &c.meta_files {
            // What a zero-hunk file contributes is a domain rule, and this
            // loop used to state it again in its own words — deletion, then
            // the recorded mode and oid, then the same two error strings.
            let f = &view.files[fi];
            entries.push(IndexEntry::from_staged(
                plan::zero_hunk_state(f)?,
                f.path.clone(),
                // The rule never answers `Apply` for a zero-hunk file, and an
                // error says so where a panic would only assert it.
                || {
                    Err(EngineError::Invariant(format!(
                        "zero-hunk file {} was asked to apply hunks it has none of",
                        String::from_utf8_lossy(&f.path)
                    )))
                },
            )?);
        }
        session.stage(&entries)?;

        let tree = session.write_tree()?;
        let msg = format!("{}\n\n{}\n\n{}\n", c.subject, c.body, trailer);
        // The synthetic identity is the renderer's policy, expressed as data
        // rather than as an environment the whole session inherits.
        let sha = git.commit_tree(&tree, &parent, msg.as_bytes(), IDENTITY)?;
        commits.push(StackCommit {
            sha: sha.clone(),
            subject: c.subject.clone(),
            hunks: c.hunks.len(),
        });
        parent = sha;
    }
    Ok((commits, parent))
}

/// Stage one file's cumulative state: full-application deletions become
/// removals; everything else is content computed by applying the hunks seen so
/// far (submodules become gitlinks from the pseudo-hunk's commit id).
fn stage_file<G>(
    git: &G,
    base: &str,
    view: &DiffView,
    fi: usize,
    applied: &HashMap<usize, Vec<usize>>,
    base_blobs: &mut HashMap<usize, Option<Vec<u8>>>,
) -> Result<IndexEntry, EngineError>
where
    G: ObjectReader + ObjectWriter,
{
    let f = &view.files[fi];
    let applied_here = applied.get(&fi).map_or(0, Vec::len);

    IndexEntry::from_staged(
        plan::cumulative_state(f, applied_here)?,
        f.path.clone(),
        || {
            if let std::collections::hash_map::Entry::Vacant(e) = base_blobs.entry(fi) {
                e.insert(git.blob(base, &f.path)?);
            }
            let hunks: Vec<&differential_engine::model::Hunk> = applied
                .get(&fi)
                .map(|v| v.iter().map(|&h| &view.hunks[h]).collect())
                .unwrap_or_default();
            let content = apply_hunks(base_blobs[&fi].as_deref(), &hunks);
            git.write_blob(&content)
        },
    )
}

/// Output of the full stack pipeline.
pub struct StackOutput {
    pub pipeline: differential_engine::PipelineOutput,
    /// `None` iff invariants failed upstream (no document, nothing rendered).
    pub stack: Option<StackResult>,
}

/// Full production path for the shadow-branch renderer: grouped pipeline
/// (core -> group -> order, in the engine) -> commit stack.
pub fn run_stack_pipeline<G, C, A>(
    git: &G,
    source: &plan::ReviewSource,
    config: &differential_engine::config::Config,
    langs: &differential_engine::lang::LanguageRegistry,
    symbols: &differential_engine::artefact::symbols::SymbolReaders,
    grouping: &differential_engine::grouping::GroupingOptions<C, A>,
    stack: &StackOptions,
) -> Result<StackOutput, EngineError>
where
    G: ObjectReader
        + ObjectWriter
        + TreeBuilder
        + CommitWriter
        + TreeResolver
        + RecountSource
        + RefWriter
        + RangeResolver
        + DiffSource
        + AttributeSource,
    C: differential_engine::ports::GroupingCache,
    A: differential_engine::ports::ArtefactStore,
{
    let mut out =
        differential_engine::run_grouped_pipeline(git, source, config, langs, symbols, grouping)?;
    // Invariants 3 and 4. This renderer is the reason they exist: its commits
    // are trees built from these hunks, so the tree assertion is about exactly
    // the path taken below. The engine's pipeline no longer runs them, because
    // a consumer that never builds a tree is not protected by them.
    differential_engine::verify(git, &mut out)?;
    if !out.report.all_ok() {
        return Ok(StackOutput {
            pipeline: out,
            stack: None,
        });
    }
    let Some(doc) = &out.document else {
        return Ok(StackOutput {
            pipeline: out,
            stack: None,
        });
    };
    let result = build_stack(git, doc, &out.view, stack)?;
    Ok(StackOutput {
        pipeline: out,
        stack: Some(result),
    })
}