reftable/merged: allocation-less dropping of shadowed records
[git/gitster.git] / builtin / patch-id.c
blob3894d2b970612cab4a47250dd53da5b0908c820e
1 #include "builtin.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "gettext.h"
5 #include "hash.h"
6 #include "hex.h"
7 #include "parse-options.h"
9 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
11 if (patchlen)
12 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
15 static int remove_space(char *line)
17 char *src = line;
18 char *dst = line;
19 unsigned char c;
21 while ((c = *src++) != '\0') {
22 if (!isspace(c))
23 *dst++ = c;
25 return dst - line;
28 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
30 static const char digits[] = "0123456789";
31 const char *q, *r;
32 int n;
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;
44 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
45 return 0;
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;
56 if (n == 0)
57 return 0;
59 return 1;
62 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
63 struct strbuf *line_buf, int stable, int verbatim)
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;
71 the_hash_algo->init_fn(&ctx);
72 oidclr(result);
74 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
75 char *line = line_buf->buf;
76 const char *p = line;
77 int len;
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;
88 if (!get_oid_hex(p, next_oid)) {
89 found_next = 1;
90 break;
93 /* Ignore commit comments */
94 if (!patchlen && !starts_with(line, "diff "))
95 continue;
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);
122 continue;
123 } else if (starts_with(line, "--- "))
124 before = after = 1;
125 else if (!isalpha(line[0]))
126 break;
129 if (diff_is_binary) {
130 if (starts_with(line, "diff ")) {
131 diff_is_binary = 0;
132 before = -1;
134 continue;
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;
145 /* Split at the end of the patch. */
146 if (!starts_with(line, "diff "))
147 break;
149 /* Else we're parsing another header. */
150 if (stable)
151 flush_one_hunk(result, &ctx);
152 before = after = -1;
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--;
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);
167 if (!found_next)
168 oidclr(next_oid);
170 flush_one_hunk(result, &ctx);
172 return patchlen;
175 static void generate_id_list(int stable, int verbatim)
177 struct object_id oid, n, result;
178 int patchlen;
179 struct strbuf line_buf = STRBUF_INIT;
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);
187 strbuf_release(&line_buf);
190 static const char *const patch_id_usage[] = {
191 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
194 struct patch_id_opts {
195 int stable;
196 int verbatim;
199 static int git_patch_id_config(const char *var, const char *value,
200 const struct config_context *ctx, void *cb)
202 struct patch_id_opts *opts = cb;
204 if (!strcmp(var, "patchid.stable")) {
205 opts->stable = git_config_bool(var, value);
206 return 0;
208 if (!strcmp(var, "patchid.verbatim")) {
209 opts->verbatim = git_config_bool(var, value);
210 return 0;
213 return git_default_config(var, value, ctx, cb);
216 int cmd_patch_id(int argc, const char **argv, const char *prefix)
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()
231 git_config(git_patch_id_config, &config);
233 /* verbatim implies stable */
234 if (config.verbatim)
235 config.stable = 1;
237 argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
238 patch_id_usage, 0);
240 generate_id_list(opts ? opts > 1 : config.stable,
241 opts ? opts == 3 : config.verbatim);
242 return 0;