git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
From: Denton Liu <liu.denton@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: "brian m . carlson" <sandals@crustytoothpaste.net>
Subject: [PATCH 2/4] builtin/rev-parse: learn --null-oid
Date: Fri, 18 Sep 2020 04:19:03 -0700	[thread overview]
Message-ID: <004f2e4c92918a7a4e452d49e98ef15f1c5ac545.1600427894.git.liu.denton@gmail.com> (raw)
In-Reply-To: <cover.1600427894.git.liu.denton@gmail.com>

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@gmail.com>
---
 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


  parent reply	other threads:[~2020-09-18 11:19 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-18 11:19 [PATCH 0/4] sample hooks: become hash agnostic Denton Liu
2020-09-18 11:19 ` [PATCH 1/4] hooks--pre-push.sample: prefer $() for command substitution Denton Liu
2020-09-18 16:57   ` Eric Sunshine
2020-09-18 11:19 ` Denton Liu [this message]
2020-09-18 14:11   ` [PATCH 2/4] builtin/rev-parse: learn --null-oid Taylor Blau
2020-09-18 14:16     ` Taylor Blau
2020-09-18 18:16       ` Junio C Hamano
2020-09-18 18:21         ` Taylor Blau
2020-09-18 21:26     ` brian m. carlson
2020-09-20  4:25       ` Chris Torek
2020-09-20 18:58         ` brian m. carlson
2020-09-20 15:35       ` Taylor Blau
2020-09-20 16:03       ` Andreas Schwab
2020-09-18 11:19 ` [PATCH 3/4] hooks--pre-push.sample: use hash-agnostic null OID Denton Liu
2020-09-18 17:06   ` Eric Sunshine
2020-09-18 11:19 ` [PATCH 4/4] hooks--update.sample: " Denton Liu
2020-09-18 17:08   ` Eric Sunshine
2020-09-18 21:35 ` [PATCH 0/4] sample hooks: become hash agnostic brian m. carlson
2020-09-23  9:38 ` [PATCH v2 0/3] " Denton Liu
2020-09-23  9:38   ` [PATCH v2 1/3] hooks--pre-push.sample: modernize script Denton Liu
2020-09-23  9:38   ` [PATCH v2 2/3] hooks--pre-push.sample: use hash-agnostic zero OID Denton Liu
2020-09-23  9:38   ` [PATCH v2 3/3] hooks--update.sample: " Denton Liu
2020-09-23 16:34   ` [PATCH v2 0/3] sample hooks: become hash agnostic Junio C Hamano

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=004f2e4c92918a7a4e452d49e98ef15f1c5ac545.1600427894.git.liu.denton@gmail.com \
    --to=liu.denton@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=sandals@crustytoothpaste.net \
    --subject='Re: [PATCH 2/4] builtin/rev-parse: learn --null-oid' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).