3 #include "object-name.h"
5 #include "repository.h"
8 #include "string-list.h"
10 /* key is an oid and value is a name (could be a refname for example) */
12 struct oidmap_entry entry
;
13 char name
[FLEX_ARRAY
];
16 #define DELIM " \t\r\n"
19 * Read stdin line by line and print result of commands to stdout:
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...
28 int cmd__oidmap(int argc UNUSED
, const char **argv UNUSED
)
30 struct string_list parts
= STRING_LIST_INIT_NODUP
;
31 struct strbuf line
= STRBUF_INIT
;
32 struct oidmap map
= OIDMAP_INIT
;
34 setup_git_directory();
39 /* process commands from stdin */
40 while (strbuf_getline(&line
, stdin
) != EOF
) {
42 struct test_entry
*entry
;
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);
50 /* ignore empty lines */
53 if (!*parts
.items
[0].string
|| *parts
.items
[0].string
== '#')
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
;
60 if (!strcmp("put", cmd
) && p1
&& p2
) {
62 if (repo_get_oid(the_repository
, p1
, &oid
)) {
63 printf("Unknown oid: %s\n", p1
);
67 /* create entry with oid_key = p1, name_value = p2 */
68 FLEX_ALLOC_STR(entry
, name
, p2
);
69 oidcpy(&entry
->entry
.oid
, &oid
);
71 /* add / replace entry */
72 entry
= oidmap_put(&map
, entry
);
74 /* print and free replaced entry, if any */
75 puts(entry
? entry
->name
: "NULL");
78 } else if (!strcmp("get", cmd
) && p1
) {
80 if (repo_get_oid(the_repository
, p1
, &oid
)) {
81 printf("Unknown oid: %s\n", p1
);
85 /* lookup entry in oidmap */
86 entry
= oidmap_get(&map
, &oid
);
89 puts(entry
? entry
->name
: "NULL");
91 } else if (!strcmp("remove", cmd
) && p1
) {
93 if (repo_get_oid(the_repository
, p1
, &oid
)) {
94 printf("Unknown oid: %s\n", p1
);
98 /* remove entry from oidmap */
99 entry
= oidmap_remove(&map
, &oid
);
101 /* print result and free entry*/
102 puts(entry
? entry
->name
: "NULL");
105 } else if (!strcmp("iterate", cmd
)) {
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
);
114 printf("Unknown command %s\n", cmd
);
119 string_list_clear(&parts
, 0);
120 strbuf_release(&line
);
121 oidmap_free(&map
, 1);