From 47cfc9bd7d0add617cf6d928e96b7d207be614f1 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Sat, 14 Jan 2023 09:30:38 +0100 Subject: attr: add flag `--source` to work with tree-ish The contents of the .gitattributes files may evolve over time, but "git check-attr" always checks attributes against them in the working tree and/or in the index. It may be beneficial to optionally allow the users to check attributes taken from a commit other than HEAD against paths. Add a new flag `--source` which will allow users to check the attributes against a commit (actually any tree-ish would do). When the user uses this flag, we go through the stack of .gitattributes files but instead of checking the current working tree and/or in the index, we check the blobs from the provided tree-ish object. This allows the command to also be used in bare repositories. Since we use a tree-ish object, the user can pass "--source HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository. We cannot simply use the `:` syntax without the `--source` flag, similar to how it is used in `git show` because any non-flag parameter before `--` is treated as an attribute and any parameter after `--` is treated as a pathname. The change involves creating a new function `read_attr_from_blob`, which given the path reads the blob for the path against the provided source and parses the attributes line by line. This function is plugged into `read_attr()` function wherein we go through the stack of attributes files. Signed-off-by: Karthik Nayak Signed-off-by: Toon Claes Co-authored-by: toon@iotcl.com Signed-off-by: Junio C Hamano --- attr.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'attr.h') diff --git a/attr.h b/attr.h index 3fb40cced0..fca6c30430 100644 --- a/attr.h +++ b/attr.h @@ -108,6 +108,7 @@ */ struct index_state; +struct object_id; /** * An attribute is an opaque object that is identified by its name. Pass the @@ -190,13 +191,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 { -- cgit v1.2.3