Merge branch 'kn/attr-from-tree'

"git check-attr" learned to take an optional tree-ish to read the
.gitattributes file from.

* kn/attr-from-tree:
  attr: add flag `--source` to work with tree-ish
  t0003: move setup for `--all` into new block
This commit is contained in:
Junio C Hamano 2023-01-23 13:39:50 -08:00
commit 577bff3a81
12 changed files with 156 additions and 55 deletions

View File

@ -9,8 +9,8 @@ git-check-attr - Display gitattributes information
SYNOPSIS
--------
[verse]
'git check-attr' [-a | --all | <attr>...] [--] <pathname>...
'git check-attr' --stdin [-z] [-a | --all | <attr>...]
'git check-attr' [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>...
'git check-attr' --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]
DESCRIPTION
-----------
@ -36,6 +36,11 @@ OPTIONS
If `--stdin` is also given, input paths are separated
with a NUL character instead of a linefeed character.
--source=<tree-ish>::
Check attributes against the specified tree-ish. It is common to
specify the source tree by naming a commit, branch or tag associated
with it.
\--::
Interpret all preceding arguments as attributes and all following
arguments as path names.

View File

@ -120,7 +120,7 @@ static const struct attr_check *get_archive_attrs(struct index_state *istate,
static struct attr_check *check;
if (!check)
check = attr_check_initl("export-ignore", "export-subst", NULL);
git_check_attr(istate, path, check);
git_check_attr(istate, NULL, path, check);
return check;
}

97
attr.c
View File

