]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-oidmap.c
The fifth batch
[thirdparty/git.git] / t / helper / test-oidmap.c
CommitLineData
11510dec 1#include "test-tool.h"
41771fa4 2#include "hex.h"
dabab1d6 3#include "object-name.h"
11510dec 4#include "oidmap.h"
d1cbe1e6 5#include "repository.h"
e38da487 6#include "setup.h"
11510dec 7#include "strbuf.h"
deeabc1f 8#include "string-list.h"
11510dec
CC
9
10/* key is an oid and value is a name (could be a refname for example) */
11struct 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 */
126e3b3d 28int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
11510dec 29{
deeabc1f 30 struct string_list parts = STRING_LIST_INIT_NODUP;
11510dec
CC
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) {
deeabc1f 41 char *cmd, *p1, *p2;
11510dec
CC
42 struct test_entry *entry;
43 struct object_id oid;
44
45 /* break line into command and up to two parameters */
deeabc1f
TB
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
11510dec 50 /* ignore empty lines */
deeabc1f
TB
51 if (!parts.nr)
52 continue;
53 if (!*parts.items[0].string || *parts.items[0].string == '#')
11510dec
CC
54 continue;
55
deeabc1f
TB
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;
11510dec 59
84f559f7 60 if (!strcmp("put", cmd) && p1 && p2) {
11510dec 61
d850b7a5 62 if (repo_get_oid(the_repository, p1, &oid)) {
11510dec
CC
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
d850b7a5 80 if (repo_get_oid(the_repository, p1, &oid)) {
11510dec
CC
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
d850b7a5 93 if (repo_get_oid(the_repository, p1, &oid)) {
11510dec
CC
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
deeabc1f 119 string_list_clear(&parts, 0);
11510dec
CC
120 strbuf_release(&line);
121 oidmap_free(&map, 1);
122 return 0;
123}