t / helper / test-sha1-array.con commit Merge branch 'jk/grep-e-could-be-extended-beyond-posix' (38f1370)
   1#include "cache.h"
   2#include "sha1-array.h"
   3
   4static int print_sha1(const unsigned char sha1[20], void *data)
   5{
   6        puts(sha1_to_hex(sha1));
   7        return 0;
   8}
   9
  10int cmd_main(int argc, const char **argv)
  11{
  12        struct sha1_array array = SHA1_ARRAY_INIT;
  13        struct strbuf line = STRBUF_INIT;
  14
  15        while (strbuf_getline(&line, stdin) != EOF) {
  16                const char *arg;
  17                unsigned char sha1[20];
  18
  19                if (skip_prefix(line.buf, "append ", &arg)) {
  20                        if (get_sha1_hex(arg, sha1))
  21                                die("not a hexadecimal SHA1: %s", arg);
  22                        sha1_array_append(&array, sha1);
  23                } else if (skip_prefix(line.buf, "lookup ", &arg)) {
  24                        if (get_sha1_hex(arg, sha1))
  25                                die("not a hexadecimal SHA1: %s", arg);
  26                        printf("%d\n", sha1_array_lookup(&array, sha1));
  27                } else if (!strcmp(line.buf, "clear"))
  28                        sha1_array_clear(&array);
  29                else if (!strcmp(line.buf, "for_each_unique"))
  30                        sha1_array_for_each_unique(&array, print_sha1, NULL);
  31                else
  32                        die("unknown command: %s", line.buf);
  33        }
  34        return 0;
  35}