2012-03-29 07:21:21 +00:00
|
|
|
/*
|
|
|
|
* test-revision-walking.c: test revision walking API.
|
|
|
|
*
|
|
|
|
* (C) 2012 Heiko Voigt <hvoigt@hvoigt.net>
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2018-03-24 07:44:54 +00:00
|
|
|
#include "test-tool.h"
|
2012-03-29 07:21:21 +00:00
|
|
|
#include "commit.h"
|
|
|
|
#include "diff.h"
|
2023-05-16 06:34:00 +00:00
|
|
|
#include "repository.h"
|
2012-03-29 07:21:21 +00:00
|
|
|
#include "revision.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2012-03-29 07:21:21 +00:00
|
|
|
|
|
|
|
static void print_commit(struct commit *commit)
|
|
|
|
{
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
struct pretty_print_context ctx = {0};
|
convert "enum date_mode" into a struct
In preparation for adding date modes that may carry extra
information beyond the mode itself, this patch converts the
date_mode enum into a struct.
Most of the conversion is fairly straightforward; we pass
the struct as a pointer and dereference the type field where
necessary. Locations that declare a date_mode can use a "{}"
constructor. However, the tricky case is where we use the
enum labels as constants, like:
show_date(t, tz, DATE_NORMAL);
Ideally we could say:
show_date(t, tz, &{ DATE_NORMAL });
but of course C does not allow that. Likewise, we cannot
cast the constant to a struct, because we need to pass an
actual address. Our options are basically:
1. Manually add a "struct date_mode d = { DATE_NORMAL }"
definition to each caller, and pass "&d". This makes
the callers uglier, because they sometimes do not even
have their own scope (e.g., they are inside a switch
statement).
2. Provide a pre-made global "date_normal" struct that can
be passed by address. We'd also need "date_rfc2822",
"date_iso8601", and so forth. But at least the ugliness
is defined in one place.
3. Provide a wrapper that generates the correct struct on
the fly. The big downside is that we end up pointing to
a single global, which makes our wrapper non-reentrant.
But show_date is already not reentrant, so it does not
matter.
This patch implements 3, along with a minor macro to keep
the size of the callers sane.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-25 16:55:02 +00:00
|
|
|
ctx.date_mode.type = DATE_NORMAL;
|
2023-03-28 13:58:51 +00:00
|
|
|
repo_format_commit_message(the_repository, commit, " %m %s", &sb,
|
|
|
|
&ctx);
|
2012-03-29 07:21:21 +00:00
|
|
|
printf("%s\n", sb.buf);
|
|
|
|
strbuf_release(&sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_revision_walk(void)
|
|
|
|
{
|
|
|
|
struct rev_info rev;
|
|
|
|
struct commit *commit;
|
|
|
|
const char *argv[] = {NULL, "--all", NULL};
|
|
|
|
int argc = ARRAY_SIZE(argv) - 1;
|
|
|
|
int got_revision = 0;
|
|
|
|
|
2018-09-21 15:57:38 +00:00
|
|
|
repo_init_revisions(the_repository, &rev, NULL);
|
2012-03-29 07:21:21 +00:00
|
|
|
setup_revisions(argc, argv, &rev, NULL);
|
|
|
|
if (prepare_revision_walk(&rev))
|
|
|
|
die("revision walk setup failed");
|
|
|
|
|
|
|
|
while ((commit = get_revision(&rev)) != NULL) {
|
|
|
|
print_commit(commit);
|
|
|
|
got_revision = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset_revision_walk();
|
2022-04-13 20:01:36 +00:00
|
|
|
release_revisions(&rev);
|
2012-03-29 07:21:21 +00:00
|
|
|
return got_revision;
|
|
|
|
}
|
|
|
|
|
2018-03-24 07:44:54 +00:00
|
|
|
int cmd__revision_walking(int argc, const char **argv)
|
2012-03-29 07:21:21 +00:00
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
return 1;
|
|
|
|
|
2016-03-05 22:16:50 +00:00
|
|
|
setup_git_directory();
|
|
|
|
|
2012-03-29 07:21:21 +00:00
|
|
|
if (!strcmp(argv[1], "run-twice")) {
|
|
|
|
printf("1st\n");
|
|
|
|
if (!run_revision_walk())
|
|
|
|
return 1;
|
|
|
|
printf("2nd\n");
|
|
|
|
if (!run_revision_walk())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "check usage\n");
|
|
|
|
return 1;
|
|
|
|
}
|