diff --git a/gix/src/repository/mailmap.rs b/gix/src/repository/mailmap.rs index 02921ec142e..ae725216857 100644 --- a/gix/src/repository/mailmap.rs +++ b/gix/src/repository/mailmap.rs @@ -1,7 +1,6 @@ use crate::{Id, config::tree::Mailmap}; impl crate::Repository { - // TODO: tests /// Similar to [`open_mailmap_into()`][crate::Repository::open_mailmap_into()], but ignores all errors and returns at worst /// an empty mailmap, e.g. if there is no mailmap or if there were errors loading them. /// @@ -13,7 +12,6 @@ impl crate::Repository { out } - // TODO: tests /// Try to merge mailmaps from the following locations into `target`: /// /// - read the `.mailmap` file without following symlinks from the working tree, if present diff --git a/gix/tests/fixtures/generated-archives/make_mailmap_repo.tar b/gix/tests/fixtures/generated-archives/make_mailmap_repo.tar new file mode 100644 index 00000000000..ec9434becb8 Binary files /dev/null and b/gix/tests/fixtures/generated-archives/make_mailmap_repo.tar differ diff --git a/gix/tests/fixtures/generated-archives/make_mailmap_repo_sha256.tar b/gix/tests/fixtures/generated-archives/make_mailmap_repo_sha256.tar new file mode 100644 index 00000000000..c755fc473cf Binary files /dev/null and b/gix/tests/fixtures/generated-archives/make_mailmap_repo_sha256.tar differ diff --git a/gix/tests/fixtures/make_mailmap_repo.sh b/gix/tests/fixtures/make_mailmap_repo.sh new file mode 100755 index 00000000000..16ad37d3d27 --- /dev/null +++ b/gix/tests/fixtures/make_mailmap_repo.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -eu -o pipefail + +git init -q +git checkout -b main + +git config user.name "Committer" +git config user.email "committer@example.com" +git config commit.gpgsign false + +# A .mailmap that remaps an author's display name by email. +# Format: +cat >.mailmap <<'EOF' +Proper Name +EOF + +touch a +git add a .mailmap +git commit -q -m "initial with mailmap" diff --git a/gix/tests/gix/repository/mailmap.rs b/gix/tests/gix/repository/mailmap.rs new file mode 100644 index 00000000000..4b22f931409 --- /dev/null +++ b/gix/tests/gix/repository/mailmap.rs @@ -0,0 +1,55 @@ +use gix_date::parse::TimeBuf; + +use crate::{Result, named_repo}; + +fn signature(name: &str, email: &str) -> gix_actor::Signature { + gix_actor::Signature { + name: name.into(), + email: email.into(), + time: gix_date::parse_header("42 +0800").expect("static input parses"), + } +} + +#[test] +fn empty_when_no_mailmap_present() -> Result { + let repo = named_repo("make_basic_repo.sh")?; + let snapshot = repo.open_mailmap(); + assert!( + snapshot.entries().is_empty(), + "a repo without any .mailmap or mailmap.* config yields an empty snapshot" + ); + + let mut into = gix_mailmap::Snapshot::default(); + repo.open_mailmap_into(&mut into) + .expect("with no mailmap sources, no IO error is collected"); + assert!( + into.entries().is_empty(), + "open_mailmap_into mirrors open_mailmap when there are no sources" + ); + Ok(()) +} + +#[test] +fn reads_mailmap_from_worktree_root() -> Result { + let repo = named_repo("make_mailmap_repo.sh")?; + let snapshot = repo.open_mailmap(); + assert_eq!( + snapshot.entries().len(), + 1, + "the single entry from the worktree .mailmap is loaded" + ); + + let mut buf = TimeBuf::default(); + let resolved = snapshot + .try_resolve(signature("Old Name", "proper@example.com").to_ref(&mut buf)) + .expect("the entry rewrites the display name when the email matches"); + assert_eq!( + resolved.name, "Proper Name", + "the mapped name from .mailmap replaces the original" + ); + assert_eq!( + resolved.email, "proper@example.com", + "the email is preserved (this entry only changes the name)" + ); + Ok(()) +} diff --git a/gix/tests/gix/repository/mod.rs b/gix/tests/gix/repository/mod.rs index f60ad9e4192..53d9fa9d455 100644 --- a/gix/tests/gix/repository/mod.rs +++ b/gix/tests/gix/repository/mod.rs @@ -11,6 +11,8 @@ mod config; mod excludes; #[cfg(feature = "attributes")] mod filter; +#[cfg(feature = "mailmap")] +mod mailmap; #[cfg(feature = "merge")] mod merge; mod object;