grit-lib 0.5.25

Core library for the grit Git implementation
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
//! Commit message trailer parsing and rewriting (Git-compatible).
//!
//! Behaviour matches upstream `git/trailer.c` / `git interpret-trailers`.

use std::collections::HashMap;
use std::path::Path;

use crate::config::{ConfigEntry, ConfigSet};

const CUT_LINE: &str = "------------------------ >8 ------------------------";

const GIT_GENERATED_PREFIXES: &[&str] = &["Signed-off-by: ", "(cherry picked from commit "];

const TRAILER_ARG_PLACEHOLDER: &str = "$ARG";

/// Placement of a new trailer relative to an anchor trailer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TrailerWhere {
    #[default]
    /// `Default` variant.
    Default,
    /// `End` variant.
    End,
    /// `After` variant.
    After,
    /// `Before` variant.
    Before,
    /// `Start` variant.
    Start,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
/// `TrailerIfExists` enum.
pub enum TrailerIfExists {
    #[default]
    /// `Default` variant.
    Default,
    /// `AddIfDifferentNeighbor` variant.
    AddIfDifferentNeighbor,
    /// `AddIfDifferent` variant.
    AddIfDifferent,
    /// `Add` variant.
    Add,
    /// `Replace` variant.
    Replace,
    /// `DoNothing` variant.
    DoNothing,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
/// `TrailerIfMissing` enum.
pub enum TrailerIfMissing {
    #[default]
    /// `Default` variant.
    Default,
    /// `Add` variant.
    Add,
    /// `DoNothing` variant.
    DoNothing,
}

#[derive(Debug, Clone, Default)]
/// `ProcessTrailerOptions` struct.
pub struct ProcessTrailerOptions {
    /// `trim_empty` field.
    pub trim_empty: bool,
    /// `only_trailers` field.
    pub only_trailers: bool,
    /// `only_input` field.
    pub only_input: bool,
    /// `unfold` field.
    pub unfold: bool,
    /// `no_divider` field.
    pub no_divider: bool,
}

#[derive(Debug, Clone)]
/// `NewTrailerArg` struct.
pub struct NewTrailerArg {
    /// `text` field.
    pub text: String,
    /// `where_` field.
    pub where_: TrailerWhere,
    /// `if_exists` field.
    pub if_exists: TrailerIfExists,
    /// `if_missing` field.
    pub if_missing: TrailerIfMissing,
}

#[derive(Debug, Clone)]
struct ConfInfo {
    name: String,
    key: Option<String>,
    command: Option<String>,
    cmd: Option<String>,
    where_: TrailerWhere,
    if_exists: TrailerIfExists,
    if_missing: TrailerIfMissing,
}

#[derive(Debug, Clone)]
struct TrailerItem {
    token: Option<String>,
    value: String,
}

#[derive(Debug, Clone)]
struct ArgItem {
    token: String,
    value: String,
    conf: ConfInfo,
}

impl ArgItem {
    fn into_item(self) -> TrailerItem {
        TrailerItem {
            token: Some(self.token),
            value: self.value,
        }
    }
}

#[derive(Debug)]
struct TrailerBlock {
    blank_line_before: bool,
    start: usize,
    end: usize,
    lines: Vec<String>,
}

/// Parse `trailer.where` / `--where` values (Git spelling).
pub fn trailer_where_from_str(s: &str) -> Option<TrailerWhere> {
    match s.to_ascii_lowercase().as_str() {
        "after" => Some(TrailerWhere::After),
        "before" => Some(TrailerWhere::Before),
        "end" => Some(TrailerWhere::End),
        "start" => Some(TrailerWhere::Start),
        _ => None,
    }
}

/// Parse `trailer.ifexists` / `--if-exists` values.
pub fn trailer_if_exists_from_str(s: &str) -> Option<TrailerIfExists> {
    match s.to_ascii_lowercase().as_str() {
        "addifdifferent" => Some(TrailerIfExists::AddIfDifferent),
        "addifdifferentneighbor" => Some(TrailerIfExists::AddIfDifferentNeighbor),
        "add" => Some(TrailerIfExists::Add),
        "replace" => Some(TrailerIfExists::Replace),
        "donothing" => Some(TrailerIfExists::DoNothing),
        _ => None,
    }
}

/// Parse `trailer.ifmissing` / `--if-missing` values.
pub fn trailer_if_missing_from_str(s: &str) -> Option<TrailerIfMissing> {
    match s.to_ascii_lowercase().as_str() {
        "add" => Some(TrailerIfMissing::Add),
        "donothing" => Some(TrailerIfMissing::DoNothing),
        _ => None,
    }
}

fn after_or_end(where_: TrailerWhere) -> bool {
    matches!(where_, TrailerWhere::After | TrailerWhere::End)
}

fn token_len_without_separator(token: &str) -> usize {
    let b = token.as_bytes();
    let mut len = token.len();
    while len > 0 && !b[len - 1].is_ascii_alphanumeric() {
        len -= 1;
    }
    len
}

fn same_token(a_token: Option<&str>, b_token: &str) -> bool {
    let Some(a) = a_token else {
        return false;
    };
    let a_len = token_len_without_separator(a);
    let b_len = token_len_without_separator(b_token);
    let min_len = a_len.min(b_len);
    a[..min_len].eq_ignore_ascii_case(&b_token[..min_len])
}

fn same_trailer(a: &TrailerItem, b: &ArgItem) -> bool {
    same_token(a.token.as_deref(), &b.token) && a.value.eq_ignore_ascii_case(&b.value)
}

fn is_blank_line(s: &str) -> bool {
    s.chars().all(|c| c.is_whitespace())
}

fn last_non_space_char(s: &str) -> Option<char> {
    s.chars().rev().find(|c| !c.is_whitespace())
}

fn line_end(buf: &str, bol: usize, limit: usize) -> usize {
    bol + buf[bol..limit].find('\n').unwrap_or(limit - bol)
}

fn after_line(buf: &str, bol: usize, limit: usize) -> usize {
    let le = line_end(buf, bol, limit);
    if le < limit {
        le + 1
    } else {
        limit
    }
}

/// Start of the line that ends at `len` (`None` when `len` is 0).
fn last_line_start(buf: &str, len: usize) -> Option<usize> {
    let before_last_byte = buf.as_bytes().get(..len.checked_sub(1)?)?;
    Some(
        before_last_byte
            .iter()
            .rposition(|&b| b == b'\n')
            .map_or(0, |i| i + 1),
    )
}

/// Offset of the scissors line (`# --- >8 ---`) within `s[..len]`, or `len` when there is none.
fn cut_line_start(s: &str, len: usize, comment_prefix: &str) -> usize {
    let head = format!("{comment_prefix} {CUT_LINE}\n");
    if s.starts_with(&head) {
        return 0;
    }
    let pattern = format!("\n{head}");
    if let Some(p) = s[..len].find(&pattern) {
        let newlen = p + 1;
        if newlen < len {
            return newlen;
        }
    }
    len
}

fn ignored_log_message_bytes(buf: &str, len: usize, comment_prefix: &str) -> usize {
    let cutoff = cut_line_start(buf, len, comment_prefix);
    let mut bol = 0usize;
    let mut boc = 0usize;
    let mut in_old_conflicts = false;

    while bol < cutoff {
        let le = line_end(buf, bol, cutoff);
        let line = &buf[bol..le];

        let is_comment = line.starts_with(comment_prefix) || line.is_empty();
        if is_comment {
            if boc == 0 {
                boc = bol;
            }
        } else if line.starts_with("Conflicts:") {
            in_old_conflicts = true;
            if boc == 0 {
                boc = bol;
            }
        } else if in_old_conflicts && line.starts_with('\t') {
            // path in conflicts block
        } else if boc != 0 {
            boc = 0;
            in_old_conflicts = false;
        }

        bol = after_line(buf, bol, cutoff);
    }

    if boc != 0 {
        len - boc
    } else {
        len - cutoff
    }
}

fn find_end_of_log_message(input: &str, no_divider: bool, comment_prefix: &str) -> usize {
    let is_divider = |line: &str| {
        line.strip_prefix("---")
            .is_some_and(|rest| rest.chars().next().is_none_or(|c| c.is_ascii_whitespace()))
    };
    let mut end = input.len();
    let mut pos = 0usize;
    while !no_divider && pos < input.len() {
        if is_divider(&input[pos..]) {
            end = pos;
            break;
        }
        pos = after_line(input, pos, input.len());
    }
    end - ignored_log_message_bytes(input, end, comment_prefix)
}

fn find_separator(line: &str, separators: &str) -> Option<usize> {
    let mut whitespace_found = false;
    for (i, c) in line.char_indices() {
        if separators.contains(c) {
            return Some(i);
        }
        if !whitespace_found && (c.is_ascii_alphanumeric() || c == '-') {
            continue;
        }
        if i > 0 && (c == ' ' || c == '\t') {
            whitespace_found = true;
            continue;
        }
        break;
    }
    None
}

impl ConfInfo {
    /// Whether `token` (text before the separator) starts with this trailer's alias name or with
    /// its configured key, compared case-insensitively and ignoring the key's trailing separator.
    fn matches_token(&self, token: &str) -> bool {
        let token_len = token_len_without_separator(token);
        let has_prefix =
            |want: &str| token_len >= want.len() && token[..want.len()].eq_ignore_ascii_case(want);
        has_prefix(&self.name)
            || self
                .key
                .as_deref()
                .is_some_and(|key| has_prefix(&key[..token_len_without_separator(key)]))
    }

