git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 03/15] rev-parse: add a --show-object-format option

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Oct 28, 2019, 00:58 UTC
Message-ID
<20191028005907.24985-4-sandals@crustytoothpaste.net>
In-Reply-To
<20191028005907.24985-1-sandals@crustytoothpaste.net>

Add an option to print the object format used for input, output, or storage. This allows shell scripts to discover the hash algorithm in use.

Since the transition plan allows for multiple input algorithms, document that we may provide multiple results for input, and the format that the results may take. While we don't support this now, documenting it early means that script authors can future-proof their scripts for when we do.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 Documentation/git-rev-parse.txt |  7 +++++++
 builtin/rev-parse.c             | 11 +++++++++++
 t/t1500-rev-parse.sh            | 15 +++++++++++++++
 3 files changed, 33 insertions(+)
diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
index e72d332b83..9985477efe 100644
--- a/Documentation/git-rev-parse.txt
+++ b/Documentation/git-rev-parse.txt
@@ -274,6 +274,13 @@ print a message to stderr and exit with nonzero status.
 	Show the path to the shared index file in split index mode, or
 	empty if not in split-index mode.
 
+--show-object-format[=(storage|input|output)]::
+	Show the object format (hash algorithm) used for the repository
+	for storage inside the `.git` directory, input, or output. For
+	input, multiple algorithms may be printed, space-separated.
+	If not specified, the default is "storage".
+
+
 Other Options
 ~~~~~~~~~~~~~
 
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 308c67e4fc..85ce2095bf 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -919,6 +919,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				show_datestring("--min-age=", arg);
 				continue;
 			}
+			if (opt_with_value(arg, "--show-object-format", &arg)) {
+				const char *val = arg ? arg : "storage";
+
+				if (strcmp(val, "storage") &&
+				    strcmp(val, "input") &&
+				    strcmp(val, "output"))
+					die("unknown mode for --show-object-format: %s",
+					    arg);
+				puts(the_hash_algo->name);
+				continue;
+			}
 			if (show_flag(arg) && verify)
 				die_no_single_rev(quiet);
 			continue;
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 01abee533d..0177fd815c 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -59,6 +59,7 @@ test_rev_parse () {
 ROOT=$(pwd)
 
 test_expect_success 'setup' '
+	test_oid_init &&
 	mkdir -p sub/dir work &&
 	cp -R .git repo.git
 '
@@ -131,6 +132,20 @@ test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
 	test_cmp expect actual
 '
 
+test_expect_success 'rev-parse --show-object-format in repo' '
+	echo "$(test_oid algo)" >expect &&
+	git rev-parse --show-object-format >actual &&
+	test_cmp expect actual &&
+	git rev-parse --show-object-format=storage >actual &&
+	test_cmp expect actual &&
+	git rev-parse --show-object-format=input >actual &&
+	test_cmp expect actual &&
+	git rev-parse --show-object-format=output >actual &&
+	test_cmp expect actual &&
+	test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
+	grep "unknown mode for --show-object-format: squeamish-ossifrage" err
+'
+
 test_expect_success 'showing the superproject correctly' '
 	git rev-parse --show-superproject-working-tree >out &&
 	test_must_be_empty out &&
Previous: brian m. carlsonNext: brian m. carlson
Message 3 of 19 in “SHA-256 test fixes, part 6”
  1. 00/15 SHA-256 test fixes, part 6brian m. carlson, Oct 28, 2019
  2. 01/15 t/oid-info: allow looking up hash algorithm namebrian m. carlson, Oct 28, 2019
  3. 03/15 rev-parse: add a --show-object-format optionbrian m. carlson, Oct 28, 2019
  4. 02/15 t/oid-info: add empty tree and empty blob valuesbrian m. carlson, Oct 28, 2019
  5. 04/15 t1305: avoid comparing extensionsbrian m. carlson, Oct 28, 2019
  6. 05/15 t3429: remove SHA1 annotationbrian m. carlson, Oct 28, 2019
  7. 06/15 t4010: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  8. 07/15 t4011: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  9. Junio C HamanoOct 28, 2019
  10. brian m. carlsonOct 28, 2019
  11. 08/15 t4015: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  12. 10/15 t4034: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  13. 11/15 t4038: abstract away SHA-1 specific constantsbrian m. carlson, Oct 28, 2019
  14. 09/15 t4027: make hash-size independentbrian m. carlson, Oct 28, 2019
  15. 12/15 t4039: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  16. 15/15 t4048: abstract away SHA-1-specific constantsbrian m. carlson, Oct 28, 2019
  17. 13/15 t4044: update test to work with SHA-256brian m. carlson, Oct 28, 2019
  18. 14/15 t4045: make hash-size independentbrian m. carlson, Oct 28, 2019
  19. Junio C HamanoOct 29, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.