jouet-git 0.0.4

The git recipes behind jouet commit, review-package, handoff and pickup
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! `jouet commit PATH... -m MSG`: commit only the named paths.
//!
//! In a checkout shared by several sessions, `git add -A` or `git commit -a`
//! sweeps up whatever else is there: untracked caches, build output, another
//! session's half-done work. This commits exactly the paths given, as they
//! are in the working tree, with `git commit --only`, so anything else
//! already staged stays staged and out of the commit. Untracked files are
//! added only when a named path covers them, and are unstaged again if the
//! commit fails.
//!
//! Hooks run as they would for `git commit`; `--no-verify` is never passed.
//! git's output, hooks included, goes to a log, and a failed commit reports
//! the end of it.
//!
//! A [`Scanner`] may look at the content first: [`run_scanned`] builds a
//! temporary index holding HEAD plus the named paths, exactly what `git
//! commit --only` will record, and refuses the commit when the scanner
//! finds a secret there. The real index is never touched.

use crate::conventional::{self, Policy};
use crate::{Error, Git, nul_fields};
use jouet_output::exit::Exit;
use serde::Serialize;
use std::io::Write as _;
use std::path::{Path, PathBuf};

/// Lines of git's output shown when a commit fails.
const HOOK_TAIL: usize = 40;

#[derive(Debug, Clone, Default)]
pub struct Request {
    pub paths: Vec<String>,
    pub message: String,
    /// `Key: value` lines to append, already expanded.
    pub trailers: Vec<String>,
    pub conventional: Policy,
    /// The allowed conventional-commit types.
    pub types: Vec<String>,
    pub dry_run: bool,
    /// Where a failed commit's full output is kept.
    pub log_dir: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct File {
    pub path: String,
    /// `added`, `modified`, `deleted` or `type_changed`.
    pub status: &'static str,
    /// Lines added and removed; absent for binary files and, in a dry run,
    /// for files not yet tracked.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub added: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub removed: Option<u64>,
}

/// The output of a commit that git refused, usually because a hook failed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Failure {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exit_code: Option<i32>,
    /// The last lines of git's output, hooks included.
    pub output: Vec<String>,
    /// How many lines came before `output`.
    #[serde(skip_serializing_if = "is_zero")]
    pub lines_omitted: usize,
    pub log_path: String,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Report {
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub committed: bool,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub dry_run: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    pub subject: String,
    /// The whole message, trailers included.
    pub message: String,
    pub files: Vec<File>,
    /// Untracked files a named path covered, added by this commit.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub newly_tracked: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub failure: Option<Failure>,
    /// What the secret scan found, when one ran or was skipped.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret_scan: Option<SecretScan>,
    /// What is still uncommitted after a commit: changed, staged or untracked
    /// paths, as git's status names them. None before a commit, or when git
    /// could not say. Agents ran `git status` after each commit to learn it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub left: Option<Vec<String>>,
}

impl Report {
    /// Whether the secret scan found a secret, so nothing was committed.
    pub fn secret_found(&self) -> bool {
        self.secret_scan
            .as_ref()
            .is_some_and(|s| s.status == ScanStatus::Found)
    }
}

/// A secret scan of the content a commit would record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SecretScan {
    /// What scanned: `jouet` (gitleaks' rules), `fnox`, or `jouet+fnox`.
    pub tool: String,
    pub status: ScanStatus,
    /// How many files it read.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub files_scanned: Option<u64>,
    /// Where a secret was found. Never the secret itself.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub findings: Vec<SecretFinding>,
    /// Why the scan, or a part of it, was skipped.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// What the scanner warned about, such as secrets it could not check.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ScanStatus {
    Clean,
    Found,
    Skipped,
}

/// One secret, found in one file: a fnox secret's value, named by
/// `secret`, or what a gitleaks rule matched, named by `rule`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct SecretFinding {
    /// The fnox secret's name.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub secret: String,
    /// The gitleaks rule.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rule: Option<String>,
    pub path: String,
    pub line: u64,
    /// The rule's match, with the secret cut to its first characters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redacted: Option<String>,
}

/// Scans the content a commit would record.
pub trait Scanner {
    /// `index` is a git index file holding exactly what the commit will
    /// record: HEAD plus the named paths. Point `GIT_INDEX_FILE` at it and
    /// read the staged content.
    fn scan(&self, top: &Path, index: &Path) -> Result<SecretScan, Error>;
}

fn is_zero(n: &usize) -> bool {
    *n == 0
}