    /// Applies a `where`, `ifexists` or `ifmissing` setting; unparsable values are ignored.
    /// Returns `false` when `var` is not one of those settings.
    fn apply_placement_setting(&mut self, var: &str, value: Option<&str>) -> bool {
        if var.eq_ignore_ascii_case("where") {
            if let Some(v) = value.and_then(trailer_where_from_str) {
                self.where_ = v;
            }
        } else if var.eq_ignore_ascii_case("ifexists") {
            if let Some(v) = value.and_then(trailer_if_exists_from_str) {
                self.if_exists = v;
            }
        } else if var.eq_ignore_ascii_case("ifmissing") {
            if let Some(v) = value.and_then(trailer_if_missing_from_str) {
                self.if_missing = v;
            }
        } else {
            return false;
        }
        true
    }
}

fn find_trailer_block_start(
    buf: &str,
    len: usize,
    conf: &[ConfInfo],
    separators: &str,
    comment_prefix: &str,
) -> usize {
    let mut end_of_title = len;
    let mut pos = 0usize;
    while pos < len {
        let line = &buf[pos..line_end(buf, pos, len)];
        if !line.starts_with(comment_prefix) && is_blank_line(line) {
            end_of_title = pos;
            break;
        }
        pos = after_line(buf, pos, len);
    }

    let mut only_spaces = true;
    let mut recognized_prefix = false;
    let mut trailer_lines = 0i32;
    let mut non_trailer_lines = 0i32;
    let mut possible_continuation = 0i32;

    let mut next = last_line_start(buf, len);
    while let Some(bol) = next {
        if bol < end_of_title {
            break;
        }
        next = last_line_start(buf, bol);
        let line = &buf[bol..line_end(buf, bol, len)];

        if line.starts_with(comment_prefix) {
            non_trailer_lines += possible_continuation;
            possible_continuation = 0;
            continue;
        }

        if is_blank_line(line) {
            if only_spaces {
                continue;
            }
            non_trailer_lines += possible_continuation;
            let is_trailer_block = (recognized_prefix && trailer_lines * 3 >= non_trailer_lines)
                || (trailer_lines > 0 && non_trailer_lines == 0);
            return if is_trailer_block {
                after_line(buf, bol, len)
            } else {
                len
            };
        }
        only_spaces = false;

        let indented = line.starts_with(char::is_whitespace);
        if GIT_GENERATED_PREFIXES.iter().any(|p| line.starts_with(p)) {
            trailer_lines += 1;
            possible_continuation = 0;
            recognized_prefix = true;
        } else if let Some(sep_pos) =
            find_separator(line, separators).filter(|&p| p >= 1 && !indented)
        {
            trailer_lines += 1;
            possible_continuation = 0;
            recognized_prefix = recognized_prefix
                || conf
                    .iter()
                    .any(|item| item.matches_token(line[..sep_pos].trim_end()));
        } else if indented {
            possible_continuation += 1;
        } else {
            non_trailer_lines += 1 + possible_continuation;
            possible_continuation = 0;
        }
    }

    len
}

fn ends_with_blank_line(buf: &str, trailer_block_start: usize) -> bool {
    if trailer_block_start == 0 {
        return false;
    }
    let slice = &buf[..trailer_block_start];
    last_line_start(slice, slice.len()).is_some_and(|i| is_blank_line(&slice[i..]))
}

fn unfold_value(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\n' {
            while chars.peek().is_some_and(|x| x.is_whitespace()) {
                chars.next();
            }
            if !out.is_empty() && !out.ends_with(' ') {
                out.push(' ');
            }
        } else {
            out.push(c);
        }
    }
    out.trim().to_string()
}

