jj-hooks 0.3.4

Run pre-commit / lefthook / hk hooks against jj bookmark pushes
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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Tests for the multi-bookmark batch entrypoints
//! [`run_for_updates_parallel`] / [`run_for_updates_sequential`].
//!
//! These exercise the library API directly (not via the `jj-hp push`
//! CLI) because the batch entrypoints don't have a CLI surface — they
//! exist for downstream callers like `jj-gt submit` that want a fan-out
//! over an N-bookmark stack.

mod harness;

use harness::{PRE_PUSH_FAILING, PRE_PUSH_PASSING, show};
use jj_hooks::bookmark_updates::{BookmarkUpdate, UpdateType};
use jj_hooks::hooks::{
    Cancel, RunOpts, run_for_partitioned_updates_parallel, run_for_updates_parallel,
    run_for_updates_sequential,
};
use jj_hooks::jj::{JjCli, primary_git_dir};
use jj_hooks::runner::{Runner, Stage};

use harness::TestRepo;

/// Build a 3-bookmark stack `main → b1 → b2 → b3` and return the
/// commit ids for each. Each bookmark sits one commit above its
/// parent.
fn build_three_stack(repo: &TestRepo) -> (String, String, String, String) {
    repo.write("a.txt", "a\n");
    let out = repo.jj(&["commit", "-m", "b1 commit"]);
    assert!(out.status.success(), "{}", show(&out));
    let out = repo.jj(&["bookmark", "create", "b1", "-r", "@-"]);
    assert!(out.status.success(), "{}", show(&out));

    repo.write("b.txt", "b\n");
    let out = repo.jj(&["commit", "-m", "b2 commit"]);
    assert!(out.status.success(), "{}", show(&out));
    let out = repo.jj(&["bookmark", "create", "b2", "-r", "@-"]);
    assert!(out.status.success(), "{}", show(&out));

    repo.write("c.txt", "c\n");
    let out = repo.jj(&["commit", "-m", "b3 commit"]);
    assert!(out.status.success(), "{}", show(&out));
    let out = repo.jj(&["bookmark", "create", "b3", "-r", "@-"]);
    assert!(out.status.success(), "{}", show(&out));

    (
        repo.commit_id_of("main"),
        repo.commit_id_of("b1"),
        repo.commit_id_of("b2"),
        repo.commit_id_of("b3"),
    )
}

fn updates_for_three_stack(main: &str, b1: &str, b2: &str, b3: &str) -> Vec<BookmarkUpdate> {
    vec![
        BookmarkUpdate {
            remote: "origin".into(),
            bookmark: "b1".into(),
            update_type: UpdateType::Add,
            old_commit: Some(main.to_owned()),
            new_commit: Some(b1.to_owned()),
        },
        BookmarkUpdate {
            remote: "origin".into(),
            bookmark: "b2".into(),
            update_type: UpdateType::Add,
            old_commit: Some(b1.to_owned()),
            new_commit: Some(b2.to_owned()),
        },
        BookmarkUpdate {
            remote: "origin".into(),
            bookmark: "b3".into(),
            update_type: UpdateType::Add,
            old_commit: Some(b2.to_owned()),
            new_commit: Some(b3.to_owned()),
        },
    ]
}

#[test]
fn run_for_updates_parallel_returns_results_in_input_order() {
    // Three bookmarks, all passing hooks. The parallel API must:
    //   1. Run all three through the pre-push hook.
    //   2. Return outcomes in the SAME order as the input updates,
    //      regardless of which thread finished first.
    //   3. Capture each run's output into HookOutcome::captured_output.
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_PASSING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let updates = updates_for_three_stack(&main, &b1, &b2, &b3);

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();

    let opts = RunOpts {
        retry_after_fixup: true,
        all_files: false,
        capture_output: true,
    };

    let outcomes = run_for_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &updates,
        opts,
        |_idx, _update| {},
        |_idx, _update, _outcome| {},
    )
    .unwrap();

    assert_eq!(outcomes.len(), 3);
    for (idx, outcome) in outcomes.iter().enumerate() {
        assert!(
            outcome.success,
            "outcome #{idx} failed: captured = {:?}",
            outcome.captured_output
        );
        assert!(
            outcome.captured_output.is_some(),
            "outcome #{idx} has no captured output despite capture_output=true",
        );
    }
}

