]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-oidmap.c
3 #include "object-name.h"
8 /* key is an oid and value is a name (could be a refname for example) */
10 struct oidmap_entry entry
;
11 char name
[FLEX_ARRAY
];
14 #define DELIM " \t\r\n"
17 * Read stdin line by line and print result of commands to stdout:
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...
26 int cmd__oidmap(int argc UNUSED
, const char **argv UNUSED
)
28 struct strbuf line
= STRBUF_INIT
;
29 struct oidmap map
= OIDMAP_INIT
;
31 setup_git_directory();
36 /* process commands from stdin */
37 while (strbuf_getline(&line
, stdin
) != EOF
) {
38 char *cmd
, *p1
= NULL
, *p2
= NULL
;
39 struct test_entry
*entry
;
42 /* break line into command and up to two parameters */
43 cmd
= strtok(line
.buf
, DELIM
);
44 /* ignore empty lines */
45 if (!cmd
|| *cmd
== '#')
48 p1
= strtok(NULL
, DELIM
);
50 p2
= strtok(NULL
, DELIM
);
52 if (!strcmp("put", cmd
) && p1
&& p2
) {
54 if (repo_get_oid(the_repository
, p1
, &oid
)) {
55 printf("Unknown oid: %s\n", p1
);
59 /* create entry with oid_key = p1, name_value = p2 */
60 FLEX_ALLOC_STR(entry
, name
, p2
);
61 oidcpy(&entry
->entry
.oid
, &oid
);
63 /* add / replace entry */
64 entry
= oidmap_put(&map
, entry
);
66 /* print and free replaced entry, if any */
67 puts(entry
? entry
->name
: "NULL");
70 } else if (!strcmp("get", cmd
) && p1
) {
72 if (repo_get_oid(the_repository
, p1
, &oid
)) {
73 printf("Unknown oid: %s\n", p1
);
77 /* lookup entry in oidmap */
78 entry
= oidmap_get(&map
, &oid
);
81 puts(entry
? entry
->name
: "NULL");
83 } else if (!strcmp("remove", cmd
) && p1
) {
85 if (repo_get_oid(the_repository
, p1
, &oid
)) {
86 printf("Unknown oid: %s\n", p1
);
90 /* remove entry from oidmap */
91 entry
= oidmap_remove(&map
, &oid
);
93 /* print result and free entry*/
94 puts(entry
? entry
->name
: "NULL");
97 } else if (!strcmp("iterate", cmd
)) {
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
);
106 printf("Unknown command %s\n", cmd
);
111 strbuf_release(&line
);
112 oidmap_free(&map
, 1);