lintje 0.11.3

Lintje is an opinionated linter for Git.
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
pub mod hooks;

use regex::Regex;

use crate::branch::Branch;
use crate::command::{run_command, run_command_with_stdin};
use crate::commit::Commit;

const SCISSORS: &str = "------------------------ >8 ------------------------";
const COMMIT_DELIMITER: &str = "------------------------ COMMIT >! ------------------------";
const COMMIT_BODY_DELIMITER: &str = "------------------------ BODY >! ------------------------";
const COMMIT_TRAILERS_DELIMITER: &str =
    "------------------------ TRAILERS >! ------------------------";

lazy_static! {
    pub static ref SUBJECT_WITH_MERGE_REMOTE_BRANCH: Regex =
        Regex::new(r"^Merge branch '.+' of .+ into .+").unwrap();
    static ref SUBJECT_WITH_SQUASH_PR: Regex = Regex::new(r".+ \(#\d+\)$").unwrap();
    static ref MESSAGE_CONTAINS_MERGE_REQUEST_REFERENCE: Regex =
        Regex::new(r"^See merge request .+/.+!\d+$").unwrap();
    static ref SUBJECT_WITH_MERGE_ONLY: Regex =
        Regex::new(r"Merge [a-z0-9]{40} into [a-z0-9]{40}").unwrap();
}

#[derive(Debug, PartialEq)]
enum CleanupMode {
    Strip,
    Whitespace,
    Verbatim,
    Scissors,
    Default,
}

pub fn fetch_and_parse_branch() -> Result<Branch, String> {
    let output = match run_command("git", &["rev-parse", "--abbrev-ref", "HEAD"]) {
        Ok(o) => o,
        Err(e) => {
            debug!("Failed to fetch Git branch: {:?}", e);
            return Err(e.message());
        }
    };
    let name = output.trim().to_string();
    let mut branch = Branch::new(name);
    branch.validate();
    Ok(branch)
}

pub fn fetch_and_parse_commits(selector: &Option<String>) -> Result<Vec<Commit>, String> {
    let mut commits = Vec::<Commit>::new();
    // Format definition per commit:
    // COMMIT_DELIMITER: Tell commits apart, split the string on this later
    // Format:
    //   - Line 1: Commit SHA in long form
    //   - Line 2: Commit author email address
    //   - Line 3 to end of body: Commit subject and message, including trailers
    // COMMIT_TRAILERS_DELIMITER: Separator for commit message body and trailers
    // %(trailers): Trailers, based on https://git-scm.com/docs/git-interpret-trailers/
    // COMMIT_BODY_DELIMITER: Separator for end of the message body and trailers
    // `--name-only`: Prints filenames of files changed
    let commit_format = "%H%n%ae%n%B";
    let mut args = vec![
        "log".to_string(),
        format!(
            "--pretty=\
             {COMMIT_DELIMITER}%n\
             {commit_format}%n\
             {COMMIT_TRAILERS_DELIMITER}%n\
             %(trailers)%n\
             {COMMIT_BODY_DELIMITER}"
        ),
        "--name-only".to_string(),
    ];
    match selector {
        Some(selection) => {
            let selection = selection.trim().to_string();
            if !selection.contains("..") {
                // Only select one commit if no commit range was selected
                args.push("-n 1".to_string());
            }
            args.push(selection);
        }
        None => {
            args.push("-n 1".to_string());
            args.push("HEAD".to_string());
        }
    };

    let output = match run_command("git", &args) {
        Ok(o) => o,
        Err(e) => {
            debug!("Failed to fetch Git log: {:?}", e);
            return Err(e.message());
        }
    };
    let messages = output.split(COMMIT_DELIMITER);
    for message in messages {
        let trimmed_message = message.trim();
        if !trimmed_message.is_empty() {
            match parse_commit(trimmed_message) {
                Some(commit) => commits.push(commit),
                None => debug!("Commit ignored: {:?}", message),
            }
        }
    }
    Ok(commits)
}

