]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/patch-id.c
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis'
[thirdparty/git.git] / builtin / patch-id.c
CommitLineData
c2e86add 1#include "builtin.h"
b2141fc1 2#include "config.h"
a8f6855f 3#include "diff.h"
f394e093 4#include "gettext.h"
df6e8744 5#include "hash.h"
41771fa4 6#include "hex.h"
2871f4d4 7#include "parse-options.h"
f9767222 8
1a876a69 9static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
f9767222 10{
4507ecc7
RS
11 if (patchlen)
12 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
f9767222
LT
13}
14
15static 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
580fb25b
PB
28static 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;
757e75c8 38 *p_before = atoi(q);
580fb25b 39 n = strspn(q, digits);
757e75c8
JZ
40 } else {
41 *p_before = 1;
580fb25b 42 }
757e75c8 43
580fb25b
PB
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;
757e75c8 51 *p_after = atoi(r);
580fb25b 52 n = strspn(r, digits);
757e75c8
JZ
53 } else {
54 *p_after = 1;
580fb25b
PB
55 }
56 if (n == 0)
57 return 0;
58
580fb25b
PB
59 return 1;
60}
61
1a876a69 62static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
2871f4d4 63 struct strbuf *line_buf, int stable, int verbatim)
f9767222 64{
9ae144fb 65 int patchlen = 0, found_next = 0;
580fb25b 66 int before = -1, after = -1;
0df19eb9
JZ
67 int diff_is_binary = 0;
68 char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1];
36261e42 69 git_hash_ctx ctx;
30e12b92 70
36261e42 71 the_hash_algo->init_fn(&ctx);
1a876a69 72 oidclr(result);
f9767222 73
b9ab810b
MS
74 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
75 char *line = line_buf->buf;
2bb73ae8 76 const char *p = line;
f9767222
LT
77 int len;
78
0d32ae8d
JZ
79 /* Possibly skip over the prefix added by "log" or "format-patch" */
80 if (!skip_prefix(line, "commit ", &p) &&
2bb73ae8 81 !skip_prefix(line, "From ", &p) &&
2871f4d4
JZ
82 starts_with(line, "\\ ") && 12 < strlen(line)) {
83 if (verbatim)
84 the_hash_algo->update_fn(&ctx, line, strlen(line));
2485eab5 85 continue;
2871f4d4 86 }
f9767222 87
1a876a69 88 if (!get_oid_hex(p, next_oid)) {
9ae144fb
PB
89 found_next = 1;
90 break;
f9767222
LT
91 }
92
93 /* Ignore commit comments */
2bb73ae8 94 if (!patchlen && !starts_with(line, "diff "))
f9767222
LT
95 continue;
96
580fb25b
PB
97 /* Parsing diff header? */
98 if (before == -1) {
0df19eb9
JZ
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);
580fb25b 109 continue;
0df19eb9
JZ
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, "--- "))
580fb25b
PB
124 before = after = 1;
125 else if (!isalpha(line[0]))
126 break;
127 }
9fabdedc 128
0df19eb9
JZ
129 if (diff_is_binary) {
130 if (starts_with(line, "diff ")) {
131 diff_is_binary = 0;
132 before = -1;
133 }
134 continue;
135 }
136
580fb25b
PB
137 /* Looking for a valid hunk header? */
138 if (before == 0 && after == 0) {
2bb73ae8 139 if (starts_with(line, "@@ -")) {
580fb25b
PB
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. */
2bb73ae8 146 if (!starts_with(line, "diff "))
580fb25b
PB
147 break;
148
149 /* Else we're parsing another header. */
30e12b92
MT
150 if (stable)
151 flush_one_hunk(result, &ctx);
580fb25b
PB
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--;
f9767222 160
2871f4d4
JZ
161 /* Add line to hash algo (possibly removing whitespace) */
162 len = verbatim ? strlen(line) : remove_space(line);
f9767222 163 patchlen += len;
36261e42 164 the_hash_algo->update_fn(&ctx, line, len);
9ae144fb
PB
165 }
166
167 if (!found_next)
1a876a69 168 oidclr(next_oid);
9ae144fb 169
30e12b92
MT
170 flush_one_hunk(result, &ctx);
171
9ae144fb
PB
172 return patchlen;
173}
174
2871f4d4 175static void generate_id_list(int stable, int verbatim)
9ae144fb 176{
1a876a69 177 struct object_id oid, n, result;
9ae144fb 178 int patchlen;
b9ab810b 179 struct strbuf line_buf = STRBUF_INIT;
9ae144fb 180
1a876a69 181 oidclr(&oid);
9ae144fb 182 while (!feof(stdin)) {
2871f4d4 183 patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim);
1a876a69 184 flush_current_id(patchlen, &oid, &result);
185 oidcpy(&oid, &n);
f9767222 186 }
b9ab810b 187 strbuf_release(&line_buf);
f9767222
LT
188}
189
2871f4d4
JZ
190static const char *const patch_id_usage[] = {
191 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
192};
193
194struct patch_id_opts {
195 int stable;
196 int verbatim;
197};
30e12b92 198
a4e7e317
GC
199static int git_patch_id_config(const char *var, const char *value,
200 const struct config_context *ctx, void *cb)
30e12b92 201{
2871f4d4 202 struct patch_id_opts *opts = cb;
30e12b92
MT
203
204 if (!strcmp(var, "patchid.stable")) {
2871f4d4
JZ
205 opts->stable = git_config_bool(var, value);
206 return 0;
207 }
208 if (!strcmp(var, "patchid.verbatim")) {
209 opts->verbatim = git_config_bool(var, value);
30e12b92
MT
210 return 0;
211 }
212
a4e7e317 213 return git_default_config(var, value, ctx, cb);
30e12b92 214}
f9767222 215
dedc0ec5 216int cmd_patch_id(int argc, const char **argv, const char *prefix)
f9767222 217{
2871f4d4
JZ
218 /* if nothing is set, default to unstable */
219 struct patch_id_opts config = {0, 0};
220 int opts = 0;
221 struct option builtin_patch_id_options[] = {
222 OPT_CMDMODE(0, "unstable", &opts,
223 N_("use the unstable patch-id algorithm"), 1),
224 OPT_CMDMODE(0, "stable", &opts,
225 N_("use the stable patch-id algorithm"), 2),
226 OPT_CMDMODE(0, "verbatim", &opts,
227 N_("don't strip whitespace from the patch"), 3),
228 OPT_END()
229 };
230
231 git_config(git_patch_id_config, &config);
232
233 /* verbatim implies stable */
234 if (config.verbatim)
235 config.stable = 1;
236
237 argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
238 patch_id_usage, 0);
239
240 generate_id_list(opts ? opts > 1 : config.stable,
241 opts ? opts == 3 : config.verbatim);
f9767222 242 return 0;
a6080a0a 243}