| 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 "line-log.h" |
| 17 | #include "object-name.h" |
| 18 | #include "repository.h" |
| 19 | #include "revision.h" |
| 20 | #include "setup.h" |
| 21 | #include "string-list.h" |
| 22 | |
| 23 | static void print_commit(struct commit *commit) |
| 24 | { |
| 25 | struct strbuf sb = STRBUF_INIT; |
| 26 | struct pretty_print_context ctx = {0}; |
| 27 | ctx.date_mode.type = DATE_NORMAL; |
| 28 | repo_format_commit_message(the_repository, commit, " %m %s", &sb, |
| 29 | &ctx); |
| 30 | printf("%s\n", sb.buf); |
| 31 | strbuf_release(&sb); |
| 32 | } |
| 33 | |
| 34 | static int run_revision_walk(void) |
| 35 | { |
| 36 | struct rev_info rev; |
| 37 | struct commit *commit; |
| 38 | const char *argv[] = {NULL, "--all", NULL}; |
| 39 | int argc = ARRAY_SIZE(argv) - 1; |
| 40 | int got_revision = 0; |
| 41 | |
| 42 | repo_init_revisions(the_repository, &rev, NULL); |
| 43 | setup_revisions(argc, argv, &rev, NULL); |
| 44 | if (prepare_revision_walk(&rev)) |
| 45 | die("revision walk setup failed"); |
| 46 | |
| 47 | while ((commit = get_revision(&rev)) != NULL) { |
| 48 | print_commit(commit); |
| 49 | got_revision = 1; |
| 50 | } |
| 51 | |
| 52 | reset_revision_walk(); |
| 53 | release_revisions(&rev); |
| 54 | return got_revision; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Check that get_commit_action() is a pure predicate by evaluating it on a |
| 59 | * commit the walk has not reached yet. No git command makes that out-of-order |
| 60 | * call, so this probe does it deliberately, and reports whether the call |
| 61 | * mutated the peeked commit: a pure get_commit_action() leaves it untouched. |
| 62 | * We compare the commit's flags rather than the emitted commit list because |
| 63 | * range merges are idempotent, so a side effect would not change which commits |
| 64 | * are shown. Only meaningful for a plain "-L" walk with no parent rewriting. |
| 65 | */ |
| 66 | static int line_log_peek(const char **argv) |
| 67 | { |
| 68 | struct repository *repo = the_repository; |
| 69 | struct rev_info rev; |
| 70 | struct string_list range_args = STRING_LIST_INIT_DUP; |
| 71 | struct object_id oid; |
| 72 | struct commit *peek; |
| 73 | const char *rev_argv[3]; |
| 74 | unsigned before, after; |
| 75 | |
| 76 | if (repo_get_oid(repo, argv[0], &oid)) |
| 77 | die("bad peek commit: %s", argv[0]); |
| 78 | peek = lookup_commit_reference(repo, &oid); |
| 79 | if (!peek || repo_parse_commit(repo, peek)) |
| 80 | die("cannot parse peek commit: %s", argv[0]); |
| 81 | |
| 82 | repo_init_revisions(repo, &rev, NULL); |
| 83 | rev.diffopt.flags.recursive = 1; |
| 84 | rev.line_level_traverse = 1; |
| 85 | string_list_append(&range_args, argv[1]); |
| 86 | |
| 87 | rev_argv[0] = "line-log-peek"; |
| 88 | rev_argv[1] = argv[2]; |
| 89 | rev_argv[2] = NULL; |
| 90 | setup_revisions(2, rev_argv, &rev, NULL); |
| 91 | |
| 92 | line_log_init(&rev, NULL, &range_args); |
| 93 | |
| 94 | if (rev.rewrite_parents || rev.children.name) |
| 95 | die("line-log-peek requires a non-ancestry (-L, no --graph) walk"); |
| 96 | |
| 97 | if (prepare_revision_walk(&rev)) |
| 98 | die("prepare_revision_walk failed"); |
| 99 | |
| 100 | before = peek->object.flags; |
| 101 | get_commit_action(&rev, peek); |
| 102 | after = peek->object.flags; |
| 103 | |
| 104 | printf("mutated %d\n", before != after); |
| 105 | |
| 106 | release_revisions(&rev); |
| 107 | string_list_clear(&range_args, 0); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int cmd__revision_walking(int argc, const char **argv) |
| 112 | { |
| 113 | if (argc < 2) |
| 114 | return 1; |
| 115 | |
| 116 | setup_git_directory(the_repository); |
| 117 | |
| 118 | if (!strcmp(argv[1], "run-twice")) { |
| 119 | printf("1st\n"); |
| 120 | if (!run_revision_walk()) |
| 121 | return 1; |
| 122 | printf("2nd\n"); |
| 123 | if (!run_revision_walk()) |
| 124 | return 1; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | if (!strcmp(argv[1], "line-log-peek")) { |
| 130 | if (argc != 5) |
| 131 | die("usage: test-tool revision-walking line-log-peek <peek-commit> <start,end:file> <rev>"); |
| 132 | return line_log_peek(argv + 2); |
| 133 | } |
| 134 | |
| 135 | fprintf(stderr, "check usage\n"); |
| 136 | return 1; |
| 137 | } |