fn parse_commit(message: &str) -> Option<Commit> {
    let mut long_sha = None;
    let mut email = None;
    let mut subject = None;
    let mut message_lines = vec![];
    let mut message_parts = message.split(COMMIT_TRAILERS_DELIMITER);
    match message_parts.next() {
        Some(body) => {
            for (index, line) in body.lines().enumerate() {
                match index {
                    0 => long_sha = Some(line),
                    1 => email = Some(line.to_string()),
                    2 => subject = Some(line),
                    _ => message_lines.push(line.to_string()),
                }
            }
        }
        None => error!("No commit body found!"),
    }

    let mut extras_str = message_parts
        .next()
        .unwrap_or("")
        .split(COMMIT_BODY_DELIMITER);
    let trailers = extras_str.next().unwrap_or("").trim().to_string();
    let file_changes_str = extras_str.next().unwrap_or("").trim();
    let file_changes = file_changes_str
        .lines()
        .map(std::string::ToString::to_string)
        .collect::<Vec<String>>();
    if file_changes.is_empty() {
        debug!("No stats found for commit '{}'", long_sha.unwrap_or(""));
    } else {
        debug!(
            "Stats line found for commit '{}': {}",
            long_sha.unwrap_or(""),
            file_changes.join(", ")
        );
    }

    // Trailers are included twice, once for the body and once for the trailers. Replace the
    // trailers in the body, we already have them in the trailers string.
    let message_body_str = message_lines.join("\n");
    let message_body = strip_trailers_from_message(&message_body_str, &trailers);

    match (long_sha, subject) {
        (Some(long_sha), subject) => {
            let used_subject = subject.unwrap_or_else(|| {
                debug!("Commit subject not present in message: {:?}", message);
                ""
            });
            Some(Commit::new(
                Some(long_sha.to_string()),
                email,
                used_subject,
                message_body,
                trailers,
                file_changes,
            ))
        }
        _ => {
            debug!("Commit ignored: SHA was not present: {}", message);
            None
        }
    }
}

pub fn parse_commit_file(contents: &str) -> Commit {
    let (subject, message) = parse_commit_hook_format(contents, &cleanup_mode(), &comment_char());
    let trailers = parse_trailers_from_message([subject.to_owned(), message.to_owned()].join("\n"));
    let message = strip_trailers_from_message(&message, &trailers);
    // Run the diff command to fetch the current staged changes and determine if the commit is
    // empty or not. The contents of the commit message file is too unreliable as it depends on
    // user config and how the user called the `git commit` command.
    let file_changes = current_file_changes();
    Commit::new(None, None, &subject, message, trailers, file_changes)
}

fn parse_commit_hook_format(
    file_contents: &str,
    cleanup_mode: &CleanupMode,
    comment_char: &str,
) -> (String, String) {
    let mut subject = None;
    let mut message_lines = vec![];
    let scissor_line = format!("{} {}", comment_char, SCISSORS);
    debug!("Using clean up mode: {:?}", cleanup_mode);
    debug!("Using config core.commentChar: {:?}", comment_char);
    for line in file_contents.lines() {
        // A scissor line has been detected.
        //
        // A couple reasons why this could happen:
        //
        // - A scissor line was found in cleanup mode "scissors". All content after this line is
        //   ignored.
        // - A scissor line was found in a different cleanup mode with the `--verbose` option.
        //   Lintje cannot detect this verbose mode so it assumes it's for the verbose mode
        //   and ignores all content after this line.
        // - The commit message is entirely empty, leaving only the comments added to the file by
        //   Git. Unless `--allow-empty-message` is specified this is the user telling Git it stop
        //   the commit process.
        if line == scissor_line {
            debug!("Found scissors line. Stop parsing message.");
            break;
        }

        // The first non-empty line is the subject line in every cleanup mode but the Verbatim
        // mode.
        if subject.is_none() {
            if cleanup_mode == &CleanupMode::Verbatim {
                // Set subject, doesn't matter what the content is. Even empty lines are considered
                // subjects in Verbatim cleanup mode.
                subject = Some(line.to_string());
            } else if let Some(cleaned_line) = cleanup_line(line, cleanup_mode, comment_char) {
                if !cleaned_line.is_empty() {
                    // Skip leading empty lines in every other cleanup mode than Verbatim.
                    subject = Some(cleaned_line);
                }
            }
            // Skips this line if the cleanup mode is Strip and the line is a comment.
            // See when the `cleanup_line` function returns `None`.
            continue;
        }

        if let Some(cleaned_line) = cleanup_line(line, cleanup_mode, comment_char) {
            message_lines.push(cleaned_line);
        }
    }
    let used_subject = subject.unwrap_or_else(|| {
        debug!("Commit subject not present in message: {:?}", file_contents);
        "".to_string()
    });

    (used_subject, message_lines.join("\n"))
}