/// Checks the request, then commits (or, for a dry run, reports what would
/// be committed). A commit git refuses is `Ok` with `failure` set.
pub fn run(git: &Git, req: &Request) -> Result<Report, Error> {
    run_scanned(git, req, None)
}

/// [`run`], with `scanner` reading the content before it is committed. A
/// secret it finds is `Ok` with `secret_scan` saying so, and nothing is
/// committed or left staged. A dry run does not scan.
pub fn run_scanned(
    git: &Git,
    req: &Request,
    scanner: Option<&dyn Scanner>,
) -> Result<Report, Error> {
    let top = git.toplevel()?;
    if req.paths.is_empty() {
        return Err(no_paths());
    }
    let message = req.message.trim_end();
    if message.trim().is_empty() {
        return Err(Error::new(Exit::Usage, "the commit message is empty"));
    }
    for path in &req.paths {
        check_path(&top, &git.resolve(path), path)?;
    }
    // Untracked files are listed relative to the top, so they are added
    // from there.
    let at_top = Git::new(&top);
    let head = git.head();
    let mut files = Vec::new();
    let mut untracked = Vec::new();
    for path in &req.paths {
        let (changed, new) = changes(git, head.as_deref(), path)?;
        if changed.is_empty() && new.is_empty() {
            return Err(unchanged(git, path));
        }
        files.extend(changed);
        untracked.extend(new);
    }
    dedupe(&mut files, |f| f.path.clone());
    dedupe(&mut untracked, Clone::clone);
    for path in &untracked {
        if !files.iter().any(|f| &f.path == path) {
            files.push(File {
                path: path.clone(),
                status: "added",
                added: None,
                removed: None,
            });
        }
    }
    files.sort_by(|a, b| a.path.cmp(&b.path));

    let mut warnings = Vec::new();
    if req.conventional != Policy::Off
        && let Some(source) = conventional::detect(&top)
    {
        let problems = conventional::lint(message, &req.types);
        if !problems.is_empty() {
            let why = format!(
                "the subject is not a conventional commit, which {source} asks for: {}",
                problems.join("; ")
            );
            if req.conventional == Policy::Refuse {
                return Err(Error::new(Exit::Precondition, why)
                    .hint("fix the subject, or set [commit] conventional = \"warn\""));
            }
            warnings.push(why);
        }
    }
    let message = with_trailers(git, message, &req.trailers)?;
    let subject = message.lines().next().unwrap_or_default().to_string();
    let mut report = Report {
        dry_run: req.dry_run,
        branch: git.branch(),
        subject,
        message,
        files,
        newly_tracked: untracked.clone(),
        warnings,
        ..Report::default()
    };
    if req.dry_run {
        return Ok(report);
    }
    if let Some(scanner) = scanner {
        let scan = scan(git, &top, head.as_deref(), &req.paths, scanner)?;
        report.secret_scan = Some(scan);
        if report.secret_found() {
            return Ok(report);
        }
    }

    if !untracked.is_empty() {
        let mut args = vec!["add".to_string(), "--".to_string()];
        args.extend(untracked.iter().cloned());
        at_top.text(&args)?;
    }
    match commit(git, &req.paths, &report.message, &req.log_dir) {
        Ok(None) => {}
        Ok(Some(failure)) => {
            unstage(&at_top, head.as_deref(), &untracked);
            report.failure = Some(failure);
            return Ok(report);
        }
        Err(e) => {
            unstage(&at_top, head.as_deref(), &untracked);
            return Err(e);
        }
    }
    report.committed = true;
    report.sha = git.head();
    report.files = committed_files(git)?;
    report.left = left(git);
    // A commit-msg hook may have changed the message.
    if let Some(message) = git.line(&["log", "-1", "--format=%B", "HEAD"]) {
        report.subject = message.lines().next().unwrap_or_default().to_string();
        report.message = message;
    }
    Ok(report)
}

/// The paths `git status` still reports, changed, staged or untracked.
fn left(git: &Git) -> Option<Vec<String>> {
    let out = git
        .text(&["status", "--porcelain=v1", "-z", "--untracked-files=normal"])
        .ok()?;
    let mut paths = Vec::new();
    let mut fields = crate::nul_fields(&out);
    while let Some(entry) = fields.next() {
        let Some(path) = entry.get(3..) else { continue };
        // A rename is followed by the name it had.
        if entry.starts_with('R') || entry.starts_with('C') {
            fields.next();
        }
        paths.push(path.to_string());
    }
    Some(paths)
}

