6 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
9 printf("%s %s\n", oid_to_hex(result), oid_to_hex(id));
12 static int remove_space(char *line)
18 while ((c = *src++) != '\0') {
25 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
27 static const char digits[] = "0123456789";
32 n = strspn(q, digits);
35 n = strspn(q, digits);
37 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
41 n = strspn(r, digits);
44 n = strspn(r, digits);
54 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
55 struct strbuf *line_buf, int stable)
57 int patchlen = 0, found_next = 0;
58 int before = -1, after = -1;
61 the_hash_algo->init_fn(&ctx);
64 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
65 char *line = line_buf->buf;
69 if (!skip_prefix(line, "diff-tree ", &p) &&
70 !skip_prefix(line, "commit ", &p) &&
71 !skip_prefix(line, "From ", &p) &&
72 starts_with(line, "\\ ") && 12 < strlen(line))
75 if (!get_oid_hex(p, next_oid)) {
80 /* Ignore commit comments */
81 if (!patchlen && !starts_with(line, "diff "))
84 /* Parsing diff header? */
86 if (starts_with(line, "index "))
88 else if (starts_with(line, "--- "))
90 else if (!isalpha(line[0]))
94 /* Looking for a valid hunk header? */
95 if (before == 0 && after == 0) {
96 if (starts_with(line, "@@ -")) {
97 /* Parse next hunk, but ignore line numbers. */
98 scan_hunk_header(line, &before, &after);
102 /* Split at the end of the patch. */
103 if (!starts_with(line, "diff "))
106 /* Else we're parsing another header. */
108 flush_one_hunk(result, &ctx);
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 the_hash_algo->update_fn(&ctx, line, len);
127 flush_one_hunk(result, &ctx);
132 static void generate_id_list(int stable)
134 struct object_id oid, n, result;
136 struct strbuf line_buf = STRBUF_INIT;
139 while (!feof(stdin)) {
140 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
141 flush_current_id(patchlen, &oid, &result);
144 strbuf_release(&line_buf);
147 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
149 static int git_patch_id_config(const char *var, const char *value, void *cb)
153 if (!strcmp(var, "patchid.stable")) {
154 *stable = git_config_bool(var, value);
158 return git_default_config(var, value, cb);
161 int cmd_patch_id(int argc, const char **argv, const char *prefix)
165 git_config(git_patch_id_config, &stable);
167 /* If nothing is set, default to unstable. */
171 if (argc == 2 && !strcmp(argv[1], "--stable"))
173 else if (argc == 2 && !strcmp(argv[1], "--unstable"))
176 usage(patch_id_usage);
178 generate_id_list(stable);