Merge branch 'cc/multi-promisor'
[git] / t / helper / test-oidmap.c
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) */
7 struct 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  */
24 int 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("put", 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 oid_key = p1, name_value = p2 */
58                         FLEX_ALLOC_STR(entry, name, p2);
59                         oidcpy(&entry->entry.oid, &oid);
60
61                         /* add / replace entry */
62                         entry = oidmap_put(&map, entry);
63
64                         /* print and free replaced entry, if any */
65                         puts(entry ? entry->name : "NULL");
66                         free(entry);
67
68                 } else if (!strcmp("get", cmd) && p1) {
69
70                         if (get_oid(p1, &oid)) {
71                                 printf("Unknown oid: %s\n", p1);
72                                 continue;
73                         }
74
75                         /* lookup entry in oidmap */
76                         entry = oidmap_get(&map, &oid);
77
78                         /* print result */
79                         puts(entry ? entry->name : "NULL");
80
81                 } else if (!strcmp("remove", cmd) && p1) {
82
83                         if (get_oid(p1, &oid)) {
84                                 printf("Unknown oid: %s\n", p1);
85                                 continue;
86                         }
87
88                         /* remove entry from oidmap */
89                         entry = oidmap_remove(&map, &oid);
90
91                         /* print result and free entry*/
92                         puts(entry ? entry->name : "NULL");
93                         free(entry);
94
95                 } else if (!strcmp("iterate", cmd)) {
96
97                         struct oidmap_iter iter;
98                         oidmap_iter_init(&map, &iter);
99                         while ((entry = oidmap_iter_next(&iter)))
100                                 printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
101
102                 } else {
103
104                         printf("Unknown command %s\n", cmd);
105
106                 }
107         }
108
109         strbuf_release(&line);
110         oidmap_free(&map, 1);
111         return 0;
112 }