t / helper / test-oidmap.con commit t/helper: add test-oidmap.c (11510de)
   1#include "test-tool.h"
   2#include "cache.h"
   3#include "oidmap.h"
   4#include "strbuf.h"
   5
   6/* key is an oid and value is a name (could be a refname for example) */
   7struct test_entry {
   8        struct oidmap_entry entry;
   9        char name[FLEX_ARRAY];
  10};
  11
  12#define DELIM " \t\r\n"
  13
  14/*
  15 * Read stdin line by line and print result of commands to stdout:
  16 *
  17 * hash oidkey -> sha1hash(oidkey)
  18 * put oidkey namevalue -> NULL / old namevalue
  19 * get oidkey -> NULL / namevalue
  20 * remove oidkey -> NULL / old namevalue
  21 * iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
  22 *
  23 */
  24int cmd__oidmap(int argc, const char **argv)
  25{
  26        struct strbuf line = STRBUF_INIT;
  27        struct oidmap map = OIDMAP_INIT;
  28
  29        setup_git_directory();
  30
  31        /* init oidmap */
  32        oidmap_init(&map, 0);
  33
  34        /* process commands from stdin */
  35        while (strbuf_getline(&line, stdin) != EOF) {
  36                char *cmd, *p1 = NULL, *p2 = NULL;
  37                struct test_entry *entry;
  38                struct object_id oid;
  39
  40                /* break line into command and up to two parameters */
  41                cmd = strtok(line.buf, DELIM);
  42                /* ignore empty lines */
  43                if (!cmd || *cmd == '#')
  44                        continue;
  45
  46                p1 = strtok(NULL, DELIM);
  47                if (p1)
  48                        p2 = strtok(NULL, DELIM);
  49
  50                if (!strcmp("add", cmd) && p1 && p2) {
  51
  52                        if (get_oid(p1, &oid)) {
  53                                printf("Unknown oid: %s\n", p1);
  54                                continue;
  55                        }
  56
  57                        /* create entry with oidkey from p1, value = p2 */
  58                        FLEX_ALLOC_STR(entry, name, p2);
  59                        oidcpy(&entry->entry.oid, &oid);
  60
  61                        /* add to oidmap */
  62                        oidmap_put(&map, entry);
  63
  64                } else if (!strcmp("put", cmd) && p1 && p2) {
  65
  66                        if (get_oid(p1, &oid)) {
  67                                printf("Unknown oid: %s\n", p1);
  68                                continue;
  69                        }
  70
  71                        /* create entry with oid_key = p1, name_value = p2 */
  72                        FLEX_ALLOC_STR(entry, name, p2);
  73                        oidcpy(&entry->entry.oid, &oid);
  74
  75                        /* add / replace entry */
  76                        entry = oidmap_put(&map, entry);
  77
  78                        /* print and free replaced entry, if any */
  79                        puts(entry ? entry->name : "NULL");
  80                        free(entry);
  81
  82                } else if (!strcmp("get", cmd) && p1) {
  83
  84                        if (get_oid(p1, &oid)) {
  85                                printf("Unknown oid: %s\n", p1);
  86                                continue;
  87                        }
  88
  89                        /* lookup entry in oidmap */
  90                        entry = oidmap_get(&map, &oid);
  91
  92                        /* print result */
  93                        puts(entry ? entry->name : "NULL");
  94
  95                } else if (!strcmp("remove", cmd) && p1) {
  96
  97                        if (get_oid(p1, &oid)) {
  98                                printf("Unknown oid: %s\n", p1);
  99                                continue;
 100                        }
 101
 102                        /* remove entry from oidmap */
 103                        entry = oidmap_remove(&map, &oid);
 104
 105                        /* print result and free entry*/
 106                        puts(entry ? entry->name : "NULL");
 107                        free(entry);
 108
 109                } else if (!strcmp("iterate", cmd)) {
 110
 111                        struct oidmap_iter iter;
 112                        oidmap_iter_init(&map, &iter);
 113                        while ((entry = oidmap_iter_next(&iter)))
 114                                printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
 115
 116                } else {
 117
 118                        printf("Unknown command %s\n", cmd);
 119
 120                }
 121        }
 122
 123        strbuf_release(&line);
 124        oidmap_free(&map, 1);
 125        return 0;
 126}