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