Bug with --abbrev option in git log?
Hello Git community,
please see next what I believe to be a bug with the --abbrev option in git log. If this is not a bug, can someone explain why Git is giving this output?
Thanks
Mário Guimarães
======================================================
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.
What did you do before the bug happened? (Steps to reproduce your issue)
In the Git repository run this command:
git log --raw -r -c --abbrev=6 --pretty=oneline -n1 a7144d4bc5ab58f306a1e5c73b27196fb999a63d
What did you expect to happen? (Expected behavior)
I was expecting to get this line in the output
::100644 100644 100644 639a41 111378 05ba1d MM Documentation/git-rebase.txt
What happened instead? (Actual behavior)
I got the following line instead
::100644 100644 100644 639a41 111378 05ba1d3 MM Documentation/git-rebase.txt
What's different between what you expected and what actually happened?
Since --abbrev is set to 6 the expected hash is 05ba1d not 05ba1d3.
Anything else you want to add:
If --abbrev is set to N then the size of all hashes should be the first N hex digits.
The problem with --abbrev is more evident with values below 7. Try with 1, 2, 3, 4, and 5, to see for yourself.
Please review the rest of the bug report below.
You can delete any lines you don't wish to share.
[System Info]
git version:
git version 2.29.2
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Darwin 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64 compiler info: clang: 12.0.0 (clang-1200.0.32.21)
libc info: no libc information available
$SHELL (typically, interactive shell): /bin/bash
[Enabled Hooks]
======================================================
Quoted 4 lines · Mário Guimarães
In the Git repository run this command:
git log --raw -r -c --abbrev=6 --pretty=oneline -n1
a7144d4bc5ab58f306a1e5c73b27196fb999a63d
This is not one of my object, so above won't be usable as reproduction recipe. But that is OK.
Quoted 11 lines
What did you expect to happen? (Expected behavior)
I was expecting to get this line in the output
::100644 100644 100644 639a41 111378 05ba1d MM
Documentation/git-rebase.txtWhat happened instead? (Actual behavior)
I got the following line instead
::100644 100644 100644 639a41 111378 05ba1d3 MM
Documentation/git-rebase.txt
The above command tells us that you locally have an object whose name begins with 05ba1d3, which I do not have. But my history shared with the entire world has an object whose name begins with 05ba1d7, and I am reasonably sure that your repository has it, since it is a part of 17f26a9e (git-am: fix shell quoting, 2009-01-14).
So in my repository, 05ba1d is enough to uniquely identify 05ba1d7, but in your repository 05ba1d is not enough to uniquely identify 05ba1d3, because 05ba1d7 also exists in your repository. And when the object cannot be uniquely specified with the given abbreviation width, Git makes sure its output is usable to uniquely identify the object it wants to name.
In short, I think what you observed is totally expected.
The documentation has some room for improvement, I also think.
How about saying something like this?
Documentation/diff-options.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/diff-options.txt +2 -1
@@ -446,7 +446,8 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. + lines, show only a partial prefix that uses at least '<n>' + hexdigits. In diff-patch output format, `--full-index` takes higher precedence, i.e. if `--full-index` is specified, full blob names will be shown regardless of `--abbrev`.
PATCH 1/1 doc: clarify that most --abbrev[=<n>] are about minimum length
Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.
Luckily, the text written for "git describe" in 2011 explains that the output is made to name the object uniquely using at least <n> hexdigits much clearly.
Model the explanation and update documentation for the diff family of commands, "blame", "branch --verbose", "ls-files" and "ls-tree".
--- Documentation/diff-options.txt | 3 ++- Documentation/git-blame.txt | 4 +++- Documentation/git-branch.txt | 8 +++++--- Documentation/git-ls-files.txt | 5 +++-- Documentation/git-ls-tree.txt | 3 ++- Documentation/pretty-options.txt | 3 ++- 6 files changed, 17 insertions(+), 9 deletions(-)
Documentation/diff-options.txt +2 -1
@@ -446,7 +446,8 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. + lines, show <n> hexadecimal digits, or as many digits + as needed to form a unique object name. In diff-patch output format, `--full-index` takes higher precedence, i.e. if `--full-index` is specified, full blob names will be shown regardless of `--abbrev`.
Documentation/git-blame.txt +3 -1
@@ -87,7 +87,9 @@ include::blame-options.txt[] --abbrev=<n>:: Instead of using the default 7+1 hexadecimal digits as the - abbreviated object name, use <n>+1 digits. Note that 1 column + abbreviated object name, use <m>+1 digits, where <m> is at + least <n> but ensures the commit object names are unique. + Note that 1 column is used for a caret to mark the boundary commit.
Documentation/git-branch.txt +5 -3
@@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git branch' [--color[=<when>] | --no-color] [--show-current] - [-v [--abbrev=<length> | --no-abbrev]] + [-v [--abbrev=<n> | --no-abbrev]] [--column[=<options>] | --no-column] [--sort=<key>] [--merged [<commit>]] [--no-merged [<commit>]] [--contains [<commit>]] [--no-contains [<commit>]]
@@ -194,8 +194,10 @@ This option is only applicable in non-verbose mode. Be more quiet when creating or deleting a branch, suppressing non-error messages. ---abbrev=<length>:: - Alter the sha1's minimum display length in the output listing. +--abbrev=<n>:: + In the verbose listing that show the commit object name, + use <n>, or as many hexdigits as needed to form a unique + object name. The default value is 7 and can be overridden by the `core.abbrev` config option.
Documentation/git-ls-files.txt +3 -2
@@ -19,7 +19,7 @@ SYNOPSIS [--exclude-standard] [--error-unmatch] [--with-tree=<tree-ish>] [--full-name] [--recurse-submodules] - [--abbrev] [--] [<file>...] + [--abbrev[=<n>]] [--] [<file>...] DESCRIPTION -----------
@@ -153,7 +153,8 @@ a space) at the start of each line: --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object - lines, show only a partial prefix. + lines, show <n>, or as many hexadecimal digits + as needed, to form a unique object name. Non default number of digits can be specified with --abbrev=<n>. --debug::
Documentation/git-ls-tree.txt +2 -1
@@ -62,7 +62,8 @@ OPTIONS --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object - lines, show only a partial prefix. + lines, show <n>, or as many hexadecimal digits + as needed, to form a unique object name. Non default number of digits can be specified with --abbrev=<n>. --full-name::
Documentation/pretty-options.txt +2 -1
@@ -16,7 +16,8 @@ configuration (see linkgit:git-config[1]). --abbrev-commit:: Instead of showing the full 40-byte hexadecimal commit object - name, show only a partial prefix. Non default number of + name, show only a partial prefix that names the object uniquely. + Non default number of digits can be specified with "--abbrev=<n>" (which also modifies diff output, if it is displayed). +
Signature · 1 line
2.29.2-287-gba574db674
Re: [PATCH] doc: clarify that most --abbrev[=<n>] are about minimum length
Quoted 11 lines · Junio C Hamano
Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.
Luckily, the text written for "git describe" in 2011 explains that the output is made to name the object uniquely using at least <n> hexdigits much clearly.
Model the explanation and update documentation for the diff family of commands, "blame", "branch --verbose", "ls-files" and "ls-tree".
Thanks for being thorough here! Your patch is already an improvement on what we have. I hope that my suggestions below help to improve it further.
Documentation/diff-options.txt endif::git-format-patch[] · line 450
Quoted 9 lines
Documentation/diff-options.txt +2
-1@@ -446,7 +446,8 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. + lines, show <n> hexadecimal digits, or as many digits + as needed to form a unique object name.
A reoccurring theme in this patch is that the --abbrev option provides a lower bound, even if fewer digits are needed for a unique object name. Using these two phrases with an "or" has some ambiguity about which is chosen.
Perhaps we should try to describe it as "<n> is the default, but can grow as needed". Here is an attempt:
- lines, show only a partial prefix.
+ lines, show at least <n> hexadecimal digits, increasing the
+ prefix length as needed to uniquely identify an object.
If you prefer wording like this, then there are a few very similar snippets later in git-branch.txt and git-ls-files.txt.
Documentation/git-blame.txt include::blame-options.txt[] · line 93
Quoted 11 lines
Documentation/git-blame.txt +3
-1@@ -87,7 +87,9 @@ include::blame-options.txt[] --abbrev=<n>:: Instead of using the default 7+1 hexadecimal digits as the - abbreviated object name, use <n>+1 digits. Note that 1 column + abbreviated object name, use <m>+1 digits, where <m> is at + least <n> but ensures the commit object names are unique. + Note that 1 column is used for a caret to mark the boundary commit.
This captures the "at least <n>" pretty well, especially considering the complications around "+1".
Documentation/git-blame.txt include::blame-options.txt[]
Quoted 6 lines
Documentation/git-blame.txt +4
-2---abbrev=<length>:: - Alter the sha1's minimum display length in the output listing. +--abbrev=<n>:: + In the verbose listing that show the commit object name, + use <n>, or as many hexdigits as needed to form a unique + object name.
Adapting from your wording here made me think that this might be a simpler way to phrase it:
+--abbrev=<n>::
+ In the verbose listing that show the commit object name,
+ use at least <n> hexdigits to form a unique object name.
Documentation/pretty-options.txt configuration (see linkgit:git-config[1]). · line 19
Quoted 10 lines
Documentation/pretty-options.txt +1
-1@@ -16,7 +16,8 @@ configuration (see linkgit:git-config[1]). --abbrev-commit:: Instead of showing the full 40-byte hexadecimal commit object - name, show only a partial prefix. Non default number of + name, show only a partial prefix that names the object uniquely.
Perhaps "show a prefix that uniquely identifies the object" ?
Documentation/pretty-options.txt configuration (see linkgit:git-config[1]). · line 22
Quoted 3 lines
Documentation/pretty-options.txt +1
-0+ Non default number of digits can be specified with "--abbrev=<n>" (which also modifies diff output, if it is displayed).
I know this is in the existing docs, but this last sentence should probably start with "A non-default number of digits..." Alternatively, we could rephrase the entire thing to use more active voice:
+ The --abbrev=<n> option modifies the minimum number of digits
+ for these prefixes, including objects reported in displayed diff
+ output.
Thanks,
-Stolee
PATCH v2 1/1 doc: clarify that --abbrev=<n> is about the minimum length
Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.
Update documentation for the diff family of commands, "blame", "branch --verbose", "ls-files" and "ls-tree" to stress that the short prefix must uniquely refer to an object, and <n> is merely the mininum number of hexdigits used in the prefix.
---
* Thanks for your suggestion. I decided to use "the shortest prefix that is at least <n> hexdigits long".
Range-diff:
1: 01c62c8d6f ! 1: 4f8524079c doc: clarify that most --abbrev[=<n>] are about minimum length
@@ Metadata
Author: Junio C Hamano <[email protected]>
## Commit message ##
- doc: clarify that most --abbrev[=<n>] are about minimum length
+ doc: clarify that --abbrev=<n> is about the minimum length
Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.
- Luckily, the text written for "git describe" in 2011 explains that
- the output is made to name the object uniquely using at least <n>
- hexdigits much clearly.
-
- Model the explanation and update documentation for the diff family
- of commands, "blame", "branch --verbose", "ls-files" and "ls-tree".
+ Update documentation for the diff family of commands, "blame",
+ "branch --verbose", "ls-files" and "ls-tree" to stress that the
+ short prefix must uniquely refer to an object, and <n> is merely
+ the mininum number of hexdigits used in the prefix.
+ Helped-by: Derrick Stolee <[email protected]>
## Documentation/diff-options.txt ##
@@ Documentation/diff-options.txt: endif::git-format-patch[]
Instead of showing the full 40-byte hexadecimal object
name in diff-raw format output and diff-tree header
- lines, show only a partial prefix.
-+ lines, show <n> hexadecimal digits, or as many digits
-+ as needed to form a unique object name.
++ lines, show the shortest prefix that is at least '<n>'
++ hexdigits long that uniquely refers the object.
In diff-patch output format, --full-index takes higher precedence, i.e. if --full-index is specified, full blob
names will be shown regardless of --abbrev.
@@ Documentation/git-branch.txt: This option is only applicable in non-verbose mode
- Alter the sha1's minimum display length in the output listing.
+--abbrev=<n>::
+ In the verbose listing that show the commit object name,
-+ use <n>, or as many hexdigits as needed to form a unique
-+ object name.
++ show the shortest prefix that is at least '<n>' hexdigits
++ long that uniquely refers the object.
The default value is 7 and can be overridden by the core.abbrev config option.
@@ Documentation/git-ls-files.txt: a space) at the start of each line:
--abbrev[=<n>]::
Instead of showing the full 40-byte hexadecimal object
- lines, show only a partial prefix.
-+ lines, show <n>, or as many hexadecimal digits
-+ as needed, to form a unique object name.
++ lines, show the shortest prefix that is at least '<n>'
++ hexdigits long that uniquely refers the object.
Non default number of digits can be specified with --abbrev=<n>.
--debug::
@@ Documentation/git-ls-tree.txt: OPTIONS
--abbrev[=<n>]::
Instead of showing the full 40-byte hexadecimal object
- lines, show only a partial prefix.
-+ lines, show <n>, or as many hexadecimal digits
-+ as needed, to form a unique object name.
++ lines, show the shortest prefix that is at least '<n>'
++ hexdigits long that uniquely refers the object.
Non default number of digits can be specified with --abbrev=<n>.
--full-name::
@@ Documentation/pretty-options.txt: configuration (see linkgit:git-config[1]).
--abbrev-commit::
Instead of showing the full 40-byte hexadecimal commit object
- name, show only a partial prefix. Non default number of
-+ name, show only a partial prefix that names the object uniquely.
-+ Non default number of
- digits can be specified with "--abbrev=<n>" (which also modifies
- diff output, if it is displayed).
+- digits can be specified with "--abbrev=<n>" (which also modifies
+- diff output, if it is displayed).
++ name, show a prefix that names the object uniquely.
++ "--abbrev=<n>" (which also modifies diff output, if it is displayed)
++ option can be used to specify the minimum length of the prefix.
+
+ This should make "--pretty=oneline" a whole lot more readable for
+ people using 80-column terminals.
Documentation/diff-options.txt | 3 ++- Documentation/git-blame.txt | 4 +++- Documentation/git-branch.txt | 8 +++++--- Documentation/git-ls-files.txt | 5 +++-- Documentation/git-ls-tree.txt | 3 ++- Documentation/pretty-options.txt | 6 +++--- 6 files changed, 18 insertions(+), 11 deletions(-)
Documentation/diff-options.txt +2 -1
@@ -446,7 +446,8 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. + lines, show the shortest prefix that is at least '<n>' + hexdigits long that uniquely refers the object. In diff-patch output format, `--full-index` takes higher precedence, i.e. if `--full-index` is specified, full blob names will be shown regardless of `--abbrev`.
Documentation/git-blame.txt +3 -1
@@ -87,7 +87,9 @@ include::blame-options.txt[] --abbrev=<n>:: Instead of using the default 7+1 hexadecimal digits as the - abbreviated object name, use <n>+1 digits. Note that 1 column + abbreviated object name, use <m>+1 digits, where <m> is at + least <n> but ensures the commit object names are unique. + Note that 1 column is used for a caret to mark the boundary commit.
Documentation/git-branch.txt +5 -3
@@ -9,7 +9,7 @@ SYNOPSIS -------- [verse] 'git branch' [--color[=<when>] | --no-color] [--show-current] - [-v [--abbrev=<length> | --no-abbrev]] + [-v [--abbrev=<n> | --no-abbrev]] [--column[=<options>] | --no-column] [--sort=<key>] [--merged [<commit>]] [--no-merged [<commit>]] [--contains [<commit>]] [--no-contains [<commit>]]
@@ -194,8 +194,10 @@ This option is only applicable in non-verbose mode. Be more quiet when creating or deleting a branch, suppressing non-error messages. ---abbrev=<length>:: - Alter the sha1's minimum display length in the output listing. +--abbrev=<n>:: + In the verbose listing that show the commit object name, + show the shortest prefix that is at least '<n>' hexdigits + long that uniquely refers the object. The default value is 7 and can be overridden by the `core.abbrev` config option.
Documentation/git-ls-files.txt +3 -2
@@ -19,7 +19,7 @@ SYNOPSIS [--exclude-standard] [--error-unmatch] [--with-tree=<tree-ish>] [--full-name] [--recurse-submodules] - [--abbrev] [--] [<file>...] + [--abbrev[=<n>]] [--] [<file>...] DESCRIPTION -----------
@@ -153,7 +153,8 @@ a space) at the start of each line: --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object - lines, show only a partial prefix. + lines, show the shortest prefix that is at least '<n>' + hexdigits long that uniquely refers the object. Non default number of digits can be specified with --abbrev=<n>. --debug::
Documentation/git-ls-tree.txt +2 -1
@@ -62,7 +62,8 @@ OPTIONS --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object - lines, show only a partial prefix. + lines, show the shortest prefix that is at least '<n>' + hexdigits long that uniquely refers the object. Non default number of digits can be specified with --abbrev=<n>. --full-name::
Documentation/pretty-options.txt +3 -3
@@ -16,9 +16,9 @@ configuration (see linkgit:git-config[1]). --abbrev-commit:: Instead of showing the full 40-byte hexadecimal commit object - name, show only a partial prefix. Non default number of - digits can be specified with "--abbrev=<n>" (which also modifies - diff output, if it is displayed). + name, show a prefix that names the object uniquely. + "--abbrev=<n>" (which also modifies diff output, if it is displayed) + option can be used to specify the minimum length of the prefix. + This should make "--pretty=oneline" a whole lot more readable for people using 80-column terminals.
Signature · 1 line
2.29.2-287-gba574db674
Re: [PATCH v2] doc: clarify that --abbrev=<n> is about the minimum length
Quoted 10 lines · Junio C Hamano
Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.
Update documentation for the diff family of commands, "blame", "branch --verbose", "ls-files" and "ls-tree" to stress that the short prefix must uniquely refer to an object, and <n> is merely the mininum number of hexdigits used in the prefix.
Quoted 6 lines
Helped-by Derrick Stolee <[email protected]>Signed-off-by Junio C Hamano <[email protected]>---
* Thanks for your suggestion. I decided to use "the shortest prefix that is at least <n> hexdigits long".
I like this version. Thanks!
-Stolee
Hello
Junio C Hamano <[email protected]> escreveu no dia quarta, 4/11/2020 à(s) 01:12:
Quoted 52 lines
Quoted 4 lines · Mário Guimarães
In the Git repository run this command:
git log --raw -r -c --abbrev=6 --pretty=oneline -n1
a7144d4bc5ab58f306a1e5c73b27196fb999a63dThis is not one of my object, so above won't be usable as reproduction recipe. But that is OK.
Quoted 11 lines
What did you expect to happen? (Expected behavior)
I was expecting to get this line in the output
::100644 100644 100644 639a41 111378 05ba1d MM
Documentation/git-rebase.txtWhat happened instead? (Actual behavior)
I got the following line instead
::100644 100644 100644 639a41 111378 05ba1d3 MM
Documentation/git-rebase.txtThe above command tells us that you locally have an object whose name begins with 05ba1d3, which I do not have. But my history shared with the entire world has an object whose name begins with 05ba1d7, and I am reasonably sure that your repository has it, since it is a part of
17f26a9e(git-am: fix shell quoting, 2009-01-14).So in my repository, 05ba1d is enough to uniquely identify 05ba1d7, but in your repository 05ba1d is not enough to uniquely identify 05ba1d3, because 05ba1d7 also exists in your repository. And when the object cannot be uniquely specified with the given abbreviation width, Git makes sure its output is usable to uniquely identify the object it wants to name.
In short, I think what you observed is totally expected.
The documentation has some room for improvement, I also think.
How about saying something like this?
Documentation/diff-options.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Documentation/diff-options.txt +0
-0
Documentation/diff-options.txt endif::git-format-patch[] · line 453
Quoted 9 lines
Documentation/diff-options.txt +2
-1@@ -446,7 +446,8 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. + lines, show only a partial prefix that uses at least '<n>' + hexdigits. In diff-patch output format, `--full-index` takes higher precedence, i.e. if `--full-index` is specified, full blob names will be shown regardless of `--abbrev`.
What about this? It makes clear that the prefix is still one that can be used to identify the object in the repository.
Documentation/diff-options.txt +2 -2
@@ -446,8 +446,9 @@ endif::git-format-patch[] --abbrev[=<n>]:: Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header - lines, show only a partial prefix. - In diff-patch output format, `--full-index` takes higher + lines, show only the minimal prefix that identifies + the object using at least `<n>` hexdigits. In diff-patch output format, `--full-index` takes higher precedence, i.e. if `--full-index` is specified, full blob names will be shown regardless of `--abbrev`.
Non default number of digits can be specified with --abbrev=<n>.