2018-07-11 22:42:42 +00:00
|
|
|
#include "test-tool.h"
|
|
|
|
#include "commit-graph.h"
|
|
|
|
#include "commit.h"
|
2023-03-21 06:26:03 +00:00
|
|
|
#include "environment.h"
|
2023-02-24 00:09:27 +00:00
|
|
|
#include "hex.h"
|
2018-07-11 22:42:42 +00:00
|
|
|
#include "object.h"
|
|
|
|
#include "repository.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2018-07-11 22:42:42 +00:00
|
|
|
#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;
|
|
|
|
|
2018-08-20 18:24:24 +00:00
|
|
|
setup_git_env(gitdir);
|
|
|
|
|
2018-12-15 00:09:42 +00:00
|
|
|
memset(the_repository, 0, sizeof(*the_repository));
|
|
|
|
|
2018-08-20 18:24:24 +00:00
|
|
|
if (repo_init(&r, gitdir, worktree))
|
|
|
|
die("Couldn't init repo");
|
2018-07-11 22:42:42 +00:00
|
|
|
|
2020-02-22 20:17:36 +00:00
|
|
|
repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
|
|
|
|
|
2018-07-11 22:42:42 +00:00
|
|
|
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;
|
|
|
|
|
2018-08-20 18:24:24 +00:00
|
|
|
setup_git_env(gitdir);
|
|
|
|
|
2018-12-15 00:09:42 +00:00
|
|
|
memset(the_repository, 0, sizeof(*the_repository));
|
|
|
|
|
2018-08-20 18:24:24 +00:00
|
|
|
if (repo_init(&r, gitdir, worktree))
|
|
|
|
die("Couldn't init repo");
|
2018-07-11 22:42:42 +00:00
|
|
|
|
2020-02-22 20:17:36 +00:00
|
|
|
repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
|
|
|
|
|
2018-07-11 22:42:42 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-02-22 20:17:35 +00:00
|
|
|
int nongit_ok = 0;
|
|
|
|
|
|
|
|
setup_git_directory_gently(&nongit_ok);
|
|
|
|
|
2018-07-11 22:42:42 +00:00
|
|
|
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;
|
|
|
|
}
|