1#include "cache.h"
2#include "exec_cmd.h"
34
static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
5{
6unsigned char result[20];
7char name[50];
89
if (!patchlen)
10return;
1112
git_SHA1_Final(result, c);
13memcpy(name, sha1_to_hex(id), 41);
14printf("%s %s\n", sha1_to_hex(result), name);
15git_SHA1_Init(c);
16}
1718
static int remove_space(char *line)
19{
20char *src = line;
21char *dst = line;
22unsigned char c;
2324
while ((c = *src++) != '\0') {
25if (!isspace(c))
26*dst++ = c;
27}
28return dst - line;
29}
3031
static int scan_hunk_header(const char *p, int *p_before, int *p_after)
32{
33static const char digits[] = "0123456789";
34const char *q, *r;
35int n;
3637
q = p + 4;
38n = strspn(q, digits);
39if (q[n] == ',') {
40q += n + 1;
41n = strspn(q, digits);
42}
43if (n == 0 || q[n] != ' ' || q[n+1] != '+')
44return 0;
4546
r = q + n + 2;
47n = strspn(r, digits);
48if (r[n] == ',') {
49r += n + 1;
50n = strspn(r, digits);
51}
52if (n == 0)
53return 0;
5455
*p_before = atoi(q);
56*p_after = atoi(r);
57return 1;
58}
5960
int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
61{
62static char line[1000];
63int patchlen = 0, found_next = 0;
64int before = -1, after = -1;
6566
while (fgets(line, sizeof(line), stdin) != NULL) {
67char *p = line;
68int len;
6970
if (!memcmp(line, "diff-tree ", 10))
71p += 10;
72else if (!memcmp(line, "commit ", 7))
73p += 7;
74else if (!memcmp(line, "From ", 5))
75p += 5;
7677
if (!get_sha1_hex(p, next_sha1)) {
78found_next = 1;
79break;
80}
8182
/* Ignore commit comments */
83if (!patchlen && memcmp(line, "diff ", 5))
84continue;
8586
/* Parsing diff header? */
87if (before == -1) {
88if (!memcmp(line, "index ", 6))
89continue;
90else if (!memcmp(line, "--- ", 4))
91before = after = 1;
92else if (!isalpha(line[0]))
93break;
94}
9596
/* Looking for a valid hunk header? */
97if (before == 0 && after == 0) {
98if (!memcmp(line, "@@ -", 4)) {
99/* Parse next hunk, but ignore line numbers. */
100scan_hunk_header(line, &before, &after);
101continue;
102}
103104
/* Split at the end of the patch. */
105if (memcmp(line, "diff ", 5))
106break;
107108
/* Else we're parsing another header. */
109before = after = -1;
110}
111112
/* If we get here, we're inside a hunk. */
113if (line[0] == '-' || line[0] == ' ')
114before--;
115if (line[0] == '+' || line[0] == ' ')
116after--;
117118
/* Compute the sha without whitespace */
119len = remove_space(line);
120patchlen += len;
121git_SHA1_Update(ctx, line, len);
122}
123124
if (!found_next)
125hashclr(next_sha1);
126127
return patchlen;
128}
129130
static void generate_id_list(void)
131{
132unsigned char sha1[20], n[20];
133git_SHA_CTX ctx;
134int patchlen;
135136
git_SHA1_Init(&ctx);
137hashclr(sha1);
138while (!feof(stdin)) {
139patchlen = get_one_patchid(n, &ctx);
140flush_current_id(patchlen, sha1, &ctx);
141hashcpy(sha1, n);
142}
143}
144145
static const char patch_id_usage[] = "git patch-id < patch";
146147
int cmd_patch_id(int argc, const char **argv, const char *prefix)
148{
149if (argc != 1)
150usage(patch_id_usage);
151152
generate_id_list();
153return 0;
154}