@ -13,6 +13,8 @@
#include "dir.h"
#include "utf8.h"
#include "quote.h"
#include "revision.h"
#include "object-store.h"
#include "thread-utils.h"
const char git_attr__true[] = "(builtin)true";
@ -744,13 +746,61 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
return res;
}
static struct attr_stack *read_attr_from_index(struct index_state *istate,
const char *path,
unsigned flags)
static struct attr_stack *read_attr_from_buf(char *buf, const char *path,
unsigned flags)
{
struct attr_stack *res;
char *buf, *sp;
char *sp;
int lineno = 0;
if (!buf)
return NULL;
CALLOC_ARRAY(res, 1);
for (sp = buf; *sp;) {
char *ep;
int more;
ep = strchrnul(sp, '\n');
more = (*ep == '\n');
*ep = '\0';
handle_attr_line(res, sp, path, ++lineno, flags);
sp = ep + more;
}
free(buf);
return res;
}
static struct attr_stack *read_attr_from_blob(struct index_state *istate,
const struct object_id *tree_oid,
const char *path, unsigned flags)
{
struct object_id oid;
unsigned long sz;
enum object_type type;
void *buf;
unsigned short mode;
if (!tree_oid)
return NULL;
if (get_tree_entry(istate->repo, tree_oid, path, &oid, &mode))
return NULL;
buf = repo_read_object_file(istate->repo, &oid, &type, &sz);
if (!buf || type != OBJ_BLOB) {
free(buf);
return NULL;
}
return read_attr_from_buf(buf, path, flags);
}
static struct attr_stack *read_attr_from_index(struct index_state *istate,
const char *path, unsigned flags)
{
char *buf;
unsigned long size;
if (!istate)
@ -778,28 +828,19 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate,
return NULL;
}
CALLOC_ARRAY(res, 1);
for (sp = buf; *sp; ) {
char *ep;
int more;
ep = strchrnul(sp, '\n');
more = (*ep == '\n');
*ep = '\0';
handle_attr_line(res, sp, path, ++lineno, flags);
sp = ep + more;
}
free(buf);
return res;
return read_attr_from_buf(buf, path, flags);
}
static struct attr_stack *read_attr(struct index_state *istate,
const struct object_id *tree_oid,
const char *path, unsigned flags)
{
struct attr_stack *res = NULL;
if (direction == GIT_ATTR_INDEX) {
res = read_attr_from_index(istate, path, flags);
} else if (tree_oid) {
res = read_attr_from_blob(istate, tree_oid, path, flags);
} else if (!is_bare_repository()) {
if (direction == GIT_ATTR_CHECKOUT) {
res = read_attr_from_index(istate, path, flags);
@ -859,6 +900,7 @@ static void push_stack(struct attr_stack **attr_stack_p,
}
static void bootstrap_attr_stack(struct index_state *istate,
const struct object_id *tree_oid,
struct attr_stack **stack)
{
struct attr_stack *e;
@ -884,7 +926,7 @@ static void bootstrap_attr_stack(struct index_state *istate,
}
/* root directory */
e = read_attr(istate, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);
e = read_attr(istate, tree_oid, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);
push_stack(stack, e, xstrdup(""), 0);
/* info frame */
@ -898,6 +940,7 @@ static void bootstrap_attr_stack(struct index_state *istate,
}
static void prepare_attr_stack(struct index_state *istate,
const struct object_id *tree_oid,
const char *path, int dirlen,
struct attr_stack **stack)
{
@ -919,7 +962,7 @@ static void prepare_attr_stack(struct index_state *istate,
* .gitattributes in deeper directories to shallower ones,
* and finally use the built-in set as the default.
*/
bootstrap_attr_stack(istate, stack);
bootstrap_attr_stack(istate, tree_oid, stack);
/*
* Pop the "info" one that is always at the top of the stack.
@ -974,7 +1017,7 @@ static void prepare_attr_stack(struct index_state *istate,
strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));
strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE);
next = read_attr(istate, pathbuf.buf, READ_ATTR_NOFOLLOW);
next = read_attr(istate, tree_oid, pathbuf.buf, READ_ATTR_NOFOLLOW);
/* reset the pathbuf to not include "/.gitattributes" */
strbuf_setlen(&pathbuf, len);
@ -1094,8 +1137,8 @@ static void determine_macros(struct all_attrs_item *all_attrs,
* Otherwise all attributes are collected.
*/
static void collect_some_attrs(struct index_state *istate,
const char *path,
struct attr_check *check)
const struct object_id *tree_oid,
const char *path, struct attr_check *check)
{
int pathlen, rem, dirlen;
const char *cp, *last_slash = NULL;
@ -1114,7 +1157,7 @@ static void collect_some_attrs(struct index_state *istate,
dirlen = 0;
}
prepare_attr_stack(istate, path, dirlen, &check->stack);
prepare_attr_stack(istate, tree_oid, path, dirlen, &check->stack);
all_attrs_init(&g_attr_hashmap, check);
determine_macros(check->all_attrs, check->stack);
@ -1123,12 +1166,12 @@ static void collect_some_attrs(struct index_state *istate,
}
void git_check_attr(struct index_state *istate,
const char *path,
const struct object_id *tree_oid, const char *path,
struct attr_check *check)
{
int i;
collect_some_attrs(istate, path, check);
collect_some_attrs(istate, tree_oid, path, check);
for (i = 0; i < check->nr; i++) {
unsigned int n = check->items[i].attr->attr_nr;
@ -1139,13 +1182,13 @@ void git_check_attr(struct index_state *istate,
}
}
void git_all_attrs(struct index_state *istate,
void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,
const char *path, struct attr_check *check)
{
int i;
attr_check_reset(check);
collect_some_attrs(istate, path, check);
collect_some_attrs(istate, tree_oid, path, check);
for (i = 0; i < check->all_attrs_nr; i++) {
const char *name = check->all_attrs[i].attr->name;

6
attr.h
View File

@ -120,6 +120,7 @@
#define ATTR_MAX_FILE_SIZE (100 * 1024 * 1024)
struct index_state;
struct object_id;
/**
* An attribute is an opaque object that is identified by its name. Pass the
@ -202,13 +203,14 @@ void attr_check_free(struct attr_check *check);
const char *git_attr_name(const struct git_attr *);
void git_check_attr(struct index_state *istate,
const char *path, struct attr_check *check);
const struct object_id *tree_oid, const char *path,
struct attr_check *check);
/*
* Retrieve all attributes that apply to the specified path.
* check holds the attributes and their values.
*/
void git_all_attrs(struct index_state *istate,
void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,
const char *path, struct attr_check *check);
enum git_attr_direction {

View File

@ -9,9 +9,10 @@
static int all_attrs;
static int cached_attrs;
static int stdin_paths;
static char *source;
static const char * const check_attr_usage[] = {
N_("git check-attr [-a | --all | <attr>...] [--] <pathname>..."),
N_("git check-attr --stdin [-z] [-a | --all | <attr>...]"),
N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
NULL
};
@ -23,6 +24,7 @@ static const struct option check_attr_options[] = {
OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
OPT_BOOL('z', NULL, &nul_term_line,
N_("terminate input and output records by a NUL character")),
OPT_STRING(0, "source", &source, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
OPT_END()
};
@ -55,27 +57,26 @@ static void output_attr(struct attr_check *check, const char *file)
}
}
static void check_attr(const char *prefix,
struct attr_check *check,
int collect_all,
static void check_attr(const char *prefix, struct attr_check *check,
const struct object_id *tree_oid, int collect_all,
const char *file)
{
char *full_path =
prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
if (collect_all) {
git_all_attrs(&the_index, full_path, check);
git_all_attrs(&the_index, tree_oid, full_path, check);
} else {
git_check_attr(&the_index, full_path, check);
git_check_attr(&the_index, tree_oid, full_path, check);
}
output_attr(check, file);
free(full_path);
}
static void check_attr_stdin_paths(const char *prefix,
struct attr_check *check,
int collect_all)
static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
const struct object_id *tree_oid, int collect_all)
{
struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
@ -89,7 +90,7 @@ static void check_attr_stdin_paths(const char *prefix,
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
check_attr(prefix, check, collect_all, buf.buf);
check_attr(prefix, check, tree_oid, collect_all, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout");
}
strbuf_release(&buf);
@ -105,6 +106,8 @@ static NORETURN void error_with_usage(const char *msg)
int cmd_check_attr(int argc, const char **argv, const char *prefix)
{
struct attr_check *check;
struct object_id *tree_oid = NULL;
struct object_id initialized_oid;
int cnt, i, doubledash, filei;
if (!is_bare_repository())
@ -176,11 +179,17 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
}
}
if (source) {
if (repo_get_oid_tree(the_repository, source, &initialized_oid))
die("%s: not a valid tree-ish source", source);
tree_oid = &initialized_oid;
}
if (stdin_paths)
check_attr_stdin_paths(prefix, check, all_attrs);
check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
else {
for (i = filei; i < argc; i++)
check_attr(prefix, check, all_attrs, argv[i]);
check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
maybe_flush_or_die(stdout, "attribute to stdout");
}

View File

@ -1318,7 +1318,7 @@ static int no_try_delta(const char *path)
if (!check)
check = attr_check_initl("delta", NULL);
git_check_attr(the_repository->index, path, check);
git_check_attr(the_repository->index, NULL, path, check);
if (ATTR_FALSE(check->items[0].value))
return 1;
return 0;

View File

@ -1308,7 +1308,7 @@ void convert_attrs(struct index_state *istate,
git_config(read_convert_config, NULL);
}
git_check_attr(istate, path, check);
git_check_attr(istate, NULL, path, check);
ccheck = check->items;
ca->crlf_action = git_path_check_crlf(ccheck + 4);
if (ca->crlf_action == CRLF_UNDEFINED)

View File

@ -391,7 +391,7 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
normalize_file(theirs, path, istate);
}
git_check_attr(istate, path, check);
git_check_attr(istate, NULL, path, check);
ll_driver_name = check->items[0].value;
if (check->items[1].value) {
marker_size = atoi(check->items[1].value);
@ -419,7 +419,7 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
if (!check)
check = attr_check_initl("conflict-marker-size", NULL);
git_check_attr(istate, path, check);
git_check_attr(istate, NULL, path, check);
if (check->items[0].value) {
marker_size = atoi(check->items[0].value);
if (marker_size <= 0)

View File

@ -730,7 +730,7 @@ int match_pathspec_attrs(struct index_state *istate,
if (name[namelen])
name = to_free = xmemdupz(name, namelen);
git_check_attr(istate, name, item->attr_check);
git_check_attr(istate, NULL, name, item->attr_check);
free(to_free);

View File

@ -25,7 +25,15 @@ attr_check_quote () {
git check-attr test -- "$path" >actual &&
echo "\"$quoted_path\": test: $expect" >expect &&
test_cmp expect actual
}
attr_check_source () {
path="$1" expect="$2" source="$3" git_opts="$4" &&
git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
echo "$path: test: $expect" >expect &&
test_cmp expect actual &&
test_must_be_empty err
}
test_expect_success 'open-quoted pathname' '
@ -33,7 +41,6 @@ test_expect_success 'open-quoted pathname' '
attr_check a unspecified
'
test_expect_success 'setup' '
mkdir -p a/b/d a/c b &&
(
@ -80,12 +87,23 @@ test_expect_success 'setup' '
EOF
'
test_expect_success 'setup branches' '
mkdir -p foo/bar &&
test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
"f test=f\na/i test=n\n" tag-1 &&
test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
"g test=g\na/i test=m\n" tag-2 &&
rm foo/bar/.gitattributes
'
test_expect_success 'command line checks' '
test_must_fail git check-attr &&
test_must_fail git check-attr -- &&
test_must_fail git check-attr test &&
test_must_fail git check-attr test -- &&
test_must_fail git check-attr -- f &&
test_must_fail git check-attr --source &&
test_must_fail git check-attr --source not-a-valid-ref &&
echo "f" | test_must_fail git check-attr --stdin &&
echo "f" | test_must_fail git check-attr --stdin -- f &&
echo "f" | test_must_fail git check-attr --stdin test -- f &&
@ -203,9 +221,12 @@ test_expect_success 'attribute test: read paths from stdin' '
test_cmp expect actual
'
test_expect_success 'attribute test: --all option' '
test_expect_success 'setup --all option' '
grep -v unspecified <expect-all | sort >specified-all &&
sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
sed -e "s/:.*//" <expect-all | uniq >stdin-all
'
test_expect_success 'attribute test: --all option' '
git check-attr --stdin --all <stdin-all >tmp &&
sort tmp >actual &&
test_cmp specified-all actual
@ -284,6 +305,15 @@ test_expect_success 'using --git-dir and --work-tree' '
)
'
test_expect_success 'using --source' '
attr_check_source foo/bar/f f tag-1 &&
attr_check_source foo/bar/a/i n tag-1 &&
attr_check_source foo/bar/f unspecified tag-2 &&
attr_check_source foo/bar/a/i m tag-2 &&
attr_check_source foo/bar/g g tag-2 &&
attr_check_source foo/bar/g unspecified tag-1
'
test_expect_success 'setup bare' '
git clone --template= --bare . bare.git
'
@ -303,6 +333,18 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
)
'
test_expect_success 'bare repository: with --source' '
(
cd bare.git &&
attr_check_source foo/bar/f f tag-1 &&
attr_check_source foo/bar/a/i n tag-1 &&
attr_check_source foo/bar/f unspecified tag-2 &&
attr_check_source foo/bar/a/i m tag-2 &&
attr_check_source foo/bar/g g tag-2 &&
attr_check_source foo/bar/g unspecified tag-1
)
'
test_expect_success 'bare repository: check that --cached honors index' '
(
cd bare.git &&

View File

@ -413,7 +413,7 @@ struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
check = attr_check_initl("diff", NULL);
if (!path)
return NULL;
git_check_attr(istate, path, check);
git_check_attr(istate, NULL, path, check);
if (ATTR_TRUE(check->items[0].value))
return &driver_true;

2
ws.c
View File

@ -79,7 +79,7 @@ unsigned whitespace_rule(struct index_state *istate, const char *pathname)
if (!attr_whitespace_rule)
attr_whitespace_rule = attr_check_initl("whitespace", NULL);
git_check_attr(istate, pathname, attr_whitespace_rule);
git_check_attr(istate, NULL, pathname, attr_whitespace_rule);
value = attr_whitespace_rule->items[0].value;
if (ATTR_TRUE(value)) {
/* true (whitespace) */