fn no_paths() -> Error {
    Error::new(Exit::Usage, "name the paths to commit")
        .hint("jouet commit stages only the paths it is given, never everything: jouet commit src/a.rs src/b.rs -m MSG")
}

/// Refuses a path that names the whole tree, or lies outside it. `full` is
/// `path` made absolute.
fn check_path(top: &Path, full: &Path, path: &str) -> Result<(), Error> {
    if path.is_empty() {
        return Err(Error::new(Exit::Usage, "an empty path names nothing"));
    }
    let full = normalize(full);
    // Canonicalize what exists, so symlinked checkouts (macOS's /tmp) still
    // compare equal to git's top.
    let top = top.canonicalize().unwrap_or_else(|_| top.to_path_buf());
    let full = existing_prefix_canonical(&full);
    if full == top {
        return Err(
            Error::new(Exit::Usage, format!("`{path}` is the whole repository")).hint(
                "name the files or directories to commit; jouet commit never sweeps up everything",
            ),
        );
    }
    if !full.starts_with(&top) {
        return Err(Error::new(
            Exit::Usage,
            format!("`{path}` is outside the repository at {}", top.display()),
        ));
    }
    Ok(())
}

/// Removes `.` and folds `..` without touching the filesystem.
fn normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for part in path.components() {
        match part {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                out.pop();
            }
            other => out.push(other),
        }
    }
    out
}

/// Canonicalizes the longest existing prefix of `path` and appends the rest,
/// so a deleted file still resolves through a symlinked parent.
fn existing_prefix_canonical(path: &Path) -> PathBuf {
    let mut rest = Vec::new();
    let mut cur = path.to_path_buf();
    loop {
        if let Ok(real) = cur.canonicalize() {
            let mut out = real;
            for part in rest.iter().rev() {
                out.push(part);
            }
            return out;
        }
        match (cur.file_name().map(ToOwned::to_owned), cur.parent()) {
            (Some(name), Some(parent)) => {
                rest.push(name);
                cur = parent.to_path_buf();
            }
            _ => return path.to_path_buf(),
        }
    }
}

/// The tracked files under `path` that differ from HEAD (the working tree
/// against HEAD, staged or not), and the untracked, not ignored files under
/// it.
fn changes(git: &Git, head: Option<&str>, path: &str) -> Result<(Vec<File>, Vec<String>), Error> {
    let mut files = Vec::new();
    match head {
        Some(head) => {
            let status = git.text(&[
                "diff",
                "--no-renames",
                "--name-status",
                "-z",
                head,
                "--",
                path,
            ])?;
            let numstat =
                git.text(&["diff", "--no-renames", "--numstat", "-z", head, "--", path])?;
            let counts = parse_numstat(&numstat);
            let mut fields = nul_fields(&status);
            while let (Some(code), Some(name)) = (fields.next(), fields.next()) {
                let (added, removed) = counts.get(name).copied().unwrap_or((None, None));
                files.push(File {
                    path: name.to_string(),
                    status: status_name(code),
                    added,
                    removed,
                });
            }
        }
        None => {
            // An unborn branch: every tracked file is new.
            let cached = git.text(&["ls-files", "-z", "--cached", "--full-name", "--", path])?;
            for name in nul_fields(&cached) {
                files.push(File {
                    path: name.to_string(),
                    status: "added",
                    added: None,
                    removed: None,
                });
            }
        }
    }
    let others = git.text(&[
        "ls-files",
        "-z",
        "--others",
        "--exclude-standard",
        "--full-name",
        "--",
        path,
    ])?;
    let untracked = nul_fields(&others).map(str::to_string).collect();
    Ok((files, untracked))
}

fn parse_numstat(text: &str) -> std::collections::HashMap<&str, (Option<u64>, Option<u64>)> {
    let mut out = std::collections::HashMap::new();
    for field in nul_fields(text) {
        let mut parts = field.splitn(3, '\t');
        if let (Some(a), Some(r), Some(name)) = (parts.next(), parts.next(), parts.next()) {
            out.insert(name, (a.parse().ok(), r.parse().ok()));
        }
    }
    out
}

fn status_name(code: &str) -> &'static str {
    match code.chars().next() {
        Some('A') => "added",
        Some('D') => "deleted",
        Some('T') => "type_changed",
        _ => "modified",
    }
}