#[test]
fn run_for_updates_parallel_first_failure_aborts_with_per_bookmark_attribution() {
    // Three bookmarks. Hook fails for all of them (PRE_PUSH_FAILING).
    // The batch entrypoint should:
    //   1. Run all three (the parallel path doesn't short-circuit;
    //      every thread is already launched).
    //   2. Surface success=false for each.
    //   3. Capture each one's failure output separately so the
    //      caller can attribute the failure to the right bookmark.
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_FAILING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let updates = updates_for_three_stack(&main, &b1, &b2, &b3);

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();

    let opts = RunOpts {
        retry_after_fixup: false,
        all_files: false,
        capture_output: true,
    };

    let outcomes = run_for_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &updates,
        opts,
        |_idx, _update| {},
        |_idx, _update, _outcome| {},
    )
    .unwrap();

    assert_eq!(outcomes.len(), 3);
    let mut had_failure = false;
    for (idx, outcome) in outcomes.iter().enumerate() {
        // Each outcome is either a real failure (success=false,
        // cancelled=false) or a fail-fast short-circuit (success=true,
        // cancelled=true). We must NOT see a real pass (success=true,
        // cancelled=false) — that would mean the hook config silently
        // ignored the failure config.
        if outcome.cancelled {
            assert!(
                outcome.success,
                "cancelled outcome #{idx} should have success=true, got {:?}",
                outcome
            );
        } else {
            had_failure = true;
            assert!(
                !outcome.success,
                "non-cancelled outcome #{idx} unexpectedly passed: captured = {:?}",
                outcome.captured_output
            );
            let captured = outcome
                .captured_output
                .as_deref()
                .unwrap_or_else(|| panic!("outcome #{idx} has no captured output"));
            assert!(
                !captured.is_empty(),
                "outcome #{idx} captured an empty buffer",
            );
        }
    }
    // PR-fail-fast: with cancellation in play, at LEAST one of the
    // outcomes is an actual hook failure (the one that tripped the
    // cancellation); the others may have observed cancellation
    // before they got to spawn their own subprocess and short-
    // circuited as cancelled-not-failed.
    assert!(
        had_failure,
        "expected at least one outcome to be a real hook failure, got all-cancelled",
    );
}

#[test]
fn run_for_updates_parallel_progress_callback_fires_per_update() {
    // The progress callback runs on each thread as soon as it
    // finishes. We don't pin completion order (parallel — any order
    // is fine); we just check the callback fires once per update
    // and the captured outcome is consistent with the returned one.
    use std::sync::Mutex;
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_PASSING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let updates = updates_for_three_stack(&main, &b1, &b2, &b3);

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();

    let opts = RunOpts {
        retry_after_fixup: true,
        all_files: false,
        capture_output: true,
    };

    let progress_fired: Mutex<Vec<(usize, String, bool)>> = Mutex::new(Vec::new());
    let outcomes = run_for_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &updates,
        opts,
        |_idx, _update| {},
        |idx, update, outcome| {
            progress_fired
                .lock()
                .unwrap()
                .push((idx, update.bookmark.clone(), outcome.success));
        },
    )
    .unwrap();

    let mut fired = progress_fired.into_inner().unwrap();
    fired.sort_by_key(|(idx, _, _)| *idx);
    assert_eq!(
        fired.len(),
        3,
        "expected 3 progress callbacks, got {fired:?}"
    );
    assert_eq!(fired[0], (0, "b1".into(), true));
    assert_eq!(fired[1], (1, "b2".into(), true));
    assert_eq!(fired[2], (2, "b3".into(), true));
    assert!(outcomes.iter().all(|o| o.success));
}

#[test]
fn run_for_updates_sequential_matches_parallel_results() {
    // Same fixture, sequential entrypoint. Results should be
    // identical (same hooks, same updates, same configuration) —
    // pins the contract that --hooks-sequential is just an
    // execution-strategy switch, not a semantic change.
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_PASSING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let updates = updates_for_three_stack(&main, &b1, &b2, &b3);

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();

    let opts = RunOpts {
        retry_after_fixup: true,
        all_files: false,
        // Sequential doesn't require capture; toggle on so we can
        // assert the output buffer plumbing still works in
        // sequential mode.
        capture_output: true,
    };

    let outcomes = run_for_updates_sequential(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &updates,
        opts,
        |_idx, _update, _outcome| {},
    )
    .unwrap();

    assert_eq!(outcomes.len(), 3);
    for (idx, outcome) in outcomes.iter().enumerate() {
        assert!(
            outcome.success,
            "outcome #{idx} failed: captured = {:?}",
            outcome.captured_output
        );
        assert!(
            outcome.captured_output.is_some(),
            "sequential with capture_output=true should still capture, got None for #{idx}",
        );
    }
}

#[test]
#[should_panic(expected = "run_for_updates_parallel requires capture_output=true")]
fn run_for_updates_parallel_without_capture_panics() {
    // The parallel API asserts on capture_output=true at entry —
    // passing false is a programmer error (it would garble the
    // terminal). Pin that the assert fires with a clear message.
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_PASSING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let updates = updates_for_three_stack(&main, &b1, &b2, &b3);

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();

    let opts = RunOpts {
        retry_after_fixup: true,
        all_files: false,
        capture_output: false,
    };

    let _ = run_for_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &updates,
        opts,
        |_idx, _update| {},
        |_idx, _update, _outcome| {},
    );
}

#[test]
fn cancel_token_round_trip() {
    let c = Cancel::new();
    assert!(!c.is_cancelled());
    c.cancel();
    assert!(c.is_cancelled());

    // Clone shares the underlying atomic.
    let c2 = Cancel::new();
    let c2_clone = c2.clone();
    assert!(!c2.is_cancelled());
    c2_clone.cancel();
    assert!(c2.is_cancelled());

    // never() is just a no-op alias for new().
    let n = Cancel::never();
    assert!(!n.is_cancelled());
}

