When a user needed the null OID for scripting purposes, it used to be very easy: hardcode 40 zeros. However, since Git started supporting SHA-256, this assumption became false which may break some scripts. Allow users to fix their broken scripts by providing users with a hash-agnostic method of obtaining the null OID. Signed-off-by: Denton Liu <liu.denton@xxxxxxxxx> --- Documentation/git-rev-parse.txt | 4 ++++ builtin/rev-parse.c | 4 ++++ t/t1500-rev-parse.sh | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index 19b12b6d43..b370d425d7 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -285,6 +285,10 @@ print a message to stderr and exit with nonzero status. Other Options ~~~~~~~~~~~~~ +--null-oid:: + Print the null OID (the OID containing all zeros). This OID is + used to represent a non-existent object. + --since=datestring:: --after=datestring:: Parse the date string, and output the corresponding diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index ed200c8af1..4e4ca99775 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -910,6 +910,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) } continue; } + if (!strcmp(arg, "--null-oid")) { + puts(oid_to_hex(&null_oid)); + continue; + } if (skip_prefix(arg, "--since=", &arg)) { show_datestring("--max-age=", arg); continue; diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index 408b97d5af..8c1bd543ef 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -185,4 +185,10 @@ test_expect_success 'showing the superproject correctly' ' test_cmp expect out ' +test_expect_success 'rev-parse --null-oid' ' + echo "$(test_oid zero)" >expect && + git rev-parse --null-oid >actual && + test_cmp expect actual +' + test_done -- 2.28.0.618.gf4bc123cb7