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