msvc: add a Makefile target to pre-generate the Visual Studio solution
[git/raj.git] / builtin / patch-id.c
blobbd28b80b2d0f3cd5e9f0b451678279198c52d24d
1 #include "builtin.h"
2 #include "config.h"
3 #include "diff.h"
5 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
7 char name[50];
9 if (!patchlen)
10 return;
12 memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
13 printf("%s %s\n", oid_to_hex(result), name);
16 static int remove_space(char *line)
18 char *src = line;
19 char *dst = line;
20 unsigned char c;
22 while ((c = *src++) != '\0') {
23 if (!isspace(c))
24 *dst++ = c;
26 return dst - line;
29 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
31 static const char digits[] = "0123456789";
32 const char *q, *r;
33 int n;
35 q = p + 4;
36 n = strspn(q, digits);
37 if (q[n] == ',') {
38 q += n + 1;
39 n = strspn(q, digits);
41 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
42 return 0;
44 r = q + n + 2;
45 n = strspn(r, digits);
46 if (r[n] == ',') {
47 r += n + 1;
48 n = strspn(r, digits);
50 if (n == 0)
51 return 0;
53 *p_before = atoi(q);
54 *p_after = atoi(r);
55 return 1;
58 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
59 struct strbuf *line_buf, int stable)
61 int patchlen = 0, found_next = 0;
62 int before = -1, after = -1;
63 git_SHA_CTX ctx;
65 git_SHA1_Init(&ctx);
66 oidclr(result);
68 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
69 char *line = line_buf->buf;
70 const char *p = line;
71 int len;
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))
77 continue;
79 if (!get_oid_hex(p, next_oid)) {
80 found_next = 1;
81 break;
84 /* Ignore commit comments */
85 if (!patchlen && !starts_with(line, "diff "))
86 continue;
88 /* Parsing diff header? */
89 if (before == -1) {
90 if (starts_with(line, "index "))
91 continue;
92 else if (starts_with(line, "--- "))
93 before = after = 1;
94 else if (!isalpha(line[0]))
95 break;
98 /* Looking for a valid hunk header? */
99 if (before == 0 && after == 0) {
100 if (starts_with(line, "@@ -")) {
101 /* Parse next hunk, but ignore line numbers. */
102 scan_hunk_header(line, &before, &after);
103 continue;
106 /* Split at the end of the patch. */
107 if (!starts_with(line, "diff "))
108 break;
110 /* Else we're parsing another header. */
111 if (stable)
112 flush_one_hunk(result, &ctx);
113 before = after = -1;
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--;
122 /* Compute the sha without whitespace */
123 len = remove_space(line);
124 patchlen += len;
125 git_SHA1_Update(&ctx, line, len);
128 if (!found_next)
129 oidclr(next_oid);
131 flush_one_hunk(result, &ctx);
133 return patchlen;
136 static void generate_id_list(int stable)
138 struct object_id oid, n, result;
139 int patchlen;
140 struct strbuf line_buf = STRBUF_INIT;
142 oidclr(&oid);
143 while (!feof(stdin)) {
144 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
145 flush_current_id(patchlen, &oid, &result);
146 oidcpy(&oid, &n);
148 strbuf_release(&line_buf);
151 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
153 static int git_patch_id_config(const char *var, const char *value, void *cb)
155 int *stable = cb;
157 if (!strcmp(var, "patchid.stable")) {
158 *stable = git_config_bool(var, value);
159 return 0;
162 return git_default_config(var, value, cb);
165 int cmd_patch_id(int argc, const char **argv, const char *prefix)
167 int stable = -1;
169 git_config(git_patch_id_config, &stable);
171 /* If nothing is set, default to unstable. */
172 if (stable < 0)
173 stable = 0;
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)
180 usage(patch_id_usage);
182 generate_id_list(stable);
183 return 0;