t/helper: stop using `the_index`
[git.git] / t / helper / test-oidmap.c
blobbd30244a54cc88370ab27da5d25108b78c296f85
1 #include "test-tool.h"
2 #include "hex.h"
3 #include "object-name.h"
4 #include "oidmap.h"
5 #include "repository.h"
6 #include "setup.h"
7 #include "strbuf.h"
8 #include "string-list.h"
10 /* key is an oid and value is a name (could be a refname for example) */
11 struct test_entry {
12 struct oidmap_entry entry;
13 char name[FLEX_ARRAY];
16 #define DELIM " \t\r\n"
19 * Read stdin line by line and print result of commands to stdout:
21 * hash oidkey -> sha1hash(oidkey)
22 * put oidkey namevalue -> NULL / old namevalue
23 * get oidkey -> NULL / namevalue
24 * remove oidkey -> NULL / old namevalue
25 * iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
28 int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
30 struct string_list parts = STRING_LIST_INIT_NODUP;
31 struct strbuf line = STRBUF_INIT;
32 struct oidmap map = OIDMAP_INIT;
34 setup_git_directory();
36 /* init oidmap */
37 oidmap_init(&map, 0);
39 /* process commands from stdin */
40 while (strbuf_getline(&line, stdin) != EOF) {
41 char *cmd, *p1, *p2;
42 struct test_entry *entry;
43 struct object_id oid;
45 /* break line into command and up to two parameters */
46 string_list_setlen(&parts, 0);
47 string_list_split_in_place(&parts, line.buf, DELIM, 2);
48 string_list_remove_empty_items(&parts, 0);
50 /* ignore empty lines */
51 if (!parts.nr)
52 continue;
53 if (!*parts.items[0].string || *parts.items[0].string == '#')
54 continue;
56 cmd = parts.items[0].string;
57 p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
58 p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
60 if (!strcmp("put", cmd) && p1 && p2) {
62 if (repo_get_oid(the_repository, p1, &oid)) {
63 printf("Unknown oid: %s\n", p1);
64 continue;
67 /* create entry with oid_key = p1, name_value = p2 */
68 FLEX_ALLOC_STR(entry, name, p2);
69 oidcpy(&entry->entry.oid, &oid);
71 /* add / replace entry */
72 entry = oidmap_put(&map, entry);
74 /* print and free replaced entry, if any */
75 puts(entry ? entry->name : "NULL");
76 free(entry);
78 } else if (!strcmp("get", cmd) && p1) {
80 if (repo_get_oid(the_repository, p1, &oid)) {
81 printf("Unknown oid: %s\n", p1);
82 continue;
85 /* lookup entry in oidmap */
86 entry = oidmap_get(&map, &oid);
88 /* print result */
89 puts(entry ? entry->name : "NULL");
91 } else if (!strcmp("remove", cmd) && p1) {
93 if (repo_get_oid(the_repository, p1, &oid)) {
94 printf("Unknown oid: %s\n", p1);
95 continue;
98 /* remove entry from oidmap */
99 entry = oidmap_remove(&map, &oid);
101 /* print result and free entry*/
102 puts(entry ? entry->name : "NULL");
103 free(entry);
105 } else if (!strcmp("iterate", cmd)) {
107 struct oidmap_iter iter;
108 oidmap_iter_init(&map, &iter);
109 while ((entry = oidmap_iter_next(&iter)))
110 printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
112 } else {
114 printf("Unknown command %s\n", cmd);
119 string_list_clear(&parts, 0);
120 strbuf_release(&line);
121 oidmap_free(&map, 1);
122 return 0;