fn parse_trailers_from_message(message: String) -> String {
    match run_command_with_stdin("git", &["interpret-trailers", "--only-trailers"], message) {
        Ok(stdout) => stdout.trim().to_string(),
        Err(e) => {
            error!(
                "Unable to determine commit message trailers.\nError: {:?}",
                e
            );
            "".to_string()
        }
    }
}

fn cleanup_line(line: &str, cleanup_mode: &CleanupMode, comment_char: &str) -> Option<String> {
    match cleanup_mode {
        CleanupMode::Default | CleanupMode::Strip => {
            if line.starts_with(comment_char) {
                return None;
            }
            Some(line.trim_end().to_string())
        }
        CleanupMode::Whitespace | CleanupMode::Scissors => Some(line.trim_end().to_string()),
        CleanupMode::Verbatim => Some(line.to_string()),
    }
}

pub fn is_commit_ignored(commit: &Commit) -> bool {
    let subject = &commit.subject;
    let message = &commit.message;
    if let Some(email) = &commit.email {
        if email.ends_with("[bot]@users.noreply.github.com") {
            debug!(
                "Ignoring commit because it is from a bot account: {}",
                email
            );
            return true;
        }
    }
    if subject.starts_with("Merge tag ") {
        debug!(
            "Ignoring commit because it's a merge commit of a tag: {}",
            subject
        );
        return true;
    }
    if subject.starts_with("Merge pull request") {
        debug!(
            "Ignoring commit because it's a 'merge pull request' commit: {}",
            subject
        );
        return true;
    }
    if subject.starts_with("Merge branch ")
        && MESSAGE_CONTAINS_MERGE_REQUEST_REFERENCE.is_match(message)
    {
        debug!(
            "Ignoring commit because it's a 'merge request' commit: {}",
            subject
        );
        return true;
    }
    if SUBJECT_WITH_SQUASH_PR.is_match(subject) {
        // Subject ends with a GitHub squash PR marker: ` (#123)`
        debug!(
            "Ignoring commit because it's a 'merge pull request' squash commit: {}",
            subject
        );
        return true;
    }
    if subject.starts_with("Merge branch ") && !SUBJECT_WITH_MERGE_REMOTE_BRANCH.is_match(subject) {
        debug!(
            "Ignoring commit because it's a local merge commit: {}",
            subject
        );
        return true;
    }
    if subject.starts_with("Revert \"")
        && subject.ends_with('"')
        && message.contains("This reverts commit ")
    {
        debug!("Ignoring commit because it's a revert commit: {}", subject);
        return true;
    }
    if SUBJECT_WITH_MERGE_ONLY.is_match(subject) {
        debug!(
            "Ignoring commit because it's a merge into commit: {}",
            subject
        );
        return true;
    }

    false
}

fn cleanup_mode() -> CleanupMode {
    match run_command("git", &["config", "commit.cleanup"]) {
        Ok(stdout) => match stdout.trim() {
            "default" | "" => CleanupMode::Default,
            "scissors" => CleanupMode::Scissors,
            "strip" => CleanupMode::Strip,
            "verbatim" => CleanupMode::Verbatim,
            "whitespace" => CleanupMode::Whitespace,
            option => {
                info!(
                    "Unsupported Git commit.cleanup config: {}\nFalling back on 'default'.",
                    option
                );
                CleanupMode::Default
            }
        },
        Err(e) => {
            let message = format!(
                "Unable to determine Git's commit.cleanup config. \
                Falling back on default commit.cleanup config.\nError: {:?}",
                e
            );
            if e.error.is_exit_code(1) {
                // Git returns exit code 1 if the config option is not set
                // So no need to error when that happens
                debug!("{}", message);
            } else {
                // Other error that we do not expect so print the error
                error!("{}", message);
            }
            CleanupMode::Default
        }
    }
}