#[test]
fn run_for_partitioned_updates_parallel_two_independent_stacks() {
    // PR-fail-fast: two-partition input. Both pass. Outcomes shape
    // matches the input partition shape; nothing is cancelled
    // because nothing fails.
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_PASSING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    // Partition 1: just b1. Partition 2: b2, b3.
    let p1: Vec<BookmarkUpdate> = vec![BookmarkUpdate {
        remote: "origin".into(),
        bookmark: "b1".into(),
        update_type: UpdateType::Add,
        old_commit: Some(main.clone()),
        new_commit: Some(b1.clone()),
    }];
    let p2: Vec<BookmarkUpdate> = vec![
        BookmarkUpdate {
            remote: "origin".into(),
            bookmark: "b2".into(),
            update_type: UpdateType::Add,
            old_commit: Some(b1.clone()),
            new_commit: Some(b2.clone()),
        },
        BookmarkUpdate {
            remote: "origin".into(),
            bookmark: "b3".into(),
            update_type: UpdateType::Add,
            old_commit: Some(b2.clone()),
            new_commit: Some(b3.clone()),
        },
    ];
    let partitions = vec![p1, p2];

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();
    let opts = RunOpts {
        retry_after_fixup: true,
        all_files: false,
        capture_output: true,
    };

    let outcomes = run_for_partitioned_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &partitions,
        opts,
        |_p, _u, _update| {},
        |_p, _u, _update, _outcome| {},
    )
    .unwrap();

    assert_eq!(outcomes.len(), 2, "expected 2 partitions, got {outcomes:?}");
    assert_eq!(outcomes[0].len(), 1);
    assert_eq!(outcomes[1].len(), 2);
    for partition_outcomes in &outcomes {
        for o in partition_outcomes {
            assert!(o.success, "expected pass, got {o:?}");
            assert!(!o.cancelled, "no failures means no cancellation");
        }
    }
}

#[test]
fn run_for_partitioned_updates_parallel_failure_in_one_stack_does_not_cancel_the_other() {
    // The user-facing motivation for the partitioned API: when the
    // user passes `-b X -b Y` for two unrelated stacks, a failure
    // in X should NOT cancel Y. Stack Y must run to completion
    // (success or failure of its own) regardless of what stack X
    // does.
    //
    // We can't easily get a "one stack passes, the other fails" in a
    // single hook config (PRE_PUSH_PASSING and PRE_PUSH_FAILING are
    // global config knobs), so the test pins the structural
    // invariant: when ALL partitions use a failing config, each
    // partition's outcomes show real-failure-not-just-cancelled at
    // least once. That proves the partitions' Cancel scopes are
    // independent — if they were shared, stack X's failure would
    // cancel stack Y before stack Y had a chance to fail on its
    // own, and stack Y's outcome would be "cancelled".
    let repo = TestRepo::new();
    repo.write_pre_commit_config(PRE_PUSH_FAILING);
    let (main, b1, b2, b3) = build_three_stack(&repo);
    let p1: Vec<BookmarkUpdate> = vec![BookmarkUpdate {
        remote: "origin".into(),
        bookmark: "b1".into(),
        update_type: UpdateType::Add,
        old_commit: Some(main.clone()),
        new_commit: Some(b1.clone()),
    }];
    let p2: Vec<BookmarkUpdate> = vec![BookmarkUpdate {
        remote: "origin".into(),
        bookmark: "b2".into(),
        update_type: UpdateType::Add,
        old_commit: Some(b1.clone()),
        new_commit: Some(b2.clone()),
    }];
    let p3: Vec<BookmarkUpdate> = vec![BookmarkUpdate {
        remote: "origin".into(),
        bookmark: "b3".into(),
        update_type: UpdateType::Add,
        old_commit: Some(b2.clone()),
        new_commit: Some(b3.clone()),
    }];
    let partitions = vec![p1, p2, p3];

    let jj = JjCli::new(repo.primary().to_path_buf());
    let primary_git_dir = primary_git_dir(repo.primary()).unwrap();
    let opts = RunOpts {
        retry_after_fixup: false,
        all_files: false,
        capture_output: true,
    };

    let outcomes = run_for_partitioned_updates_parallel(
        &jj,
        &primary_git_dir,
        repo.primary(),
        Some(Runner::PreCommit),
        Stage::PrePush,
        &partitions,
        opts,
        |_p, _u, _update| {},
        |_p, _u, _update, _outcome| {},
    )
    .unwrap();

    assert_eq!(outcomes.len(), 3);
    for (p_idx, partition_outcomes) in outcomes.iter().enumerate() {
        // Each partition's single-bookmark outcome must be a real
        // failure (success=false, cancelled=false). If any partition
        // came back cancelled-not-failed, cancellation leaked across
        // partition boundaries — the bug we're guarding against.
        assert_eq!(partition_outcomes.len(), 1);
        let o = &partition_outcomes[0];
        assert!(
            !o.cancelled,
            "partition {p_idx} outcome was cancelled — cancellation leaked across partition boundaries: {o:?}",
        );
        assert!(
            !o.success,
            "partition {p_idx} outcome unexpectedly passed: {o:?}",
        );
    }
}