impl Default for ConfInfo {
    fn default() -> Self {
        Self {
            name: String::new(),
            key: None,
            command: None,
            cmd: None,
            where_: TrailerWhere::End,
            if_exists: TrailerIfExists::AddIfDifferentNeighbor,
            if_missing: TrailerIfMissing::Add,
        }
    }
}

fn token_from_item(item: &ConfInfo, fallback: &str) -> String {
    item.key.clone().unwrap_or_else(|| fallback.to_string())
}

/// Splits `trailer` into token and value. When the token matches a configured alias, the token
/// is replaced by the alias key and the matching configuration is returned alongside.
fn parse_trailer<'c>(
    trailer: &str,
    separators: &str,
    conf: &'c [ConfInfo],
) -> (String, String, Option<&'c ConfInfo>) {
    let (tok, val) = match find_separator(trailer, separators) {
        Some(pos) => (trailer[..pos].trim(), trailer[pos + 1..].trim()),
        None => (trailer.trim(), ""),
    };
    let picked = conf.iter().find(|item| item.matches_token(tok));
    let tok = picked.map_or_else(|| tok.to_string(), |item| token_from_item(item, tok));
    (tok, val.to_string(), picked)
}

fn comment_line_prefix(cfg: &ConfigSet) -> String {
    match cfg.get("core.commentChar") {
        Some(s) => {
            let t = s.trim();
            if t.is_empty() || t.eq_ignore_ascii_case("auto") {
                "#".to_string()
            } else {
                t.chars().next().unwrap_or('#').to_string()
            }
        }
        None => "#".to_string(),
    }
}

