Skip to content

Commit

Permalink
attr: attr.allowInvalidSource config to allow invalid revision
Browse files Browse the repository at this point in the history
44451a2 (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.
  • Loading branch information
john-cai committed Sep 19, 2023
1 parent 1fc548b commit a3a6d14
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
6 changes: 6 additions & 0 deletions Documentation/config/attr.txt
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 9 additions & 2 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions t/t0003-attributes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit a3a6d14

Please sign in to comment.