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