fn load_trailer_config(cfg: &ConfigSet) -> (ConfInfo, Vec<ConfInfo>, String) {
    // Git reads `trailer.*` globals first (`git_trailer_default_config`), then per-alias keys.
    // Each `trailer.<alias>.*` entry inherits the current global defaults at creation time.
    let mut default_conf = ConfInfo::default();
    let mut map: HashMap<String, ConfInfo> = HashMap::new();
    let mut separators = ":".to_string();

    for ConfigEntry { key, value, .. } in cfg.entries() {
        let Some(var) = key
            .strip_prefix("trailer.")
            .filter(|rest| !rest.contains('.'))
        else {
            continue;
        };
        if !default_conf.apply_placement_setting(var, value.as_deref())
            && var.eq_ignore_ascii_case("separators")
        {
            if let Some(v) = value {
                separators = v.clone();
            }
        }
    }

    for ConfigEntry { key, value, .. } in cfg.entries() {
        let Some((name_part, var)) = key
            .strip_prefix("trailer.")
            .and_then(|rest| rest.rsplit_once('.'))
        else {
            continue;
        };

        let entry = map
            .entry(name_part.to_string())
            .or_insert_with(|| ConfInfo {
                name: name_part.to_string(),
                ..default_conf.clone()
            });
        if entry.apply_placement_setting(var, value.as_deref()) {
            continue;
        }
        let slot = if var.eq_ignore_ascii_case("key") {
            &mut entry.key
        } else if var.eq_ignore_ascii_case("command") {
            &mut entry.command
        } else if var.eq_ignore_ascii_case("cmd") {
            &mut entry.cmd
        } else {
            continue;
        };
        if let Some(v) = value {
            *slot = Some(v.clone());
        }
    }

    (default_conf, map.into_values().collect(), separators)
}