fn comment_char() -> String {
    match run_command("git", &["config", "core.commentChar"]) {
        Ok(stdout) => {
            let character = stdout.trim().to_string();
            if character.is_empty() {
                debug!("No Git core.commentChar config found. Using default `#` character.");
                "#".to_string()
            } else {
                character
            }
        }
        Err(e) => {
            let message = format!(
                "Unable to determine Git's core.commentChar config. \
                Falling back on default core.commentChar: `#`\nError: {:?}",
                e
            );
            if e.error.is_exit_code(1) {
                // Git returns exit code 1 if the config option is not set
                // So no need to error when that happens
                debug!("{}", message);
            } else {
                // Other error that we do not expect so print the error
                error!("{}", message);
            }
            "#".to_string()
        }
    }
}

fn current_file_changes() -> Vec<String> {
    match run_command("git", &["diff", "--cached", "--name-only"]) {
        Ok(stdout) => stdout
            .trim()
            .lines()
            .map(std::string::ToString::to_string)
            .collect::<Vec<String>>(),
        Err(e) => {
            error!("Unable to determine commit changes.\nError: {:?}", e);
            vec![]
        }
    }
}

pub fn repo_has_changesets() -> bool {
    // Find all changesets directories in the repo
    match run_command(
        "git",
        &[
            "ls-files",
            "--cached",  // Only committed files
            "--ignored", // List all --exclude matches
            "--exclude=.changesets/",
            "--exclude=**/*/.changesets/", // Match sub directories
            "--exclude=.changeset/",
            "--exclude=**/*/.changeset/", // Match sub directories
        ],
    ) {
        Ok(stdout) => {
            // If no output is printed no changeset directory was found
            !stdout.is_empty()
        }
        Err(e) => {
            // Other error that we do not expect so print the error
            let message = format!("Unable to read files from Git repository.\nError: {:?}", e);
            error!("{}", message);
            false
        }
    }
}

pub fn strip_trailers_from_message(message: &str, trailers: &str) -> String {
    let (body, _removed_trailers) = message
        .rsplit_once(&trailers) // Split from the back so only the last occurrence of the trailers is removed
        .unwrap_or(("", ""));
    body.trim_end().to_string()
}

#[cfg(test)]
mod tests {
    use super::Commit;
    use super::{
        is_commit_ignored, parse_commit, parse_commit_hook_format, strip_trailers_from_message,
        CleanupMode, COMMIT_BODY_DELIMITER, COMMIT_TRAILERS_DELIMITER,
    };
    use crate::config::ValidationContext;
    use crate::issue::IssueType;

    fn default_context() -> ValidationContext {
        ValidationContext { changesets: false }
    }

    fn assert_commit_is_invalid(commit: &mut Commit) {
        commit.validate(&default_context());
        assert!(!commit.issues.is_empty());
    }

    fn assert_commit_is_ignored(result: &Option<Commit>) {
        match result {
            Some(commit) => assert!(is_commit_ignored(commit)),
            None => panic!("Result is not a commit!"),
        }
    }

    fn assert_commit_is_not_ignored(result: &Option<Commit>) {
        match result {
            Some(commit) => assert!(!is_commit_ignored(commit)),
            None => panic!("Result is not a commit!"),
        }
    }

    fn commit_with_file_changes(message: &str) -> String {
        format!(
            "{}\n{COMMIT_TRAILERS_DELIMITER}\n{COMMIT_BODY_DELIMITER}\n{}",
            message, "\nsrc/main.rs\nsrc/utils.rs\n"
        )
    }

    fn commit_without_file_changes(message: &str) -> String {
        format!(
            "{}\n{COMMIT_TRAILERS_DELIMITER}\n{COMMIT_BODY_DELIMITER}\n{}",
            message, ""
        )
    }

    fn commit_with_trailers(message: &str, trailers: &str) -> String {
        format!(
            "{message}\n\
             \n\
             {trailers}
             {COMMIT_TRAILERS_DELIMITER}\n\
             {trailers}\n\
             {COMMIT_BODY_DELIMITER}\n\
             src/main.rs\n\
             README.md\n",
        )
    }

