7 #include "parse-options.h"
10 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
13 printf("%s %s\n", oid_to_hex(result
), oid_to_hex(id
));
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
);
40 n
= strspn(q
, digits
);
45 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
49 n
= strspn(r
, digits
);
53 n
= strspn(r
, digits
);
63 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
64 struct strbuf
*line_buf
, int stable
, int verbatim
)
66 int patchlen
= 0, found_next
= 0;
67 int before
= -1, after
= -1;
68 int diff_is_binary
= 0;
69 char pre_oid_str
[GIT_MAX_HEXSZ
+ 1], post_oid_str
[GIT_MAX_HEXSZ
+ 1];
72 the_hash_algo
->init_fn(&ctx
);
73 oidclr(result
, the_repository
->hash_algo
);
75 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
76 char *line
= line_buf
->buf
;
80 /* Possibly skip over the prefix added by "log" or "format-patch" */
81 if (!skip_prefix(line
, "commit ", &p
) &&
82 !skip_prefix(line
, "From ", &p
) &&
83 starts_with(line
, "\\ ") && 12 < strlen(line
)) {
85 the_hash_algo
->update_fn(&ctx
, line
, strlen(line
));
89 if (!get_oid_hex(p
, next_oid
)) {
94 /* Ignore commit comments */
95 if (!patchlen
&& !starts_with(line
, "diff "))
98 /* Parsing diff header? */
100 if (starts_with(line
, "GIT binary patch") ||
101 starts_with(line
, "Binary files")) {
104 the_hash_algo
->update_fn(&ctx
, pre_oid_str
,
105 strlen(pre_oid_str
));
106 the_hash_algo
->update_fn(&ctx
, post_oid_str
,
107 strlen(post_oid_str
));
109 flush_one_hunk(result
, &ctx
);
111 } else if (skip_prefix(line
, "index ", &p
)) {
112 char *oid1_end
= strstr(line
, "..");
113 char *oid2_end
= NULL
;
115 oid2_end
= strstr(oid1_end
, " ");
117 oid2_end
= line
+ strlen(line
) - 1;
118 if (oid1_end
!= NULL
&& oid2_end
!= NULL
) {
119 *oid1_end
= *oid2_end
= '\0';
120 strlcpy(pre_oid_str
, p
, GIT_MAX_HEXSZ
+ 1);
121 strlcpy(post_oid_str
, oid1_end
+ 2, GIT_MAX_HEXSZ
+ 1);
124 } else if (starts_with(line
, "--- "))
126 else if (!isalpha(line
[0]))
130 if (diff_is_binary
) {
131 if (starts_with(line
, "diff ")) {
138 /* Looking for a valid hunk header? */
139 if (before
== 0 && after
== 0) {
140 if (starts_with(line
, "@@ -")) {
141 /* Parse next hunk, but ignore line numbers. */
142 scan_hunk_header(line
, &before
, &after
);
146 /* Split at the end of the patch. */
147 if (!starts_with(line
, "diff "))
150 /* Else we're parsing another header. */
152 flush_one_hunk(result
, &ctx
);
156 /* If we get here, we're inside a hunk. */
157 if (line
[0] == '-' || line
[0] == ' ')
159 if (line
[0] == '+' || line
[0] == ' ')
162 /* Add line to hash algo (possibly removing whitespace) */
163 len
= verbatim
? strlen(line
) : remove_space(line
);
165 the_hash_algo
->update_fn(&ctx
, line
, len
);
169 oidclr(next_oid
, the_repository
->hash_algo
);
171 flush_one_hunk(result
, &ctx
);
176 static void generate_id_list(int stable
, int verbatim
)
178 struct object_id oid
, n
, result
;
180 struct strbuf line_buf
= STRBUF_INIT
;
182 oidclr(&oid
, the_repository
->hash_algo
);
183 while (!feof(stdin
)) {
184 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
, verbatim
);
185 flush_current_id(patchlen
, &oid
, &result
);
188 strbuf_release(&line_buf
);
191 static const char *const patch_id_usage
[] = {
192 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
195 struct patch_id_opts
{
200 static int git_patch_id_config(const char *var
, const char *value
,
201 const struct config_context
*ctx
, void *cb
)
203 struct patch_id_opts
*opts
= cb
;
205 if (!strcmp(var
, "patchid.stable")) {
206 opts
->stable
= git_config_bool(var
, value
);
209 if (!strcmp(var
, "patchid.verbatim")) {
210 opts
->verbatim
= git_config_bool(var
, value
);
214 return git_default_config(var
, value
, ctx
, cb
);
217 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
219 /* if nothing is set, default to unstable */
220 struct patch_id_opts config
= {0, 0};
222 struct option builtin_patch_id_options
[] = {
223 OPT_CMDMODE(0, "unstable", &opts
,
224 N_("use the unstable patch-id algorithm"), 1),
225 OPT_CMDMODE(0, "stable", &opts
,
226 N_("use the stable patch-id algorithm"), 2),
227 OPT_CMDMODE(0, "verbatim", &opts
,
228 N_("don't strip whitespace from the patch"), 3),
232 git_config(git_patch_id_config
, &config
);
234 /* verbatim implies stable */
238 argc
= parse_options(argc
, argv
, prefix
, builtin_patch_id_options
,
242 * We rely on `the_hash_algo` to compute patch IDs. This is dubious as
243 * it means that the hash algorithm now depends on the object hash of
244 * the repository, even though git-patch-id(1) clearly defines that
245 * patch IDs always use SHA1.
247 * NEEDSWORK: This hack should be removed in favor of converting
248 * the code that computes patch IDs to always use SHA1.
251 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
253 generate_id_list(opts
? opts
> 1 : config
.stable
,
254 opts
? opts
== 3 : config
.verbatim
);