Skip to content

Commit bbb82f8

Browse files
pks-tgitster
authored andcommitted
attr: don't recompute default attribute source
The `default_attr_source()` function lazily computes the attr source supposedly once, only. This is done via a static variable `attr_source` that contains the resolved object ID of the attr source's tree. If the variable is the null object ID then we try to look up the attr source, otherwise we skip over it. This approach is flawed though: the variable will never be set to anything else but the null object ID in case there is no attr source. Consequently, we re-compute the information on every call. And in the worst case, when we silently ignore bad trees, this will cause us to try and look up the treeish every single time. Improve this by introducing a separate variable `has_attr_source` to track whether we already computed the attr source and, if so, whether we have an attr source or not. This also allows us to convert the `ignore_bad_attr_tree` to not be static anymore as the code will only be executed once anyway. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7afb46 commit bbb82f8

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

attr.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,15 +1206,16 @@ static void collect_some_attrs(struct index_state *istate,
12061206
}
12071207

12081208
static const char *default_attr_source_tree_object_name;
1209-
static int ignore_bad_attr_tree;
12101209

12111210
void set_git_attr_source(const char *tree_object_name)
12121211
{
12131212
default_attr_source_tree_object_name = xstrdup(tree_object_name);
12141213
}
12151214

1216-
static void compute_default_attr_source(struct object_id *attr_source)
1215+
static int compute_default_attr_source(struct object_id *attr_source)
12171216
{
1217+
int ignore_bad_attr_tree = 0;
1218+
12181219
if (!default_attr_source_tree_object_name)
12191220
default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
12201221

@@ -1223,22 +1224,28 @@ static void compute_default_attr_source(struct object_id *attr_source)
12231224
ignore_bad_attr_tree = 1;
12241225
}
12251226

1226-
if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
1227-
return;
1227+
if (!default_attr_source_tree_object_name)
1228+
return 0;
12281229

12291230
if (repo_get_oid_treeish(the_repository,
12301231
default_attr_source_tree_object_name,
1231-
attr_source) && !ignore_bad_attr_tree)
1232-
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1232+
attr_source)) {
1233+
if (!ignore_bad_attr_tree)
1234+
die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1235+
return 0;
1236+
}
1237+
1238+
return 1;
12331239
}
12341240

12351241
static struct object_id *default_attr_source(void)
12361242
{
12371243
static struct object_id attr_source;
1244+
static int has_attr_source = -1;
12381245

1239-
if (is_null_oid(&attr_source))
1240-
compute_default_attr_source(&attr_source);
1241-
if (is_null_oid(&attr_source))
1246+
if (has_attr_source < 0)
1247+
has_attr_source = compute_default_attr_source(&attr_source);
1248+
if (!has_attr_source)
12421249
return NULL;
12431250
return &attr_source;
12441251
}

0 commit comments

Comments
 (0)