]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-patch-id.c
fast-import: introduce "feature notes" command
[thirdparty/git.git] / builtin-patch-id.c
CommitLineData
f9767222 1#include "cache.h"
2fb3f6db 2#include "exec_cmd.h"
f9767222 3
9126f009 4static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
f9767222
LT
5{
6 unsigned char result[20];
7 char name[50];
8
9 if (!patchlen)
10 return;
11
9126f009 12 git_SHA1_Final(result, c);
f9767222
LT
13 memcpy(name, sha1_to_hex(id), 41);
14 printf("%s %s\n", sha1_to_hex(result), name);
9126f009 15 git_SHA1_Init(c);
f9767222
LT
16}
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];
9126f009 35 git_SHA_CTX ctx;
f9767222
LT
36 int patchlen = 0;
37
9126f009 38 git_SHA1_Init(&ctx);
f9767222
LT
39 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;
8d3cbd27
JS
46 else if (!memcmp(line, "commit ", 7))
47 p += 7;
f9767222
LT
48
49 if (!get_sha1_hex(p, n)) {
50 flush_current_id(patchlen, sha1, &ctx);
e702496e 51 hashcpy(sha1, n);
f9767222
LT
52 patchlen = 0;
53 continue;
54 }
55
56 /* Ignore commit comments */
57 if (!patchlen && memcmp(line, "diff ", 5))
58 continue;
59
9fabdedc
KR
60 /* Ignore git-diff index header */
61 if (!memcmp(line, "index ", 6))
62 continue;
63
f9767222
LT
64 /* 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;
9126f009 71 git_SHA1_Update(&ctx, line, len);
f9767222
LT
72 }
73 flush_current_id(patchlen, sha1, &ctx);
74}
75
34263de0 76static const char patch_id_usage[] = "git patch-id < patch";
f9767222 77
dedc0ec5 78int cmd_patch_id(int argc, const char **argv, const char *prefix)
f9767222
LT
79{
80 if (argc != 1)
81 usage(patch_id_usage);
82
83 generate_id_list();
84 return 0;
a6080a0a 85}