5 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
12 memcpy(name
, oid_to_hex(id
), GIT_SHA1_HEXSZ
+ 1);
13 printf("%s %s\n", oid_to_hex(result
), name
);
16 static int remove_space(char *line
)
22 while ((c
= *src
++) != '\0') {
29 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
31 static const char digits
[] = "0123456789";
36 n
= strspn(q
, digits
);
39 n
= strspn(q
, digits
);
41 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
45 n
= strspn(r
, digits
);
48 n
= strspn(r
, digits
);
58 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
59 struct strbuf
*line_buf
, int stable
)
61 int patchlen
= 0, found_next
= 0;
62 int before
= -1, after
= -1;
68 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
69 char *line
= line_buf
->buf
;
73 if (!skip_prefix(line
, "diff-tree ", &p
) &&
74 !skip_prefix(line
, "commit ", &p
) &&
75 !skip_prefix(line
, "From ", &p
) &&
76 starts_with(line
, "\\ ") && 12 < strlen(line
))
79 if (!get_oid_hex(p
, next_oid
)) {
84 /* Ignore commit comments */
85 if (!patchlen
&& !starts_with(line
, "diff "))
88 /* Parsing diff header? */
90 if (starts_with(line
, "index "))
92 else if (starts_with(line
, "--- "))
94 else if (!isalpha(line
[0]))
98 /* Looking for a valid hunk header? */
99 if (before
== 0 && after
== 0) {
100 if (starts_with(line
, "@@ -")) {
101 /* Parse next hunk, but ignore line numbers. */
102 scan_hunk_header(line
, &before
, &after
);
106 /* Split at the end of the patch. */
107 if (!starts_with(line
, "diff "))
110 /* Else we're parsing another header. */
112 flush_one_hunk(result
, &ctx
);
116 /* If we get here, we're inside a hunk. */
117 if (line
[0] == '-' || line
[0] == ' ')
119 if (line
[0] == '+' || line
[0] == ' ')
122 /* Compute the sha without whitespace */
123 len
= remove_space(line
);
125 git_SHA1_Update(&ctx
, line
, len
);
131 flush_one_hunk(result
, &ctx
);
136 static void generate_id_list(int stable
)
138 struct object_id oid
, n
, result
;
140 struct strbuf line_buf
= STRBUF_INIT
;
143 while (!feof(stdin
)) {
144 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
);
145 flush_current_id(patchlen
, &oid
, &result
);
148 strbuf_release(&line_buf
);
151 static const char patch_id_usage
[] = "git patch-id [--stable | --unstable]";
153 static int git_patch_id_config(const char *var
, const char *value
, void *cb
)
157 if (!strcmp(var
, "patchid.stable")) {
158 *stable
= git_config_bool(var
, value
);
162 return git_default_config(var
, value
, cb
);
165 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
169 git_config(git_patch_id_config
, &stable
);
171 /* If nothing is set, default to unstable. */
175 if (argc
== 2 && !strcmp(argv
[1], "--stable"))
177 else if (argc
== 2 && !strcmp(argv
[1], "--unstable"))
180 usage(patch_id_usage
);
182 generate_id_list(stable
);