fn trailer_block_get(
    input: &str,
    opts: &ProcessTrailerOptions,
    conf: &[ConfInfo],
    separators: &str,
    comment_prefix: &str,
) -> TrailerBlock {
    let end_of_log = find_end_of_log_message(input, opts.no_divider, comment_prefix);
    let trailer_start =
        find_trailer_block_start(input, end_of_log, conf, separators, comment_prefix);
    let mut lines: Vec<String> = Vec::new();
    let mut continues_trailer = false;
    for line in input[trailer_start..end_of_log].split_terminator('\n') {
        if continues_trailer && line.starts_with([' ', '\t']) {
            if let Some(last) = lines.last_mut() {
                last.push('\n');
                last.push_str(line);
            }
            continue;
        }
        continues_trailer = find_separator(line, separators)
            .is_some_and(|p| p >= 1 && !line.starts_with(char::is_whitespace));
        lines.push(line.to_string());
    }

    TrailerBlock {
        blank_line_before: ends_with_blank_line(input, trailer_start),
        start: trailer_start,
        end: end_of_log,
        lines,
    }
}

fn parse_trailers_from_input(
    block: &TrailerBlock,
    opts: &ProcessTrailerOptions,
    conf: &[ConfInfo],
    separators: &str,
    comment_prefix: &str,
) -> Vec<TrailerItem> {
    block
        .lines
        .iter()
        .filter(|line| !line.starts_with(comment_prefix))
        .filter_map(|line| match find_separator(line, separators) {
            Some(0) => None,
            Some(_) => {
                let (tok, val, _) = parse_trailer(line, separators, conf);
                Some(TrailerItem {
                    token: Some(tok),
                    value: if opts.unfold { unfold_value(&val) } else { val },
                })
            }
            None => (!opts.only_trailers).then(|| TrailerItem {
                token: None,
                value: line.clone(),
            }),
        })
        .collect()
}

fn apply_command(conf: &ConfInfo, arg: Option<&str>, cwd: Option<&Path>) -> String {
    let arg = arg.unwrap_or("");
    let dir = cwd.unwrap_or_else(|| Path::new("."));
    use crate::command_runner::{
        system_command_runner, CommandEnvironment, CommandSpec, CommandStdin, CommandStdio,
        ShellInvocation,
    };
    let runner = system_command_runner();
    let output = if let Some(cmd) = &conf.cmd {
        let script = format!("{cmd} \"$@\"");
        let spec = CommandSpec {
            program: "sh".into(),
            args: Vec::new(),
            shell: Some(ShellInvocation::DashC {
                script,
                argv0: Some(cmd.clone()),
                args: vec![std::ffi::OsString::from(arg)],
            }),
            cwd: Some(dir.to_path_buf()),
            env: CommandEnvironment::inherit_process_only(),
            stdin: CommandStdin::Null,
            stdout: CommandStdio::Pipe,
            stderr: CommandStdio::Inherit,
        };
        runner
            .spawn(&spec)
            .ok()
            .and_then(|h| h.wait_with_output().ok())
    } else if let Some(command) = &conf.command {
        let cmd_line = command.replace(TRAILER_ARG_PLACEHOLDER, arg);
        crate::command_runner::run_sh_dash_c(
            runner.as_ref(),
            &cmd_line,
            Some(dir),
            CommandStdin::Null,
            CommandStdio::Inherit,
        )
        .ok()
    } else {
        return String::new();
    };

    match output {
        Some(o) if o.status.success => String::from_utf8_lossy(&o.stdout).trim().to_string(),
        _ => String::new(),
    }
}

