commit.cocci: refactor code, avoid double rewrite
"maybe" pointer in 'struct commit' is tricky because it can be lazily initialized to take advantage of commit-graph if available. This makes it not safe to access directly. This leads to a rule in commit.cocci to rewrite 'x->maybe_tree' to 'get_commit_tree(x)'. But that rule alone could lead to incorrectly rewrite assignments, e.g. from x->maybe_tree = yes to get_commit_tree(x) = yes Because of this we have a second rule to revert this effect. Szeder found out that we could do better by performing the assignment rewrite rule first, then the remaining is read-only access and handled by the current first rule. For this to work, we need to transform "x->maybe_tree = y" to something that does NOT contain "x->maybe_tree" to avoid the original first rule. This is where set_commit_tree() comes in. Helped-by: SZEDER Gábor <szeder.dev@gmail.com> Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
7fdff47432
commit
a133c40b23
9
commit.c
9
commit.c
@ -340,6 +340,11 @@ void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
|
||||
}
|
||||
}
|
||||
|
||||
static inline void set_commit_tree(struct commit *c, struct tree *t)
|
||||
{
|
||||
c->maybe_tree = t;
|
||||
}
|
||||
|
||||
struct tree *get_commit_tree(const struct commit *commit)
|
||||
{
|
||||
if (commit->maybe_tree || !commit->object.parsed)
|
||||
@ -358,7 +363,7 @@ struct object_id *get_commit_tree_oid(const struct commit *commit)
|
||||
|
||||
void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
|
||||
{
|
||||
c->maybe_tree = NULL;
|
||||
set_commit_tree(c, NULL);
|
||||
c->index = 0;
|
||||
free_commit_buffer(pool, c);
|
||||
free_commit_list(c->parents);
|
||||
@ -406,7 +411,7 @@ int parse_commit_buffer(struct repository *r, struct commit *item, const void *b
|
||||
if (get_oid_hex(bufptr + 5, &parent) < 0)
|
||||
return error("bad tree pointer in commit %s",
|
||||
oid_to_hex(&item->object.oid));
|
||||
item->maybe_tree = lookup_tree(r, &parent);
|
||||
set_commit_tree(item, lookup_tree(r, &parent));
|
||||
bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
|
||||
pptr = &item->parents;
|
||||
|
||||
|
Reference in New Issue
Block a user