t / helper / test-tool.con commit general improvements (43abf13)
   1#include "git-compat-util.h"
   2#include "test-tool.h"
   3#include "trace2.h"
   4#include "parse-options.h"
   5
   6static const char * const test_tool_usage[] = {
   7        "test-tool [-C <directory>] <command [<arguments>...]]",
   8        NULL
   9};
  10
  11struct test_cmd {
  12        const char *name;
  13        int (*fn)(int argc, const char **argv);
  14};
  15
  16static struct test_cmd cmds[] = {
  17        { "chmtime", cmd__chmtime },
  18        { "config", cmd__config },
  19        { "ctype", cmd__ctype },
  20        { "date", cmd__date },
  21        { "delta", cmd__delta },
  22        { "dir-iterator", cmd__dir_iterator },
  23        { "drop-caches", cmd__drop_caches },
  24        { "dump-cache-tree", cmd__dump_cache_tree },
  25        { "dump-fsmonitor", cmd__dump_fsmonitor },
  26        { "dump-split-index", cmd__dump_split_index },
  27        { "dump-untracked-cache", cmd__dump_untracked_cache },
  28        { "example-decorate", cmd__example_decorate },
  29        { "genrandom", cmd__genrandom },
  30        { "genzeros", cmd__genzeros },
  31        { "hashmap", cmd__hashmap },
  32        { "hash-speed", cmd__hash_speed },
  33        { "index-version", cmd__index_version },
  34        { "json-writer", cmd__json_writer },
  35        { "lazy-init-name-hash", cmd__lazy_init_name_hash },
  36        { "match-trees", cmd__match_trees },
  37        { "mergesort", cmd__mergesort },
  38        { "mktemp", cmd__mktemp },
  39        { "oidmap", cmd__oidmap },
  40        { "online-cpus", cmd__online_cpus },
  41        { "parse-options", cmd__parse_options },
  42        { "path-utils", cmd__path_utils },
  43        { "pkt-line", cmd__pkt_line },
  44        { "prio-queue", cmd__prio_queue },
  45        { "reach", cmd__reach },
  46        { "read-cache", cmd__read_cache },
  47        { "read-midx", cmd__read_midx },
  48        { "ref-store", cmd__ref_store },
  49        { "regex", cmd__regex },
  50        { "repository", cmd__repository },
  51        { "revision-walking", cmd__revision_walking },
  52        { "run-command", cmd__run_command },
  53        { "scrap-cache-tree", cmd__scrap_cache_tree },
  54        { "serve-v2", cmd__serve_v2 },
  55        { "sha1", cmd__sha1 },
  56        { "sha1-array", cmd__sha1_array },
  57        { "sha256", cmd__sha256 },
  58        { "sigchain", cmd__sigchain },
  59        { "strcmp-offset", cmd__strcmp_offset },
  60        { "string-list", cmd__string_list },
  61        { "submodule-config", cmd__submodule_config },
  62        { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
  63        { "subprocess", cmd__subprocess },
  64        { "trace2", cmd__trace2 },
  65        { "urlmatch-normalization", cmd__urlmatch_normalization },
  66        { "xml-encode", cmd__xml_encode },
  67        { "wildmatch", cmd__wildmatch },
  68#ifdef GIT_WINDOWS_NATIVE
  69        { "windows-named-pipe", cmd__windows_named_pipe },
  70#endif
  71        { "write-cache", cmd__write_cache },
  72};
  73
  74static NORETURN void die_usage(void)
  75{
  76        size_t i;
  77
  78        fprintf(stderr, "usage: test-tool <toolname> [args]\n");
  79        for (i = 0; i < ARRAY_SIZE(cmds); i++)
  80                fprintf(stderr, "  %s\n", cmds[i].name);
  81        exit(128);
  82}
  83
  84int cmd_main(int argc, const char **argv)
  85{
  86        int i;
  87        const char *working_directory = NULL;
  88        struct option options[] = {
  89                OPT_STRING('C', NULL, &working_directory, "directory",
  90                           "change the working directory"),
  91                OPT_END()
  92        };
  93
  94        BUG_exit_code = 99;
  95        argc = parse_options(argc, argv, NULL, options, test_tool_usage,
  96                             PARSE_OPT_STOP_AT_NON_OPTION |
  97                             PARSE_OPT_KEEP_ARGV0);
  98
  99        if (argc < 2)
 100                die_usage();
 101
 102        if (working_directory && chdir(working_directory) < 0)
 103                die("Could not cd to '%s'", working_directory);
 104
 105        for (i = 0; i < ARRAY_SIZE(cmds); i++) {
 106                if (!strcmp(cmds[i].name, argv[1])) {
 107                        argv++;
 108                        argc--;
 109                        trace2_cmd_name(cmds[i].name);
 110                        trace2_cmd_list_config();
 111                        return cmds[i].fn(argc, argv);
 112                }
 113        }
 114        error("there is no tool named '%s'", argv[1]);
 115        die_usage();
 116}