/// Whether `arg` differs from every trailer visited from `start_idx`: only that trailer when
/// `check_all` is false, otherwise every trailer from it towards the placement end.
fn check_if_different(
    head: &[TrailerItem],
    start_idx: usize,
    arg: &ArgItem,
    check_all: bool,
) -> bool {
    let visited = if !check_all {
        &head[start_idx..=start_idx]
    } else if after_or_end(arg.conf.where_) {
        &head[..=start_idx]
    } else {
        &head[start_idx..]
    };
    !visited.iter().any(|item| same_trailer(item, arg))
}

/// Inserts `item` after (`after` / `end`) or before `head[anchor_idx]` and returns its index.
fn insert_relative_to(
    head: &mut Vec<TrailerItem>,
    anchor_idx: usize,
    item: TrailerItem,
    where_: TrailerWhere,
) -> usize {
    let at = match head.len().checked_sub(1) {
        None => 0,
        Some(last) if after_or_end(where_) => anchor_idx.min(last) + 1,
        Some(last) => anchor_idx.min(last),
    };
    head.insert(at, item);
    at
}

fn apply_item_command(
    head: &[TrailerItem],
    in_idx: Option<usize>,
    arg: &mut ArgItem,
    cwd: Option<&Path>,
) {
    if arg.conf.command.is_none() && arg.conf.cmd.is_none() {
        return;
    }
    let in_val = in_idx
        .and_then(|i| head.get(i))
        .filter(|t| t.token.is_some())
        .map(|t| t.value.as_str());
    let arg_for_cmd = if arg.value.is_empty() {
        in_val
    } else {
        Some(arg.value.as_str())
    };
    arg.value = apply_command(&arg.conf, arg_for_cmd, cwd);
}

fn apply_arg_if_exists(
    head: &mut Vec<TrailerItem>,
    in_idx: usize,
    mut arg: ArgItem,
    on_idx: usize,
    cwd: Option<&Path>,
) {
    let if_exists = arg.conf.if_exists;
    if matches!(
        if_exists,
        TrailerIfExists::DoNothing | TrailerIfExists::Default
    ) {
        return;
    }
    apply_item_command(head, Some(in_idx), &mut arg, cwd);
    let add = match if_exists {
        TrailerIfExists::AddIfDifferent => check_if_different(head, in_idx, &arg, true),
        TrailerIfExists::AddIfDifferentNeighbor => check_if_different(head, on_idx, &arg, false),
        _ => true,
    };
    if !add {
        return;
    }
    let where_ = arg.conf.where_;
    let at = insert_relative_to(head, on_idx, arg.into_item(), where_);
    if if_exists == TrailerIfExists::Replace {
        // The replacement is placed relative to the anchor before the old trailer is removed.
        head.remove(if at <= in_idx { in_idx + 1 } else { in_idx });
    }
}

fn apply_arg_if_missing(head: &mut Vec<TrailerItem>, mut arg: ArgItem, cwd: Option<&Path>) {
    if arg.conf.if_missing == TrailerIfMissing::DoNothing {
        return;
    }
    apply_item_command(head, None, &mut arg, cwd);
    let at = if after_or_end(arg.conf.where_) {
        head.len()
    } else {
        0
    };
    head.insert(at, arg.into_item());
}

