]>
Commit | Line | Data |
---|---|---|
f9767222 | 1 | #include "cache.h" |
2fb3f6db | 2 | #include "exec_cmd.h" |
f9767222 | 3 | |
9126f009 | 4 | static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) |
f9767222 LT |
5 | { |
6 | unsigned char result[20]; | |
7 | char name[50]; | |
8 | ||
9 | if (!patchlen) | |
10 | return; | |
11 | ||
9126f009 | 12 | git_SHA1_Final(result, c); |
f9767222 LT |
13 | memcpy(name, sha1_to_hex(id), 41); |
14 | printf("%s %s\n", sha1_to_hex(result), name); | |
9126f009 | 15 | git_SHA1_Init(c); |
f9767222 LT |
16 | } |
17 | ||
18 | static int remove_space(char *line) | |
19 | { | |
20 | char *src = line; | |
21 | char *dst = line; | |
22 | unsigned char c; | |
23 | ||
24 | while ((c = *src++) != '\0') { | |
25 | if (!isspace(c)) | |
26 | *dst++ = c; | |
27 | } | |
28 | return dst - line; | |
29 | } | |
30 | ||
580fb25b PB |
31 | static int scan_hunk_header(const char *p, int *p_before, int *p_after) |
32 | { | |
33 | static const char digits[] = "0123456789"; | |
34 | const char *q, *r; | |
35 | int n; | |
36 | ||
37 | q = p + 4; | |
38 | n = strspn(q, digits); | |
39 | if (q[n] == ',') { | |
40 | q += n + 1; | |
41 | n = strspn(q, digits); | |
42 | } | |
43 | if (n == 0 || q[n] != ' ' || q[n+1] != '+') | |
44 | return 0; | |
45 | ||
46 | r = q + n + 2; | |
47 | n = strspn(r, digits); | |
48 | if (r[n] == ',') { | |
49 | r += n + 1; | |
50 | n = strspn(r, digits); | |
51 | } | |
52 | if (n == 0) | |
53 | return 0; | |
54 | ||
55 | *p_before = atoi(q); | |
56 | *p_after = atoi(r); | |
57 | return 1; | |
58 | } | |
59 | ||
9ae144fb | 60 | int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx) |
f9767222 | 61 | { |
f9767222 | 62 | static char line[1000]; |
9ae144fb | 63 | int patchlen = 0, found_next = 0; |
580fb25b | 64 | int before = -1, after = -1; |
f9767222 | 65 | |
f9767222 | 66 | while (fgets(line, sizeof(line), stdin) != NULL) { |
f9767222 LT |
67 | char *p = line; |
68 | int len; | |
69 | ||
70 | if (!memcmp(line, "diff-tree ", 10)) | |
71 | p += 10; | |
8d3cbd27 JS |
72 | else if (!memcmp(line, "commit ", 7)) |
73 | p += 7; | |
580fb25b PB |
74 | else if (!memcmp(line, "From ", 5)) |
75 | p += 5; | |
f9767222 | 76 | |
9ae144fb PB |
77 | if (!get_sha1_hex(p, next_sha1)) { |
78 | found_next = 1; | |
79 | break; | |
f9767222 LT |
80 | } |
81 | ||
82 | /* Ignore commit comments */ | |
83 | if (!patchlen && memcmp(line, "diff ", 5)) | |
84 | continue; | |
85 | ||
580fb25b PB |
86 | /* Parsing diff header? */ |
87 | if (before == -1) { | |
88 | if (!memcmp(line, "index ", 6)) | |
89 | continue; | |
90 | else if (!memcmp(line, "--- ", 4)) | |
91 | before = after = 1; | |
92 | else if (!isalpha(line[0])) | |
93 | break; | |
94 | } | |
9fabdedc | 95 | |
580fb25b PB |
96 | /* Looking for a valid hunk header? */ |
97 | if (before == 0 && after == 0) { | |
98 | if (!memcmp(line, "@@ -", 4)) { | |
99 | /* Parse next hunk, but ignore line numbers. */ | |
100 | scan_hunk_header(line, &before, &after); | |
101 | continue; | |
102 | } | |
103 | ||
104 | /* Split at the end of the patch. */ | |
105 | if (memcmp(line, "diff ", 5)) | |
106 | break; | |
107 | ||
108 | /* Else we're parsing another header. */ | |
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; | |
9ae144fb PB |
121 | git_SHA1_Update(ctx, line, len); |
122 | } | |
123 | ||
124 | if (!found_next) | |
125 | hashclr(next_sha1); | |
126 | ||
127 | return patchlen; | |
128 | } | |
129 | ||
130 | static void generate_id_list(void) | |
131 | { | |
132 | unsigned char sha1[20], n[20]; | |
133 | git_SHA_CTX ctx; | |
134 | int patchlen; | |
135 | ||
136 | git_SHA1_Init(&ctx); | |
137 | hashclr(sha1); | |
138 | while (!feof(stdin)) { | |
139 | patchlen = get_one_patchid(n, &ctx); | |
140 | flush_current_id(patchlen, sha1, &ctx); | |
141 | hashcpy(sha1, n); | |
f9767222 | 142 | } |
f9767222 LT |
143 | } |
144 | ||
34263de0 | 145 | static const char patch_id_usage[] = "git patch-id < patch"; |
f9767222 | 146 | |
dedc0ec5 | 147 | int cmd_patch_id(int argc, const char **argv, const char *prefix) |
f9767222 LT |
148 | { |
149 | if (argc != 1) | |
150 | usage(patch_id_usage); | |
151 | ||
152 | generate_id_list(); | |
153 | return 0; | |
a6080a0a | 154 | } |