Raw
1 /*
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
11 #define USE_THE_REPOSITORY_VARIABLE
12
13 #include "test-tool.h"
14 #include "commit.h"
15 #include "diff.h"
16 #include "repository.h"
17 #include "revision.h"
18 #include "setup.h"
19
20 static void print_commit(struct commit *commit)
21 {
22 struct strbuf sb = STRBUF_INIT;
23 struct pretty_print_context ctx = {0};
24 ctx.date_mode.type = DATE_NORMAL;
25 repo_format_commit_message(the_repository, commit, " %m %s", &sb,
26 &ctx);
27 printf("%s\n", sb.buf);
28 strbuf_release(&sb);
29 }
30
31 static 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
39 repo_init_revisions(the_repository, &rev, NULL);
40 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();
50 release_revisions(&rev);
51 return got_revision;
52 }
53
54 int cmd__revision_walking(int argc, const char **argv)
55 {
56 if (argc < 2)
57 return 1;
58
59 setup_git_directory(the_repository);
60
61 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 }