3 static void flush_current_id(int patchlen, unsigned char *id, unsigned char *result)
10 memcpy(name, sha1_to_hex(id), 41);
11 printf("%s %s\n", sha1_to_hex(result), name);
14 static int remove_space(char *line)
20 while ((c = *src++) != '\0') {
27 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
29 static const char digits[] = "0123456789";
34 n = strspn(q, digits);
37 n = strspn(q, digits);
39 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
43 n = strspn(r, digits);
46 n = strspn(r, digits);
56 static void flush_one_hunk(unsigned char *result, git_SHA_CTX *ctx)
58 unsigned char hash[20];
59 unsigned short carry = 0;
62 git_SHA1_Final(hash, ctx);
64 /* 20-byte sum, with carry */
65 for (i = 0; i < 20; ++i) {
66 carry += result[i] + hash[i];
72 static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
73 struct strbuf *line_buf, int stable)
75 int patchlen = 0, found_next = 0;
76 int before = -1, after = -1;
82 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
83 char *line = line_buf->buf;
87 if (!memcmp(line, "diff-tree ", 10))
89 else if (!memcmp(line, "commit ", 7))
91 else if (!memcmp(line, "From ", 5))
93 else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
96 if (!get_sha1_hex(p, next_sha1)) {
101 /* Ignore commit comments */
102 if (!patchlen && memcmp(line, "diff ", 5))
105 /* Parsing diff header? */
107 if (!memcmp(line, "index ", 6))
109 else if (!memcmp(line, "--- ", 4))
111 else if (!isalpha(line[0]))
115 /* Looking for a valid hunk header? */
116 if (before == 0 && after == 0) {
117 if (!memcmp(line, "@@ -", 4)) {
118 /* Parse next hunk, but ignore line numbers. */
119 scan_hunk_header(line, &before, &after);
123 /* Split at the end of the patch. */
124 if (memcmp(line, "diff ", 5))
127 /* Else we're parsing another header. */
129 flush_one_hunk(result, &ctx);
133 /* If we get here, we're inside a hunk. */
134 if (line[0] == '-' || line[0] == ' ')
136 if (line[0] == '+' || line[0] == ' ')
139 /* Compute the sha without whitespace */
140 len = remove_space(line);
142 git_SHA1_Update(&ctx, line, len);
148 flush_one_hunk(result, &ctx);
153 static void generate_id_list(int stable)
155 unsigned char sha1[20], n[20], result[20];
157 struct strbuf line_buf = STRBUF_INIT;
160 while (!feof(stdin)) {
161 patchlen = get_one_patchid(n, result, &line_buf, stable);
162 flush_current_id(patchlen, sha1, result);
165 strbuf_release(&line_buf);
168 static const char patch_id_usage[] = "git patch-id [--stable | --unstable] < patch";
170 static int git_patch_id_config(const char *var, const char *value, void *cb)
174 if (!strcmp(var, "patchid.stable")) {
175 *stable = git_config_bool(var, value);
179 return git_default_config(var, value, cb);
182 int cmd_patch_id(int argc, const char **argv, const char *prefix)
186 git_config(git_patch_id_config, &stable);
188 /* If nothing is set, default to unstable. */
192 if (argc == 2 && !strcmp(argv[1], "--stable"))
194 else if (argc == 2 && !strcmp(argv[1], "--unstable"))
197 usage(patch_id_usage);
199 generate_id_list(stable);