4 static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)
6 unsigned char result[20];
12 git_SHA1_Final(result, c);
13 memcpy(name, sha1_to_hex(id), 41);
14 printf("%s %s\n", sha1_to_hex(result), name);
18 static int remove_space(char *line)
24 while ((c = *src++) != '\0') {
31 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
33 static const char digits[] = "0123456789";
38 n = strspn(q, digits);
41 n = strspn(q, digits);
43 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
47 n = strspn(r, digits);
50 n = strspn(r, digits);
60 int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
62 static char line[1000];
63 int patchlen = 0, found_next = 0;
64 int before = -1, after = -1;
66 while (fgets(line, sizeof(line), stdin) != NULL) {
70 if (!memcmp(line, "diff-tree ", 10))
72 else if (!memcmp(line, "commit ", 7))
74 else if (!memcmp(line, "From ", 5))
77 if (!get_sha1_hex(p, next_sha1)) {
82 /* Ignore commit comments */
83 if (!patchlen && memcmp(line, "diff ", 5))
86 /* Parsing diff header? */
88 if (!memcmp(line, "index ", 6))
90 else if (!memcmp(line, "--- ", 4))
92 else if (!isalpha(line[0]))
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);
104 /* Split at the end of the patch. */
105 if (memcmp(line, "diff ", 5))
108 /* Else we're parsing another header. */
112 /* If we get here, we're inside a hunk. */
113 if (line[0] == '-' || line[0] == ' ')
115 if (line[0] == '+' || line[0] == ' ')
118 /* Compute the sha without whitespace */
119 len = remove_space(line);
121 git_SHA1_Update(ctx, line, len);
130 static void generate_id_list(void)
132 unsigned char sha1[20], n[20];
138 while (!feof(stdin)) {
139 patchlen = get_one_patchid(n, &ctx);
140 flush_current_id(patchlen, sha1, &ctx);
145 static const char patch_id_usage[] = "git patch-id < patch";
147 int cmd_patch_id(int argc, const char **argv, const char *prefix)
150 usage(patch_id_usage);