    #[test]
    fn test_parse_commit() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        This is a subject\n\
        \n\
        This is my multi line message.\n\
        Line 2.",
        ));

        assert_commit_is_not_ignored(&result);
        let commit = result.unwrap();
        assert_eq!(
            commit.long_sha,
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
        );
        assert_eq!(commit.short_sha, Some("aaaaaaa".to_string()));
        assert_eq!(commit.email, Some("test@example.com".to_string()));
        assert_eq!(commit.subject, "This is a subject");
        assert_eq!(commit.message, "\nThis is my multi line message.\nLine 2.");
        assert_eq!(commit.trailers, "");
        assert_eq!(
            commit.file_changes,
            vec!["src/main.rs".to_string(), "src/utils.rs".to_string()]
        );
        assert!(!commit
            .issues
            .into_iter()
            .any(|i| i.r#type == IssueType::Error));
    }

    #[test]
    fn test_parse_commit_with_trailers() {
        let result = parse_commit(&commit_with_trailers(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
            test@example.com\n\
            This is a subject\n\
            \n\
            This is a message\n",
            "Co-authored-by: Person A <name@domain.com>\n\
             Co-authored-by: Person B <name@domain.com>\n",
        ));

        assert_commit_is_not_ignored(&result);
        let commit = result.unwrap();
        assert_eq!(
            commit.long_sha,
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
        );
        assert_eq!(commit.short_sha, Some("aaaaaaa".to_string()));
        assert_eq!(commit.email, Some("test@example.com".to_string()));
        assert_eq!(commit.subject, "This is a subject");
        assert_eq!(commit.message, "\nThis is a message");
        assert_eq!(
            commit.trailers,
            "Co-authored-by: Person A <name@domain.com>\n\
             Co-authored-by: Person B <name@domain.com>"
        );
        assert!(commit.has_changes());
    }

    #[test]
    fn test_parse_commit_with_errors() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
            test@example.com\n\
            This is a subject",
        ));

        assert_commit_is_not_ignored(&result);
        let mut commit = result.unwrap();
        assert_eq!(
            commit.long_sha,
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
        );
        assert_eq!(commit.short_sha, Some("aaaaaaa".to_string()));
        assert_eq!(commit.email, Some("test@example.com".to_string()));
        assert_eq!(commit.subject, "This is a subject");
        assert_eq!(commit.message, "");
        assert!(commit.has_changes());
        assert_commit_is_invalid(&mut commit);
    }

    #[test]
    fn test_parse_commit_empty() {
        let result = parse_commit("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");

        assert_commit_is_not_ignored(&result);
        let mut commit = result.unwrap();
        assert_eq!(
            commit.long_sha,
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
        );
        assert_eq!(commit.short_sha, Some("aaaaaaa".to_string()));
        assert_eq!(commit.email, None);
        assert_eq!(commit.subject, "");
        assert_eq!(commit.message, "");
        assert!(!commit.has_changes());
        assert_commit_is_invalid(&mut commit);
    }

    #[test]
    fn test_parse_commit_without_file_changes() {
        let result = parse_commit(&commit_without_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
            test@example.com\n\
            This is a subject\n\
            \n\
            This is a message.",
        ));

        assert_commit_is_not_ignored(&result);
        let mut commit = result.unwrap();
        assert_eq!(
            commit.long_sha,
            Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string())
        );
        assert_eq!(commit.short_sha, Some("aaaaaaa".to_string()));
        assert_eq!(commit.email, Some("test@example.com".to_string()));
        assert_eq!(commit.subject, "This is a subject");
        assert_eq!(commit.message, "\nThis is a message.");
        assert_eq!(commit.file_changes, Vec::<String>::new());
        assert!(!commit.has_changes());
        assert_commit_is_invalid(&mut commit);
    }

    #[test]
    fn test_parse_commit_ignore_bot_commit() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
            12345678+bot-name[bot]@users.noreply.github.com\n\
            Commit by bot without description",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_tag_merge_commit() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge tag 'v1.2.3' into main",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_merge_commit_pull_request() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge pull request #123 from tombruijn/repo\n\
        \n\
        This is my multi line message.\n\
        Line 2.",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_squash_merge_commit_pull_request() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Fix some issue that's squashed (#123)\n\
        \n\
        This is my multi line message.\n\
        Line 2.",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_merge_commits_merge_request() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge branch 'branch' into main\n\
        \n\
        This is my multi line message.\n\
        Line 2.\n\
        \n\
        See merge request gitlab-org/repo!123",
        ));

        assert_commit_is_ignored(&result);

        // This is not a full reference, but a shorthand. GitLab merge commits
        // use the full org + repo + Merge Request ID reference.
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Fix some issue\n\
        \n\
        This is my multi line message.\n\
        Line 2.\n\
        \n\
        See merge request !123 for more info about the orignal fix",
        ));

        assert_commit_is_not_ignored(&result);

        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Fix some issue\n\
        \n\
        This is my multi line message.\n\
        Line 2.\n\
        \n\
        Also See merge request !123",
        ));

        assert_commit_is_not_ignored(&result);

        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Fix some issue\n\
        \n\
        This is my multi line message.\n\
        Line 2. See merge request org/repo!123",
        ));

        assert_commit_is_not_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_merge_commits_without_into() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge branch 'branch'",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_ignore_revert_commit() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
            test@example.com\n\
            Revert \"Some commit\"\n\
            \n\
            This reverts commit 0d02b90cbf0c79acf9c0b56de00d52389272ec6f",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_merge_remote_commits() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge branch 'branch' of github.com/org/repo into branch",
        ));

        assert_commit_is_not_ignored(&result);
    }

    #[test]
    fn test_parse_commit_merge_into_commit() {
        let result = parse_commit(&commit_with_file_changes(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n\
        test@example.com\n\
        Merge 3af48fbbdf7c2bd77c35e829bc7561fb7c660b21 into 17e2def8fbb2a0d500bffb79c7fe85381f24d415",
        ));

        assert_commit_is_ignored(&result);
    }

    #[test]
    fn test_parse_commit_hook_format() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject\n\nThis is a message.",
            &CleanupMode::Default,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is a message.");
    }

    #[test]
    fn test_parse_commit_hook_format_without_message() {
        let (subject, message) =
            parse_commit_hook_format("This is a subject", &CleanupMode::Default, "#");

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "");
    }

    // Same as Default
    #[test]
    fn test_parse_commit_hook_format_with_strip() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.  \n\
            # This is a commented line.\n\
            \n\
            Another line.\n\
            \n\
            # Other things that are not part of the message.\n\
            ",
            &CleanupMode::Strip,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.\n\nAnother line.\n");
    }

    #[test]
    fn test_parse_commit_hook_format_with_leading_empty_lines() {
        let (subject, message) = parse_commit_hook_format(
            "  \n\
            This is a subject  \n\
            \n\
            This is the message body.  \n\
            # This is a commented line.\n\
            \n\
            Another line.\n\
            \n\
            # Other things that are not part of the message.\n\
            ",
            &CleanupMode::Strip,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.\n\nAnother line.\n");
    }

    #[test]
    fn test_parse_commit_hook_format_with_leading_comment_lines() {
        let (subject, message) = parse_commit_hook_format(
            "# This is a comment\n\
            This is a subject  \n\
            \n\
            This is the message body.  \n\
            # This is a commented line.\n\
            \n\
            Another line.\n\
            \n\
            # Other things that are not part of the message.\n\
            ",
            &CleanupMode::Strip,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.\n\nAnother line.\n");
    }

    #[test]
    fn test_parse_commit_hook_format_with_strip_custom_comment_char() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.  \n\
            - This is a commented line.\n\
            \n\
            Another line.\n\
            \n\
            - Other things that are not part of the message.\n\
            ",
            &CleanupMode::Strip,
            "-",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.\n\nAnother line.\n");
    }

    #[test]
    fn test_parse_commit_hook_format_with_scissors() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.  \n\
            \n\
            This is line 2.\n\
            # ------------------------ >8 ------------------------\n\
            Other things that are not part of the message.\n\
            ",
            &CleanupMode::Scissors,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.\n\nThis is line 2.");
    }

    #[test]
    fn test_parse_commit_hook_format_with_scissors_empty_message() {
        let (subject, message) = parse_commit_hook_format(
            "# ------------------------ >8 ------------------------\n\
            Other things that are not part of the message.\n\
            ",
            &CleanupMode::Scissors,
            "#",
        );

        assert_eq!(subject, "");
        assert_eq!(message, "");
    }

    #[test]
    fn test_parse_commit_hook_format_with_verbatim() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            ",
            &CleanupMode::Verbatim,
            "#",
        );

        assert_eq!(subject, "This is a subject  ");
        assert_eq!(
            message,
            "\nThis is the message body.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            "
        );
    }

    #[test]
    fn test_parse_commit_hook_format_with_verbatim_leading_empty_lines() {
        let (subject, message) = parse_commit_hook_format(
            "  \n\
            This is the message body.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            ",
            &CleanupMode::Verbatim,
            "#",
        );

        assert_eq!(subject, "  ");
        assert_eq!(
            message,
            "This is the message body.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            "
        );
    }

    #[test]
    fn test_parse_commit_hook_format_with_whitespace() {
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.  \n\
            \n\
            This is line 2.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            ",
            &CleanupMode::Whitespace,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(
            message,
            "\nThis is the message body.\n\
            \n\
            This is line 2.\n\
            # This is a comment\n\
            # Other things that are not part of the message.\n\
            Extra suprise!\
            "
        );
    }

    #[test]
    fn test_parse_commit_hook_format_with_whitespace_leading_comment_lines() {
        let (subject, message) = parse_commit_hook_format(
            "# This is a comment\n\
            This is a subject  \n\
            \n\
            This is the message body.",
            &CleanupMode::Whitespace,
            "#",
        );

        assert_eq!(subject, "# This is a comment");
        assert_eq!(message, "This is a subject\n\nThis is the message body.");
    }

    #[test]
    fn test_parse_commit_hook_format_with_strip_and_scissor_line() {
        // Even in default mode the scissor line is seen as the end of the message.
        // This can happen when `git commit` is called with the `--verbose` flag.
        let (subject, message) = parse_commit_hook_format(
            "This is a subject  \n\
            \n\
            This is the message body.  \n\
            # This is a comment before scissor line\n\
            # ------------------------ >8 ------------------------\n\
            # Other things that are not part of the message.\n\
            List of file changes",
            &CleanupMode::Strip,
            "#",
        );

        assert_eq!(subject, "This is a subject");
        assert_eq!(message, "\nThis is the message body.");
    }

    #[test]
    fn strip_trailers_from_message_without_trailers() {
        let result = strip_trailers_from_message("Subject\n\nMy message body\n", "");
        assert_eq!(result, "Subject\n\nMy message body");
    }

    #[test]
    fn strip_trailers_from_message_with_trailers() {
        let result = strip_trailers_from_message(
            "Subject\n\
            \n\
            My message body\n
            \n\
            Co-authored-by: Person A\n",
            "Co-authored-by: Person A",
        );
        assert_eq!(result, "Subject\n\nMy message body");
    }

    #[test]
    fn strip_trailers_from_message_with_multiple_trailers() {
        let result = strip_trailers_from_message(
            "Subject\n\
            \n\
            My message body\n
            \n\
            Co-authored-by: Person A\n\
            Signed-off-by: Person B\n\
            Fix: #123\n",
            "Co-authored-by: Person A\n\
            Signed-off-by: Person B\n\
            Fix: #123",
        );
        assert_eq!(result, "Subject\n\nMy message body");
    }

    #[test]
    fn strip_trailers_from_message_with_duplicate_trailers() {
        let result = strip_trailers_from_message(
            "Subject\n\
            \n\
            Co-authored-by: Person A\n\
            Signed-off-by: Person B\n
            My message body\n
            \n\
            Co-authored-by: Person A\n\
            Signed-off-by: Person B\n",
            "Co-authored-by: Person A\n\
            Signed-off-by: Person B",
        );
        assert_eq!(
            result,
            "Subject\n\
            \n\
            Co-authored-by: Person A\n\
            Signed-off-by: Person B\n
            My message body"
        );
    }
}