Skip to content

Commit 4468d44

Browse files
szedergitster
authored andcommitted
object_as_type: initialize commit-graph-related fields of 'struct commit'
When the commit graph and generation numbers were introduced in commits 177722b (commit: integrate commit graph with commit parsing, 2018-04-10) and 83073cc (commit: add generation number to struct commit, 2018-04-25), they tried to make sure that the corresponding 'graph_pos' and 'generation' fields of 'struct commit' are initialized conservatively, as if the commit were not included in the commit-graph file. Alas, initializing those fields only in alloc_commit_node() missed the case when an object that happens to be a commit is first looked up via lookup_unknown_object(), and is then later converted to a 'struct commit' via the object_as_type() helper function (either calling it directly, or as part of a subsequent lookup_commit() call). Consequently, both of those fields incorrectly remain set to zero, which means e.g. that the commit is present in and is the first entry of the commit-graph file. This will result in wrong timestamp, parent and root tree hashes, if such a 'struct commit' instance is later filled from the commit-graph. Extract the initialization of 'struct commit's fields from alloc_commit_node() into a helper function, and call it from object_as_type() as well, to make sure that it properly initializes the two commit-graph-related fields, too. With this helper function it is hopefully less likely that any new fields added to 'struct commit' in the future would remain uninitialized. With this change alloc_commit_index() won't have any remaining callers outside of 'alloc.c', so mark it as static. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98cdfbb commit 4468d44

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

alloc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,23 @@ void *alloc_object_node(struct repository *r)
9999
return obj;
100100
}
101101

102-
unsigned int alloc_commit_index(struct repository *r)
102+
static unsigned int alloc_commit_index(struct repository *r)
103103
{
104104
return r->parsed_objects->commit_count++;
105105
}
106106

107-
void *alloc_commit_node(struct repository *r)
107+
void init_commit_node(struct repository *r, struct commit *c)
108108
{
109-
struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
110109
c->object.type = OBJ_COMMIT;
111110
c->index = alloc_commit_index(r);
112111
c->graph_pos = COMMIT_NOT_FROM_GRAPH;
113112
c->generation = GENERATION_NUMBER_INFINITY;
113+
}
114+
115+
void *alloc_commit_node(struct repository *r)
116+
{
117+
struct commit *c = alloc_node(r->parsed_objects->commit_state, sizeof(struct commit));
118+
init_commit_node(r, c);
114119
return c;
115120
}
116121

alloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ struct repository;
99

1010
void *alloc_blob_node(struct repository *r);
1111
void *alloc_tree_node(struct repository *r);
12+
void init_commit_node(struct repository *r, struct commit *c);
1213
void *alloc_commit_node(struct repository *r);
1314
void *alloc_tag_node(struct repository *r);
1415
void *alloc_object_node(struct repository *r);
1516
void alloc_report(struct repository *r);
16-
unsigned int alloc_commit_index(struct repository *r);
1717

1818
struct alloc_state *allocate_alloc_state(void);
1919
void clear_alloc_state(struct alloc_state *s);

object.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ void *object_as_type(struct repository *r, struct object *obj, enum object_type
164164
return obj;
165165
else if (obj->type == OBJ_NONE) {
166166
if (type == OBJ_COMMIT)
167-
((struct commit *)obj)->index = alloc_commit_index(r);
168-
obj->type = type;
167+
init_commit_node(r, (struct commit *) obj);
168+
else
169+
obj->type = type;
169170
return obj;
170171
}
171172
else {

0 commit comments

Comments
 (0)