'git check-attr' cannot currently find attributes of a file within a sparse directory. This is due to .gitattributes files are irrelevant in sparse-checkout cone mode, as the file is considered sparse only if all paths within its parent directory are also sparse. In addition, searching for a .gitattributes file causes expansion of the sparse index, which is avoided to prevent potential performance degradation. However, this behavior can lead to missing attributes for files inside sparse directories, causing inconsistencies in file handling. To resolve this, revise 'git check-attr' to allow attribute reading for files in sparse directories from the corresponding .gitattributes files: 1.Utilize path_in_cone_mode_sparse_checkout() and index_name_pos_sparse to check if a path falls within a sparse directory. 2.If path is inside a sparse directory, employ the value of index_name_pos_sparse() to find the sparse directory containing path and path relative to sparse directory. Proceed to read attributes from the tree OID of the sparse directory using read_attr_from_blob(). 3.If path is not inside a sparse directory,ensure that attributes are fetched from the index blob with read_blob_data_from_index(). Helped-by: Victoria Dye <vdye@xxxxxxxxxx> Signed-off-by: Shuqi Liang <cheskaqiqi@xxxxxxxxx> --- attr.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/attr.c b/attr.c index 7d39ac4a29..03deb18fb3 100644 --- a/attr.c +++ b/attr.c @@ -808,35 +808,35 @@ static struct attr_stack *read_attr_from_blob(struct index_state *istate, static struct attr_stack *read_attr_from_index(struct index_state *istate, const char *path, unsigned flags) { + struct attr_stack *stack = NULL; char *buf; unsigned long size; + int pos; if (!istate) return NULL; - /* - * The .gitattributes file only applies to files within its - * parent directory. In the case of cone-mode sparse-checkout, - * the .gitattributes file is sparse if and only if all paths - * within that directory are also sparse. Thus, don't load the - * .gitattributes file since it will not matter. - * - * In the case of a sparse index, it is critical that we don't go - * looking for a .gitattributes file, as doing so would cause the - * index to expand. - */ - if (!path_in_cone_mode_sparse_checkout(path, istate)) - return NULL; + pos = index_name_pos_sparse(istate, path, strlen(path)); + pos = -pos-2; + if (!path_in_cone_mode_sparse_checkout(path, istate) && pos>=0) { + if (!S_ISSPARSEDIR(istate->cache[pos]->ce_mode)) + return NULL; - buf = read_blob_data_from_index(istate, path, &size); - if (!buf) - return NULL; - if (size >= ATTR_MAX_FILE_SIZE) { - warning(_("ignoring overly large gitattributes blob '%s'"), path); - return NULL; + if (strncmp(istate->cache[pos]->name, path, ce_namelen(istate->cache[pos])) == 0) { + const char *relative_path = path + ce_namelen(istate->cache[pos]); + stack = read_attr_from_blob(istate, &istate->cache[pos]->oid, relative_path, flags); + } + } else { + buf = read_blob_data_from_index(istate, path, &size); + if (!buf) + return NULL; + if (size >= ATTR_MAX_FILE_SIZE) { + warning(_("ignoring overly large gitattributes blob '%s'"), path); + return NULL; + } + stack = read_attr_from_buf(buf, path, flags); } - - return read_attr_from_buf(buf, path, flags); + return stack; } static struct attr_stack *read_attr(struct index_state *istate, -- 2.39.0