blob: 0df4cb086ba26d1f4d56b8347a6a7bcf219832f5 [file] [log] [blame]
Linus Torvaldsf9767222005-06-23 15:06:04 -07001#include "cache.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +01002#include "exec_cmd.h"
Linus Torvaldsf9767222005-06-23 15:06:04 -07003
Nicolas Pitre9126f002008-10-01 14:05:20 -04004static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
Linus Torvaldsf9767222005-06-23 15:06:04 -07005{
6 unsigned char result[20];
7 char name[50];
8
9 if (!patchlen)
10 return;
11
Nicolas Pitre9126f002008-10-01 14:05:20 -040012 git_SHA1_Final(result, c);
Linus Torvaldsf9767222005-06-23 15:06:04 -070013 memcpy(name, sha1_to_hex(id), 41);
14 printf("%s %s\n", sha1_to_hex(result), name);
Nicolas Pitre9126f002008-10-01 14:05:20 -040015 git_SHA1_Init(c);
Linus Torvaldsf9767222005-06-23 15:06:04 -070016}
17
18static int remove_space(char *line)
19{
20 char *src = line;
21 char *dst = line;
22 unsigned char c;
23
24 while ((c = *src++) != '\0') {
25 if (!isspace(c))
26 *dst++ = c;
27 }
28 return dst - line;
29}
30
31static void generate_id_list(void)
32{
33 static unsigned char sha1[20];
34 static char line[1000];
Nicolas Pitre9126f002008-10-01 14:05:20 -040035 git_SHA_CTX ctx;
Linus Torvaldsf9767222005-06-23 15:06:04 -070036 int patchlen = 0;
37
Nicolas Pitre9126f002008-10-01 14:05:20 -040038 git_SHA1_Init(&ctx);
Linus Torvaldsf9767222005-06-23 15:06:04 -070039 while (fgets(line, sizeof(line), stdin) != NULL) {
40 unsigned char n[20];
41 char *p = line;
42 int len;
43
44 if (!memcmp(line, "diff-tree ", 10))
45 p += 10;
Johannes Schindelin8d3cbd22006-06-23 17:36:21 +020046 else if (!memcmp(line, "commit ", 7))
47 p += 7;
Linus Torvaldsf9767222005-06-23 15:06:04 -070048
49 if (!get_sha1_hex(p, n)) {
50 flush_current_id(patchlen, sha1, &ctx);
Shawn Pearcee7024962006-08-23 02:49:00 -040051 hashcpy(sha1, n);
Linus Torvaldsf9767222005-06-23 15:06:04 -070052 patchlen = 0;
53 continue;
54 }
55
56 /* Ignore commit comments */
57 if (!patchlen && memcmp(line, "diff ", 5))
58 continue;
59
Kai Ruemmler9fabded2005-10-09 16:52:50 -070060 /* Ignore git-diff index header */
61 if (!memcmp(line, "index ", 6))
62 continue;
63
Linus Torvaldsf9767222005-06-23 15:06:04 -070064 /* Ignore line numbers when computing the SHA1 of the patch */
65 if (!memcmp(line, "@@ -", 4))
66 continue;
67
68 /* Compute the sha without whitespace */
69 len = remove_space(line);
70 patchlen += len;
Nicolas Pitre9126f002008-10-01 14:05:20 -040071 git_SHA1_Update(&ctx, line, len);
Linus Torvaldsf9767222005-06-23 15:06:04 -070072 }
73 flush_current_id(patchlen, sha1, &ctx);
74}
75
Alexander Potashev34263de2009-01-04 21:39:27 +030076static const char patch_id_usage[] = "git patch-id < patch";
Linus Torvaldsf9767222005-06-23 15:06:04 -070077
78int main(int argc, char **argv)
79{
80 if (argc != 1)
81 usage(patch_id_usage);
82
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010083 git_extract_argv0_path(argv[0]);
84
Linus Torvaldsf9767222005-06-23 15:06:04 -070085 generate_id_list();
86 return 0;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070087}