/// Why a named path has nothing to commit.
fn unchanged(git: &Git, path: &str) -> Error {
    if git.ignores(path) {
        return Error::new(
            Exit::Precondition,
            format!("`{path}` is ignored by git, so it cannot be committed"),
        );
    }
    let tracked = git.line(&["ls-files", "--", path]).is_some();
    if !tracked && std::fs::symlink_metadata(git.resolve(path)).is_err() {
        return Error::new(
            Exit::NotFound,
            format!("`{path}`: no such file or directory"),
        );
    }
    Error::new(
        Exit::Precondition,
        format!("`{path}` has no changes to commit"),
    )
    .hint("name only the paths you changed")
}

fn dedupe<T, K: PartialEq>(items: &mut Vec<T>, key: impl Fn(&T) -> K) {
    let mut seen: Vec<K> = Vec::new();
    items.retain(|item| {
        let k = key(item);
        if seen.contains(&k) {
            false
        } else {
            seen.push(k);
            true
        }
    });
}

/// The message with `trailers` appended by `git interpret-trailers`, which
/// joins an existing trailer block and skips a trailer already present. One
/// the message already gives in its own words is skipped too: a co-author
/// with the same address, or a second AI disclosure.
fn with_trailers(git: &Git, message: &str, trailers: &[String]) -> Result<String, Error> {
    // Check every configured trailer, including one the message makes
    // redundant, so a bad setting fails whatever the message says.
    for t in trailers {
        match t.split_once(':') {
            Some((key, value))
                if !key.trim().is_empty()
                    && !key.contains(char::is_whitespace)
                    && !value.trim().is_empty() => {}
            _ => {
                return Err(Error::new(
                    Exit::Usage,
                    format!("a trailer is `Key: value`, not `{t}`"),
                ));
            }
        }
    }
    let given = given_trailers(message);
    let trailers: Vec<&String> = trailers
        .iter()
        .filter(|t| !given.iter().any(|g| same_trailer(g, t)))
        .collect();
    if trailers.is_empty() {
        return Ok(format!("{message}\n"));
    }
    let mut args = vec![
        "-c".to_string(),
        "trailer.ifexists=addIfDifferent".to_string(),
        "-c".to_string(),
        "trailer.where=end".to_string(),
        "interpret-trailers".to_string(),
    ];
    for t in trailers {
        args.push("--trailer".into());
        args.push(t.to_string());
    }
    let mut child = git
        .command(&args)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .map_err(|e| Error::new(Exit::Failed, format!("running git: {e}")))?;
    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(format!("{message}\n").as_bytes())
            .map_err(|e| Error::new(Exit::Failed, format!("writing to git: {e}")))?;
    }
    let out = child
        .wait_with_output()
        .map_err(|e| Error::new(Exit::Failed, format!("running git: {e}")))?;
    if !out.status.success() {
        return Err(Error::new(
            Exit::Failed,
            format!(
                "git interpret-trailers failed: {}",
                String::from_utf8_lossy(&out.stderr).trim()
            ),
        ));
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

/// The `Key: value` lines of the message's last paragraph, when it has
/// one after the subject. Blank lines split paragraphs whatever the line
/// ending.
fn given_trailers(message: &str) -> Vec<&str> {
    let lines: Vec<&str> = message.trim_end().lines().collect();
    let Some(blank) = lines.iter().rposition(|l| l.trim().is_empty()) else {
        return Vec::new();
    };
    let lines = &lines[blank + 1..];
    let trailer = |l: &&str| {
        l.split_once(':')
            .is_some_and(|(k, _)| !k.is_empty() && !k.contains(char::is_whitespace))
    };
    if lines.iter().all(trailer) {
        lines.to_vec()
    } else {
        Vec::new()
    }
}

/// Whether `given` already says what `trailer` would: the same key and
/// address (`Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>` gives
/// `Co-Authored-By: Claude <noreply@anthropic.com>`), or an `AI-Assisted`
/// disclosure, which a commit carries once.
fn same_trailer(given: &str, trailer: &str) -> bool {
    let split = |t: &str| {
        t.split_once(':')
            .map(|(k, v)| (k.trim().to_ascii_lowercase(), v.trim().to_string()))
    };
    let (Some((key, value)), Some((wanted_key, wanted))) = (split(given), split(trailer)) else {
        return false;
    };
    let address = |v: &str| {
        let (_, rest) = v.rsplit_once('<')?;
        Some(rest.strip_suffix('>')?.to_ascii_lowercase())
    };
    key == wanted_key
        && (key == "ai-assisted" || address(&value).is_some_and(|a| address(&wanted) == Some(a)))
}

/// Runs `scanner` over a temporary index of HEAD plus `paths` as they are in
/// the working tree, which is what `git commit --only` records.
fn scan(
    git: &Git,
    top: &Path,
    head: Option<&str>,
    paths: &[String],
    scanner: &dyn Scanner,
) -> Result<SecretScan, Error> {
    let io =
        |e: std::io::Error| Error::new(Exit::Failed, format!("preparing the secret scan: {e}"));
    let dir = tempfile::tempdir().map_err(io)?;
    let index = dir.path().join("index");
    let run = |args: &[String]| -> Result<(), Error> {
        let out = git
            .command(args)
            .env("GIT_INDEX_FILE", &index)
            .output()
            .map_err(|e| Error::new(Exit::Failed, format!("running git: {e}")))?;
        if out.status.success() {
            return Ok(());
        }
        Err(Error::new(
            Exit::Failed,
            format!(
                "git {} failed: {}",
                args.join(" "),
                String::from_utf8_lossy(&out.stderr).trim()
            ),
        ))
    };
    match head {
        Some(head) => run(&["read-tree".into(), head.into()])?,
        None => run(&["read-tree".into(), "--empty".into()])?,
    }
    let mut add = vec!["add".to_string(), "--".to_string()];
    add.extend(paths.iter().cloned());
    run(&add)?;
    scanner.scan(top, &index)
}

/// `git commit --only -F MESSAGE -- PATHS`, its output to a log. `None`
/// when it succeeded.
fn commit(
    git: &Git,
    paths: &[String],
    message: &str,
    log_dir: &Path,
) -> Result<Option<Failure>, Error> {
    let io = |e: std::io::Error| Error::new(Exit::Failed, format!("writing the commit log: {e}"));
    let mut file = tempfile::NamedTempFile::new().map_err(io)?;
    file.write_all(message.as_bytes()).map_err(io)?;
    let (log_path, log) = crate::new_log(log_dir, "commit").map_err(io)?;
    let mut args: Vec<std::ffi::OsString> = vec![
        "commit".into(),
        "--only".into(),
        "--cleanup=whitespace".into(),
        "-F".into(),
        file.path().as_os_str().to_owned(),
        "--".into(),
    ];
    args.extend(paths.iter().map(|p| crate::literal(p).into()));
    // Not `--literal-pathspecs`, which would reach the hooks.
    let status = git
        .hooked_command(&args)
        .stdout(log.try_clone().map_err(io)?)
        .stderr(log)
        .status()
        .map_err(|e| Error::new(Exit::Failed, format!("running git commit: {e}")))?;
    if status.success() {
        let _ = std::fs::remove_file(&log_path);
        // A hook that fixed and staged a path (hk's fixers) changed the
        // commit, but git wrote the path into the real index as it was
        // before the hook. The commit is what the index should hold.
        let mut reset = vec!["reset".to_string(), "-q".into(), "--".into()];
        reset.extend(paths.iter().cloned());
        let _ = git.text(&reset);
        return Ok(None);
    }
    let text = String::from_utf8_lossy(&std::fs::read(&log_path).unwrap_or_default()).into_owned();
    let (output, lines_omitted) = crate::tail(&text, HOOK_TAIL);
    Ok(Some(Failure {
        exit_code: status.code(),
        output,
        lines_omitted,
        log_path: log_path.display().to_string(),
    }))
}

/// Unstages the untracked files this commit added, after it failed.
fn unstage(git: &Git, head: Option<&str>, paths: &[String]) {
    if paths.is_empty() {
        return;
    }
    let mut args: Vec<String> = match head {
        Some(_) => vec!["reset".into(), "-q".into()],
        None => vec!["rm".into(), "-q".into(), "--cached".into()],
    };
    args.push("--".into());
    args.extend(paths.iter().cloned());
    let _ = git.text(&args);
}

/// The files in HEAD's commit, with line counts.
fn committed_files(git: &Git) -> Result<Vec<File>, Error> {
    let base = [
        "diff-tree",
        "-r",
        "--root",
        "--no-commit-id",
        "--no-renames",
        "-z",
    ];
    let status = git.text(&[&base[..], &["--name-status", "HEAD"]].concat())?;
    let numstat = git.text(&[&base[..], &["--numstat", "HEAD"]].concat())?;
    let counts = parse_numstat(&numstat);
    let mut files = Vec::new();
    let mut fields = nul_fields(&status);
    while let (Some(code), Some(name)) = (fields.next(), fields.next()) {
        let (added, removed) = counts.get(name).copied().unwrap_or((None, None));
        files.push(File {
            path: name.to_string(),
            status: status_name(code),
            added,
            removed,
        });
    }
    Ok(files)
}

#[cfg(test)]
#[path = "commit_tests.rs"]
mod tests;