Merge branch 'en/test-selector'
[git] / builtin / patch-id.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "config.h"
4 #include "diff.h"
5
6 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
7 {
8         if (patchlen)
9                 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
10 }
11
12 static 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
25 static 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
54 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
55                            struct strbuf *line_buf, int stable)
56 {
57         int patchlen = 0, found_next = 0;
58         int before = -1, after = -1;
59         git_hash_ctx ctx;
60
61         the_hash_algo->init_fn(&ctx);
62         oidclr(result);
63
64         while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
65                 char *line = line_buf->buf;
66                 const char *p = line;
67                 int len;
68
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;
74
75                 if (!get_oid_hex(p, next_oid)) {
76                         found_next = 1;
77                         break;
78                 }
79
80                 /* Ignore commit comments */
81                 if (!patchlen && !starts_with(line, "diff "))
82                         continue;
83
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;
92                 }
93
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;
100                         }
101
102                         /* Split at the end of the patch.  */
103                         if (!starts_with(line, "diff "))
104                                 break;
105
106                         /* Else we're parsing another header.  */
107                         if (stable)
108                                 flush_one_hunk(result, &ctx);
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--;
117
118                 /* Compute the sha without whitespace */
119                 len = remove_space(line);
120                 patchlen += len;
121                 the_hash_algo->update_fn(&ctx, line, len);
122         }
123
124         if (!found_next)
125                 oidclr(next_oid);
126
127         flush_one_hunk(result, &ctx);
128
129         return patchlen;
130 }
131
132 static void generate_id_list(int stable)
133 {
134         struct object_id oid, n, result;
135         int patchlen;
136         struct strbuf line_buf = STRBUF_INIT;
137
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);
143         }
144         strbuf_release(&line_buf);
145 }
146
147 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
148
149 static 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 }
160
161 int cmd_patch_id(int argc, const char **argv, const char *prefix)
162 {
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)
176                 usage(patch_id_usage);
177
178         generate_id_list(stable);
179         return 0;
180 }