archive mirror
 help / color / mirror / Atom feed
From: Jeff King <>
To: Bryan Turner <>
Cc: SURA <>, Git Users <>
Subject: [PATCH] mailmap: only look for .mailmap in work tree
Date: Tue, 9 Feb 2021 12:29:38 -0500	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

On Fri, Feb 05, 2021 at 12:02:03PM -0800, Bryan Turner wrote:

> On Fri, Feb 5, 2021 at 9:28 AM Jeff King <> wrote:
> >
> > It's possible somebody is relying on this in order to read ".mailmap" in
> > a bare repository, but it seems rather unlikely. And the documentation
> > says "If the file .mailmap exists at the toplevel of the repository",
> > which I think pretty clearly means the top of the working tree.
> There was a time, before the change was made to have bare repositories
> read the HEAD:.mailmap blob if one exists, when Bitbucket Server
> relied on this in order to have mailmapping. We'd unpack the
> `HEAD:.mailmap` blob to `.mailmap` in the bare repository whenever it
> changed. Now that it's automatically read out of the repository,
> though, that manual unpacking code was removed.
> (Not disagreeing with your "seems rather unlikely", by the way. With
> the blob reading behavior I think it's probably true.)

Heh, thanks for the data point. Anytime I think "surely nobody would do
this, right...?" then it's almost guaranteed that somebody will pipe up. :)

I prepared the patch below, which I think is pretty reasonable. It is a
change to long-standing behavior, so of course there's a risk somebody
is relying on what I consider to be buggy behavior. And the benefit
isn't that huge, either (most of the time, we wouldn't find a stray
.mailmap file in the cwd!). But I consider it mostly a hygiene thing. I
get nervous any time Git reads unexpected files from the filesystem.

-- >8 --
Subject: [PATCH] mailmap: only look for .mailmap in work tree

When trying to find a .mailmap file, we will always look for it in the
current directory. This makes sense in a repository with a working tree,
since we'd always go to the toplevel directory at startup. But for a
bare repository, it can be confusing. With an option like --git-dir (or
$GIT_DIR in the environment), we don't chdir at all, and we'd read
.mailmap from whatever directory you happened to be in before starting

(Note that --git-dir without specifying a working tree historically
means "the current directory is the root of the working tree", but most
bare repositories will have core.bare set these days, meaning they will
realize there is no working tree at all).

The documentation for gitmailmap(5) says:

  If the file `.mailmap` exists at the toplevel of the repository[...]

which likewise reinforces the notion that we are looking in the working

This patch prevents us from looking for such a file when we're in a bare
repository. This does break something that used to work:

  cd bare.git
  git cat-file blob HEAD:.mailmap >.mailmap
  git shortlog

But that was never advertised in the documentation. And these days we
have mailmap.blob (which defaults to HEAD:.mailmap) to do the same thing
in a much cleaner way.

However, there's one more interesting case: we might not have a
repository at all! The git-shortlog command can be run with git-log
output fed on its stdin, and it will apply the mailmap. In that case, it
probably does make sense to read .mailmap from the current directory.
This patch will continue to do so.

That leads to one even weirder case: if you run git-shortlog to process
stdin, the input _could_ be from a different repository entirely. Should
we respect the in-tree .mailmap then? Probably yes. Whatever the source
of the input, if shortlog is running in a repository, the documentation
claims that we'd read the .mailmap from its top-level (and of course
it's reasonably likely that it _is_ from the same repo, and the user
just preferred to run git-log and git-shortlog separately for whatever

The included test covers these cases, and we now document the "no repo"
case explicitly.

Signed-off-by: Jeff King <>
 Documentation/git-shortlog.txt |  4 ++++
 mailmap.c                      |  3 ++-
 t/             | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-shortlog.txt b/Documentation/git-shortlog.txt
index c16cc3b608..e69c823335 100644
--- a/Documentation/git-shortlog.txt
+++ b/Documentation/git-shortlog.txt
@@ -113,6 +113,10 @@ MAPPING AUTHORS
 See linkgit:gitmailmap[5].
+Note that if `git shortlog` is run outside of a repository (to process
+log contents on stdin), it will look for a `.mailmap` file in the
+current directory.
 Part of the linkgit:git[1] suite
diff --git a/mailmap.c b/mailmap.c
index eb77c6e77c..9bb9cf8b30 100644
--- a/mailmap.c
+++ b/mailmap.c
@@ -225,7 +225,8 @@ int read_mailmap(struct string_list *map)
 	if (!git_mailmap_blob && is_bare_repository())
 		git_mailmap_blob = "HEAD:.mailmap";
-	err |= read_mailmap_file(map, ".mailmap");
+	if (!startup_info->have_repository || !is_bare_repository())
+		err |= read_mailmap_file(map, ".mailmap");
 	if (startup_info->have_repository)
 		err |= read_mailmap_blob(map, git_mailmap_blob);
 	err |= read_mailmap_file(map, git_mailmap_file);
diff --git a/t/ b/t/
index 621f9962d5..bf7a8add53 100755
--- a/t/
+++ b/t/
@@ -889,4 +889,38 @@ test_expect_success 'empty syntax: setup' '
 	test_cmp expect actual
+test_expect_success 'set up mailmap location tests' '
+	git init --bare loc-bare &&
+	git --git-dir=loc-bare --work-tree=. commit \
+		--allow-empty -m foo --author="Orig <>" &&
+	echo "New <> <>" >loc-bare/.mailmap
+test_expect_success 'bare repo with --work-tree finds mailmap at top-level' '
+	git -C loc-bare --work-tree=. log -1 --format=%aE >actual &&
+	echo >expect &&
+	test_cmp expect actual
+test_expect_success 'bare repo does not look in current directory' '
+	git -C loc-bare log -1 --format=%aE >actual &&
+	echo >expect &&
+	test_cmp expect actual
+test_expect_success 'non-git shortlog respects mailmap in current dir' '
+	git --git-dir=loc-bare log -1 >input &&
+	nongit cp "$TRASH_DIRECTORY/loc-bare/.mailmap" . &&
+	nongit git shortlog -s <input >actual &&
+	echo "     1	New" >expect &&
+	test_cmp expect actual
+test_expect_success 'shortlog on stdin respects mailmap from repo' '
+	cp loc-bare/.mailmap . &&
+	git shortlog -s <input >actual &&
+	echo "     1	New" >expect &&
+	test_cmp expect actual

  reply	other threads:[~2021-02-09 17:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-01  3:05 I've noticed the command `git --git-dir=<path> shortlog` SURA
2021-02-05 16:57 ` Jeff King
2021-02-05 20:02   ` Bryan Turner
2021-02-09 17:29     ` Jeff King [this message]
2021-02-09 19:00       ` [PATCH] mailmap: only look for .mailmap in work tree Eric Sunshine
2021-02-09 22:16         ` Junio C Hamano
2021-02-10 16:05         ` Jeff King
2021-02-09 21:23       ` Junio C Hamano
2021-02-10 16:15         ` Jeff King
2021-02-10 20:10           ` Junio C Hamano
2021-02-10 20:31             ` Jeff King
2021-02-10 20:34               ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).