blob: 071f5bd1e21974ba2104a76305a5156ee2c2961f [file] [log] [blame]
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +02001/*
2 * test-revision-walking.c: test revision walking API.
3 *
4 * (C) 2012 Heiko Voigt <hvoigt@hvoigt.net>
5 *
6 * This code is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Patrick Steinhardte7da9382024-06-14 08:50:23 +020011#define USE_THE_REPOSITORY_VARIABLE
12
Nguyễn Thái Ngọc Duy77d4b8c2018-03-24 08:44:54 +010013#include "test-tool.h"
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020014#include "commit.h"
15#include "diff.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000016#include "repository.h"
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020017#include "revision.h"
Elijah Newrene38da482023-03-21 06:26:05 +000018#include "setup.h"
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020019
20static void print_commit(struct commit *commit)
21{
22 struct strbuf sb = STRBUF_INIT;
23 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -040024 ctx.date_mode.type = DATE_NORMAL;
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +020025 repo_format_commit_message(the_repository, commit, " %m %s", &sb,
26 &ctx);
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020027 printf("%s\n", sb.buf);
28 strbuf_release(&sb);
29}
30
31static int run_revision_walk(void)
32{
33 struct rev_info rev;
34 struct commit *commit;
35 const char *argv[] = {NULL, "--all", NULL};
36 int argc = ARRAY_SIZE(argv) - 1;
37 int got_revision = 0;
38
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +020039 repo_init_revisions(the_repository, &rev, NULL);
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020040 setup_revisions(argc, argv, &rev, NULL);
41 if (prepare_revision_walk(&rev))
42 die("revision walk setup failed");
43
44 while ((commit = get_revision(&rev)) != NULL) {
45 print_commit(commit);
46 got_revision = 1;
47 }
48
49 reset_revision_walk();
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +020050 release_revisions(&rev);
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020051 return got_revision;
52}
53
Nguyễn Thái Ngọc Duy77d4b8c2018-03-24 08:44:54 +010054int cmd__revision_walking(int argc, const char **argv)
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020055{
56 if (argc < 2)
57 return 1;
58
Jeff King11e6b3f2016-03-05 17:16:50 -050059 setup_git_directory();
60
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +020061 if (!strcmp(argv[1], "run-twice")) {
62 printf("1st\n");
63 if (!run_revision_walk())
64 return 1;
65 printf("2nd\n");
66 if (!run_revision_walk())
67 return 1;
68
69 return 0;
70 }
71
72 fprintf(stderr, "check usage\n");
73 return 1;
74}