2018-03-24 08:44:32 +01:00
|
|
|
#include "test-tool.h"
|
2006-06-24 09:59:49 +02:00
|
|
|
#include "cache.h"
|
|
|
|
|
2018-03-24 08:44:32 +01:00
|
|
|
int cmd__sha1(int ac, const char **av)
|
2006-06-24 09:59:49 +02:00
|
|
|
{
|
2008-10-01 20:05:20 +02:00
|
|
|
git_SHA_CTX ctx;
|
2006-06-24 09:59:49 +02:00
|
|
|
unsigned char sha1[20];
|
2006-06-24 11:59:20 +02:00
|
|
|
unsigned bufsz = 8192;
|
2013-08-23 01:12:42 +02:00
|
|
|
int binary = 0;
|
2006-06-24 11:59:20 +02:00
|
|
|
char *buffer;
|
|
|
|
|
2013-08-23 01:12:42 +02:00
|
|
|
if (ac == 2) {
|
|
|
|
if (!strcmp(av[1], "-b"))
|
|
|
|
binary = 1;
|
|
|
|
else
|
|
|
|
bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
|
|
|
|
}
|
2006-06-24 11:59:20 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2006-06-24 09:59:49 +02:00
|
|
|
|
2008-10-01 20:05:20 +02:00
|
|
|
git_SHA1_Init(&ctx);
|
2006-06-24 09:59:49 +02:00
|
|
|
|
|
|
|
while (1) {
|
2006-06-24 11:59:20 +02:00
|
|
|
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)
|
2009-06-27 17:58:46 +02:00
|
|
|
die_errno("test-sha1");
|
2006-06-24 11:59:20 +02:00
|
|
|
this_sz += sz;
|
|
|
|
cp += sz;
|
|
|
|
room -= sz;
|
|
|
|
}
|
|
|
|
if (this_sz == 0)
|
2006-06-24 09:59:49 +02:00
|
|
|
break;
|
2008-10-01 20:05:20 +02:00
|
|
|
git_SHA1_Update(&ctx, buffer, this_sz);
|
2006-06-24 09:59:49 +02:00
|
|
|
}
|
2008-10-01 20:05:20 +02:00
|
|
|
git_SHA1_Final(sha1, &ctx);
|
2013-08-23 01:12:42 +02:00
|
|
|
|
|
|
|
if (binary)
|
|
|
|
fwrite(sha1, 1, 20, stdout);
|
|
|
|
else
|
|
|
|
puts(sha1_to_hex(sha1));
|
2006-06-24 09:59:49 +02:00
|
|
|
exit(0);
|
|
|
|
}
|