/// Returns `None` if `arg` was applied to an existing trailer, `Some(arg)` if no match.
fn find_same_and_apply_arg(
    head: &mut Vec<TrailerItem>,
    arg: ArgItem,
    cwd: Option<&Path>,
) -> Option<ArgItem> {
    let where_ = arg.conf.where_;
    let backwards = after_or_end(where_);
    let same = |item: &TrailerItem| same_token(item.token.as_deref(), &arg.token);
    let found = if backwards {
        head.iter().rposition(same)
    } else {
        head.iter().position(same)
    };
    let Some(idx) = found else {
        return Some(arg);
    };
    let on_idx = match where_ {
        TrailerWhere::After | TrailerWhere::Before => idx,
        _ if backwards => head.len() - 1,
        _ => 0,
    };
    apply_arg_if_exists(head, idx, arg, on_idx, cwd);
    None
}

fn merge_arg_conf(mut conf: ConfInfo, new_arg: &NewTrailerArg) -> ConfInfo {
    if new_arg.where_ != TrailerWhere::Default {
        conf.where_ = new_arg.where_;
    }
    if new_arg.if_exists != TrailerIfExists::Default {
        conf.if_exists = new_arg.if_exists;
    }
    if new_arg.if_missing != TrailerIfMissing::Default {
        conf.if_missing = new_arg.if_missing;
    }
    conf
}

fn parse_trailers_from_config(conf_list: &[ConfInfo]) -> Vec<ArgItem> {
    conf_list
        .iter()
        .filter(|item| item.command.is_some())
        .map(|item| ArgItem {
            token: token_from_item(item, &item.name),
            value: String::new(),
            conf: item.clone(),
        })
        .collect()
}

fn parse_command_line_trailers(
    new_args: &[NewTrailerArg],
    default_conf: &ConfInfo,
    conf_list: &[ConfInfo],
    separators: &str,
) -> Vec<ArgItem> {
    let cl_separators = format!("={separators}");
    new_args
        .iter()
        .filter(|nt| find_separator(&nt.text, &cl_separators) != Some(0))
        .map(|nt| {
            let (token, value, picked) = parse_trailer(&nt.text, &cl_separators, conf_list);
            let base = picked
                .filter(|item| !item.name.is_empty())
                .unwrap_or(default_conf);
            ArgItem {
                token,
                value,
                conf: merge_arg_conf(base.clone(), nt),
            }
        })
        .collect()
}

fn process_trailers_lists(head: &mut Vec<TrailerItem>, args: Vec<ArgItem>, cwd: Option<&Path>) {
    for arg_tok in args {
        if let Some(a) = find_same_and_apply_arg(head, arg_tok, cwd) {
            apply_arg_if_missing(head, a, cwd);
        }
    }
}

fn format_trailers_list(
    items: &[TrailerItem],
    opts: &ProcessTrailerOptions,
    separators: &str,
    out: &mut String,
) {
    for item in items {
        match &item.token {
            None if opts.only_trailers => continue,
            None => {}
            Some(_) if opts.trim_empty && item.value.is_empty() => continue,
            Some(tok) => {
                out.push_str(tok);
                if last_non_space_char(tok).is_none_or(|ch| !separators.contains(ch)) {
                    out.push(separators.chars().next().unwrap_or(':'));
                    out.push(' ');
                }
            }
        }
        out.push_str(&item.value);
        out.push('\n');
    }
}

