1#include "cache.h"2#include "exec_cmd.h"34static void flush_current_id(int patchlen, unsigned char *id, git_SHA_CTX *c)5{6unsigned char result[20];7char name[50];89if (!patchlen)10return;1112git_SHA1_Final(result, c);13memcpy(name, sha1_to_hex(id), 41);14printf("%s %s\n", sha1_to_hex(result), name);15git_SHA1_Init(c);16}1718static int remove_space(char *line)19{20char *src = line;21char *dst = line;22unsigned char c;2324while ((c = *src++) != '\0') {25if (!isspace(c))26*dst++ = c;27}28return dst - line;29}3031static int scan_hunk_header(const char *p, int *p_before, int *p_after)32{33static const char digits[] = "0123456789";34const char *q, *r;35int n;3637q = p + 4;38n = strspn(q, digits);39if (q[n] == ',') {40q += n + 1;41n = strspn(q, digits);42}43if (n == 0 || q[n] != ' ' || q[n+1] != '+')44return 0;4546r = q + n + 2;47n = strspn(r, digits);48if (r[n] == ',') {49r += n + 1;50n = strspn(r, digits);51}52if (n == 0)53return 0;5455*p_before = atoi(q);56*p_after = atoi(r);57return 1;58}5960int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)61{62static char line[1000];63int patchlen = 0, found_next = 0;64int before = -1, after = -1;6566while (fgets(line, sizeof(line), stdin) != NULL) {67char *p = line;68int len;6970if (!memcmp(line, "diff-tree ", 10))71p += 10;72else if (!memcmp(line, "commit ", 7))73p += 7;74else if (!memcmp(line, "From ", 5))75p += 5;76else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))77continue;7879if (!get_sha1_hex(p, next_sha1)) {80found_next = 1;81break;82}8384/* Ignore commit comments */85if (!patchlen && memcmp(line, "diff ", 5))86continue;8788/* Parsing diff header? */89if (before == -1) {90if (!memcmp(line, "index ", 6))91continue;92else if (!memcmp(line, "--- ", 4))93before = after = 1;94else if (!isalpha(line[0]))95break;96}9798/* Looking for a valid hunk header? */99if (before == 0 && after == 0) {100if (!memcmp(line, "@@ -", 4)) {101/* Parse next hunk, but ignore line numbers. */102scan_hunk_header(line, &before, &after);103continue;104}105106/* Split at the end of the patch. */107if (memcmp(line, "diff ", 5))108break;109110/* Else we're parsing another header. */111before = after = -1;112}113114/* If we get here, we're inside a hunk. */115if (line[0] == '-' || line[0] == ' ')116before--;117if (line[0] == '+' || line[0] == ' ')118after--;119120/* Compute the sha without whitespace */121len = remove_space(line);122patchlen += len;123git_SHA1_Update(ctx, line, len);124}125126if (!found_next)127hashclr(next_sha1);128129return patchlen;130}131132static void generate_id_list(void)133{134unsigned char sha1[20], n[20];135git_SHA_CTX ctx;136int patchlen;137138git_SHA1_Init(&ctx);139hashclr(sha1);140while (!feof(stdin)) {141patchlen = get_one_patchid(n, &ctx);142flush_current_id(patchlen, sha1, &ctx);143hashcpy(sha1, n);144}145}146147static const char patch_id_usage[] = "git patch-id < patch";148149int cmd_patch_id(int argc, const char **argv, const char *prefix)150{151if (argc != 1)152usage(patch_id_usage);153154generate_id_list();155return 0;156}