Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 2 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 3 | static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c) |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 4 | { |
| 5 | unsigned char result[20]; |
| 6 | char name[50]; |
| 7 | |
| 8 | if (!patchlen) |
| 9 | return; |
| 10 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 11 | git_SHA1_Final(result, c); |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 12 | memcpy(name, sha1_to_hex(id), 41); |
| 13 | printf("%s %s\n", sha1_to_hex(result), name); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 14 | git_SHA1_Init(c); |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static int remove_space(char *line) |
| 18 | { |
| 19 | char *src = line; |
| 20 | char *dst = line; |
| 21 | unsigned char c; |
| 22 | |
| 23 | while ((c = *src++) != '\0') { |
| 24 | if (!isspace(c)) |
| 25 | *dst++ = c; |
| 26 | } |
| 27 | return dst - line; |
| 28 | } |
| 29 | |
Paolo Bonzini | 580fb25 | 2010-04-19 10:46:14 +0200 | [diff] [blame] | 30 | static int scan_hunk_header(const char *p, int *p_before, int *p_after) |
| 31 | { |
| 32 | static const char digits[] = "0123456789"; |
| 33 | const char *q, *r; |
| 34 | int n; |
| 35 | |
| 36 | q = p + 4; |
| 37 | n = strspn(q, digits); |
| 38 | if (q[n] == ',') { |
| 39 | q += n + 1; |
| 40 | n = strspn(q, digits); |
| 41 | } |
| 42 | if (n == 0 || q[n] != ' ' || q[n+1] != '+') |
| 43 | return 0; |
| 44 | |
| 45 | r = q + n + 2; |
| 46 | n = strspn(r, digits); |
| 47 | if (r[n] == ',') { |
| 48 | r += n + 1; |
| 49 | n = strspn(r, digits); |
| 50 | } |
| 51 | if (n == 0) |
| 52 | return 0; |
| 53 | |
| 54 | *p_before = atoi(q); |
| 55 | *p_after = atoi(r); |
| 56 | return 1; |
| 57 | } |
| 58 | |
Michael Schubert | b9ab810 | 2011-09-21 14:42:22 +0200 | [diff] [blame] | 59 | static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct strbuf *line_buf) |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 60 | { |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 61 | int patchlen = 0, found_next = 0; |
Paolo Bonzini | 580fb25 | 2010-04-19 10:46:14 +0200 | [diff] [blame] | 62 | int before = -1, after = -1; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 63 | |
Michael Schubert | b9ab810 | 2011-09-21 14:42:22 +0200 | [diff] [blame] | 64 | while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) { |
| 65 | char *line = line_buf->buf; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 66 | char *p = line; |
| 67 | int len; |
| 68 | |
| 69 | if (!memcmp(line, "diff-tree ", 10)) |
| 70 | p += 10; |
Johannes Schindelin | 8d3cbd2 | 2006-06-23 17:36:21 +0200 | [diff] [blame] | 71 | else if (!memcmp(line, "commit ", 7)) |
| 72 | p += 7; |
Paolo Bonzini | 580fb25 | 2010-04-19 10:46:14 +0200 | [diff] [blame] | 73 | else if (!memcmp(line, "From ", 5)) |
| 74 | p += 5; |
Michael J Gruber | 2485eab | 2011-02-17 08:44:42 +0100 | [diff] [blame] | 75 | else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line)) |
| 76 | continue; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 77 | |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 78 | if (!get_sha1_hex(p, next_sha1)) { |
| 79 | found_next = 1; |
| 80 | break; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Ignore commit comments */ |
| 84 | if (!patchlen && memcmp(line, "diff ", 5)) |
| 85 | continue; |
| 86 | |
Paolo Bonzini | 580fb25 | 2010-04-19 10:46:14 +0200 | [diff] [blame] | 87 | /* Parsing diff header? */ |
| 88 | if (before == -1) { |
| 89 | if (!memcmp(line, "index ", 6)) |
| 90 | continue; |
| 91 | else if (!memcmp(line, "--- ", 4)) |
| 92 | before = after = 1; |
| 93 | else if (!isalpha(line[0])) |
| 94 | break; |
| 95 | } |
Kai Ruemmler | 9fabded | 2005-10-09 16:52:50 -0700 | [diff] [blame] | 96 | |
Paolo Bonzini | 580fb25 | 2010-04-19 10:46:14 +0200 | [diff] [blame] | 97 | /* Looking for a valid hunk header? */ |
| 98 | if (before == 0 && after == 0) { |
| 99 | if (!memcmp(line, "@@ -", 4)) { |
| 100 | /* Parse next hunk, but ignore line numbers. */ |
| 101 | scan_hunk_header(line, &before, &after); |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | /* Split at the end of the patch. */ |
| 106 | if (memcmp(line, "diff ", 5)) |
| 107 | break; |
| 108 | |
| 109 | /* Else we're parsing another header. */ |
| 110 | before = after = -1; |
| 111 | } |
| 112 | |
| 113 | /* If we get here, we're inside a hunk. */ |
| 114 | if (line[0] == '-' || line[0] == ' ') |
| 115 | before--; |
| 116 | if (line[0] == '+' || line[0] == ' ') |
| 117 | after--; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 118 | |
| 119 | /* Compute the sha without whitespace */ |
| 120 | len = remove_space(line); |
| 121 | patchlen += len; |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 122 | git_SHA1_Update(ctx, line, len); |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 123 | } |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 124 | |
| 125 | if (!found_next) |
| 126 | hashclr(next_sha1); |
| 127 | |
| 128 | return patchlen; |
| 129 | } |
| 130 | |
| 131 | static void generate_id_list(void) |
| 132 | { |
| 133 | unsigned char sha1[20], n[20]; |
| 134 | git_SHA_CTX ctx; |
| 135 | int patchlen; |
Michael Schubert | b9ab810 | 2011-09-21 14:42:22 +0200 | [diff] [blame] | 136 | struct strbuf line_buf = STRBUF_INIT; |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 137 | |
| 138 | git_SHA1_Init(&ctx); |
| 139 | hashclr(sha1); |
| 140 | while (!feof(stdin)) { |
Michael Schubert | b9ab810 | 2011-09-21 14:42:22 +0200 | [diff] [blame] | 141 | patchlen = get_one_patchid(n, &ctx, &line_buf); |
Paolo Bonzini | 9ae144f | 2010-04-19 10:46:13 +0200 | [diff] [blame] | 142 | flush_current_id(patchlen, sha1, &ctx); |
| 143 | hashcpy(sha1, n); |
| 144 | } |
Michael Schubert | b9ab810 | 2011-09-21 14:42:22 +0200 | [diff] [blame] | 145 | strbuf_release(&line_buf); |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alexander Potashev | 34263de | 2009-01-04 21:39:27 +0300 | [diff] [blame] | 148 | static const char patch_id_usage[] = "git patch-id < patch"; |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 149 | |
Linus Torvalds | dedc0ec | 2010-01-21 20:31:25 -0800 | [diff] [blame] | 150 | int cmd_patch_id(int argc, const char **argv, const char *prefix) |
Linus Torvalds | f976722 | 2005-06-23 15:06:04 -0700 | [diff] [blame] | 151 | { |
| 152 | if (argc != 1) |
| 153 | usage(patch_id_usage); |
| 154 | |
| 155 | generate_id_list(); |
| 156 | return 0; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 157 | } |