From 44451a2e5eec5360378be23e2cdbd9ecee49e14e Mon Sep 17 00:00:00 2001 From: John Cai Date: Sat, 6 May 2023 04:15:29 +0000 Subject: attr: teach "--attr-source=" global option to "git" Earlier, 47cfc9bd (attr: add flag `--source` to work with tree-ish, 2023-01-14) taught "git check-attr" the "--source=" 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=" 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 Signed-off-by: Junio C Hamano --- attr.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'attr.c') diff --git a/attr.c b/attr.c index 2d8aeb8b58..11238d6083 100644 --- a/attr.c +++ b/attr.c @@ -20,6 +20,7 @@ #include "object-store.h" #include "setup.h" #include "thread-utils.h" +#include "object-name.h" const char git_attr__true[] = "(builtin)true"; const char git_attr__false[] = "\0(builtin)false"; @@ -1169,11 +1170,42 @@ static void collect_some_attrs(struct index_state *istate, fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem); } +static const char *default_attr_source_tree_object_name; + +void set_git_attr_source(const char *tree_object_name) +{ + default_attr_source_tree_object_name = xstrdup(tree_object_name); +} + +static void compute_default_attr_source(struct object_id *attr_source) +{ + if (!default_attr_source_tree_object_name) + default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); + + if (!default_attr_source_tree_object_name || !is_null_oid(attr_source)) + return; + + if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, attr_source)) + die(_("bad --attr-source or GIT_ATTR_SOURCE")); +} + +static struct object_id *default_attr_source(void) +{ + static struct object_id attr_source; + + if (is_null_oid(&attr_source)) + compute_default_attr_source(&attr_source); + if (is_null_oid(&attr_source)) + return NULL; + return &attr_source; +} + void git_check_attr(struct index_state *istate, - const struct object_id *tree_oid, const char *path, + const char *path, struct attr_check *check) { int i; + const struct object_id *tree_oid = default_attr_source(); collect_some_attrs(istate, tree_oid, path, check); @@ -1186,10 +1218,11 @@ void git_check_attr(struct index_state *istate, } } -void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid, +void git_all_attrs(struct index_state *istate, const char *path, struct attr_check *check) { int i; + const struct object_id *tree_oid = default_attr_source(); attr_check_reset(check); collect_some_attrs(istate, tree_oid, path, check); -- cgit v1.2.3