Skip to content

Commit 2386535

Browse files
john-caigitster
authored andcommitted
attr: read attributes from HEAD when bare repo
The motivation for 44451a2 (attr: teach "--attr-source=<tree>" global option to "git" , 2023-05-06), was to make it possible to use gitattributes with bare repositories. To make it easier to read gitattributes in bare repositories however, let's just make HEAD:.gitattributes the default. This is in line with how mailmap works, 8c473ce (mailmap: default mailmap.blob in bare repositories, 2012-12-13). Signed-off-by: John Cai <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43c8a30 commit 2386535

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

attr.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ static void collect_some_attrs(struct index_state *istate,
11731173
}
11741174

11751175
static const char *default_attr_source_tree_object_name;
1176+
static int ignore_bad_attr_tree;
11761177

11771178
void set_git_attr_source(const char *tree_object_name)
11781179
{
@@ -1184,10 +1185,19 @@ static void compute_default_attr_source(struct object_id *attr_source)
11841185
if (!default_attr_source_tree_object_name)
11851186
default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
11861187

1188+
if (!default_attr_source_tree_object_name &&
1189+
startup_info->have_repository &&
1190+
is_bare_repository()) {
1191+
default_attr_source_tree_object_name = "HEAD";
1192+
ignore_bad_attr_tree = 1;
1193+
}
1194+
11871195
if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
11881196
return;
11891197

1190-
if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source))
1198+
if (repo_get_oid_treeish(the_repository,
1199+
default_attr_source_tree_object_name,
1200+
attr_source) && !ignore_bad_attr_tree)
11911201
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
11921202
}
11931203

t/t0003-attributes.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
342342
)
343343
'
344344

345+
346+
test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
347+
test_when_finished rm -rf test bare_with_gitattribute &&
348+
git init test &&
349+
test_commit -C test gitattributes .gitattributes "f/path test=val" &&
350+
git clone --bare test bare_with_gitattribute &&
351+
echo "f/path: test: val" >expect &&
352+
git -C bare_with_gitattribute check-attr test -- f/path >actual &&
353+
test_cmp expect actual
354+
'
355+
345356
test_expect_success 'bare repository: with --source' '
346357
(
347358
cd bare.git &&

t/t5001-archive-attr.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ test_expect_success 'git archive with worktree attributes, bare' '
138138
'
139139

140140
test_expect_missing bare-worktree/ignored
141-
test_expect_exists bare-worktree/ignored-by-tree
141+
test_expect_missing bare-worktree/ignored-by-tree
142142
test_expect_exists bare-worktree/ignored-by-worktree
143143

144144
test_expect_success 'export-subst' '

0 commit comments

Comments
 (0)