3 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
10 memcpy(name, oid_to_hex(id), GIT_SHA1_HEXSZ + 1);
11 printf("%s %s\n", oid_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(struct object_id *result, git_SHA_CTX *ctx)
58 unsigned char hash[GIT_MAX_RAWSZ];
59 unsigned short carry = 0;
62 git_SHA1_Final(hash, ctx);
64 /* 20-byte sum, with carry */
65 for (i = 0; i < GIT_SHA1_RAWSZ; ++i) {
66 carry += result->hash[i] + hash[i];
67 result->hash[i] = carry;
72 static int get_one_patchid(struct object_id *next_oid, struct object_id *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 (!skip_prefix(line, "diff-tree ", &p) &&
88 !skip_prefix(line, "commit ", &p) &&
89 !skip_prefix(line, "From ", &p) &&
90 starts_with(line, "\\ ") && 12 < strlen(line))
93 if (!get_oid_hex(p, next_oid)) {
98 /* Ignore commit comments */
99 if (!patchlen && !starts_with(line, "diff "))
102 /* Parsing diff header? */
104 if (starts_with(line, "index "))
106 else if (starts_with(line, "--- "))
108 else if (!isalpha(line[0]))
112 /* Looking for a valid hunk header? */
113 if (before == 0 && after == 0) {
114 if (starts_with(line, "@@ -")) {
115 /* Parse next hunk, but ignore line numbers. */
116 scan_hunk_header(line, &before, &after);
120 /* Split at the end of the patch. */
121 if (!starts_with(line, "diff "))
124 /* Else we're parsing another header. */
126 flush_one_hunk(result, &ctx);
130 /* If we get here, we're inside a hunk. */
131 if (line[0] == '-' || line[0] == ' ')
133 if (line[0] == '+' || line[0] == ' ')
136 /* Compute the sha without whitespace */
137 len = remove_space(line);
139 git_SHA1_Update(&ctx, line, len);
145 flush_one_hunk(result, &ctx);
150 static void generate_id_list(int stable)
152 struct object_id oid, n, result;
154 struct strbuf line_buf = STRBUF_INIT;
157 while (!feof(stdin)) {
158 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
159 flush_current_id(patchlen, &oid, &result);
162 strbuf_release(&line_buf);
165 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
167 static int git_patch_id_config(const char *var, const char *value, void *cb)
171 if (!strcmp(var, "patchid.stable")) {
172 *stable = git_config_bool(var, value);
176 return git_default_config(var, value, cb);
179 int cmd_patch_id(int argc, const char **argv, const char *prefix)
183 git_config(git_patch_id_config, &stable);
185 /* If nothing is set, default to unstable. */
189 if (argc == 2 && !strcmp(argv[1], "--stable"))
191 else if (argc == 2 && !strcmp(argv[1], "--unstable"))
194 usage(patch_id_usage);
196 generate_id_list(stable);