attr: convert git_all_attrs() to use "struct attr_check"

This updates the other two ways the attribute check is done via an
array of "struct attr_check_item" elements.  These two niches
appear only in "git check-attr".

 * The caller does not know offhand what attributes it wants to ask
   about and cannot use attr_check_initl() to prepare the
   attr_check structure.

 * The caller may not know what attributes it wants to ask at all,
   and instead wants to learn everything that the given path has.

Such a caller can call attr_check_alloc() to allocate an empty
attr_check, and then call attr_check_append() to add attribute names
one by one.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2017-01-30 10:06:08 -08:00
parent 37293768d0
commit 7f8641112d
3 changed files with 43 additions and 56 deletions

30
attr.c
View File

@ -906,32 +906,22 @@ int git_check_attrs(const char *path, int num, struct attr_check_item *check)
return 0; return 0;
} }
int git_all_attrs(const char *path, int *num, struct attr_check_item **check) void git_all_attrs(const char *path, struct attr_check *check)
{ {
int i, count, j; int i;
collect_some_attrs(path, 0, NULL); attr_check_reset(check);
collect_some_attrs(path, check->nr, check->items);
/* Count the number of attributes that are set. */
count = 0;
for (i = 0; i < attr_nr; i++) { for (i = 0; i < attr_nr; i++) {
const char *name = check_all_attr[i].attr->name;
const char *value = check_all_attr[i].value; const char *value = check_all_attr[i].value;
if (value != ATTR__UNSET && value != ATTR__UNKNOWN) struct attr_check_item *item;
++count; if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
continue;
item = attr_check_append(check, git_attr(name));
item->value = value;
} }
*num = count;
ALLOC_ARRAY(*check, count);
j = 0;
for (i = 0; i < attr_nr; i++) {
const char *value = check_all_attr[i].value;
if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
(*check)[j].attr = check_all_attr[i].attr;
(*check)[j].value = value;
++j;
}
}
return 0;
} }
int git_check_attr(const char *path, struct attr_check *check) int git_check_attr(const char *path, struct attr_check *check)

9
attr.h
View File

@ -56,13 +56,10 @@ int git_check_attrs(const char *path, int, struct attr_check_item *);
extern int git_check_attr(const char *path, struct attr_check *check); extern int git_check_attr(const char *path, struct attr_check *check);
/* /*
* Retrieve all attributes that apply to the specified path. *num * Retrieve all attributes that apply to the specified path.
* will be set to the number of attributes on the path; **check will * check holds the attributes and their values.
* be set to point at a newly-allocated array of git_attr_check
* objects describing the attributes and their values. *check must be
* free()ed by the caller.
*/ */
int git_all_attrs(const char *path, int *num, struct attr_check_item **check); extern void git_all_attrs(const char *path, struct attr_check *check);
enum git_attr_direction { enum git_attr_direction {
GIT_ATTR_CHECKIN, GIT_ATTR_CHECKIN,

View File

@ -24,12 +24,13 @@ static const struct option check_attr_options[] = {
OPT_END() OPT_END()
}; };
static void output_attr(int cnt, struct attr_check_item *check, static void output_attr(struct attr_check *check, const char *file)
const char *file)
{ {
int j; int j;
int cnt = check->nr;
for (j = 0; j < cnt; j++) { for (j = 0; j < cnt; j++) {
const char *value = check[j].value; const char *value = check->items[j].value;
if (ATTR_TRUE(value)) if (ATTR_TRUE(value))
value = "set"; value = "set";
@ -42,36 +43,38 @@ static void output_attr(int cnt, struct attr_check_item *check,
printf("%s%c" /* path */ printf("%s%c" /* path */
"%s%c" /* attrname */ "%s%c" /* attrname */
"%s%c" /* attrvalue */, "%s%c" /* attrvalue */,
file, 0, git_attr_name(check[j].attr), 0, value, 0); file, 0,
git_attr_name(check->items[j].attr), 0, value, 0);
} else { } else {
quote_c_style(file, NULL, stdout, 0); quote_c_style(file, NULL, stdout, 0);
printf(": %s: %s\n", git_attr_name(check[j].attr), value); printf(": %s: %s\n",
git_attr_name(check->items[j].attr), value);
} }
} }
} }
static void check_attr(const char *prefix, static void check_attr(const char *prefix,
int cnt, struct attr_check_item *check, struct attr_check *check,
int collect_all,
const char *file) const char *file)
{ {
char *full_path = char *full_path =
prefix_path(prefix, prefix ? strlen(prefix) : 0, file); prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
if (check != NULL) {
if (git_check_attrs(full_path, cnt, check)) if (collect_all) {
die("git_check_attrs died"); git_all_attrs(full_path, check);
output_attr(cnt, check, file);
} else { } else {
if (git_all_attrs(full_path, &cnt, &check)) if (git_check_attr(full_path, check))
die("git_all_attrs died"); die("git_check_attr died");
output_attr(cnt, check, file);
free(check);
} }
output_attr(check, file);
free(full_path); free(full_path);
} }
static void check_attr_stdin_paths(const char *prefix, static void check_attr_stdin_paths(const char *prefix,
int cnt, struct attr_check_item *check) struct attr_check *check,
int collect_all)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT; struct strbuf unquoted = STRBUF_INIT;
@ -85,7 +88,7 @@ static void check_attr_stdin_paths(const char *prefix,
die("line is badly quoted"); die("line is badly quoted");
strbuf_swap(&buf, &unquoted); strbuf_swap(&buf, &unquoted);
} }
check_attr(prefix, cnt, check, buf.buf); check_attr(prefix, check, collect_all, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout"); maybe_flush_or_die(stdout, "attribute to stdout");
} }
strbuf_release(&buf); strbuf_release(&buf);
@ -100,7 +103,7 @@ static NORETURN void error_with_usage(const char *msg)
int cmd_check_attr(int argc, const char **argv, const char *prefix) int cmd_check_attr(int argc, const char **argv, const char *prefix)
{ {
struct attr_check_item *check; struct attr_check *check;
int cnt, i, doubledash, filei; int cnt, i, doubledash, filei;
if (!is_bare_repository()) if (!is_bare_repository())
@ -160,28 +163,25 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
error_with_usage("No file specified"); error_with_usage("No file specified");
} }
if (all_attrs) { check = attr_check_alloc();
check = NULL; if (!all_attrs) {
} else {
check = xcalloc(cnt, sizeof(*check));
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
const char *name; struct git_attr *a = git_attr(argv[i]);
struct git_attr *a;
name = argv[i];
a = git_attr(name);
if (!a) if (!a)
return error("%s: not a valid attribute name", return error("%s: not a valid attribute name",
name); argv[i]);
check[i].attr = a; attr_check_append(check, a);
} }
} }
if (stdin_paths) if (stdin_paths)
check_attr_stdin_paths(prefix, cnt, check); check_attr_stdin_paths(prefix, check, all_attrs);
else { else {
for (i = filei; i < argc; i++) for (i = filei; i < argc; i++)
check_attr(prefix, cnt, check, argv[i]); check_attr(prefix, check, all_attrs, argv[i]);
maybe_flush_or_die(stdout, "attribute to stdout"); maybe_flush_or_die(stdout, "attribute to stdout");
} }
attr_check_free(check);
return 0; return 0;
} }