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

[PATCH 02/16] hash-object: always try to set up the git repository

From
Jeff King <peff@peff.net>
Date
Sep 13, 2016, 03:23 UTC
Message-ID
<20160913032317.eyddugalfb2atr7v@sigill.intra.peff.net>
In-Reply-To
<20160913032242.coyuhyhn6uklewuk@sigill.intra.peff.net>

When "hash-object" is run without "-w", we don't need to be in a git repository at all; we can just hash the object and write its sha1 to stdout. However, if we _are_ in a git repository, we would want to know that so we can follow the normal rules for respecting config, .gitattributes, etc.

This happens to work at the top-level of a git repository because we blindly read ".git/config", but as the included test shows, it does not work when you are in a subdirectory.

The solution is to just do a "gentle" setup in this case. We already take care to use prefix_filename() on any filename arguments we get (to handle the "-w" case), so we don't need to do anything extra to handle the side effects of repo setup.

An alternative would be to specify RUN_SETUP_GENTLY for this command in git.c, and then die if "-w" is set but we are not in a repository. However, the error messages generated at the time of setup_git_directory() are more detailed, so it's better to find out which mode we are in, and then call the appropriate function.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/hash-object.c  | 13 ++++++++-----
 t/t1007-hash-object.sh | 11 +++++++++++
 2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/builtin/hash-object.c b/builtin/hash-object.c
index f7d3567..9028e1f 100644
--- a/builtin/hash-object.c
+++ b/builtin/hash-object.c
@@ -87,6 +87,7 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 	int stdin_paths = 0;
 	int no_filters = 0;
 	int literally = 0;
+	int nongit = 0;
 	unsigned flags = HASH_FORMAT_CHECK;
 	const char *vpath = NULL;
 	const struct option hash_object_options[] = {
@@ -107,12 +108,14 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, NULL, hash_object_options,
 			     hash_object_usage, 0);
 
-	if (flags & HASH_WRITE_OBJECT) {
+	if (flags & HASH_WRITE_OBJECT)
 		prefix = setup_git_directory();
-		prefix_length = prefix ? strlen(prefix) : 0;
-		if (vpath && prefix)
-			vpath = prefix_filename(prefix, prefix_length, vpath);
-	}
+	else
+		prefix = setup_git_directory_gently(&nongit);
+
+	prefix_length = prefix ? strlen(prefix) : 0;
+	if (vpath && prefix)
+		vpath = prefix_filename(prefix, prefix_length, vpath);
 
 	git_config(git_default_config, NULL);
 
diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh
index c89faa6..acca9ac 100755
--- a/t/t1007-hash-object.sh
+++ b/t/t1007-hash-object.sh
@@ -123,6 +123,17 @@ test_expect_success 'check that appropriate filter is invoke when --path is used
 	test "$file1_sha" = "$path1_sha"
 '
 
+test_expect_success 'gitattributes also work in a subdirectory' '
+	mkdir subdir &&
+	(
+		cd subdir &&
+		subdir_sha0=$(git hash-object ../file0) &&
+		subdir_sha1=$(git hash-object ../file1) &&
+		test "$file0_sha" = "$subdir_sha0" &&
+		test "$file1_sha" = "$subdir_sha1"
+	)
+'
+
 test_expect_success 'check that --no-filters option works' '
 	nofilters_file1=$(git hash-object --no-filters file1) &&
 	test "$file0_sha" = "$nofilters_file1" &&
-- 
2.10.0.230.g6f8d04b
Previous: Stefan BellerNext: Jeff King
Message 4 of 23 in “fix config-reading in non-repos”
  1. 0/16 fix config-reading in non-reposJeff King, Sep 13, 2016
  2. 01/16 t1007: factor out repeated setupJeff King, Sep 13, 2016
  3. Stefan BellerSep 13, 2016
  4. 02/16 hash-object: always try to set up the git repositoryJeff King, Sep 13, 2016
  5. 06/16 diff: always try to set up the repositoryJeff King, Sep 13, 2016
  6. Stefan BellerSep 13, 2016
  7. Jeff KingSep 13, 2016
  8. 04/16 diff: skip implicit no-index check when given --no-indexJeff King, Sep 13, 2016
  9. 07/16 pager: remove obsolete commentJeff King, Sep 13, 2016
  10. 03/16 patch-id: use RUN_SETUP_GENTLYJeff King, Sep 13, 2016
  11. 05/16 diff: handle --no-index prefixes consistentlyJeff King, Sep 13, 2016
  12. 08/16 pager: stop loading git_default_config()Jeff King, Sep 13, 2016
  13. 09/16 pager: make pager_program a file-local staticJeff King, Sep 13, 2016
  14. 10/16 pager: use callbacks instead of configsetJeff King, Sep 13, 2016
  15. 11/16 pager: handle early configJeff King, Sep 13, 2016
  16. 12/16 t1302: use "git -C"Jeff King, Sep 13, 2016
  17. 13/16 test-config: setup git directoryJeff King, Sep 13, 2016
  18. 14/16 config: only read .git/config from configured reposJeff King, Sep 13, 2016
  19. 15/16 init: expand comments explaining config trickeryJeff King, Sep 13, 2016
  20. 16/16 init: reset cached config when entering new repoJeff King, Sep 13, 2016
  21. Stefan BellerSep 13, 2016
  22. Dennis KaarsemakerSep 14, 2016
  23. Jeff KingSep 14, 2016

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.