1#include "builtin.h"
2
3static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
4{
5 char name[50];
6
7 if (!patchlen)
8 return;
9
10 memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
11 printf("%s %s\n", oid_to_hex(result), name);
12}
13
14static int remove_space(char *line)
15{
16 char *src = line;
17 char *dst = line;
18 unsigned char c;
19
20 while ((c = *src++) != '\0') {
21 if (!isspace(c))
22 *dst++ = c;
23 }
24 return dst - line;
25}
26
27static int scan_hunk_header(const char *p, int *p_before, int *p_after)
28{
29 static const char digits[] = "0123456789";
30 const char *q, *r;
31 int n;
32
33 q = p + 4;
34 n = strspn(q, digits);
35 if (q[n] == ',') {
36 q += n + 1;
37 n = strspn(q, digits);
38 }
39 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
40 return 0;
41
42 r = q + n + 2;
43 n = strspn(r, digits);
44 if (r[n] == ',') {
45 r += n + 1;
46 n = strspn(r, digits);
47 }
48 if (n == 0)
49 return 0;
50
51 *p_before = atoi(q);
52 *p_after = atoi(r);
53 return 1;
54}
55
56static void flush_one_hunk(struct object_id *result, git_SHA_CTX *ctx)
57{
58 unsigned char hash[GIT_SHA1_RAWSZ];
59 unsigned short carry = 0;
60 int i;
61
62 git_SHA1_Final(hash, ctx);
63 git_SHA1_Init(ctx);
64 /* 20-byte sum, with carry */
65 for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
66 carry += result->hash[i] + hash[i];
67 result->hash[i] = carry;
68 carry >>= 8;
69 }
70}
71
72static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
73 struct strbuf *line_buf, int stable)
74{
75 int patchlen = 0, found_next = 0;
76 int before = -1, after = -1;
77 git_SHA_CTX ctx;
78
79 git_SHA1_Init(&ctx);
80 oidclr(result);
81
82 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
83 char *line = line_buf->buf;
84 const char *p = line;
85 int len;
86
87 if (!skip_prefix(line, "diff-tree ", &p) &&
88 !skip_prefix(line, "commit ", &p) &&
89 !skip_prefix(line, "From ", &p) &&
90 starts_with(line, "\\ ") && 12 < strlen(line))
91 continue;
92
93 if (!get_oid_hex(p, next_oid)) {
94 found_next = 1;
95 break;
96 }
97
98 /* Ignore commit comments */
99 if (!patchlen && !starts_with(line, "diff "))
100 continue;
101
102 /* Parsing diff header? */
103 if (before == -1) {
104 if (starts_with(line, "index "))
105 continue;
106 else if (starts_with(line, "--- "))
107 before = after = 1;
108 else if (!isalpha(line[0]))
109 break;
110 }
111
112 /* Looking for a valid hunk header? */
113 if (before == 0 && after == 0) {
114 if (starts_with(line, "@@ -")) {
115 /* Parse next hunk, but ignore line numbers. */
116 scan_hunk_header(line, &before, &after);
117 continue;
118 }
119
120 /* Split at the end of the patch. */
121 if (!starts_with(line, "diff "))
122 break;
123
124 /* Else we're parsing another header. */
125 if (stable)
126 flush_one_hunk(result, &ctx);
127 before = after = -1;
128 }
129
130 /* If we get here, we're inside a hunk. */
131 if (line[0] == '-' || line[0] == ' ')
132 before--;
133 if (line[0] == '+' || line[0] == ' ')
134 after--;
135
136 /* Compute the sha without whitespace */
137 len = remove_space(line);
138 patchlen += len;
139 git_SHA1_Update(&ctx, line, len);
140 }
141
142 if (!found_next)
143 oidclr(next_oid);
144
145 flush_one_hunk(result, &ctx);
146
147 return patchlen;
148}
149
150static void generate_id_list(int stable)
151{
152 struct object_id oid, n, result;
153 int patchlen;
154 struct strbuf line_buf = STRBUF_INIT;
155
156 oidclr(&oid);
157 while (!feof(stdin)) {
158 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
159 flush_current_id(patchlen, &oid, &result);
160 oidcpy(&oid, &n);
161 }
162 strbuf_release(&line_buf);
163}
164
165static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
166
167static int git_patch_id_config(const char *var, const char *value, void *cb)
168{
169 int *stable = cb;
170
171 if (!strcmp(var, "patchid.stable")) {
172 *stable = git_config_bool(var, value);
173 return 0;
174 }
175
176 return git_default_config(var, value, cb);
177}
178
179int cmd_patch_id(int argc, const char **argv, const char *prefix)
180{
181 int stable = -1;
182
183 git_config(git_patch_id_config, &stable);
184
185 /* If nothing is set, default to unstable. */
186 if (stable < 0)
187 stable = 0;
188
189 if (argc == 2 && !strcmp(argv[1], "--stable"))
190 stable = 1;
191 else if (argc == 2 && !strcmp(argv[1], "--unstable"))
192 stable = 0;
193 else if (argc != 1)
194 usage(patch_id_usage);
195
196 generate_id_list(stable);
197 return 0;
198}