I've noticed the command `git --git-dir=<path> shortlog`
Hello everyone
OS: manjaro 20.2.1
git version: 2.30.0
I've noticed the command git --git-dir=<path> shortlog
It gives me some interesting output
$ git --git-dir=<path> shortlog --summary --email --numbered --committer HEAD --end-of-options | wc -l
When run it, it will tell me how many committers on repo of <path>
But when run it on git repo dir, something interesting happened
$ git clone --bare https://github.com/gitlabhq/gitlabhq.git $ git clone https://gitlab.com/gitlab-org/gitaly.git
$ git --git-dir=./gitlabhq.git shortlog --summary --email --numbered --committer HEAD --end-of-options | wc -l
Quoted 1 line
2592
Ok, we can known the gitlab have 2592 committers on HEAD (refs/heads/master) ref
Then something magical happened
$ cd gitaly $ git --git-dir=../gitlabhq.git shortlog --summary --email --numbered --committer HEAD --end-of-options | wc -l
Quoted 1 line
2587
Obviously these two outputs are inconsistent, and the second command was affected by the gitaly repo
Shouldn't I use --git-dir to point to a bare repo? Or something else went wrong?
Thank u!
Quoted 11 lines · SURA
$ git clone --bare https://github.com/gitlabhq/gitlabhq.git $ git clone https://gitlab.com/gitlab-org/gitaly.git$ git --git-dir=./gitlabhq.git shortlog --summary --email --numbered --committer HEAD --end-of-options | wc -lQuoted 1 line
2592
[...]
$ cd gitaly $ git --git-dir=../gitlabhq.git shortlog --summary --email --numbered --committer HEAD --end-of-options | wc -lQuoted 1 line
2587
I think what's happening is that the second command is using the .mailmap file found in the working tree of the gitaly repository.
If the first checkout were non-bare, I would say that is the right behavior (because --git-dir without otherwise specifying the working tree means that the current directory becomes the working tree, and we read ".mailmap" out of the top of the working tree). But because it was cloned with "--bare", it should have the core.bare config set, and will think there is no working tree at all.
So I do think there is a bug, which is: Git should avoid looking for ".mailmap" in the current directory if we do not have a working tree. I.e., something like:
mailmap.c +2 -1
@@ -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 (!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);
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.
-Peff
Quoted 5 lines · Jeff King
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.)
Bryan
PATCH 1/1 mailmap: only look for .mailmap in work tree
Quoted 14 lines · Bryan Turner
Quoted 5 lines · Jeff King
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:.mailmapblob to.mailmapin the bare repository whenever it changed. Now that it's automatically read out of the repository, though, that manual unpacking code was removed.
Quoted 2 lines
(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 Git.
(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 tree.
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 reason).
The included test covers these cases, and we now document the "no repo" case explicitly.
--- Documentation/git-shortlog.txt | 4 ++++ mailmap.c | 3 ++- t/t4203-mailmap.sh | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-)
Documentation/git-shortlog.txt +4 -0
@@ -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. + GIT --- Part of the linkgit:git[1] suite
mailmap.c +2 -1
@@ -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);
t/t4203-mailmap.sh +34 -0
@@ -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 <[email protected]>" && + echo "New <[email protected]> <[email protected]>" >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 [email protected] >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 [email protected] >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 +' + test_done
Signature · 1 line
2.30.1.887.ge7d57fcab0
Re: [PATCH] mailmap: only look for .mailmap in work tree
Documentation/git-shortlog.txt MAPPING AUTHORS · line 115
Quoted 9 lines · Jeff King
Subject: [PATCH] mailmap: only look for .mailmap in work tree
[...]Signed-off-by Jeff King <[email protected]>---
Documentation/git-shortlog.txt +3
-0@@ -113,6 +113,10 @@ MAPPING AUTHORS +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.
Elsewhere in this same document, techy-jargon "stdin" is spelled out fully as "standard input".
Re: [PATCH] mailmap: only look for .mailmap in work tree
Quoted 13 lines · Eric Sunshine
Documentation/git-shortlog.txt MAPPING AUTHORS · line 115
Quoted 9 lines · Jeff King
Subject: [PATCH] mailmap: only look for .mailmap in work tree
[...]Signed-off-by Jeff King <[email protected]>---
Documentation/git-shortlog.txt +3
-0@@ -113,6 +113,10 @@ MAPPING AUTHORS +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.Elsewhere in this same document, techy-jargon "stdin" is spelled out fully as "standard input".
Good point.
Re: [PATCH] mailmap: only look for .mailmap in work tree
In reply to Eric SunshineQuoted 13 lines · Eric Sunshine
Documentation/git-shortlog.txt MAPPING AUTHORS · line 115
Quoted 9 lines · Jeff King
Subject: [PATCH] mailmap: only look for .mailmap in work tree
[...]Signed-off-by Jeff King <[email protected]>---
Documentation/git-shortlog.txt +3
-0@@ -113,6 +113,10 @@ MAPPING AUTHORS +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.Elsewhere in this same document, techy-jargon "stdin" is spelled out fully as "standard input".
Thanks. We seem to use "stdin" in other manpages, but I agree it makes sense to be consistent here.
-Peff
Re: [PATCH] mailmap: only look for .mailmap in work tree
In reply to Jeff KingQuoted 10 lines · Jeff King
If the file
.mailmapexists at the toplevel of the repository[...]which likewise reinforces the notion that we are looking in the working tree.
mailmap.c +0
-0
mailmap.c int read_mailmap(struct string_list *map) · line 229
Quoted 6 lines
mailmap.c +2
-1@@ -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" );
OK. Do we know at this point that cwd is always/already at the root level of the working tree?
Thanks.
Re: [PATCH] mailmap: only look for .mailmap in work tree
Quoted 15 lines · Junio C Hamano
Quoted 13 lines · Jeff King
If the file
.mailmapexists at the toplevel of the repository[...]which likewise reinforces the notion that we are looking in the working tree.
mailmap.c +0
-0@@ -225,7 +225,8 @@ int read_mailmap(struct string_list *map)if (!git_mailmap_blob &&is_bare_repository ()) git_mailmap_blob ="HEAD:.mailmap" ;
Quoted 6 lines
mailmap.c int read_mailmap(struct string_list *map) · line 229
Quoted 3 lines
mailmap.c +2
-1- err |=read_mailmap_file (map,".mailmap" ); +if (!startup_info->have_repository || !is_bare_repository ()) + err |=read_mailmap_file (map,".mailmap" );OK. Do we know at this point that cwd is always/already at the root level of the working tree?
I think so. If we're in a non-bare repository, we'd have chdir'd during the setup/discovery steps. At any rate, this patch could not possibly be making such a situation _worse_, as we were previously reading it unconditionally.
If you are proposing that this become:
if (!startup_info->have_repository) {
/* no repository (e.g., shortlog reading stdin); use map in cwd */
err |= read_mailmap_file(map, ".mailmap");
} else if (get_git_work_tree()) {
/* we have a work tree; read from the top-level */
char *map_file = xstrfmt("%s/.mailmap", get_git_work_tree());
err |= read_mailmap_file(map, map_file);
free(map_file);
} else {
/* bare; do not read filesystem .mailmap */}
I could buy that. I suspect it may even be necessary to use a mailmap in an in-process submodule repository (in which case we should be taking a "struct repository" argument and checking it in the first two branches of the conditional). But this is orthogonal to what my patch is doing, I think. And I'd rather punt on it until there is a known upside (probably as part of a larger effort to allow mailmaps outside of the_repository).
-Peff
Re: [PATCH] mailmap: only look for .mailmap in work tree
Quoted 24 lines · Jeff King
Quoted 15 lines · Junio C Hamano
Quoted 13 lines · Jeff King
If the file
.mailmapexists at the toplevel of the repository[...]which likewise reinforces the notion that we are looking in the working tree.
mailmap.c +0
-0@@ -225,7 +225,8 @@ int read_mailmap(struct string_list *map)if (!git_mailmap_blob &&is_bare_repository ()) git_mailmap_blob ="HEAD:.mailmap" ;Quoted 6 lines
mailmap.c int read_mailmap(struct string_list *map) · line 229
Quoted 3 lines
mailmap.c +2
-1- err |=read_mailmap_file (map,".mailmap" ); +if (!startup_info->have_repository || !is_bare_repository ()) + err |=read_mailmap_file (map,".mailmap" );OK. Do we know at this point that cwd is always/already at the root level of the working tree?
Quoted 4 lines
I think so. If we're in a non-bare repository, we'd have chdir'd during the setup/discovery steps. At any rate, this patch could not possibly be making such a situation _worse_, as we were previously reading it unconditionally.
But the point of the patch is to ensure that we only read from the top of the working tree---I wanted to make sure that we previously weren't reading it from any subdirectory the command started.
Quoted 2 lines
If you are proposing that this become:
...
Not really. As long as we know we would already have moved to the top, then what we have above is perfectly fine.
Quoted 6 lines
.... I suspect it may even be necessary to use a mailmap in an in-process submodule repository (in which case we should be taking a "struct repository" argument and checking it in the first two branches of the conditional). But this is orthogonal to what my patch is doing, I think. And I'd rather punt on it until there is a known upside (probably as part of a larger effort to allow mailmaps outside of
the_repository).
Yeah, I was not interested in what happens with in-process submodule, and I'd rather leave the code as close to as-is, until it becomes necessary to update it.
Thanks.
Re: [PATCH] mailmap: only look for .mailmap in work tree
Quoted 11 lines · Junio C Hamano
Quoted 7 lines
Quoted 2 lines
OK. Do we know at this point that cwd is always/already at the root level of the working tree?
I think so. If we're in a non-bare repository, we'd have chdir'd during the setup/discovery steps. At any rate, this patch could not possibly be making such a situation _worse_, as we were previously reading it unconditionally.
But the point of the patch is to ensure that we only read from the top of the working tree---I wanted to make sure that we previously weren't reading it from any subdirectory the command started.
Ah, I see. I assume that part already worked, or people would be complaining that "git shortlog" does not work from a subdirectory. :)
It doesn't look like we actually cover that in the test suite, though. We can do that pretty easily:
t/t4203-mailmap.sh +9 -0
@@ -923,4 +923,13 @@ test_expect_success 'shortlog on stdin respects mailmap from repo' ' test_cmp expect actual ' +test_expect_success 'find top-level mailmap from subdir' ' + git clone loc-bare loc-wt && + cp loc-bare/.mailmap loc-wt && + mkdir loc-wt/subdir && + git -C loc-wt/subdir log -1 --format=%aE >actual && + echo [email protected] >expect && + test_cmp expect actual +' + test_done
I'll resend the whole thing with that squashed in, plus Eric's documentation suggestion.
-Peff
Re: [PATCH] mailmap: only look for .mailmap in work tree
Quoted 2 lines · Jeff King
I'll resend the whole thing with that squashed in, plus Eric's documentation suggestion.
And here that is.
-- >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 Git.
(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 tree.
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 reason).
The included test covers these cases, and we now document the "no repo" case explicitly.
We also add a test that confirms we find a top-level ".mailmap" even when we start in a subdirectory of the working tree. This worked both before and after this commit, but we never tested it explicitly (it works because we always chdir to the top-level of the working tree if there is one).
--- Documentation/git-shortlog.txt | 4 ++++ mailmap.c | 3 ++- t/t4203-mailmap.sh | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-)
Documentation/git-shortlog.txt +4 -0
@@ -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 standard input), it will look for a `.mailmap` file in +the current directory. + GIT --- Part of the linkgit:git[1] suite
mailmap.c +2 -1
@@ -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);
t/t4203-mailmap.sh +43 -0
47 lines not on this page · raw message
Signature · 1 line
2.30.1.898.g81d77b2936