1 #include "test-tool.h"
2 #include "cache.h"
3 #include "oid-array.h"
4 
print_oid(const struct object_id * oid,void * data)5 static int print_oid(const struct object_id *oid, void *data)
6 {
7 	puts(oid_to_hex(oid));
8 	return 0;
9 }
10 
cmd__oid_array(int argc,const char ** argv)11 int cmd__oid_array(int argc, const char **argv)
12 {
13 	struct oid_array array = OID_ARRAY_INIT;
14 	struct strbuf line = STRBUF_INIT;
15 	int nongit_ok;
16 
17 	setup_git_directory_gently(&nongit_ok);
18 
19 	while (strbuf_getline(&line, stdin) != EOF) {
20 		const char *arg;
21 		struct object_id oid;
22 
23 		if (skip_prefix(line.buf, "append ", &arg)) {
24 			if (get_oid_hex(arg, &oid))
25 				die("not a hexadecimal oid: %s", arg);
26 			oid_array_append(&array, &oid);
27 		} else if (skip_prefix(line.buf, "lookup ", &arg)) {
28 			if (get_oid_hex(arg, &oid))
29 				die("not a hexadecimal oid: %s", arg);
30 			printf("%d\n", oid_array_lookup(&array, &oid));
31 		} else if (!strcmp(line.buf, "clear"))
32 			oid_array_clear(&array);
33 		else if (!strcmp(line.buf, "for_each_unique"))
34 			oid_array_for_each_unique(&array, print_oid, NULL);
35 		else
36 			die("unknown command: %s", line.buf);
37 	}
38 
39 	strbuf_release(&line);
40 	oid_array_clear(&array);
41 
42 	return 0;
43 }
44