test-revision-walking.con commit Merge branch 'jc/index-v4' (d4a5d87)
   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#include "cache.h"
  12#include "commit.h"
  13#include "diff.h"
  14#include "revision.h"
  15
  16static void print_commit(struct commit *commit)
  17{
  18        struct strbuf sb = STRBUF_INIT;
  19        struct pretty_print_context ctx = {0};
  20        ctx.date_mode = DATE_NORMAL;
  21        format_commit_message(commit, " %m %s", &sb, &ctx);
  22        printf("%s\n", sb.buf);
  23        strbuf_release(&sb);
  24}
  25
  26static int run_revision_walk(void)
  27{
  28        struct rev_info rev;
  29        struct commit *commit;
  30        const char *argv[] = {NULL, "--all", NULL};
  31        int argc = ARRAY_SIZE(argv) - 1;
  32        int got_revision = 0;
  33
  34        init_revisions(&rev, NULL);
  35        setup_revisions(argc, argv, &rev, NULL);
  36        if (prepare_revision_walk(&rev))
  37                die("revision walk setup failed");
  38
  39        while ((commit = get_revision(&rev)) != NULL) {
  40                print_commit(commit);
  41                got_revision = 1;
  42        }
  43
  44        reset_revision_walk();
  45        return got_revision;
  46}
  47
  48int main(int argc, char **argv)
  49{
  50        if (argc < 2)
  51                return 1;
  52
  53        if (!strcmp(argv[1], "run-twice")) {
  54                printf("1st\n");
  55                if (!run_revision_walk())
  56                        return 1;
  57                printf("2nd\n");
  58                if (!run_revision_walk())
  59                        return 1;
  60
  61                return 0;
  62        }
  63
  64        fprintf(stderr, "check usage\n");
  65        return 1;
  66}