blob: 871f1d20c0e364220d23035b34685ced8737cda8 [file] [log] [blame]
Linus Torvaldsf9767222005-06-23 15:06:04 -07001#include "cache.h"
2
Nicolas Pitre9126f002008-10-01 14:05:20 -04003static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
Linus Torvaldsf9767222005-06-23 15:06:04 -07004{
5 unsigned char result[20];
6 char name[50];
7
8 if (!patchlen)
9 return;
10
Nicolas Pitre9126f002008-10-01 14:05:20 -040011 git_SHA1_Final(result, c);
Linus Torvaldsf9767222005-06-23 15:06:04 -070012 memcpy(name, sha1_to_hex(id), 41);
13 printf("%s %s\n", sha1_to_hex(result), name);
Nicolas Pitre9126f002008-10-01 14:05:20 -040014 git_SHA1_Init(c);
Linus Torvaldsf9767222005-06-23 15:06:04 -070015}
16
17static int remove_space(char *line)
18{
19 char *src = line;
20 char *dst = line;
21 unsigned char c;
22
23 while ((c = *src++) != '\0') {
24 if (!isspace(c))
25 *dst++ = c;
26 }
27 return dst - line;
28}
29
30static void generate_id_list(void)
31{
32 static unsigned char sha1[20];
33 static char line[1000];
Nicolas Pitre9126f002008-10-01 14:05:20 -040034 git_SHA_CTX ctx;
Linus Torvaldsf9767222005-06-23 15:06:04 -070035 int patchlen = 0;
36
Nicolas Pitre9126f002008-10-01 14:05:20 -040037 git_SHA1_Init(&ctx);
Linus Torvaldsf9767222005-06-23 15:06:04 -070038 while (fgets(line, sizeof(line), stdin) != NULL) {
39 unsigned char n[20];
40 char *p = line;
41 int len;
42
43 if (!memcmp(line, "diff-tree ", 10))
44 p += 10;
Johannes Schindelin8d3cbd22006-06-23 17:36:21 +020045 else if (!memcmp(line, "commit ", 7))
46 p += 7;
Linus Torvaldsf9767222005-06-23 15:06:04 -070047
48 if (!get_sha1_hex(p, n)) {
49 flush_current_id(patchlen, sha1, &ctx);
Shawn Pearcee7024962006-08-23 02:49:00 -040050 hashcpy(sha1, n);
Linus Torvaldsf9767222005-06-23 15:06:04 -070051 patchlen = 0;
52 continue;
53 }
54
55 /* Ignore commit comments */
56 if (!patchlen && memcmp(line, "diff ", 5))
57 continue;
58
Kai Ruemmler9fabded2005-10-09 16:52:50 -070059 /* Ignore git-diff index header */
60 if (!memcmp(line, "index ", 6))
61 continue;
62
Linus Torvaldsf9767222005-06-23 15:06:04 -070063 /* Ignore line numbers when computing the SHA1 of the patch */
64 if (!memcmp(line, "@@ -", 4))
65 continue;
66
67 /* Compute the sha without whitespace */
68 len = remove_space(line);
69 patchlen += len;
Nicolas Pitre9126f002008-10-01 14:05:20 -040070 git_SHA1_Update(&ctx, line, len);
Linus Torvaldsf9767222005-06-23 15:06:04 -070071 }
72 flush_current_id(patchlen, sha1, &ctx);
73}
74
Junio C Hamano54c68702005-07-27 00:04:16 -070075static const char patch_id_usage[] = "git-patch-id < patch";
Linus Torvaldsf9767222005-06-23 15:06:04 -070076
77int main(int argc, char **argv)
78{
79 if (argc != 1)
80 usage(patch_id_usage);
81
82 generate_id_list();
83 return 0;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070084}