Skip to content

Commit 26dd307

Browse files
committed
Merge branch 'jc/attr-tree-config'
The attribute subsystem learned to honor `attr.tree` configuration that specifies which tree to read the .gitattributes files from. * jc/attr-tree-config: attr: add attr.tree for setting the treeish to read attributes from attr: read attributes from HEAD when bare repo
2 parents 8183b63 + 9f9c40c commit 26dd307

File tree

7 files changed

+119
-3
lines changed

7 files changed

+119
-3
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ other popular tools, and describe them in your documentation.
371371

372372
include::config/advice.txt[]
373373

374+
include::config/attr.txt[]
375+
374376
include::config/core.txt[]
375377

376378
include::config/add.txt[]

Documentation/config/attr.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
attr.tree::
2+
A reference to a tree in the repository from which to read attributes,
3+
instead of the `.gitattributes` file in the working tree. In a bare
4+
repository, this defaults to `HEAD:.gitattributes`. If the value does
5+
not resolve to a valid tree object, an empty tree is used instead.
6+
When the `GIT_ATTR_SOURCE` environment variable or `--attr-source`
7+
command line option are used, this configuration variable has no effect.

attr.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
#include "git-compat-util.h"
10-
#include "parse.h"
10+
#include "config.h"
1111
#include "environment.h"
1212
#include "exec-cmd.h"
1313
#include "attr.h"
@@ -24,6 +24,8 @@
2424
#include "tree-walk.h"
2525
#include "object-name.h"
2626

27+
const char *git_attr_tree;
28+
2729
const char git_attr__true[] = "(builtin)true";
2830
const char git_attr__false[] = "\0(builtin)false";
2931
static const char git_attr__unknown[] = "(builtin)unknown";
@@ -1194,6 +1196,7 @@ static void collect_some_attrs(struct index_state *istate,
11941196
}
11951197

11961198
static const char *default_attr_source_tree_object_name;
1199+
static int ignore_bad_attr_tree;
11971200

11981201
void set_git_attr_source(const char *tree_object_name)
11991202
{
@@ -1205,10 +1208,24 @@ static void compute_default_attr_source(struct object_id *attr_source)
12051208
if (!default_attr_source_tree_object_name)
12061209
default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
12071210

1211+
if (!default_attr_source_tree_object_name && git_attr_tree) {
1212+
default_attr_source_tree_object_name = git_attr_tree;
1213+
ignore_bad_attr_tree = 1;
1214+
}
1215+
1216+
if (!default_attr_source_tree_object_name &&
1217+
startup_info->have_repository &&
1218+
is_bare_repository()) {
1219+
default_attr_source_tree_object_name = "HEAD";
1220+
ignore_bad_attr_tree = 1;
1221+
}
1222+
12081223
if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
12091224
return;
12101225

1211-
if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source))
1226+
if (repo_get_oid_treeish(the_repository,
1227+
default_attr_source_tree_object_name,
1228+
attr_source) && !ignore_bad_attr_tree)
12121229
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
12131230
}
12141231

attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,6 @@ const char *git_attr_global_file(void);
236236
/* Return whether the system gitattributes file is enabled and should be used. */
237237
int git_attr_system_is_enabled(void);
238238

239+
extern const char *git_attr_tree;
240+
239241
#endif /* ATTR_H */

config.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "repository.h"
2020
#include "lockfile.h"
2121
#include "mailmap.h"
22+
#include "attr.h"
2223
#include "exec-cmd.h"
2324
#include "strbuf.h"
2425
#include "quote.h"
@@ -1760,6 +1761,18 @@ static int git_default_mailmap_config(const char *var, const char *value)
17601761
return 0;
17611762
}
17621763

1764+
static int git_default_attr_config(const char *var, const char *value)
1765+
{
1766+
if (!strcmp(var, "attr.tree"))
1767+
return git_config_string(&git_attr_tree, var, value);
1768+
1769+
/*
1770+
* Add other attribute related config variables here and to
1771+
* Documentation/config/attr.txt.
1772+
*/
1773+
return 0;
1774+
}
1775+
17631776
int git_default_config(const char *var, const char *value,
17641777
const struct config_context *ctx, void *cb)
17651778
{
@@ -1783,6 +1796,9 @@ int git_default_config(const char *var, const char *value,
17831796
if (starts_with(var, "mailmap."))
17841797
return git_default_mailmap_config(var, value);
17851798

1799+
if (starts_with(var, "attr."))
1800+
return git_default_attr_config(var, value);
1801+
17861802
if (starts_with(var, "advice.") || starts_with(var, "color.advice"))
17871803
return git_default_advice_config(var, value);
17881804

t/t0003-attributes.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ attr_check_source () {
4040
test_cmp expect actual &&
4141
test_must_be_empty err
4242

43+
git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
44+
test_cmp expect actual &&
45+
test_must_be_empty err
46+
4347
GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
4448
test_cmp expect actual &&
4549
test_must_be_empty err
@@ -342,6 +346,74 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
342346
)
343347
'
344348

349+
bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
350+
351+
test_expect_success '--attr-source is bad' '
352+
test_when_finished rm -rf empty &&
353+
git init empty &&
354+
(
355+
cd empty &&
356+
echo "$bad_attr_source_err" >expect_err &&
357+
test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
358+
test_cmp expect_err err
359+
)
360+
'
361+
362+
test_expect_success 'attr.tree when HEAD is unborn' '
363+
test_when_finished rm -rf empty &&
364+
git init empty &&
365+
(
366+
cd empty &&
367+
echo "f/path: test: unspecified" >expect &&
368+
git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
369+
test_must_be_empty err &&
370+
test_cmp expect actual
371+
)
372+
'
373+
374+
test_expect_success 'bad attr source defaults to reading .gitattributes file' '
375+
test_when_finished rm -rf empty &&
376+
git init empty &&
377+
(
378+
cd empty &&
379+
echo "f/path test=val" >.gitattributes &&
380+
echo "f/path: test: val" >expect &&
381+
git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
382+
test_must_be_empty err &&
383+
test_cmp expect actual
384+
)
385+
'
386+
387+
test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
388+
test_when_finished rm -rf test bare_with_gitattribute &&
389+
git init test &&
390+
test_commit -C test gitattributes .gitattributes "f/path test=val" &&
391+
git clone --bare test bare_with_gitattribute &&
392+
echo "f/path: test: val" >expect &&
393+
git -C bare_with_gitattribute check-attr test -- f/path >actual &&
394+
test_cmp expect actual
395+
'
396+
397+
test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
398+
test_when_finished rm -rf empty &&
399+
git init empty &&
400+
(
401+
cd empty &&
402+
git checkout -b attr-source &&
403+
test_commit "val1" .gitattributes "f/path test=val1" &&
404+
git checkout -b attr-tree &&
405+
test_commit "val2" .gitattributes "f/path test=val2" &&
406+
git checkout attr-source &&
407+
echo "f/path: test: val1" >expect &&
408+
GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
409+
check-attr test -- f/path >actual &&
410+
test_cmp expect actual &&
411+
GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
412+
check-attr test -- f/path >actual &&
413+
test_cmp expect actual
414+
)
415+
'
416+
345417
test_expect_success 'bare repository: with --source' '
346418
(
347419
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)