aboutsummaryrefslogtreecommitdiffstats
path: root/attr.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2018-08-13 18:14:20 +0200
committerJunio C Hamano <gitster@pobox.com>2018-08-13 14:14:42 -0700
commit7a400a2c0270f2085b70690e4ddbfd8d141e69ca (patch)
tree4629e63313178eb48ffce09d61f1e5adf7bbebd5 /attr.c
parent07096c969678a999c24816805d72bf7e0e840384 (diff)
downloadgit-7a400a2c0270f2085b70690e4ddbfd8d141e69ca.tar.gz
attr: remove an implicit dependency on the_index
Make the attr API take an index_state instead of assuming the_index in attr code. All call sites are converted blindly to keep the patch simple and retain current behavior. Individual call sites may receive further updates to use the right index instead of the_index. There is one ugly temporary workaround added in attr.c that needs some more explanation. Commit c24f3abace (apply: file commited with CRLF should roundtrip diff and apply - 2017-08-19) forces one convert_to_git() call to NOT read the index at all. But what do you know, we read it anyway by falling back to the_index. When "istate" from convert_to_git is now propagated down to read_attr_from_array() we will hit segfault somewhere inside read_blob_data_from_index. The right way of dealing with this is to kill "use_index" variable and only follow "istate" but at this stage we are not ready for that: while most git_attr_set_direction() calls just passes the_index to be assigned to use_index, unpack-trees passes a different one which is used by entry.c code, which has no way to know what index to use if we delete use_index. So this has to be done later. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'attr.c')
-rw-r--r--attr.c57
1 files changed, 38 insertions, 19 deletions
diff --git a/attr.c b/attr.c
index 067fb9e0c0..863fad3bd1 100644
--- a/attr.c
+++ b/attr.c
@@ -708,10 +708,10 @@ static struct attr_stack *read_attr_from_array(const char **list)
* another thread could potentially be calling into the attribute system.
*/
static enum git_attr_direction direction;
-static struct index_state *use_index;
+static const struct index_state *use_index;
void git_attr_set_direction(enum git_attr_direction new_direction,
- struct index_state *istate)
+ const struct index_state *istate)
{
if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
BUG("non-INDEX attr direction in a bare repo");
@@ -743,13 +743,24 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
return res;
}
-static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
+static struct attr_stack *read_attr_from_index(const struct index_state *istate,
+ const char *path,
+ int macro_ok)
{
struct attr_stack *res;
char *buf, *sp;
int lineno = 0;
+ const struct index_state *to_read_from;
- buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL);
+ /*
+ * Temporary workaround for c24f3abace (apply: file commited
+ * with CRLF should roundtrip diff and apply - 2017-08-19)
+ */
+ to_read_from = use_index ? use_index : istate;
+ if (!to_read_from)
+ return NULL;
+
+ buf = read_blob_data_from_index(to_read_from, path, NULL);
if (!buf)
return NULL;
@@ -768,15 +779,16 @@ static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
return res;
}
-static struct attr_stack *read_attr(const char *path, int macro_ok)
+static struct attr_stack *read_attr(const struct index_state *istate,
+ const char *path, int macro_ok)
{
struct attr_stack *res = NULL;
if (direction == GIT_ATTR_INDEX) {
- res = read_attr_from_index(path, macro_ok);
+ res = read_attr_from_index(istate, path, macro_ok);
} else if (!is_bare_repository()) {
if (direction == GIT_ATTR_CHECKOUT) {
- res = read_attr_from_index(path, macro_ok);
+ res = read_attr_from_index(istate, path, macro_ok);
if (!res)
res = read_attr_from_file(path, macro_ok);
} else if (direction == GIT_ATTR_CHECKIN) {
@@ -788,7 +800,7 @@ static struct attr_stack *read_attr(const char *path, int macro_ok)
* We allow operation in a sparsely checked out
* work tree, so read from it.
*/
- res = read_attr_from_index(path, macro_ok);
+ res = read_attr_from_index(istate, path, macro_ok);
}
}
@@ -859,7 +871,8 @@ static void push_stack(struct attr_stack **attr_stack_p,
}
}
-static void bootstrap_attr_stack(struct attr_stack **stack)
+static void bootstrap_attr_stack(const struct index_state *istate,
+ struct attr_stack **stack)
{
struct attr_stack *e;
@@ -883,7 +896,7 @@ static void bootstrap_attr_stack(struct attr_stack **stack)
}
/* root directory */
- e = read_attr(GITATTRIBUTES_FILE, 1);
+ e = read_attr(istate, GITATTRIBUTES_FILE, 1);
push_stack(stack, e, xstrdup(""), 0);
/* info frame */
@@ -896,7 +909,8 @@ static void bootstrap_attr_stack(struct attr_stack **stack)
push_stack(stack, e, NULL, 0);
}
-static void prepare_attr_stack(const char *path, int dirlen,
+static void prepare_attr_stack(const struct index_state *istate,
+ const char *path, int dirlen,
struct attr_stack **stack)
{
struct attr_stack *info;
@@ -917,7 +931,7 @@ static void prepare_attr_stack(const char *path, int dirlen,
* .gitattributes in deeper directories to shallower ones,
* and finally use the built-in set as the default.
*/
- bootstrap_attr_stack(stack);
+ bootstrap_attr_stack(istate, stack);
/*
* Pop the "info" one that is always at the top of the stack.
@@ -973,7 +987,7 @@ static void prepare_attr_stack(const char *path, int dirlen,
strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));
strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE);
- next = read_attr(pathbuf.buf, 0);
+ next = read_attr(istate, pathbuf.buf, 0);
/* reset the pathbuf to not include "/.gitattributes" */
strbuf_setlen(&pathbuf, len);
@@ -1095,7 +1109,9 @@ static void determine_macros(struct all_attrs_item *all_attrs,
* If check->check_nr is non-zero, only attributes in check[] are collected.
* Otherwise all attributes are collected.
*/
-static void collect_some_attrs(const char *path, struct attr_check *check)
+static void collect_some_attrs(const struct index_state *istate,
+ const char *path,
+ struct attr_check *check)
{
int i, pathlen, rem, dirlen;
const char *cp, *last_slash = NULL;
@@ -1114,7 +1130,7 @@ static void collect_some_attrs(const char *path, struct attr_check *check)
dirlen = 0;
}
- prepare_attr_stack(path, dirlen, &check->stack);
+ prepare_attr_stack(istate, path, dirlen, &check->stack);
all_attrs_init(&g_attr_hashmap, check);
determine_macros(check->all_attrs, check->stack);
@@ -1136,11 +1152,13 @@ static void collect_some_attrs(const char *path, struct attr_check *check)
fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
}
-int git_check_attr(const char *path, struct attr_check *check)
+int git_check_attr(const struct index_state *istate,
+ const char *path,
+ struct attr_check *check)
{
int i;
- collect_some_attrs(path, check);
+ collect_some_attrs(istate, path, check);
for (i = 0; i < check->nr; i++) {
size_t n = check->items[i].attr->attr_nr;
@@ -1153,12 +1171,13 @@ int git_check_attr(const char *path, struct attr_check *check)
return 0;
}
-void git_all_attrs(const char *path, struct attr_check *check)
+void git_all_attrs(const struct index_state *istate,
+ const char *path, struct attr_check *check)
{
int i;
attr_check_reset(check);
- collect_some_attrs(path, check);
+ collect_some_attrs(istate, path, check);
for (i = 0; i < check->all_attrs_nr; i++) {
const char *name = check->all_attrs[i].attr->name;