attr: teach "--attr-source=<tree>" global option to "git"

Earlier, 47cfc9bd (attr: add flag `--source` to work with tree-ish,
2023-01-14) taught "git check-attr" the "--source=<tree>" option to
allow it to read attribute files from a tree-ish, but did so only
for the command.  Just like "check-attr" users wanted a way to use
attributes from a tree-ish and not from the working tree files,
users of other commands (like "git diff") would benefit from the
same.

Undo most of the UI change the commit made, while keeping the
internal logic to read attributes from a given tree-ish. Expose the
internal logic via a new "--attr-source=<tree>" command line option
given to "git", so that it can be used with any git command that
runs as part of the main git process.

Additionally, add an environment variable GIT_ATTR_SOURCE that is set
when --attr-source is passed in, so that subprocesses use the same value
for the attributes source tree.

Signed-off-by: John Cai <johncai86@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
John Cai
2023-05-06 04:15:29 +00:00
committed by Junio C Hamano
parent 48d89b51b3
commit 44451a2e5e
16 changed files with 140 additions and 29 deletions

View File

@ -63,7 +63,7 @@ static void output_attr(struct attr_check *check, const char *file)
}
static void check_attr(const char *prefix, struct attr_check *check,
const struct object_id *tree_oid, int collect_all,
int collect_all,
const char *file)
{
@ -71,9 +71,9 @@ static void check_attr(const char *prefix, struct attr_check *check,
prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
if (collect_all) {
git_all_attrs(&the_index, tree_oid, full_path, check);
git_all_attrs(&the_index, full_path, check);
} else {
git_check_attr(&the_index, tree_oid, full_path, check);
git_check_attr(&the_index, full_path, check);
}
output_attr(check, file);
@ -81,7 +81,7 @@ static void check_attr(const char *prefix, struct attr_check *check,
}
static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
const struct object_id *tree_oid, int collect_all)
int collect_all)
{
struct strbuf buf = STRBUF_INIT;
struct strbuf unquoted = STRBUF_INIT;
@ -95,7 +95,7 @@ static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
die("line is badly quoted");
strbuf_swap(&buf, &unquoted);
}
check_attr(prefix, check, tree_oid, collect_all, buf.buf);
check_attr(prefix, check, collect_all, buf.buf);
maybe_flush_or_die(stdout, "attribute to stdout");
}
strbuf_release(&buf);
@ -111,7 +111,6 @@ 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;
@ -187,14 +186,14 @@ 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;
set_git_attr_source(source);
}
if (stdin_paths)
check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
check_attr_stdin_paths(prefix, check, all_attrs);
else {
for (i = filei; i < argc; i++)
check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
check_attr(prefix, check, all_attrs, argv[i]);
maybe_flush_or_die(stdout, "attribute to stdout");
}