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
@ -343,6 +343,11 @@ static void fill_commit_graph_info(struct commit *item, struct commit_graph *g,
|
||||
item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
|
||||
}
|
||||
|
||||
static inline void set_commit_tree(struct commit *c, struct tree *t)
|
||||
{
|
||||
c->maybe_tree = t;
|
||||
}
|
||||
|
||||
static int fill_commit_in_graph(struct repository *r,
|
||||
struct commit *item,
|
||||
struct commit_graph *g, uint32_t pos)
|
||||
@ -356,7 +361,7 @@ static int fill_commit_in_graph(struct repository *r,
|
||||
item->object.parsed = 1;
|
||||
item->graph_pos = pos;
|
||||
|
||||
item->maybe_tree = NULL;
|
||||
set_commit_tree(item, NULL);
|
||||
|
||||
date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
|
||||
date_low = get_be32(commit_data + g->hash_len + 12);
|
||||
@ -442,7 +447,7 @@ static struct tree *load_tree_for_commit(struct repository *r,
|
||||
GRAPH_DATA_WIDTH * (c->graph_pos);
|
||||
|
||||
hashcpy(oid.hash, commit_data);
|
||||
c->maybe_tree = lookup_tree(r, &oid);
|
||||
set_commit_tree(c, lookup_tree(r, &oid));
|
||||
|
||||
return c->maybe_tree;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user