]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-oidmap.c
Merge branch 'bb/unicode-width-table-15'
[thirdparty/git.git] / t / helper / test-oidmap.c
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"
9
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];
14 };
15
16 #define DELIM " \t\r\n"
17
18 /*
19 * Read stdin line by line and print result of commands to stdout:
20 *
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...
26 *
27 */
28 int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
29 {
30 struct string_list parts = STRING_LIST_INIT_NODUP;
31 struct strbuf line = STRBUF_INIT;
32 struct oidmap map = OIDMAP_INIT;
33
34 setup_git_directory();
35
36 /* init oidmap */
37 oidmap_init(&map, 0);
38
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;
44
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);
49
50 /* ignore empty lines */
51 if (!parts.nr)
52 continue;
53 if (!*parts.items[0].string || *parts.items[0].string == '#')
54 continue;
55
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;
59
60 if (!strcmp("put", cmd) && p1 && p2) {
61
62 if (repo_get_oid(the_repository, p1, &oid)) {
63 printf("Unknown oid: %s\n", p1);
64 continue;
65 }
66
67 /* create entry with oid_key = p1, name_value = p2 */
68 FLEX_ALLOC_STR(entry, name, p2);
69 oidcpy(&entry->entry.oid, &oid);
70
71 /* add / replace entry */
72 entry = oidmap_put(&map, entry);
73
74 /* print and free replaced entry, if any */
75 puts(entry ? entry->name : "NULL");
76 free(entry);
77
78 } else if (!strcmp("get", cmd) && p1) {
79
80 if (repo_get_oid(the_repository, p1, &oid)) {
81 printf("Unknown oid: %s\n", p1);
82 continue;
83 }
84
85 /* lookup entry in oidmap */
86 entry = oidmap_get(&map, &oid);
87
88 /* print result */
89 puts(entry ? entry->name : "NULL");
90
91 } else if (!strcmp("remove", cmd) && p1) {
92
93 if (repo_get_oid(the_repository, p1, &oid)) {
94 printf("Unknown oid: %s\n", p1);
95 continue;
96 }
97
98 /* remove entry from oidmap */
99 entry = oidmap_remove(&map, &oid);
100
101 /* print result and free entry*/
102 puts(entry ? entry->name : "NULL");
103 free(entry);
104
105 } else if (!strcmp("iterate", cmd)) {
106
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);
111
112 } else {
113
114 printf("Unknown command %s\n", cmd);
115
116 }
117 }
118
119 string_list_clear(&parts, 0);
120 strbuf_release(&line);
121 oidmap_free(&map, 1);
122 return 0;
123 }