test-sha1.c 941 Bytes
Newer Older
1 2 3 4
#include "cache.h"

int main(int ac, char **av)
{
5
	git_SHA_CTX ctx;
6
	unsigned char sha1[20];
7
	unsigned bufsz = 8192;
8
	int binary = 0;
9 10
	char *buffer;

11 12 13 14 15 16
	if (ac == 2) {
		if (!strcmp(av[1], "-b"))
			binary = 1;
		else
			bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
	}
17 18 19 20 21 22 23 24 25 26

	if (!bufsz)
		bufsz = 8192;

	while ((buffer = malloc(bufsz)) == NULL) {
		fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
		bufsz /= 2;
		if (bufsz < 1024)
			die("OOPS");
	}
27

28
	git_SHA1_Init(&ctx);
29 30

	while (1) {
31 32 33 34 35 36 37 38 39
		ssize_t sz, this_sz;
		char *cp = buffer;
		unsigned room = bufsz;
		this_sz = 0;
		while (room) {
			sz = xread(0, cp, room);
			if (sz == 0)
				break;
			if (sz < 0)
40
				die_errno("test-sha1");
41 42 43 44 45
			this_sz += sz;
			cp += sz;
			room -= sz;
		}
		if (this_sz == 0)
46
			break;
47
		git_SHA1_Update(&ctx, buffer, this_sz);
48
	}
49
	git_SHA1_Final(sha1, &ctx);
50 51 52 53 54

	if (binary)
		fwrite(sha1, 1, 20, stdout);
	else
		puts(sha1_to_hex(sha1));
55 56
	exit(0);
}