Skip to content

Commit 301b8c7

Browse files
pcloudsgitster
authored andcommitted
commit.c: add repo_get_commit_tree()
Remove the implicit dependency on the_repository in this function. It will be used in sha1-name.c functions when they are updated to take any 'struct repository'. get_commit_tree() remains as a compat wrapper, to be slowly replaced later. Any access to "maybe_tree" field directly will result in _broken_ code after running through commit.cocci because we can't know what is the right repository to use. the_repository would be correct most of the time. But we're relying less and less on the_repository and that assumption may no longer be true. The transformation now is more of a poor man replacement for a C++ compiler catching access to private fields. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a133c40 commit 301b8c7

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

commit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,16 @@ static inline void set_commit_tree(struct commit *c, struct tree *t)
345345
c->maybe_tree = t;
346346
}
347347

348-
struct tree *get_commit_tree(const struct commit *commit)
348+
struct tree *repo_get_commit_tree(struct repository *r,
349+
const struct commit *commit)
349350
{
350351
if (commit->maybe_tree || !commit->object.parsed)
351352
return commit->maybe_tree;
352353

353354
if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
354355
BUG("commit has NULL tree, but was not loaded from commit-graph");
355356

356-
return get_commit_tree_in_graph(the_repository, commit);
357+
return get_commit_tree_in_graph(r, commit);
357358
}
358359

359360
struct object_id *get_commit_tree_oid(const struct commit *commit)

commit.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct commit {
3232

3333
/*
3434
* If the commit is loaded from the commit-graph file, then this
35-
* member may be NULL. Only access it through get_commit_tree()
35+
* member may be NULL. Only access it through repo_get_commit_tree()
3636
* or get_commit_tree_oid().
3737
*/
3838
struct tree *maybe_tree;
@@ -143,7 +143,8 @@ void repo_unuse_commit_buffer(struct repository *r,
143143
*/
144144
void free_commit_buffer(struct parsed_object_pool *pool, struct commit *);
145145

146-
struct tree *get_commit_tree(const struct commit *);
146+
struct tree *repo_get_commit_tree(struct repository *, const struct commit *);
147+
#define get_commit_tree(c) repo_get_commit_tree(the_repository, c)
147148
struct object_id *get_commit_tree_oid(const struct commit *);
148149

149150
/*

contrib/coccinelle/commit.cocci

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ expression s;
2323
// These excluded functions must access c->maybe_tree direcly.
2424
// Note that if c->maybe_tree is written somewhere outside of these
2525
// functions, then the recommended transformation will be bogus with
26-
// get_commit_tree() on the LHS.
26+
// repo_get_commit_tree() on the LHS.
2727
@@
28-
identifier f !~ "^(get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit|set_commit_tree)$";
28+
identifier f !~ "^(repo_get_commit_tree|get_commit_tree_in_graph_one|load_tree_for_commit|set_commit_tree)$";
2929
expression c;
3030
@@
3131
f(...) {<...
3232
- c->maybe_tree
33-
+ get_commit_tree(c)
33+
+ repo_get_commit_tree(specify_the_right_repo_here, c)
3434
...>}

0 commit comments

Comments
 (0)