graph: improve grammar of "invalid color" error message
[git/debian.git] / builtin / patch-id.c
blob822ffff51fbd3aa23abc166ab016fb00933f8e9a
1 #include "cache.h"
2 #include "builtin.h"
3 #include "config.h"
4 #include "diff.h"
6 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
8 if (patchlen)
9 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
12 static int remove_space(char *line)
14 char *src = line;
15 char *dst = line;
16 unsigned char c;
18 while ((c = *src++) != '\0') {
19 if (!isspace(c))
20 *dst++ = c;
22 return dst - line;
25 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
27 static const char digits[] = "0123456789";
28 const char *q, *r;
29 int n;
31 q = p + 4;
32 n = strspn(q, digits);
33 if (q[n] == ',') {
34 q += n + 1;
35 n = strspn(q, digits);
37 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
38 return 0;
40 r = q + n + 2;
41 n = strspn(r, digits);
42 if (r[n] == ',') {
43 r += n + 1;
44 n = strspn(r, digits);
46 if (n == 0)
47 return 0;
49 *p_before = atoi(q);
50 *p_after = atoi(r);
51 return 1;
54 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
55 struct strbuf *line_buf, int stable)
57 int patchlen = 0, found_next = 0;
58 int before = -1, after = -1;
59 git_hash_ctx ctx;
61 the_hash_algo->init_fn(&ctx);
62 oidclr(result);
64 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
65 char *line = line_buf->buf;
66 const char *p = line;
67 int len;
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))
73 continue;
75 if (!get_oid_hex(p, next_oid)) {
76 found_next = 1;
77 break;
80 /* Ignore commit comments */
81 if (!patchlen && !starts_with(line, "diff "))
82 continue;
84 /* Parsing diff header? */
85 if (before == -1) {
86 if (starts_with(line, "index "))
87 continue;
88 else if (starts_with(line, "--- "))
89 before = after = 1;
90 else if (!isalpha(line[0]))
91 break;
94 /* Looking for a valid hunk header? */
95 if (before == 0 && after == 0) {
96 if (starts_with(line, "@@ -")) {
97 /* Parse next hunk, but ignore line numbers. */
98 scan_hunk_header(line, &before, &after);
99 continue;
102 /* Split at the end of the patch. */
103 if (!starts_with(line, "diff "))
104 break;
106 /* Else we're parsing another header. */
107 if (stable)
108 flush_one_hunk(result, &ctx);
109 before = after = -1;
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--;
118 /* Compute the sha without whitespace */
119 len = remove_space(line);
120 patchlen += len;
121 the_hash_algo->update_fn(&ctx, line, len);
124 if (!found_next)
125 oidclr(next_oid);
127 flush_one_hunk(result, &ctx);
129 return patchlen;
132 static void generate_id_list(int stable)
134 struct object_id oid, n, result;
135 int patchlen;
136 struct strbuf line_buf = STRBUF_INIT;
138 oidclr(&oid);
139 while (!feof(stdin)) {
140 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
141 flush_current_id(patchlen, &oid, &result);
142 oidcpy(&oid, &n);
144 strbuf_release(&line_buf);
147 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
149 static int git_patch_id_config(const char *var, const char *value, void *cb)
151 int *stable = cb;
153 if (!strcmp(var, "patchid.stable")) {
154 *stable = git_config_bool(var, value);
155 return 0;
158 return git_default_config(var, value, cb);
161 int cmd_patch_id(int argc, const char **argv, const char *prefix)
163 int stable = -1;
165 git_config(git_patch_id_config, &stable);
167 /* If nothing is set, default to unstable. */
168 if (stable < 0)
169 stable = 0;
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)
176 usage(patch_id_usage);
178 generate_id_list(stable);
179 return 0;