6 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
8 char name[GIT_MAX_HEXSZ + 1];
13 memcpy(name, oid_to_hex(id), the_hash_algo->hexsz + 1);
14 printf("%s %s\n", oid_to_hex(result), name);
17 static int remove_space(char *line)
23 while ((c = *src++) != '\0') {
30 static int scan_hunk_header(const char *p, int *p_before, int *p_after)
32 static const char digits[] = "0123456789";
37 n = strspn(q, digits);
40 n = strspn(q, digits);
42 if (n == 0 || q[n] != ' ' || q[n+1] != '+')
46 n = strspn(r, digits);
49 n = strspn(r, digits);
59 static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
60 struct strbuf *line_buf, int stable)
62 int patchlen = 0, found_next = 0;
63 int before = -1, after = -1;
66 the_hash_algo->init_fn(&ctx);
69 while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
70 char *line = line_buf->buf;
74 if (!skip_prefix(line, "diff-tree ", &p) &&
75 !skip_prefix(line, "commit ", &p) &&
76 !skip_prefix(line, "From ", &p) &&
77 starts_with(line, "\\ ") && 12 < strlen(line))
80 if (!get_oid_hex(p, next_oid)) {
85 /* Ignore commit comments */
86 if (!patchlen && !starts_with(line, "diff "))
89 /* Parsing diff header? */
91 if (starts_with(line, "index "))
93 else if (starts_with(line, "--- "))
95 else if (!isalpha(line[0]))
99 /* Looking for a valid hunk header? */
100 if (before == 0 && after == 0) {
101 if (starts_with(line, "@@ -")) {
102 /* Parse next hunk, but ignore line numbers. */
103 scan_hunk_header(line, &before, &after);
107 /* Split at the end of the patch. */
108 if (!starts_with(line, "diff "))
111 /* Else we're parsing another header. */
113 flush_one_hunk(result, &ctx);
117 /* If we get here, we're inside a hunk. */
118 if (line[0] == '-' || line[0] == ' ')
120 if (line[0] == '+' || line[0] == ' ')
123 /* Compute the sha without whitespace */
124 len = remove_space(line);
126 the_hash_algo->update_fn(&ctx, line, len);
132 flush_one_hunk(result, &ctx);
137 static void generate_id_list(int stable)
139 struct object_id oid, n, result;
141 struct strbuf line_buf = STRBUF_INIT;
144 while (!feof(stdin)) {
145 patchlen = get_one_patchid(&n, &result, &line_buf, stable);
146 flush_current_id(patchlen, &oid, &result);
149 strbuf_release(&line_buf);
152 static const char patch_id_usage[] = "git patch-id [--stable | --unstable]";
154 static int git_patch_id_config(const char *var, const char *value, void *cb)
158 if (!strcmp(var, "patchid.stable")) {
159 *stable = git_config_bool(var, value);
163 return git_default_config(var, value, cb);
166 int cmd_patch_id(int argc, const char **argv, const char *prefix)
170 git_config(git_patch_id_config, &stable);
172 /* If nothing is set, default to unstable. */
176 if (argc == 2 && !strcmp(argv[1], "--stable"))
178 else if (argc == 2 && !strcmp(argv[1], "--unstable"))
181 usage(patch_id_usage);
183 generate_id_list(stable);