7 #include "parse-options.h"
9 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
12 printf("%s %s\n", oid_to_hex(result
), oid_to_hex(id
));
15 static int remove_space(char *line
)
21 while ((c
= *src
++) != '\0') {
28 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
30 static const char digits
[] = "0123456789";
35 n
= strspn(q
, digits
);
39 n
= strspn(q
, digits
);
44 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
48 n
= strspn(r
, digits
);
52 n
= strspn(r
, digits
);
62 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
63 struct strbuf
*line_buf
, int stable
, int verbatim
)
65 int patchlen
= 0, found_next
= 0;
66 int before
= -1, after
= -1;
67 int diff_is_binary
= 0;
68 char pre_oid_str
[GIT_MAX_HEXSZ
+ 1], post_oid_str
[GIT_MAX_HEXSZ
+ 1];
71 the_hash_algo
->init_fn(&ctx
);
74 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
75 char *line
= line_buf
->buf
;
79 /* Possibly skip over the prefix added by "log" or "format-patch" */
80 if (!skip_prefix(line
, "commit ", &p
) &&
81 !skip_prefix(line
, "From ", &p
) &&
82 starts_with(line
, "\\ ") && 12 < strlen(line
)) {
84 the_hash_algo
->update_fn(&ctx
, line
, strlen(line
));
88 if (!get_oid_hex(p
, next_oid
)) {
93 /* Ignore commit comments */
94 if (!patchlen
&& !starts_with(line
, "diff "))
97 /* Parsing diff header? */
99 if (starts_with(line
, "GIT binary patch") ||
100 starts_with(line
, "Binary files")) {
103 the_hash_algo
->update_fn(&ctx
, pre_oid_str
,
104 strlen(pre_oid_str
));
105 the_hash_algo
->update_fn(&ctx
, post_oid_str
,
106 strlen(post_oid_str
));
108 flush_one_hunk(result
, &ctx
);
110 } else if (skip_prefix(line
, "index ", &p
)) {
111 char *oid1_end
= strstr(line
, "..");
112 char *oid2_end
= NULL
;
114 oid2_end
= strstr(oid1_end
, " ");
116 oid2_end
= line
+ strlen(line
) - 1;
117 if (oid1_end
!= NULL
&& oid2_end
!= NULL
) {
118 *oid1_end
= *oid2_end
= '\0';
119 strlcpy(pre_oid_str
, p
, GIT_MAX_HEXSZ
+ 1);
120 strlcpy(post_oid_str
, oid1_end
+ 2, GIT_MAX_HEXSZ
+ 1);
123 } else if (starts_with(line
, "--- "))
125 else if (!isalpha(line
[0]))
129 if (diff_is_binary
) {
130 if (starts_with(line
, "diff ")) {
137 /* Looking for a valid hunk header? */
138 if (before
== 0 && after
== 0) {
139 if (starts_with(line
, "@@ -")) {
140 /* Parse next hunk, but ignore line numbers. */
141 scan_hunk_header(line
, &before
, &after
);
145 /* Split at the end of the patch. */
146 if (!starts_with(line
, "diff "))
149 /* Else we're parsing another header. */
151 flush_one_hunk(result
, &ctx
);
155 /* If we get here, we're inside a hunk. */
156 if (line
[0] == '-' || line
[0] == ' ')
158 if (line
[0] == '+' || line
[0] == ' ')
161 /* Add line to hash algo (possibly removing whitespace) */
162 len
= verbatim
? strlen(line
) : remove_space(line
);
164 the_hash_algo
->update_fn(&ctx
, line
, len
);
170 flush_one_hunk(result
, &ctx
);
175 static void generate_id_list(int stable
, int verbatim
)
177 struct object_id oid
, n
, result
;
179 struct strbuf line_buf
= STRBUF_INIT
;
182 while (!feof(stdin
)) {
183 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
, verbatim
);
184 flush_current_id(patchlen
, &oid
, &result
);
187 strbuf_release(&line_buf
);
190 static const char *const patch_id_usage
[] = {
191 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
194 struct patch_id_opts
{
199 static int git_patch_id_config(const char *var
, const char *value
,
200 const struct config_context
*ctx
, void *cb
)
202 struct patch_id_opts
*opts
= cb
;
204 if (!strcmp(var
, "patchid.stable")) {
205 opts
->stable
= git_config_bool(var
, value
);
208 if (!strcmp(var
, "patchid.verbatim")) {
209 opts
->verbatim
= git_config_bool(var
, value
);
213 return git_default_config(var
, value
, ctx
, cb
);
216 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
218 /* if nothing is set, default to unstable */
219 struct patch_id_opts config
= {0, 0};
221 struct option builtin_patch_id_options
[] = {
222 OPT_CMDMODE(0, "unstable", &opts
,
223 N_("use the unstable patch-id algorithm"), 1),
224 OPT_CMDMODE(0, "stable", &opts
,
225 N_("use the stable patch-id algorithm"), 2),
226 OPT_CMDMODE(0, "verbatim", &opts
,
227 N_("don't strip whitespace from the patch"), 3),
231 git_config(git_patch_id_config
, &config
);
233 /* verbatim implies stable */
237 argc
= parse_options(argc
, argv
, prefix
, builtin_patch_id_options
,
240 generate_id_list(opts
? opts
> 1 : config
.stable
,
241 opts
? opts
== 3 : config
.verbatim
);