show-branch: Be nicer when running in a corrupt repository.
[git/jnareb-git/bp-gitweb.git] / patch-id.c
blob5a8dc75d0e0184f8a4afa53e22b44d20f060685f
1 #include <ctype.h>
2 #include "cache.h"
4 static void flush_current_id(int patchlen, unsigned char *id, SHA_CTX *c)
6 unsigned char result[20];
7 char name[50];
9 if (!patchlen)
10 return;
12 SHA1_Final(result, c);
13 memcpy(name, sha1_to_hex(id), 41);
14 printf("%s %s\n", sha1_to_hex(result), name);
15 SHA1_Init(c);
18 static int remove_space(char *line)
20 char *src = line;
21 char *dst = line;
22 unsigned char c;
24 while ((c = *src++) != '\0') {
25 if (!isspace(c))
26 *dst++ = c;
28 return dst - line;
31 static void generate_id_list(void)
33 static unsigned char sha1[20];
34 static char line[1000];
35 SHA_CTX ctx;
36 int patchlen = 0;
38 SHA1_Init(&ctx);
39 while (fgets(line, sizeof(line), stdin) != NULL) {
40 unsigned char n[20];
41 char *p = line;
42 int len;
44 if (!memcmp(line, "diff-tree ", 10))
45 p += 10;
47 if (!get_sha1_hex(p, n)) {
48 flush_current_id(patchlen, sha1, &ctx);
49 memcpy(sha1, n, 20);
50 patchlen = 0;
51 continue;
54 /* Ignore commit comments */
55 if (!patchlen && memcmp(line, "diff ", 5))
56 continue;
58 /* Ignore line numbers when computing the SHA1 of the patch */
59 if (!memcmp(line, "@@ -", 4))
60 continue;
62 /* Compute the sha without whitespace */
63 len = remove_space(line);
64 patchlen += len;
65 SHA1_Update(&ctx, line, len);
67 flush_current_id(patchlen, sha1, &ctx);
70 static const char patch_id_usage[] = "git-patch-id < patch";
72 int main(int argc, char **argv)
74 if (argc != 1)
75 usage(patch_id_usage);
77 generate_id_list();
78 return 0;