2019-06-15 10:06:59 +00:00
|
|
|
#include "test-tool.h"
|
2023-02-24 00:09:27 +00:00
|
|
|
#include "hex.h"
|
2023-04-11 07:41:49 +00:00
|
|
|
#include "object-name.h"
|
2019-06-15 10:06:59 +00:00
|
|
|
#include "oidmap.h"
|
2023-04-22 20:17:20 +00:00
|
|
|
#include "repository.h"
|
2023-03-21 06:26:05 +00:00
|
|
|
#include "setup.h"
|
2019-06-15 10:06:59 +00:00
|
|
|
#include "strbuf.h"
|
2023-04-24 22:20:20 +00:00
|
|
|
#include "string-list.h"
|
2019-06-15 10:06:59 +00:00
|
|
|
|
|
|
|
/* key is an oid and value is a name (could be a refname for example) */
|
|
|
|
struct test_entry {
|
|
|
|
struct oidmap_entry entry;
|
|
|
|
char name[FLEX_ARRAY];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DELIM " \t\r\n"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read stdin line by line and print result of commands to stdout:
|
|
|
|
*
|
|
|
|
* hash oidkey -> sha1hash(oidkey)
|
|
|
|
* put oidkey namevalue -> NULL / old namevalue
|
|
|
|
* get oidkey -> NULL / namevalue
|
|
|
|
* remove oidkey -> NULL / old namevalue
|
|
|
|
* iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
|
|
|
|
*
|
|
|
|
*/
|
2023-03-28 20:57:25 +00:00
|
|
|
int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
|
2019-06-15 10:06:59 +00:00
|
|
|
{
|
2023-04-24 22:20:20 +00:00
|
|
|
struct string_list parts = STRING_LIST_INIT_NODUP;
|
2019-06-15 10:06:59 +00:00
|
|
|
struct strbuf line = STRBUF_INIT;
|
|
|
|
struct oidmap map = OIDMAP_INIT;
|
|
|
|
|
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
/* init oidmap */
|
|
|
|
oidmap_init(&map, 0);
|
|
|
|
|
|
|
|
/* process commands from stdin */
|
|
|
|
while (strbuf_getline(&line, stdin) != EOF) {
|
2023-04-24 22:20:20 +00:00
|
|
|
char *cmd, *p1, *p2;
|
2019-06-15 10:06:59 +00:00
|
|
|
struct test_entry *entry;
|
|
|
|
struct object_id oid;
|
|
|
|
|
|
|
|
/* break line into command and up to two parameters */
|
2023-04-24 22:20:20 +00:00
|
|
|
string_list_setlen(&parts, 0);
|
|
|
|
string_list_split_in_place(&parts, line.buf, DELIM, 2);
|
|
|
|
string_list_remove_empty_items(&parts, 0);
|
|
|
|
|
2019-06-15 10:06:59 +00:00
|
|
|
/* ignore empty lines */
|
2023-04-24 22:20:20 +00:00
|
|
|
if (!parts.nr)
|
|
|
|
continue;
|
|
|
|
if (!*parts.items[0].string || *parts.items[0].string == '#')
|
2019-06-15 10:06:59 +00:00
|
|
|
continue;
|
|
|
|
|
2023-04-24 22:20:20 +00:00
|
|
|
cmd = parts.items[0].string;
|
|
|
|
p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
|
|
|
|
p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
|
2019-06-15 10:06:59 +00:00
|
|
|
|
2019-06-29 07:57:46 +00:00
|
|
|
if (!strcmp("put", cmd) && p1 && p2) {
|
2019-06-15 10:06:59 +00:00
|
|
|
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, p1, &oid)) {
|
2019-06-15 10:06:59 +00:00
|
|
|
printf("Unknown oid: %s\n", p1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create entry with oid_key = p1, name_value = p2 */
|
|
|
|
FLEX_ALLOC_STR(entry, name, p2);
|
|
|
|
oidcpy(&entry->entry.oid, &oid);
|
|
|
|
|
|
|
|
/* add / replace entry */
|
|
|
|
entry = oidmap_put(&map, entry);
|
|
|
|
|
|
|
|
/* print and free replaced entry, if any */
|
|
|
|
puts(entry ? entry->name : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
} else if (!strcmp("get", cmd) && p1) {
|
|
|
|
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, p1, &oid)) {
|
2019-06-15 10:06:59 +00:00
|
|
|
printf("Unknown oid: %s\n", p1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lookup entry in oidmap */
|
|
|
|
entry = oidmap_get(&map, &oid);
|
|
|
|
|
|
|
|
/* print result */
|
|
|
|
puts(entry ? entry->name : "NULL");
|
|
|
|
|
|
|
|
} else if (!strcmp("remove", cmd) && p1) {
|
|
|
|
|
2023-03-28 13:58:46 +00:00
|
|
|
if (repo_get_oid(the_repository, p1, &oid)) {
|
2019-06-15 10:06:59 +00:00
|
|
|
printf("Unknown oid: %s\n", p1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove entry from oidmap */
|
|
|
|
entry = oidmap_remove(&map, &oid);
|
|
|
|
|
|
|
|
/* print result and free entry*/
|
|
|
|
puts(entry ? entry->name : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
} else if (!strcmp("iterate", cmd)) {
|
|
|
|
|
|
|
|
struct oidmap_iter iter;
|
|
|
|
oidmap_iter_init(&map, &iter);
|
|
|
|
while ((entry = oidmap_iter_next(&iter)))
|
|
|
|
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
printf("Unknown command %s\n", cmd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 22:20:20 +00:00
|
|
|
string_list_clear(&parts, 0);
|
2019-06-15 10:06:59 +00:00
|
|
|
strbuf_release(&line);
|
|
|
|
oidmap_free(&map, 1);
|
|
|
|
return 0;
|
|
|
|
}
|