7
#include "parse-options.h"
10
static void flush_current_id(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);
72
GOPID_STABLE = (1<<0),
73
GOPID_VERBATIM = (1<<1),
74
GOPID_FIND_HEADER = (1<<2),
77
static int get_one_patchid(struct object_id *next_oid, struct object_id *result,
78
struct strbuf *line_buf, unsigned flags)
80
int stable = flags & GOPID_STABLE;
81
int verbatim = flags & GOPID_VERBATIM;
82
int find_header = flags & GOPID_FIND_HEADER;
83
int patchlen = 0, found_next = 0;
84
int before = -1, after = -1;
85
int diff_is_binary = 0;
86
char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1];
89
the_hash_algo->init_fn(&ctx);
90
oidclr(result, the_repository->hash_algo);
92
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
93
char *line = line_buf->buf;
109
if (skip_prefix(line, "commit ", &p))
111
else if (skip_prefix(line, "From ", &p))
113
if (!get_oid_hex(p, next_oid)) {
115
the_hash_algo->update_fn(&ctx, line, strlen(line));
122
if (!patchlen && !starts_with(line, "diff "))
133
if (starts_with(line, "GIT binary patch") ||
134
starts_with(line, "Binary files")) {
137
the_hash_algo->update_fn(&ctx, pre_oid_str,
138
strlen(pre_oid_str));
139
the_hash_algo->update_fn(&ctx, post_oid_str,
140
strlen(post_oid_str));
142
flush_one_hunk(result, &ctx);
144
} else if (skip_prefix(line, "index ", &p)) {
145
char *oid1_end = strstr(line, "..");
146
char *oid2_end = NULL;
148
oid2_end = strstr(oid1_end, " ");
150
oid2_end = line + strlen(line) - 1;
151
if (oid1_end != NULL && oid2_end != NULL) {
152
*oid1_end = *oid2_end = '\0';
153
strlcpy(pre_oid_str, p, GIT_MAX_HEXSZ + 1);
154
strlcpy(post_oid_str, oid1_end + 2, GIT_MAX_HEXSZ + 1);
157
} else if (starts_with(line, "--- "))
159
else if (!isalpha(line[0]))
167
if (starts_with(line, "\\ ") && 12 < strlen(line)) {
169
the_hash_algo->update_fn(&ctx, line, strlen(line));
173
if (diff_is_binary) {
174
if (starts_with(line, "diff ")) {
182
if (before == 0 && after == 0) {
183
if (starts_with(line, "@@ -")) {
185
scan_hunk_header(line, &before, &after);
190
if (!starts_with(line, "diff "))
195
flush_one_hunk(result, &ctx);
200
if (line[0] == '-' || line[0] == ' ')
202
if (line[0] == '+' || line[0] == ' ')
206
len = verbatim ? strlen(line) : remove_space(line);
208
the_hash_algo->update_fn(&ctx, line, len);
212
oidclr(next_oid, the_repository->hash_algo);
214
flush_one_hunk(result, &ctx);
219
static void generate_id_list(unsigned flags)
221
struct object_id oid, n, result;
223
struct strbuf line_buf = STRBUF_INIT;
225
oidclr(&oid, the_repository->hash_algo);
226
flags |= GOPID_FIND_HEADER;
227
while (!feof(stdin)) {
228
patchlen = get_one_patchid(&n, &result, &line_buf, flags);
230
flush_current_id(&oid, &result);
232
flags &= ~GOPID_FIND_HEADER;
234
strbuf_release(&line_buf);
237
static const char *const patch_id_usage[] = {
238
N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
241
struct patch_id_opts {
246
static int git_patch_id_config(const char *var, const char *value,
247
const struct config_context *ctx, void *cb)
249
struct patch_id_opts *opts = cb;
251
if (!strcmp(var, "patchid.stable")) {
252
opts->stable = git_config_bool(var, value);
255
if (!strcmp(var, "patchid.verbatim")) {
256
opts->verbatim = git_config_bool(var, value);
260
return git_default_config(var, value, ctx, cb);
263
int cmd_patch_id(int argc, const char **argv, const char *prefix)
266
struct patch_id_opts config = {0, 0};
269
struct option builtin_patch_id_options[] = {
270
OPT_CMDMODE(0, "unstable", &opts,
271
N_("use the unstable patch-id algorithm"), 1),
272
OPT_CMDMODE(0, "stable", &opts,
273
N_("use the stable patch-id algorithm"), 2),
274
OPT_CMDMODE(0, "verbatim", &opts,
275
N_("don't strip whitespace from the patch"), 3),
279
git_config(git_patch_id_config, &config);
285
argc = parse_options(argc, argv, prefix, builtin_patch_id_options,
298
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
300
if (opts ? opts > 1 : config.stable)
301
flags |= GOPID_STABLE;
302
if (opts ? opts == 3 : config.verbatim)
303
flags |= GOPID_VERBATIM;
304
generate_id_list(flags);