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

[PATCH] attr: attr.allowInvalidSource config to allow invalid revision

From
John Cai via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 20, 2023, 14:00 UTC
Message-ID
<pull.1577.git.git.1695218431033.gitgitgadget@gmail.com>
From: John Cai <johncai86@gmail.com>

44451a2e5e (attr: teach "--attr-source=<tree>" global option to "git", 2023-05-06) provided the ability to pass in a treeish as the attr source. When a revision does not resolve to a valid tree is passed, Git will die. GitLab keeps bare repositories and always reads attributes from the default branch, so we pass in HEAD to --attr-source.

With empty repositories however, HEAD does not point to a valid treeish, causing Git to die. This means we would need to check for a valid treeish each time. To avoid this, let's add a configuration that allows Git to simply ignore --attr-source if it does not resolve to a valid tree.

Signed-off-by: John Cai <johncai86@gmail.com>
---
    attr: attr.allowInvalidSource config to allow empty revision
    
    44451a2e5e (attr: teach "--attr-source=" global option to "git",
    2023-05-06) provided the ability to pass in a treeish as the attr
    source. When a revision does not resolve to a valid tree is passed, Git
    will die. GitLab keeps bare repositories and always reads attributes
    from the default branch, so we pass in HEAD to --attr-source.
    
    With empty repositories however, HEAD does not point to a valid treeish,
    causing Git to die. This means we would need to check for a valid
    treeish each time. To avoid this, let's add a configuration that allows
    Git to simply ignore --attr-source if it does not resolve to a valid
    tree.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1577%2Fjohn-cai%2Fjc%2Fconfig-attr-invalid-source-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1577/john-cai/jc/config-attr-invalid-source-v1
Pull-Request: https://github.com/git/git/pull/1577
 Documentation/config.txt      |  2 ++
 Documentation/config/attr.txt |  6 ++++++
 attr.c                        | 11 +++++++++--
 t/t0003-attributes.sh         | 36 +++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/config/attr.txt
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 229b63a454c..b1891c2b5af 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -371,6 +371,8 @@ other popular tools, and describe them in your documentation.
 
 include::config/advice.txt[]
 
+include::config/attr.txt[]
+
 include::config/core.txt[]
 
 include::config/add.txt[]
diff --git a/Documentation/config/attr.txt b/Documentation/config/attr.txt
new file mode 100644
index 00000000000..2218f0c982a
--- /dev/null
+++ b/Documentation/config/attr.txt
@@ -0,0 +1,6 @@
+attr.allowInvalidSource::
+	If `--attr-source` cannot resolve to a valid tree object, ignore
+	`--attr-source` instead of erroring out, and fall back to looking for
+	attributes in the default locations. Useful when passing `HEAD` into
+	`attr-source` since it allows `HEAD` to point to an unborn branch in
+	cases like an empty repository.
diff --git a/attr.c b/attr.c
index 71c84fbcf86..854a3720e3f 100644
--- a/attr.c
+++ b/attr.c
@@ -1208,8 +1208,15 @@ static void compute_default_attr_source(struct object_id *attr_source)
 	if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
 		return;
 
-	if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source))
-		die(_("bad --attr-source or GIT_ATTR_SOURCE"));
+
+	if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source)) {
+		int allow_invalid_attr_source = 0;
+
+		git_config_get_bool("attr.allowinvalidsource", &allow_invalid_attr_source);
+
+		if (!allow_invalid_attr_source)
+			die(_("bad --attr-source or GIT_ATTR_SOURCE"));
+	}
 }
 
 static struct object_id *default_attr_source(void)
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 26e082f05b4..3272237ee2b 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -342,6 +342,42 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
 	)
 '
 
+bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
+
+test_expect_success 'attr.allowInvalidSource when HEAD is unborn' '
+	test_when_finished rm -rf empty &&
+	echo $bad_attr_source_err >expect_err &&
+	echo "f/path: test: unspecified" >expect &&
+	git init empty &&
+	test_must_fail git -C empty --attr-source=HEAD check-attr test -- f/path 2>err &&
+	test_cmp expect_err err &&
+	git -C empty -c attr.allowInvalidSource=true --attr-source=HEAD check-attr test -- f/path >actual 2>err &&
+	test_must_be_empty err &&
+	test_cmp expect actual
+'
+
+test_expect_success 'attr.allowInvalidSource when --attr-source points to non-existing ref' '
+	test_when_finished rm -rf empty &&
+	echo $bad_attr_source_err >expect_err &&
+	echo "f/path: test: unspecified" >expect &&
+	git init empty &&
+	test_must_fail git -C empty --attr-source=refs/does/not/exist check-attr test -- f/path 2>err &&
+	test_cmp expect_err err &&
+	git -C empty -c attr.allowInvalidSource=true --attr-source=refs/does/not/exist check-attr test -- f/path >actual 2>err &&
+	test_must_be_empty err &&
+	test_cmp expect actual
+'
+
+test_expect_success 'bad attr source defaults to reading .gitattributes file' '
+	test_when_finished rm -rf empty &&
+	git init empty &&
+	echo "f/path test=val" >empty/.gitattributes &&
+	echo "f/path: test: val" >expect &&
+	git -C empty -c attr.allowInvalidSource=true --attr-source=HEAD check-attr test -- f/path >actual 2>err &&
+	test_must_be_empty err &&
+	test_cmp expect actual
+'
+
 test_expect_success 'bare repository: with --source' '
 	(
 		cd bare.git &&

base-commit: 1fc548b2d6a3596f3e1c1f8b1930d8dbd1e30bf3
-- 
gitgitgadget
Next: Junio C Hamano
Message 1 of 34 in “attr: attr.allowInvalidSource config to allow invalid revision”
  1. attr: attr.allowInvalidSource config to allow invalid revisionJohn Cai via GitGitGadget, Sep 20, 2023
  2. Junio C HamanoSep 20, 2023
  3. Jeff KingSep 21, 2023
  4. Junio C HamanoSep 21, 2023
  5. Jeff KingSep 21, 2023
  6. John CaiSep 26, 2023
  7. John CaiSep 26, 2023
  8. John CaiSep 26, 2023
  9. 0/2 attr: add attr.tree and attr.allowInvalidSource configsJohn Cai via GitGitGadget, Oct 4, 2023
  10. 1/2 attr: add attr.tree for setting the treeish to read attributes fromJohn Cai via GitGitGadget, Oct 4, 2023
  11. Junio C HamanoOct 4, 2023
  12. Jeff KingOct 5, 2023
  13. John CaiOct 5, 2023
  14. Junio C HamanoOct 4, 2023
  15. Jonathan TanOct 6, 2023
  16. 2/2 attr: add attr.allowInvalidSource config to allow invalid revisionJohn Cai via GitGitGadget, Oct 4, 2023
  17. 0/2 attr: add attr.tree configJohn Cai via GitGitGadget, Oct 10, 2023
  18. 1/2 attr: read attributes from HEAD when bare repoJohn Cai via GitGitGadget, Oct 10, 2023
  19. Eric SunshineOct 10, 2023
  20. 2/2 attr: add attr.tree for setting the treeish to read attributes fromJohn Cai via GitGitGadget, Oct 10, 2023
  21. Junio C HamanoOct 10, 2023
  22. John CaiOct 11, 2023
  23. 0/2 attr: add attr.tree configJohn Cai via GitGitGadget, Oct 11, 2023
  24. 1/2 attr: read attributes from HEAD when bare repoJohn Cai via GitGitGadget, Oct 11, 2023
  25. 2/2 attr: add attr.tree for setting the treeish to read attributes fromJohn Cai via GitGitGadget, Oct 11, 2023
  26. Junio C HamanoOct 11, 2023
  27. John CaiOct 13, 2023
  28. 0/2 attr: add attr.tree configJohn Cai via GitGitGadget, Oct 13, 2023
  29. 1/2 attr: read attributes from HEAD when bare repoJohn Cai via GitGitGadget, Oct 13, 2023
  30. 2/2 attr: add attr.tree for setting the treeish to read attributes fromJohn Cai via GitGitGadget, Oct 13, 2023
  31. Junio C HamanoOct 13, 2023
  32. Junio C HamanoOct 13, 2023
  33. Junio C HamanoOct 13, 2023
  34. John CaiOct 19, 2023

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.