
Use of the `the_repository` variable is deprecated nowadays, and we slowly but steadily convert the codebase to not use it anymore. Instead, callers should be passing down the repository to work on via parameters. It is hard though to prove that a given code unit does not use this variable anymore. The most trivial case, merely demonstrating that there is no direct use of `the_repository`, is already a bit of a pain during code reviews as the reviewer needs to manually verify claims made by the patch author. The bigger problem though is that we have many interfaces that implicitly rely on `the_repository`. Introduce a new `USE_THE_REPOSITORY_VARIABLE` macro that allows code units to opt into usage of `the_repository`. The intent of this macro is to demonstrate that a certain code unit does not use this variable anymore, and to keep it from new dependencies on it in future changes, be it explicit or implicit For now, the macro only guards `the_repository` itself as well as `the_hash_algo`. There are many more known interfaces where we have an implicit dependency on `the_repository`, but those are not guarded at the current point in time. Over time though, we should start to add guards as required (or even better, just remove them). Define the macro as required in our code units. As expected, most of our code still relies on the global variable. Nearly all of our builtins rely on the variable as there is no way yet to pass `the_repository` to their entry point. For now, declare the macro in "biultin.h" to keep the required changes at least a little bit more contained. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
103 lines
2.4 KiB
C
103 lines
2.4 KiB
C
#define USE_THE_REPOSITORY_VARIABLE
|
|
|
|
#include "test-tool.h"
|
|
#include "commit-graph.h"
|
|
#include "commit.h"
|
|
#include "environment.h"
|
|
#include "hex.h"
|
|
#include "object.h"
|
|
#include "repository.h"
|
|
#include "setup.h"
|
|
#include "tree.h"
|
|
|
|
static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
|
|
const struct object_id *commit_oid)
|
|
{
|
|
struct repository r;
|
|
struct commit *c;
|
|
struct commit_list *parent;
|
|
|
|
setup_git_env(gitdir);
|
|
|
|
memset(the_repository, 0, sizeof(*the_repository));
|
|
|
|
if (repo_init(&r, gitdir, worktree))
|
|
die("Couldn't init repo");
|
|
|
|
repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
|
|
|
|
c = lookup_commit(&r, commit_oid);
|
|
|
|
if (!parse_commit_in_graph(&r, c))
|
|
die("Couldn't parse commit");
|
|
|
|
printf("%"PRItime, c->date);
|
|
for (parent = c->parents; parent; parent = parent->next)
|
|
printf(" %s", oid_to_hex(&parent->item->object.oid));
|
|
printf("\n");
|
|
|
|
repo_clear(&r);
|
|
}
|
|
|
|
static void test_get_commit_tree_in_graph(const char *gitdir,
|
|
const char *worktree,
|
|
const struct object_id *commit_oid)
|
|
{
|
|
struct repository r;
|
|
struct commit *c;
|
|
struct tree *tree;
|
|
|
|
setup_git_env(gitdir);
|
|
|
|
memset(the_repository, 0, sizeof(*the_repository));
|
|
|
|
if (repo_init(&r, gitdir, worktree))
|
|
die("Couldn't init repo");
|
|
|
|
repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
|
|
|
|
c = lookup_commit(&r, commit_oid);
|
|
|
|
/*
|
|
* get_commit_tree_in_graph does not automatically parse the commit, so
|
|
* parse it first.
|
|
*/
|
|
if (!parse_commit_in_graph(&r, c))
|
|
die("Couldn't parse commit");
|
|
tree = get_commit_tree_in_graph(&r, c);
|
|
if (!tree)
|
|
die("Couldn't get commit tree");
|
|
|
|
printf("%s\n", oid_to_hex(&tree->object.oid));
|
|
|
|
repo_clear(&r);
|
|
}
|
|
|
|
int cmd__repository(int argc, const char **argv)
|
|
{
|
|
int nongit_ok = 0;
|
|
|
|
setup_git_directory_gently(&nongit_ok);
|
|
|
|
if (argc < 2)
|
|
die("must have at least 2 arguments");
|
|
if (!strcmp(argv[1], "parse_commit_in_graph")) {
|
|
struct object_id oid;
|
|
if (argc < 5)
|
|
die("not enough arguments");
|
|
if (parse_oid_hex(argv[4], &oid, &argv[4]))
|
|
die("cannot parse oid '%s'", argv[4]);
|
|
test_parse_commit_in_graph(argv[2], argv[3], &oid);
|
|
} else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
|
|
struct object_id oid;
|
|
if (argc < 5)
|
|
die("not enough arguments");
|
|
if (parse_oid_hex(argv[4], &oid, &argv[4]))
|
|
die("cannot parse oid '%s'", argv[4]);
|
|
test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
|
|
} else {
|
|
die("unrecognized '%s'", argv[1]);
|
|
}
|
|
return 0;
|
|
}
|