Merge branch 'jc/diff'
[alt-git.git] / patch-id.c
blobedbc4aa3e82974168f2d4c21085bdd43b774d55e
1 #include "cache.h"
3 static void flush_current_id(int patchlen, unsigned char *id, SHA_CTX *c)
5 unsigned char result[20];
6 char name[50];
8 if (!patchlen)
9 return;
11 SHA1_Final(result, c);
12 memcpy(name, sha1_to_hex(id), 41);
13 printf("%s %s\n", sha1_to_hex(result), name);
14 SHA1_Init(c);
17 static int remove_space(char *line)
19 char *src = line;
20 char *dst = line;
21 unsigned char c;
23 while ((c = *src++) != '\0') {
24 if (!isspace(c))
25 *dst++ = c;
27 return dst - line;
30 static void generate_id_list(void)
32 static unsigned char sha1[20];
33 static char line[1000];
34 SHA_CTX ctx;
35 int patchlen = 0;
37 SHA1_Init(&ctx);
38 while (fgets(line, sizeof(line), stdin) != NULL) {
39 unsigned char n[20];
40 char *p = line;
41 int len;
43 if (!memcmp(line, "diff-tree ", 10))
44 p += 10;
46 if (!get_sha1_hex(p, n)) {
47 flush_current_id(patchlen, sha1, &ctx);
48 memcpy(sha1, n, 20);
49 patchlen = 0;
50 continue;
53 /* Ignore commit comments */
54 if (!patchlen && memcmp(line, "diff ", 5))
55 continue;
57 /* Ignore git-diff index header */
58 if (!memcmp(line, "index ", 6))
59 continue;
61 /* Ignore line numbers when computing the SHA1 of the patch */
62 if (!memcmp(line, "@@ -", 4))
63 continue;
65 /* Compute the sha without whitespace */
66 len = remove_space(line);
67 patchlen += len;
68 SHA1_Update(&ctx, line, len);
70 flush_current_id(patchlen, sha1, &ctx);
73 static const char patch_id_usage[] = "git-patch-id < patch";
75 int main(int argc, char **argv)
77 if (argc != 1)
78 usage(patch_id_usage);
80 generate_id_list();
81 return 0;