]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/patch-id.c
Merge branch 'tz/lib-gpg-prereq-fix'
[thirdparty/git.git] / builtin / patch-id.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "parse-options.h"
8
9 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
10 {
11 if (patchlen)
12 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
13 }
14
15 static int remove_space(char *line)
16 {
17 char *src = line;
18 char *dst = line;
19 unsigned char c;
20
21 while ((c = *src++) != '\0') {
22 if (!isspace(c))
23 *dst++ = c;
24 }
25 return dst - line;
26 }
27
28 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
29 {
30 static const char digits[] = "0123456789";
31 const char *q, *r;
32 int n;
33
34 q = p + 4;
35 n = strspn(q, digits);
36 if (q[n] == ',') {
37 q += n + 1;
38 *p_before = atoi(q);
39 n = strspn(q, digits);
40 } else {
41 *p_before = 1;
42 }
43
44 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
45 return 0;
46
47 r = q + n + 2;
48 n = strspn(r, digits);
49 if (r[n] == ',') {
50 r += n + 1;
51 *p_after = atoi(r);
52 n = strspn(r, digits);
53 } else {
54 *p_after = 1;
55 }
56 if (n == 0)
57 return 0;
58
59 return 1;
60 }
61
62 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
63 struct strbuf *line_buf, int stable, int verbatim)
64 {
65 int patchlen = 0, found_next = 0;
66 int before = -1, after = -1;
67 int diff_is_binary = 0;
68 char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1];
69 git_hash_ctx ctx;
70
71 the_hash_algo->init_fn(&ctx);
72 oidclr(result);
73
74 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
75 char *line = line_buf->buf;
76 const char *p = line;
77 int len;
78
79 /* Possibly skip over the prefix added by "log" or "format-patch" */
80 if (!skip_prefix(line, "commit ", &p) &&
81 !skip_prefix(line, "From ", &p) &&
82 starts_with(line, "\\ ") && 12 < strlen(line)) {
83 if (verbatim)
84 the_hash_algo->update_fn(&ctx, line, strlen(line));
85 continue;
86 }
87
88 if (!get_oid_hex(p, next_oid)) {
89 found_next = 1;
90 break;
91 }
92
93 /* Ignore commit comments */
94 if (!patchlen && !starts_with(line, "diff "))
95 continue;
96
97 /* Parsing diff header? */
98 if (before == -1) {
99 if (starts_with(line, "GIT binary patch") ||
100 starts_with(line, "Binary files")) {
101 diff_is_binary = 1;
102 before = 0;
103 the_hash_algo->update_fn(&ctx, pre_oid_str,
104 strlen(pre_oid_str));
105 the_hash_algo->update_fn(&ctx, post_oid_str,
106 strlen(post_oid_str));
107 if (stable)
108 flush_one_hunk(result, &ctx);
109 continue;
110 } else if (skip_prefix(line, "index ", &p)) {
111 char *oid1_end = strstr(line, "..");
112 char *oid2_end = NULL;
113 if (oid1_end)
114 oid2_end = strstr(oid1_end, " ");
115 if (!oid2_end)
116 oid2_end = line + strlen(line) - 1;
117 if (oid1_end != NULL && oid2_end != NULL) {
118 *oid1_end = *oid2_end = '\0';
119 strlcpy(pre_oid_str, p, GIT_MAX_HEXSZ + 1);
120 strlcpy(post_oid_str, oid1_end + 2, GIT_MAX_HEXSZ + 1);
121 }
122 continue;
123 } else if (starts_with(line, "--- "))
124 before = after = 1;
125 else if (!isalpha(line[0]))
126 break;
127 }
128
129 if (diff_is_binary) {
130 if (starts_with(line, "diff ")) {
131 diff_is_binary = 0;
132 before = -1;
133 }
134 continue;
135 }
136
137 /* Looking for a valid hunk header? */
138 if (before == 0 && after == 0) {
139 if (starts_with(line, "@@ -")) {
140 /* Parse next hunk, but ignore line numbers. */
141 scan_hunk_header(line, &before, &after);
142 continue;
143 }
144
145 /* Split at the end of the patch. */
146 if (!starts_with(line, "diff "))
147 break;
148
149 /* Else we're parsing another header. */
150 if (stable)
151 flush_one_hunk(result, &ctx);
152 before = after = -1;
153 }
154
155 /* If we get here, we're inside a hunk. */
156 if (line[0] == '-' || line[0] == ' ')
157 before--;
158 if (line[0] == '+' || line[0] == ' ')
159 after--;
160
161 /* Add line to hash algo (possibly removing whitespace) */
162 len = verbatim ? strlen(line) : remove_space(line);
163 patchlen += len;
164 the_hash_algo->update_fn(&ctx, line, len);
165 }
166
167 if (!found_next)
168 oidclr(next_oid);
169
170 flush_one_hunk(result, &ctx);
171
172 return patchlen;
173 }
174
175 static void generate_id_list(int stable, int verbatim)
176 {
177 struct object_id oid, n, result;
178 int patchlen;
179 struct strbuf line_buf = STRBUF_INIT;
180
181 oidclr(&oid);
182 while (!feof(stdin)) {
183 patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
184 flush_current_id(patchlen, &oid, &result);
185 oidcpy(&oid, &n);
186 }
187 strbuf_release(&line_buf);
188 }
189
190 static const char *const patch_id_usage[] = {
191 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
192 };
193
194 struct patch_id_opts {
195 int stable;
196 int verbatim;
197 };
198
199 static int git_patch_id_config(const char *var, const char *value, void *cb)
200 {
201 struct patch_id_opts *opts = cb;
202
203 if (!strcmp(var, "patchid.stable")) {
204 opts->stable = git_config_bool(var, value);
205 return 0;
206 }
207 if (!strcmp(var, "patchid.verbatim")) {
208 opts->verbatim = git_config_bool(var, value);
209 return 0;
210 }
211
212 return git_default_config(var, value, cb);
213 }
214
215 int cmd_patch_id(int argc, const char **argv, const char *prefix)
216 {
217 /* if nothing is set, default to unstable */
218 struct patch_id_opts config = {0, 0};
219 int opts = 0;
220 struct option builtin_patch_id_options[] = {
221 OPT_CMDMODE(0, "unstable", &opts,
222 N_("use the unstable patch-id algorithm"), 1),
223 OPT_CMDMODE(0, "stable", &opts,
224 N_("use the stable patch-id algorithm"), 2),
225 OPT_CMDMODE(0, "verbatim", &opts,
226 N_("don't strip whitespace from the patch"), 3),
227 OPT_END()
228 };
229
230 git_config(git_patch_id_config, &config);
231
232 /* verbatim implies stable */
233 if (config.verbatim)
234 config.stable = 1;
235
236 argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
237 patch_id_usage, 0);
238
239 generate_id_list(opts ? opts > 1 : config.stable,
240 opts ? opts == 3 : config.verbatim);
241 return 0;
242 }