Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 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 | |
Patrick Steinhardt | e7da938 | 2024-06-14 08:50:23 +0200 | [diff] [blame] | 11 | #define USE_THE_REPOSITORY_VARIABLE |
| 12 | |
Nguyễn Thái Ngọc Duy | 77d4b8c | 2018-03-24 08:44:54 +0100 | [diff] [blame] | 13 | #include "test-tool.h" |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 14 | #include "commit.h" |
| 15 | #include "diff.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 16 | #include "repository.h" |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 17 | #include "revision.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 18 | #include "setup.h" |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 19 | |
| 20 | static void print_commit(struct commit *commit) |
| 21 | { |
| 22 | struct strbuf sb = STRBUF_INIT; |
| 23 | struct pretty_print_context ctx = {0}; |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 24 | ctx.date_mode.type = DATE_NORMAL; |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 25 | repo_format_commit_message(the_repository, commit, " %m %s", &sb, |
| 26 | &ctx); |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 39 | repo_init_revisions(the_repository, &rev, NULL); |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 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(); |
Ævar Arnfjörð Bjarmason | 2108fe4 | 2022-04-13 22:01:36 +0200 | [diff] [blame] | 50 | release_revisions(&rev); |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 51 | return got_revision; |
| 52 | } |
| 53 | |
Nguyễn Thái Ngọc Duy | 77d4b8c | 2018-03-24 08:44:54 +0100 | [diff] [blame] | 54 | int cmd__revision_walking(int argc, const char **argv) |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 55 | { |
| 56 | if (argc < 2) |
| 57 | return 1; |
| 58 | |
Jeff King | 11e6b3f | 2016-03-05 17:16:50 -0500 | [diff] [blame] | 59 | setup_git_directory(); |
| 60 | |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 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 | } |