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