/// Process a commit message: parse trailer block, apply config and `--trailer` args, emit result.
///
/// `git_dir` selects which repository config to load (with standard cascade). When `None`, only
/// non-repo config layers are used (matches `git interpret-trailers` outside a repo).
pub fn process_trailers(
    input: &str,
    opts: &ProcessTrailerOptions,
    new_trailer_args: &[NewTrailerArg],
    git_dir: Option<&Path>,
) -> String {
    let cfg = ConfigSet::load(&crate::environment::Environment::empty(), git_dir, true)
        .unwrap_or_default();
    let comment_prefix = comment_line_prefix(&cfg);
    let (default_conf, conf_list, separators) = load_trailer_config(&cfg);

    let block = trailer_block_get(input, opts, &conf_list, &separators, &comment_prefix);
    let mut head =
        parse_trailers_from_input(&block, opts, &conf_list, &separators, &comment_prefix);

    if !opts.only_input {
        let mut arg_queue = parse_trailers_from_config(&conf_list);
        arg_queue.extend(parse_command_line_trailers(
            new_trailer_args,
            &default_conf,
            &conf_list,
            &separators,
        ));
        let cwd = Some(crate::environment::Environment::empty().cwd.clone());
        process_trailers_lists(&mut head, arg_queue, cwd.as_deref());
    }

    let mut out = String::new();
    if !opts.only_trailers {
        out.push_str(&input[..block.start]);
        if !block.blank_line_before {
            out.push('\n');
        }
    }
    format_trailers_list(&head, opts, &separators, &mut out);
    if !opts.only_trailers {
        out.push_str(&input[block.end..]);
    }
    out
}

/// Complete stdin/file input with a trailing newline when missing (Git `strbuf_complete_line`).
pub fn complete_line(s: &str) -> String {
    if s.ends_with('\n') {
        s.to_string()
    } else {
        format!("{s}\n")
    }
}

#[cfg(test)]
mod line_coverage_tests {
    use super::*;
    use tempfile::tempdir;

    #[cfg_attr(
        not(feature = "full-tests"),
        ignore = "starts system git or is heavy; run with --features full-tests"
    )]
    #[test]
    fn last_line_start_finds_the_line_ending_at_len() {
        let buf = "ab\ncd\n\né\n";
        assert_eq!(last_line_start(buf, 0), None);
        assert_eq!(last_line_start(buf, 1), Some(0));
        assert_eq!(last_line_start(buf, 3), Some(0));
        assert_eq!(last_line_start(buf, 6), Some(3));
        assert_eq!(last_line_start(buf, 7), Some(6));
        assert_eq!(last_line_start(buf, buf.len()), Some(7));
        assert_eq!(last_line_start("é", "é".len()), Some(0));
    }

    #[test]
    fn trailer_enum_parsers_and_process_trailers_matrix() {
        assert_eq!(trailer_where_from_str("after"), Some(TrailerWhere::After));
        assert_eq!(
            trailer_if_exists_from_str("addIfDifferent"),
            Some(TrailerIfExists::AddIfDifferent)
        );
        assert_eq!(
            trailer_if_missing_from_str("doNothing"),
            Some(TrailerIfMissing::DoNothing)
        );
        assert_eq!(complete_line("x"), "x\n");
        assert_eq!(complete_line("x\n"), "x\n");

        let dir = tempdir().expect("tempdir");
        std::process::Command::new("git")
            .current_dir(dir.path())
            .args(["init", "-q", "-b", "main", "."])
            .status()
            .expect("init");
        let git_dir = dir.path().join(".git");
        let input = "Subject\n\nSigned-off-by: A <a@example.com>\n";
        for only_input in [true, false] {
            for only_trailers in [true, false] {
                let opts = ProcessTrailerOptions {
                    trim_empty: true,
                    only_trailers,
                    only_input,
                    unfold: true,
                    no_divider: false,
                };
                let _ = process_trailers(input, &opts, &[], Some(git_dir.as_path()));
                let arg = NewTrailerArg {
                    text: "Reviewed-by: B".into(),
                    where_: TrailerWhere::End,
                    if_exists: TrailerIfExists::Add,
                    if_missing: TrailerIfMissing::Add,
                };
                let _ = process_trailers(input, &opts, &[arg], Some(git_dir.as_path()));
            }
        }
    }
}