Merge branch 'jm/status-ignored-directory-optim'
[git] / t / helper / test-sha1.c
1 #include "cache.h"
2
3 int cmd_main(int ac, const char **av)
4 {
5         git_SHA_CTX ctx;
6         unsigned char sha1[20];
7         unsigned bufsz = 8192;
8         int binary = 0;
9         char *buffer;
10
11         if (ac == 2) {
12                 if (!strcmp(av[1], "-b"))
13                         binary = 1;
14                 else
15                         bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
16         }
17
18         if (!bufsz)
19                 bufsz = 8192;
20
21         while ((buffer = malloc(bufsz)) == NULL) {
22                 fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
23                 bufsz /= 2;
24                 if (bufsz < 1024)
25                         die("OOPS");
26         }
27
28         git_SHA1_Init(&ctx);
29
30         while (1) {
31                 ssize_t sz, this_sz;
32                 char *cp = buffer;
33                 unsigned room = bufsz;
34                 this_sz = 0;
35                 while (room) {
36                         sz = xread(0, cp, room);
37                         if (sz == 0)
38                                 break;
39                         if (sz < 0)
40                                 die_errno("test-sha1");
41                         this_sz += sz;
42                         cp += sz;
43                         room -= sz;
44                 }
45                 if (this_sz == 0)
46                         break;
47                 git_SHA1_Update(&ctx, buffer, this_sz);
48         }
49         git_SHA1_Final(sha1, &ctx);
50
51         if (binary)
52                 fwrite(sha1, 1, 20, stdout);
53         else
54                 puts(sha1_to_hex(sha1));
55         exit(0);
56 }