]>
Commit | Line | Data |
---|---|---|
d812c3b6 | 1 | #include "git-compat-util.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
177722b3 | 4 | #include "commit-graph.h" |
7ee24e18 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
6a1a79fd | 8 | #include "repository.h" |
dabab1d6 | 9 | #include "object-name.h" |
a034e910 | 10 | #include "object-store-ll.h" |
ed09aef0 | 11 | #include "pkt-line.h" |
52883fbd | 12 | #include "utf8.h" |
199c45bf JH |
13 | #include "diff.h" |
14 | #include "revision.h" | |
a97a7468 | 15 | #include "notes.h" |
14ba97f8 | 16 | #include "alloc.h" |
ba3c69a9 | 17 | #include "gpg-interface.h" |
46905893 | 18 | #include "mergesort.h" |
a84b794a | 19 | #include "commit-slab.h" |
da24b104 | 20 | #include "prio-queue.h" |
bc626927 | 21 | #include "hash-lookup.h" |
d76650b8 | 22 | #include "wt-status.h" |
f9f99b3f | 23 | #include "advice.h" |
103148aa | 24 | #include "refs.h" |
02931217 | 25 | #include "commit-reach.h" |
49697cb7 | 26 | #include "run-command.h" |
e38da487 | 27 | #include "setup.h" |
120ad2b0 | 28 | #include "shallow.h" |
d4a4f929 | 29 | #include "tree.h" |
f443246b | 30 | #include "hook.h" |
7a5d6044 | 31 | #include "parse.h" |
175785e5 | 32 | |
82a75299 JH |
33 | static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **); |
34 | ||
60ab26de | 35 | int save_commit_buffer = 1; |
ab628588 | 36 | int no_graft_file_deprecated_advice; |
60ab26de | 37 | |
175785e5 DB |
38 | const char *commit_type = "commit"; |
39 | ||
d9a05e74 | 40 | struct commit *lookup_commit_reference_gently(struct repository *r, |
21e1ee8f | 41 | const struct object_id *oid, int quiet) |
961784ee | 42 | { |
d9a05e74 SB |
43 | struct object *obj = deref_tag(r, |
44 | parse_object(r, oid), | |
109cd76d | 45 | NULL, 0); |
961784ee LT |
46 | |
47 | if (!obj) | |
48 | return NULL; | |
6da43d93 | 49 | return object_as_type(obj, OBJ_COMMIT, quiet); |
f76412ed JH |
50 | } |
51 | ||
1f6c72fe | 52 | struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid) |
f76412ed | 53 | { |
1f6c72fe | 54 | return lookup_commit_reference_gently(r, oid, 0); |
961784ee LT |
55 | } |
56 | ||
bc83266a | 57 | struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name) |
baf18fc2 | 58 | { |
2122f675 | 59 | struct commit *c = lookup_commit_reference(the_repository, oid); |
baf18fc2 NTND |
60 | if (!c) |
61 | die(_("could not parse %s"), ref_name); | |
9001dc2a | 62 | if (!oideq(oid, &c->object.oid)) { |
baf18fc2 | 63 | warning(_("%s %s is not a commit!"), |
bc83266a | 64 | ref_name, oid_to_hex(oid)); |
baf18fc2 NTND |
65 | } |
66 | return c; | |
67 | } | |
68 | ||
b8dbfd03 PW |
69 | struct commit *lookup_commit_object(struct repository *r, |
70 | const struct object_id *oid) | |
71 | { | |
72 | struct object *obj = parse_object(r, oid); | |
73 | return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL; | |
74 | ||
75 | } | |
76 | ||
bacf1687 | 77 | struct commit *lookup_commit(struct repository *r, const struct object_id *oid) |
175785e5 | 78 | { |
d0229abd | 79 | struct object *obj = lookup_object(r, oid); |
d36f51c1 | 80 | if (!obj) |
a378509e | 81 | return create_object(r, oid, alloc_commit_node(r)); |
6da43d93 | 82 | return object_as_type(obj, OBJ_COMMIT, 0); |
175785e5 DB |
83 | } |
84 | ||
a6fa5992 PN |
85 | struct commit *lookup_commit_reference_by_name(const char *name) |
86 | { | |
7683e2e6 | 87 | struct object_id oid; |
a6fa5992 PN |
88 | struct commit *commit; |
89 | ||
d850b7a5 | 90 | if (repo_get_oid_committish(the_repository, name, &oid)) |
a6fa5992 | 91 | return NULL; |
2122f675 | 92 | commit = lookup_commit_reference(the_repository, &oid); |
ecb5091f | 93 | if (repo_parse_commit(the_repository, commit)) |
a6fa5992 PN |
94 | return NULL; |
95 | return commit; | |
96 | } | |
97 | ||
dddbad72 | 98 | static timestamp_t parse_commit_date(const char *buf, const char *tail) |
175785e5 | 99 | { |
0a617799 | 100 | const char *dateptr; |
ea1615df | 101 | const char *eol; |
175785e5 | 102 | |
0a617799 MK |
103 | if (buf + 6 >= tail) |
104 | return 0; | |
175785e5 DB |
105 | if (memcmp(buf, "author", 6)) |
106 | return 0; | |
0a617799 | 107 | while (buf < tail && *buf++ != '\n') |
175785e5 | 108 | /* nada */; |
0a617799 MK |
109 | if (buf + 9 >= tail) |
110 | return 0; | |
175785e5 DB |
111 | if (memcmp(buf, "committer", 9)) |
112 | return 0; | |
ea1615df JK |
113 | |
114 | /* | |
115 | * Jump to end-of-line so that we can walk backwards to find the | |
116 | * end-of-email ">". This is more forgiving of malformed cases | |
117 | * because unexpected characters tend to be in the name and email | |
118 | * fields. | |
119 | */ | |
120 | eol = memchr(buf, '\n', tail - buf); | |
121 | if (!eol) | |
0a617799 | 122 | return 0; |
ea1615df JK |
123 | dateptr = eol; |
124 | while (dateptr > buf && dateptr[-1] != '>') | |
125 | dateptr--; | |
089d9adf | 126 | if (dateptr == buf) |
0a617799 | 127 | return 0; |
ea1615df | 128 | |
089d9adf JK |
129 | /* |
130 | * Trim leading whitespace, but make sure we have at least one | |
131 | * non-whitespace character, as parse_timestamp() will otherwise walk | |
132 | * right past the newline we found in "eol" when skipping whitespace | |
133 | * itself. | |
134 | * | |
135 | * In theory it would be sufficient to allow any character not matched | |
136 | * by isspace(), but there's a catch: our isspace() does not | |
137 | * necessarily match the behavior of parse_timestamp(), as the latter | |
138 | * is implemented by system routines which match more exotic control | |
139 | * codes, or even locale-dependent sequences. | |
140 | * | |
141 | * Since we expect the timestamp to be a number, we can check for that. | |
142 | * Anything else (e.g., a non-numeric token like "foo") would just | |
143 | * cause parse_timestamp() to return 0 anyway. | |
144 | */ | |
145 | while (dateptr < eol && isspace(*dateptr)) | |
146 | dateptr++; | |
147 | if (!isdigit(*dateptr) && *dateptr != '-') | |
148 | return 0; | |
149 | ||
150 | /* | |
151 | * We know there is at least one digit (or dash), so we'll begin | |
152 | * parsing there and stop at worst case at eol. | |
90ef0f14 JK |
153 | * |
154 | * Note that we may feed parse_timestamp() extra characters here if the | |
155 | * commit is malformed, and it will parse as far as it can. For | |
156 | * example, "123foo456" would return "123". That might be questionable | |
157 | * (versus returning "0"), but it would help in a hypothetical case | |
158 | * like "123456+0100", where the whitespace from the timezone is | |
159 | * missing. Since such syntactic errors may be baked into history and | |
160 | * hard to correct now, let's err on trying to make our best guess | |
161 | * here, rather than insist on perfect syntax. | |
089d9adf | 162 | */ |
1aeb7e75 | 163 | return parse_timestamp(dateptr, NULL, 10); |
175785e5 DB |
164 | } |
165 | ||
8380dcd7 | 166 | static const struct object_id *commit_graft_oid_access(size_t index, const void *table) |
0a9136f3 | 167 | { |
8380dcd7 | 168 | const struct commit_graft * const *commit_graft_table = table; |
45ee13b9 | 169 | return &commit_graft_table[index]->oid; |
0a9136f3 DD |
170 | } |
171 | ||
98c431b6 | 172 | int commit_graft_pos(struct repository *r, const struct object_id *oid) |
5da5c8f4 | 173 | { |
45ee13b9 JK |
174 | return oid_pos(oid, r->parsed_objects->grafts, |
175 | r->parsed_objects->grafts_nr, | |
176 | commit_graft_oid_access); | |
5da5c8f4 JH |
177 | } |
178 | ||
4d4e49ff JT |
179 | static void unparse_commit(struct repository *r, const struct object_id *oid) |
180 | { | |
181 | struct commit *c = lookup_commit(r, oid); | |
182 | ||
183 | if (!c->object.parsed) | |
184 | return; | |
185 | free_commit_list(c->parents); | |
186 | c->parents = NULL; | |
187 | c->object.parsed = 0; | |
188 | } | |
189 | ||
a3b78e83 BW |
190 | int register_commit_graft(struct repository *r, struct commit_graft *graft, |
191 | int ignore_dups) | |
5040f17e | 192 | { |
98c431b6 | 193 | int pos = commit_graft_pos(r, &graft->oid); |
a6080a0a | 194 | |
5040f17e JH |
195 | if (0 <= pos) { |
196 | if (ignore_dups) | |
197 | free(graft); | |
198 | else { | |
a3b78e83 BW |
199 | free(r->parsed_objects->grafts[pos]); |
200 | r->parsed_objects->grafts[pos] = graft; | |
5040f17e JH |
201 | } |
202 | return 1; | |
203 | } | |
204 | pos = -pos - 1; | |
a3b78e83 BW |
205 | ALLOC_GROW(r->parsed_objects->grafts, |
206 | r->parsed_objects->grafts_nr + 1, | |
207 | r->parsed_objects->grafts_alloc); | |
208 | r->parsed_objects->grafts_nr++; | |
209 | if (pos < r->parsed_objects->grafts_nr) | |
210 | memmove(r->parsed_objects->grafts + pos + 1, | |
211 | r->parsed_objects->grafts + pos, | |
212 | (r->parsed_objects->grafts_nr - pos - 1) * | |
213 | sizeof(*r->parsed_objects->grafts)); | |
214 | r->parsed_objects->grafts[pos] = graft; | |
4d4e49ff | 215 | unparse_commit(r, &graft->oid); |
5040f17e JH |
216 | return 0; |
217 | } | |
218 | ||
9a934032 | 219 | struct commit_graft *read_graft_line(struct strbuf *line) |
5040f17e JH |
220 | { |
221 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
cfa5bf16 PO |
222 | int i, phase; |
223 | const char *tail = NULL; | |
5040f17e | 224 | struct commit_graft *graft = NULL; |
cfa5bf16 | 225 | struct object_id dummy_oid, *oid; |
5040f17e | 226 | |
9a934032 PO |
227 | strbuf_rtrim(line); |
228 | if (!line->len || line->buf[0] == '#') | |
5bc4ce58 | 229 | return NULL; |
cfa5bf16 PO |
230 | /* |
231 | * phase 0 verifies line, counts hashes in line and allocates graft | |
232 | * phase 1 fills graft | |
233 | */ | |
234 | for (phase = 0; phase < 2; phase++) { | |
235 | oid = graft ? &graft->oid : &dummy_oid; | |
236 | if (parse_oid_hex(line->buf, oid, &tail)) | |
5040f17e | 237 | goto bad_graft_data; |
cfa5bf16 PO |
238 | for (i = 0; *tail != '\0'; i++) { |
239 | oid = graft ? &graft->parent[i] : &dummy_oid; | |
240 | if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail)) | |
241 | goto bad_graft_data; | |
242 | } | |
243 | if (!graft) { | |
244 | graft = xmalloc(st_add(sizeof(*graft), | |
245 | st_mult(sizeof(struct object_id), i))); | |
246 | graft->nr_parent = i; | |
247 | } | |
5040f17e JH |
248 | } |
249 | return graft; | |
df5d43be RT |
250 | |
251 | bad_graft_data: | |
9a934032 | 252 | error("bad graft data: %s", line->buf); |
cfa5bf16 | 253 | assert(!graft); |
df5d43be | 254 | return NULL; |
5040f17e JH |
255 | } |
256 | ||
d0e5dd0e | 257 | static int read_graft_file(struct repository *r, const char *graft_file) |
5da5c8f4 | 258 | { |
e9d983f1 | 259 | FILE *fp = fopen_or_warn(graft_file, "r"); |
e228c173 | 260 | struct strbuf buf = STRBUF_INIT; |
5040f17e JH |
261 | if (!fp) |
262 | return -1; | |
ab628588 ÆAB |
263 | if (!no_graft_file_deprecated_advice && |
264 | advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED)) | |
f9f99b3f JS |
265 | advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n" |
266 | "and will be removed in a future Git version.\n" | |
267 | "\n" | |
268 | "Please use \"git replace --convert-graft-file\"\n" | |
269 | "to convert the grafts into replace refs.\n" | |
270 | "\n" | |
271 | "Turn this message off by running\n" | |
272 | "\"git config advice.graftFileDeprecated false\"")); | |
e228c173 | 273 | while (!strbuf_getwholeline(&buf, fp, '\n')) { |
5da5c8f4 | 274 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
9a934032 | 275 | struct commit_graft *graft = read_graft_line(&buf); |
5bc4ce58 JH |
276 | if (!graft) |
277 | continue; | |
d0e5dd0e | 278 | if (register_commit_graft(r, graft, 1)) |
e228c173 | 279 | error("duplicate graft data: %s", buf.buf); |
5da5c8f4 JH |
280 | } |
281 | fclose(fp); | |
e228c173 | 282 | strbuf_release(&buf); |
5040f17e JH |
283 | return 0; |
284 | } | |
285 | ||
20fd6d57 | 286 | void prepare_commit_graft(struct repository *r) |
5040f17e | 287 | { |
5040f17e JH |
288 | char *graft_file; |
289 | ||
2f6c767f | 290 | if (r->parsed_objects->commit_graft_prepared) |
5040f17e | 291 | return; |
14a9bd28 JK |
292 | if (!startup_info->have_repository) |
293 | return; | |
294 | ||
2f6c767f SB |
295 | graft_file = get_graft_file(r); |
296 | read_graft_file(r, graft_file); | |
ed09aef0 | 297 | /* make sure shallows are read */ |
2f6c767f SB |
298 | is_repository_shallow(r); |
299 | r->parsed_objects->commit_graft_prepared = 1; | |
5da5c8f4 JH |
300 | } |
301 | ||
b9dbddf6 | 302 | struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid) |
5da5c8f4 JH |
303 | { |
304 | int pos; | |
b9dbddf6 | 305 | prepare_commit_graft(r); |
98c431b6 | 306 | pos = commit_graft_pos(r, oid); |
5da5c8f4 JH |
307 | if (pos < 0) |
308 | return NULL; | |
b9dbddf6 | 309 | return r->parsed_objects->grafts[pos]; |
5da5c8f4 JH |
310 | } |
311 | ||
09d46644 | 312 | int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data) |
ed09aef0 | 313 | { |
09d46644 | 314 | int i, ret; |
6a1a79fd JN |
315 | for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++) |
316 | ret = fn(the_repository->parsed_objects->grafts[i], cb_data); | |
09d46644 | 317 | return ret; |
ed09aef0 JS |
318 | } |
319 | ||
2a69ff09 JT |
320 | void reset_commit_grafts(struct repository *r) |
321 | { | |
322 | int i; | |
323 | ||
4d4e49ff JT |
324 | for (i = 0; i < r->parsed_objects->grafts_nr; i++) { |
325 | unparse_commit(r, &r->parsed_objects->grafts[i]->oid); | |
2a69ff09 | 326 | free(r->parsed_objects->grafts[i]); |
4d4e49ff | 327 | } |
2a69ff09 JT |
328 | r->parsed_objects->grafts_nr = 0; |
329 | r->parsed_objects->commit_graft_prepared = 0; | |
330 | } | |
331 | ||
8597ea3a JK |
332 | struct commit_buffer { |
333 | void *buffer; | |
334 | unsigned long size; | |
335 | }; | |
336 | define_commit_slab(buffer_slab, struct commit_buffer); | |
c1b3c71f | 337 | |
65ea9d4b | 338 | struct buffer_slab *allocate_commit_buffer_slab(void) |
66c2827e | 339 | { |
65ea9d4b SB |
340 | struct buffer_slab *bs = xmalloc(sizeof(*bs)); |
341 | init_buffer_slab(bs); | |
342 | return bs; | |
343 | } | |
344 | ||
345 | void free_commit_buffer_slab(struct buffer_slab *bs) | |
346 | { | |
347 | clear_buffer_slab(bs); | |
348 | free(bs); | |
349 | } | |
c1b3c71f | 350 | |
1a40fc45 | 351 | void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size) |
66c2827e | 352 | { |
65ea9d4b | 353 | struct commit_buffer *v = buffer_slab_at( |
1a40fc45 | 354 | r->parsed_objects->buffer_slab, commit); |
8597ea3a JK |
355 | v->buffer = buffer; |
356 | v->size = size; | |
66c2827e JK |
357 | } |
358 | ||
4ff7e5c9 | 359 | const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep) |
152ff1cc | 360 | { |
65ea9d4b | 361 | struct commit_buffer *v = buffer_slab_peek( |
4ff7e5c9 | 362 | r->parsed_objects->buffer_slab, commit); |
862e730e JH |
363 | if (!v) { |
364 | if (sizep) | |
365 | *sizep = 0; | |
366 | return NULL; | |
367 | } | |
8597ea3a JK |
368 | if (sizep) |
369 | *sizep = v->size; | |
370 | return v->buffer; | |
152ff1cc JK |
371 | } |
372 | ||
07de3fd8 SB |
373 | const void *repo_get_commit_buffer(struct repository *r, |
374 | const struct commit *commit, | |
375 | unsigned long *sizep) | |
152ff1cc | 376 | { |
07de3fd8 | 377 | const void *ret = get_cached_commit_buffer(r, commit, sizep); |
152ff1cc JK |
378 | if (!ret) { |
379 | enum object_type type; | |
380 | unsigned long size; | |
07de3fd8 | 381 | ret = repo_read_object_file(r, &commit->object.oid, &type, &size); |
152ff1cc JK |
382 | if (!ret) |
383 | die("cannot read commit object %s", | |
f2fd0760 | 384 | oid_to_hex(&commit->object.oid)); |
152ff1cc JK |
385 | if (type != OBJ_COMMIT) |
386 | die("expected commit for %s, got %s", | |
debca9d2 | 387 | oid_to_hex(&commit->object.oid), type_name(type)); |
8597ea3a JK |
388 | if (sizep) |
389 | *sizep = size; | |
152ff1cc JK |
390 | } |
391 | return ret; | |
392 | } | |
393 | ||
70315373 SB |
394 | void repo_unuse_commit_buffer(struct repository *r, |
395 | const struct commit *commit, | |
396 | const void *buffer) | |
152ff1cc | 397 | { |
65ea9d4b | 398 | struct commit_buffer *v = buffer_slab_peek( |
70315373 | 399 | r->parsed_objects->buffer_slab, commit); |
862e730e | 400 | if (!(v && v->buffer == buffer)) |
152ff1cc JK |
401 | free((void *)buffer); |
402 | } | |
403 | ||
6a7895fd | 404 | void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit) |
0fb370da | 405 | { |
65ea9d4b | 406 | struct commit_buffer *v = buffer_slab_peek( |
6a7895fd | 407 | pool->buffer_slab, commit); |
862e730e | 408 | if (v) { |
6a83d902 | 409 | FREE_AND_NULL(v->buffer); |
862e730e JH |
410 | v->size = 0; |
411 | } | |
0fb370da JK |
412 | } |
413 | ||
a133c40b NTND |
414 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
415 | { | |
416 | c->maybe_tree = t; | |
417 | } | |
418 | ||
301b8c7f NTND |
419 | struct tree *repo_get_commit_tree(struct repository *r, |
420 | const struct commit *commit) | |
5bb03de1 | 421 | { |
7b8a21db DS |
422 | if (commit->maybe_tree || !commit->object.parsed) |
423 | return commit->maybe_tree; | |
424 | ||
c49c82aa | 425 | if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH) |
ea2dab1a | 426 | return get_commit_tree_in_graph(r, commit); |
7b8a21db | 427 | |
83487663 | 428 | return NULL; |
5bb03de1 DS |
429 | } |
430 | ||
431 | struct object_id *get_commit_tree_oid(const struct commit *commit) | |
432 | { | |
ecb5091f | 433 | struct tree *tree = repo_get_commit_tree(the_repository, commit); |
806278de | 434 | return tree ? &tree->object.oid : NULL; |
5bb03de1 DS |
435 | } |
436 | ||
6a7895fd | 437 | void release_commit_memory(struct parsed_object_pool *pool, struct commit *c) |
14ba97f8 | 438 | { |
a133c40b | 439 | set_commit_tree(c, NULL); |
6a7895fd | 440 | free_commit_buffer(pool, c); |
9784f973 | 441 | c->index = 0; |
14ba97f8 | 442 | free_commit_list(c->parents); |
14ba97f8 SB |
443 | |
444 | c->object.parsed = 0; | |
445 | } | |
446 | ||
8597ea3a | 447 | const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep) |
0fb370da | 448 | { |
65ea9d4b SB |
449 | struct commit_buffer *v = buffer_slab_peek( |
450 | the_repository->parsed_objects->buffer_slab, commit); | |
8597ea3a JK |
451 | void *ret; |
452 | ||
862e730e JH |
453 | if (!v) { |
454 | if (sizep) | |
455 | *sizep = 0; | |
456 | return NULL; | |
457 | } | |
8597ea3a JK |
458 | ret = v->buffer; |
459 | if (sizep) | |
460 | *sizep = v->size; | |
461 | ||
462 | v->buffer = NULL; | |
463 | v->size = 0; | |
0fb370da JK |
464 | return ret; |
465 | } | |
466 | ||
fd8030c7 | 467 | int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph) |
175785e5 | 468 | { |
cf7b1cad NTND |
469 | const char *tail = buffer; |
470 | const char *bufptr = buffer; | |
7683e2e6 | 471 | struct object_id parent; |
6c88be16 | 472 | struct commit_list **pptr; |
5da5c8f4 | 473 | struct commit_graft *graft; |
2770ccbd | 474 | const int tree_entry_len = the_hash_algo->hexsz + 5; |
475 | const int parent_entry_len = the_hash_algo->hexsz + 7; | |
12736d2f | 476 | struct tree *tree; |
bd2c39f5 | 477 | |
175785e5 DB |
478 | if (item->object.parsed) |
479 | return 0; | |
bf20fe4c ÆAB |
480 | /* |
481 | * Presumably this is leftover from an earlier failed parse; | |
482 | * clear it out in preparation for us re-parsing (we'll hit the | |
483 | * same error, but that's good, since it lets our caller know | |
484 | * the result cannot be trusted. | |
485 | */ | |
486 | free_commit_list(item->parents); | |
487 | item->parents = NULL; | |
228c78fb | 488 | |
3b44f15a | 489 | tail += size; |
7683e2e6 | 490 | if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) || |
491 | bufptr[tree_entry_len] != '\n') | |
f2fd0760 | 492 | return error("bogus commit object %s", oid_to_hex(&item->object.oid)); |
26ea3e7d | 493 | if (get_oid_hex(bufptr + 5, &parent) < 0) |
bd2afde8 | 494 | return error("bad tree pointer in commit %s", |
f2fd0760 | 495 | oid_to_hex(&item->object.oid)); |
12736d2f JK |
496 | tree = lookup_tree(r, &parent); |
497 | if (!tree) | |
498 | return error("bad tree pointer %s in commit %s", | |
499 | oid_to_hex(&parent), | |
500 | oid_to_hex(&item->object.oid)); | |
501 | set_commit_tree(item, tree); | |
7683e2e6 | 502 | bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 503 | pptr = &item->parents; |
5da5c8f4 | 504 | |
fd8030c7 | 505 | graft = lookup_commit_graft(r, &item->object.oid); |
ce16364e TB |
506 | if (graft) |
507 | r->parsed_objects->substituted_parent = 1; | |
7683e2e6 | 508 | while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
509 | struct commit *new_parent; |
510 | ||
7683e2e6 | 511 | if (tail <= bufptr + parent_entry_len + 1 || |
26ea3e7d | 512 | get_oid_hex(bufptr + 7, &parent) || |
7683e2e6 | 513 | bufptr[parent_entry_len] != '\n') |
f2fd0760 | 514 | return error("bad parents in commit %s", oid_to_hex(&item->object.oid)); |
7683e2e6 | 515 | bufptr += parent_entry_len + 1; |
7f3140cd JS |
516 | /* |
517 | * The clone is shallow if nr_parent < 0, and we must | |
518 | * not traverse its real parents even when we unhide them. | |
519 | */ | |
3a5f3087 | 520 | if (graft && (graft->nr_parent < 0 || !grafts_keep_true_parents)) |
5da5c8f4 | 521 | continue; |
fd8030c7 | 522 | new_parent = lookup_commit(r, &parent); |
c78fe004 JK |
523 | if (!new_parent) |
524 | return error("bad parent %s in commit %s", | |
525 | oid_to_hex(&parent), | |
526 | oid_to_hex(&item->object.oid)); | |
527 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
5da5c8f4 JH |
528 | } |
529 | if (graft) { | |
530 | int i; | |
531 | struct commit *new_parent; | |
532 | for (i = 0; i < graft->nr_parent; i++) { | |
fd8030c7 | 533 | new_parent = lookup_commit(r, |
c1f5eb49 | 534 | &graft->parent[i]); |
5da5c8f4 | 535 | if (!new_parent) |
c78fe004 JK |
536 | return error("bad graft parent %s in commit %s", |
537 | oid_to_hex(&graft->parent[i]), | |
538 | oid_to_hex(&item->object.oid)); | |
5da5c8f4 | 539 | pptr = &commit_list_insert(new_parent, pptr)->next; |
5da5c8f4 | 540 | } |
175785e5 | 541 | } |
0a617799 | 542 | item->date = parse_commit_date(bufptr, tail); |
27dedf0c | 543 | |
e2838d85 | 544 | if (check_graph) |
c7944050 | 545 | load_commit_graph_info(r, item); |
e2838d85 | 546 | |
228c78fb | 547 | item->object.parsed = 1; |
175785e5 DB |
548 | return 0; |
549 | } | |
550 | ||
9e5252ab SB |
551 | int repo_parse_commit_internal(struct repository *r, |
552 | struct commit *item, | |
553 | int quiet_on_missing, | |
554 | int use_commit_graph) | |
bd2c39f5 | 555 | { |
21666f1a | 556 | enum object_type type; |
bd2c39f5 NP |
557 | void *buffer; |
558 | unsigned long size; | |
7e2ad1cd JT |
559 | struct object_info oi = { |
560 | .typep = &type, | |
561 | .sizep = &size, | |
562 | .contentp = &buffer, | |
563 | }; | |
564 | /* | |
565 | * Git does not support partial clones that exclude commits, so set | |
566 | * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing. | |
567 | */ | |
568 | int flags = OBJECT_INFO_LOOKUP_REPLACE | OBJECT_INFO_SKIP_FETCH_OBJECT | | |
569 | OBJECT_INFO_DIE_IF_CORRUPT; | |
bd2c39f5 NP |
570 | int ret; |
571 | ||
9786f68b MK |
572 | if (!item) |
573 | return -1; | |
bd2c39f5 NP |
574 | if (item->object.parsed) |
575 | return 0; | |
7a5d6044 PS |
576 | if (use_commit_graph && parse_commit_in_graph(r, item)) { |
577 | static int commit_graph_paranoia = -1; | |
578 | ||
579 | if (commit_graph_paranoia == -1) | |
580 | commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 1); | |
581 | ||
582 | if (commit_graph_paranoia && !has_object(r, &item->object.oid, 0)) { | |
583 | unparse_commit(r, &item->object.oid); | |
584 | return quiet_on_missing ? -1 : | |
585 | error(_("commit %s exists in commit-graph but not in the object database"), | |
586 | oid_to_hex(&item->object.oid)); | |
587 | } | |
588 | ||
177722b3 | 589 | return 0; |
7a5d6044 | 590 | } |
7e2ad1cd JT |
591 | |
592 | if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0) | |
9cc2b07a JK |
593 | return quiet_on_missing ? -1 : |
594 | error("Could not read %s", | |
f2fd0760 | 595 | oid_to_hex(&item->object.oid)); |
21666f1a | 596 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
597 | free(buffer); |
598 | return error("Object %s not a commit", | |
f2fd0760 | 599 | oid_to_hex(&item->object.oid)); |
bd2c39f5 | 600 | } |
9b19adac | 601 | |
9e5252ab | 602 | ret = parse_commit_buffer(r, item, buffer, size, 0); |
60ab26de | 603 | if (save_commit_buffer && !ret) { |
9e5252ab | 604 | set_commit_buffer(r, item, buffer, size); |
3ff1fbbb LT |
605 | return 0; |
606 | } | |
bd2c39f5 NP |
607 | free(buffer); |
608 | return ret; | |
609 | } | |
610 | ||
9e5252ab SB |
611 | int repo_parse_commit_gently(struct repository *r, |
612 | struct commit *item, int quiet_on_missing) | |
9b19adac | 613 | { |
9e5252ab | 614 | return repo_parse_commit_internal(r, item, quiet_on_missing, 1); |
9b19adac DS |
615 | } |
616 | ||
7059dccc JK |
617 | void parse_commit_or_die(struct commit *item) |
618 | { | |
ecb5091f | 619 | if (repo_parse_commit(the_repository, item)) |
7059dccc | 620 | die("unable to parse commit %s", |
f2fd0760 | 621 | item ? oid_to_hex(&item->object.oid) : "(null)"); |
7059dccc JK |
622 | } |
623 | ||
11af2aae CC |
624 | int find_commit_subject(const char *commit_buffer, const char **subject) |
625 | { | |
626 | const char *eol; | |
627 | const char *p = commit_buffer; | |
628 | ||
629 | while (*p && (*p != '\n' || p[1] != '\n')) | |
630 | p++; | |
631 | if (*p) { | |
4e1b06da | 632 | p = skip_blank_lines(p + 2); |
9c9b03b1 | 633 | eol = strchrnul(p, '\n'); |
11af2aae CC |
634 | } else |
635 | eol = p; | |
636 | ||
637 | *subject = p; | |
638 | ||
639 | return eol - p; | |
640 | } | |
641 | ||
6e0e2887 CM |
642 | size_t commit_subject_length(const char *body) |
643 | { | |
644 | const char *p = body; | |
645 | while (*p) { | |
646 | const char *next = skip_blank_lines(p); | |
647 | if (next != p) | |
648 | break; | |
649 | p = strchrnul(p, '\n'); | |
650 | if (*p) | |
651 | p++; | |
652 | } | |
653 | return p - body; | |
654 | } | |
655 | ||
ac5155ef | 656 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 657 | { |
812666c8 | 658 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
659 | new_list->item = item; |
660 | new_list->next = *list_p; | |
661 | *list_p = new_list; | |
ac5155ef | 662 | return new_list; |
dd97f850 DB |
663 | } |
664 | ||
597b2c39 DS |
665 | int commit_list_contains(struct commit *item, struct commit_list *list) |
666 | { | |
667 | while (list) { | |
668 | if (list->item == item) | |
669 | return 1; | |
670 | list = list->next; | |
671 | } | |
672 | ||
673 | return 0; | |
674 | } | |
675 | ||
65319475 MV |
676 | unsigned commit_list_count(const struct commit_list *l) |
677 | { | |
678 | unsigned c = 0; | |
679 | for (; l; l = l->next ) | |
680 | c++; | |
681 | return c; | |
682 | } | |
683 | ||
53d00b39 TR |
684 | struct commit_list *copy_commit_list(struct commit_list *list) |
685 | { | |
686 | struct commit_list *head = NULL; | |
687 | struct commit_list **pp = &head; | |
688 | while (list) { | |
cb979dbd | 689 | pp = commit_list_append(list->item, pp); |
53d00b39 TR |
690 | list = list->next; |
691 | } | |
692 | return head; | |
693 | } | |
694 | ||
b0ca1205 EN |
695 | struct commit_list *reverse_commit_list(struct commit_list *list) |
696 | { | |
697 | struct commit_list *next = NULL, *current, *backup; | |
698 | for (current = list; current; current = backup) { | |
699 | backup = current->next; | |
700 | current->next = next; | |
701 | next = current; | |
702 | } | |
703 | return next; | |
704 | } | |
705 | ||
175785e5 DB |
706 | void free_commit_list(struct commit_list *list) |
707 | { | |
e510ab89 RS |
708 | while (list) |
709 | pop_commit(&list); | |
175785e5 | 710 | } |
dd97f850 | 711 | |
47e44ed1 | 712 | struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
713 | { |
714 | struct commit_list **pp = list; | |
715 | struct commit_list *p; | |
716 | while ((p = *pp) != NULL) { | |
717 | if (p->item->date < item->date) { | |
718 | break; | |
719 | } | |
720 | pp = &p->next; | |
721 | } | |
f755494c | 722 | return commit_list_insert(item, pp); |
dd97f850 DB |
723 | } |
724 | ||
c0fb5774 RS |
725 | static int commit_list_compare_by_date(const struct commit_list *a, |
726 | const struct commit_list *b) | |
46905893 | 727 | { |
c0fb5774 RS |
728 | timestamp_t a_date = a->item->date; |
729 | timestamp_t b_date = b->item->date; | |
46905893 RS |
730 | if (a_date < b_date) |
731 | return 1; | |
732 | if (a_date > b_date) | |
733 | return -1; | |
734 | return 0; | |
735 | } | |
736 | ||
c0fb5774 | 737 | DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next); |
a6080a0a | 738 | |
47e44ed1 | 739 | void commit_list_sort_by_date(struct commit_list **list) |
dd97f850 | 740 | { |
c0fb5774 | 741 | commit_list_sort(list, commit_list_compare_by_date); |
dd97f850 DB |
742 | } |
743 | ||
58e28af6 DB |
744 | struct commit *pop_most_recent_commit(struct commit_list **list, |
745 | unsigned int mark) | |
dd97f850 | 746 | { |
e510ab89 | 747 | struct commit *ret = pop_commit(list); |
dd97f850 | 748 | struct commit_list *parents = ret->parents; |
dd97f850 DB |
749 | |
750 | while (parents) { | |
4056c091 | 751 | struct commit *commit = parents->item; |
ecb5091f | 752 | if (!repo_parse_commit(the_repository, commit) && !(commit->object.flags & mark)) { |
58e28af6 | 753 | commit->object.flags |= mark; |
47e44ed1 | 754 | commit_list_insert_by_date(commit, list); |
4056c091 | 755 | } |
dd97f850 DB |
756 | parents = parents->next; |
757 | } | |
758 | return ret; | |
759 | } | |
e3bc7a3b | 760 | |
941ba8db NTND |
761 | static void clear_commit_marks_1(struct commit_list **plist, |
762 | struct commit *commit, unsigned int mark) | |
f8f9c73c | 763 | { |
60fcc2e6 JS |
764 | while (commit) { |
765 | struct commit_list *parents; | |
f8f9c73c | 766 | |
60fcc2e6 JS |
767 | if (!(mark & commit->object.flags)) |
768 | return; | |
58ecf5c1 | 769 | |
60fcc2e6 JS |
770 | commit->object.flags &= ~mark; |
771 | ||
772 | parents = commit->parents; | |
773 | if (!parents) | |
774 | return; | |
775 | ||
4cb39fcf RS |
776 | while ((parents = parents->next)) { |
777 | if (parents->item->object.flags & mark) | |
778 | commit_list_insert(parents->item, plist); | |
779 | } | |
60fcc2e6 JS |
780 | |
781 | commit = commit->parents->item; | |
f8f9c73c JH |
782 | } |
783 | } | |
784 | ||
e895cb51 | 785 | void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark) |
941ba8db NTND |
786 | { |
787 | struct commit_list *list = NULL; | |
e895cb51 JH |
788 | |
789 | while (nr--) { | |
07f7d55a | 790 | clear_commit_marks_1(&list, *commit, mark); |
e895cb51 JH |
791 | commit++; |
792 | } | |
941ba8db NTND |
793 | while (list) |
794 | clear_commit_marks_1(&list, pop_commit(&list), mark); | |
795 | } | |
796 | ||
e895cb51 JH |
797 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
798 | { | |
799 | clear_commit_marks_many(1, &commit, mark); | |
800 | } | |
801 | ||
a3437b8c JS |
802 | struct commit *pop_commit(struct commit_list **stack) |
803 | { | |
804 | struct commit_list *top = *stack; | |
805 | struct commit *item = top ? top->item : NULL; | |
806 | ||
807 | if (top) { | |
808 | *stack = top->next; | |
809 | free(top); | |
810 | } | |
811 | return item; | |
812 | } | |
813 | ||
a84b794a JH |
814 | /* |
815 | * Topological sort support | |
816 | */ | |
96c4f4a3 | 817 | |
a84b794a JH |
818 | /* count number of children that have not been emitted */ |
819 | define_commit_slab(indegree_slab, int); | |
96c4f4a3 | 820 | |
18207030 | 821 | define_commit_slab(author_date_slab, timestamp_t); |
81c6b38b | 822 | |
5284fc5c DS |
823 | void record_author_date(struct author_date_slab *author_date, |
824 | struct commit *commit) | |
81c6b38b | 825 | { |
ecb5091f ÆAB |
826 | const char *buffer = repo_get_commit_buffer(the_repository, commit, |
827 | NULL); | |
81c6b38b | 828 | struct ident_split ident; |
ea5517f0 JK |
829 | const char *ident_line; |
830 | size_t ident_len; | |
81c6b38b | 831 | char *date_end; |
dddbad72 | 832 | timestamp_t date; |
81c6b38b | 833 | |
ea5517f0 JK |
834 | ident_line = find_commit_header(buffer, "author", &ident_len); |
835 | if (!ident_line) | |
836 | goto fail_exit; /* no author line */ | |
837 | if (split_ident_line(&ident, ident_line, ident_len) || | |
838 | !ident.date_begin || !ident.date_end) | |
839 | goto fail_exit; /* malformed "author" line */ | |
81c6b38b | 840 | |
1aeb7e75 | 841 | date = parse_timestamp(ident.date_begin, &date_end, 10); |
81c6b38b JH |
842 | if (date_end != ident.date_end) |
843 | goto fail_exit; /* malformed date */ | |
844 | *(author_date_slab_at(author_date, commit)) = date; | |
845 | ||
846 | fail_exit: | |
ecb5091f | 847 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
81c6b38b JH |
848 | } |
849 | ||
5284fc5c DS |
850 | int compare_commits_by_author_date(const void *a_, const void *b_, |
851 | void *cb_data) | |
81c6b38b JH |
852 | { |
853 | const struct commit *a = a_, *b = b_; | |
854 | struct author_date_slab *author_date = cb_data; | |
dddbad72 JS |
855 | timestamp_t a_date = *(author_date_slab_at(author_date, a)); |
856 | timestamp_t b_date = *(author_date_slab_at(author_date, b)); | |
81c6b38b JH |
857 | |
858 | /* newer commits with larger date first */ | |
859 | if (a_date < b_date) | |
860 | return 1; | |
861 | else if (a_date > b_date) | |
862 | return -1; | |
863 | return 0; | |
864 | } | |
865 | ||
17587122 JK |
866 | int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, |
867 | void *unused UNUSED) | |
3afc679b DS |
868 | { |
869 | const struct commit *a = a_, *b = b_; | |
d7f92784 AK |
870 | const timestamp_t generation_a = commit_graph_generation(a), |
871 | generation_b = commit_graph_generation(b); | |
3afc679b DS |
872 | |
873 | /* newer commits first */ | |
c752ad09 | 874 | if (generation_a < generation_b) |
3afc679b | 875 | return 1; |
c752ad09 | 876 | else if (generation_a > generation_b) |
3afc679b DS |
877 | return -1; |
878 | ||
879 | /* use date as a heuristic when generations are equal */ | |
880 | if (a->date < b->date) | |
881 | return 1; | |
882 | else if (a->date > b->date) | |
883 | return -1; | |
884 | return 0; | |
885 | } | |
886 | ||
17587122 JK |
887 | int compare_commits_by_commit_date(const void *a_, const void *b_, |
888 | void *unused UNUSED) | |
da24b104 JH |
889 | { |
890 | const struct commit *a = a_, *b = b_; | |
891 | /* newer commits with larger date first */ | |
892 | if (a->date < b->date) | |
893 | return 1; | |
894 | else if (a->date > b->date) | |
895 | return -1; | |
896 | return 0; | |
897 | } | |
898 | ||
ab580ace JS |
899 | /* |
900 | * Performs an in-place topological sort on the list supplied. | |
901 | */ | |
da24b104 | 902 | void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order) |
6b6dcfc2 | 903 | { |
23c17d4a | 904 | struct commit_list *next, *orig = *list; |
23c17d4a | 905 | struct commit_list **pptr; |
a84b794a | 906 | struct indegree_slab indegree; |
da24b104 JH |
907 | struct prio_queue queue; |
908 | struct commit *commit; | |
81c6b38b | 909 | struct author_date_slab author_date; |
a6080a0a | 910 | |
23c17d4a | 911 | if (!orig) |
7d6fb370 | 912 | return; |
23c17d4a LT |
913 | *list = NULL; |
914 | ||
a84b794a | 915 | init_indegree_slab(&indegree); |
da24b104 | 916 | memset(&queue, '\0', sizeof(queue)); |
81c6b38b | 917 | |
da24b104 JH |
918 | switch (sort_order) { |
919 | default: /* REV_SORT_IN_GRAPH_ORDER */ | |
920 | queue.compare = NULL; | |
921 | break; | |
922 | case REV_SORT_BY_COMMIT_DATE: | |
923 | queue.compare = compare_commits_by_commit_date; | |
924 | break; | |
81c6b38b JH |
925 | case REV_SORT_BY_AUTHOR_DATE: |
926 | init_author_date_slab(&author_date); | |
927 | queue.compare = compare_commits_by_author_date; | |
928 | queue.cb_data = &author_date; | |
929 | break; | |
da24b104 | 930 | } |
96c4f4a3 | 931 | |
23c17d4a LT |
932 | /* Mark them and clear the indegree */ |
933 | for (next = orig; next; next = next->next) { | |
934 | struct commit *commit = next->item; | |
a84b794a | 935 | *(indegree_slab_at(&indegree, commit)) = 1; |
81c6b38b JH |
936 | /* also record the author dates, if needed */ |
937 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) | |
938 | record_author_date(&author_date, commit); | |
ab580ace | 939 | } |
23c17d4a | 940 | |
ab580ace | 941 | /* update the indegree */ |
23c17d4a | 942 | for (next = orig; next; next = next->next) { |
da24b104 | 943 | struct commit_list *parents = next->item->parents; |
ab580ace | 944 | while (parents) { |
23c17d4a | 945 | struct commit *parent = parents->item; |
a84b794a | 946 | int *pi = indegree_slab_at(&indegree, parent); |
6b6dcfc2 | 947 | |
96c4f4a3 JK |
948 | if (*pi) |
949 | (*pi)++; | |
23c17d4a | 950 | parents = parents->next; |
ab580ace | 951 | } |
ab580ace | 952 | } |
23c17d4a | 953 | |
a6080a0a | 954 | /* |
674d1727 PH |
955 | * find the tips |
956 | * | |
957 | * tips are nodes not reachable from any other node in the list | |
958 | * | |
959 | * the tips serve as a starting set for the work queue. | |
960 | */ | |
23c17d4a LT |
961 | for (next = orig; next; next = next->next) { |
962 | struct commit *commit = next->item; | |
ab580ace | 963 | |
a84b794a | 964 | if (*(indegree_slab_at(&indegree, commit)) == 1) |
da24b104 | 965 | prio_queue_put(&queue, commit); |
ab580ace | 966 | } |
4c8725f1 | 967 | |
da24b104 JH |
968 | /* |
969 | * This is unfortunate; the initial tips need to be shown | |
970 | * in the order given from the revision traversal machinery. | |
971 | */ | |
972 | if (sort_order == REV_SORT_IN_GRAPH_ORDER) | |
973 | prio_queue_reverse(&queue); | |
974 | ||
975 | /* We no longer need the commit list */ | |
976 | free_commit_list(orig); | |
23c17d4a LT |
977 | |
978 | pptr = list; | |
979 | *list = NULL; | |
da24b104 JH |
980 | while ((commit = prio_queue_get(&queue)) != NULL) { |
981 | struct commit_list *parents; | |
23c17d4a | 982 | |
23c17d4a | 983 | for (parents = commit->parents; parents ; parents = parents->next) { |
cd2b8ae9 | 984 | struct commit *parent = parents->item; |
a84b794a | 985 | int *pi = indegree_slab_at(&indegree, parent); |
23c17d4a | 986 | |
96c4f4a3 | 987 | if (!*pi) |
23c17d4a LT |
988 | continue; |
989 | ||
990 | /* | |
991 | * parents are only enqueued for emission | |
992 | * when all their children have been emitted thereby | |
993 | * guaranteeing topological order. | |
994 | */ | |
da24b104 JH |
995 | if (--(*pi) == 1) |
996 | prio_queue_put(&queue, parent); | |
ab580ace JS |
997 | } |
998 | /* | |
da24b104 JH |
999 | * all children of commit have already been |
1000 | * emitted. we can emit it now. | |
674d1727 | 1001 | */ |
a84b794a | 1002 | *(indegree_slab_at(&indegree, commit)) = 0; |
da24b104 JH |
1003 | |
1004 | pptr = &commit_list_insert(commit, pptr)->next; | |
ab580ace | 1005 | } |
96c4f4a3 | 1006 | |
a84b794a | 1007 | clear_indegree_slab(&indegree); |
da24b104 | 1008 | clear_prio_queue(&queue); |
81c6b38b JH |
1009 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) |
1010 | clear_author_date_slab(&author_date); | |
ab580ace | 1011 | } |
7c6f8aaf | 1012 | |
103148aa PK |
1013 | struct rev_collect { |
1014 | struct commit **commit; | |
1015 | int nr; | |
1016 | int alloc; | |
1017 | unsigned int initial : 1; | |
1018 | }; | |
1019 | ||
1020 | static void add_one_commit(struct object_id *oid, struct rev_collect *revs) | |
1021 | { | |
1022 | struct commit *commit; | |
1023 | ||
1024 | if (is_null_oid(oid)) | |
1025 | return; | |
1026 | ||
1027 | commit = lookup_commit(the_repository, oid); | |
1028 | if (!commit || | |
1029 | (commit->object.flags & TMP_MARK) || | |
ecb5091f | 1030 | repo_parse_commit(the_repository, commit)) |
103148aa PK |
1031 | return; |
1032 | ||
1033 | ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc); | |
1034 | revs->commit[revs->nr++] = commit; | |
1035 | commit->object.flags |= TMP_MARK; | |
1036 | } | |
1037 | ||
1038 | static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid, | |
5cf88fd8 ÆAB |
1039 | const char *ident UNUSED, |
1040 | timestamp_t timestamp UNUSED, int tz UNUSED, | |
1041 | const char *message UNUSED, void *cbdata) | |
103148aa PK |
1042 | { |
1043 | struct rev_collect *revs = cbdata; | |
1044 | ||
1045 | if (revs->initial) { | |
1046 | revs->initial = 0; | |
1047 | add_one_commit(ooid, revs); | |
1048 | } | |
1049 | add_one_commit(noid, revs); | |
1050 | return 0; | |
1051 | } | |
1052 | ||
1053 | struct commit *get_fork_point(const char *refname, struct commit *commit) | |
1054 | { | |
1055 | struct object_id oid; | |
1056 | struct rev_collect revs; | |
1057 | struct commit_list *bases; | |
1058 | int i; | |
1059 | struct commit *ret = NULL; | |
f08132f8 JH |
1060 | char *full_refname; |
1061 | ||
12cb1c10 ÆAB |
1062 | switch (repo_dwim_ref(the_repository, refname, strlen(refname), &oid, |
1063 | &full_refname, 0)) { | |
f08132f8 JH |
1064 | case 0: |
1065 | die("No such ref: '%s'", refname); | |
1066 | case 1: | |
1067 | break; /* good */ | |
1068 | default: | |
1069 | die("Ambiguous refname: '%s'", refname); | |
1070 | } | |
103148aa PK |
1071 | |
1072 | memset(&revs, 0, sizeof(revs)); | |
1073 | revs.initial = 1; | |
f08132f8 | 1074 | for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs); |
103148aa | 1075 | |
f08132f8 | 1076 | if (!revs.nr) |
103148aa PK |
1077 | add_one_commit(&oid, &revs); |
1078 | ||
1079 | for (i = 0; i < revs.nr; i++) | |
1080 | revs.commit[i]->object.flags &= ~TMP_MARK; | |
1081 | ||
cb338c23 ÆAB |
1082 | bases = repo_get_merge_bases_many(the_repository, commit, revs.nr, |
1083 | revs.commit); | |
103148aa PK |
1084 | |
1085 | /* | |
1086 | * There should be one and only one merge base, when we found | |
1087 | * a common ancestor among reflog entries. | |
1088 | */ | |
1089 | if (!bases || bases->next) | |
1090 | goto cleanup_return; | |
1091 | ||
1092 | /* And the found one must be one of the reflog entries */ | |
1093 | for (i = 0; i < revs.nr; i++) | |
1094 | if (&bases->item->object == &revs.commit[i]->object) | |
1095 | break; /* found */ | |
1096 | if (revs.nr <= i) | |
1097 | goto cleanup_return; | |
1098 | ||
1099 | ret = bases->item; | |
1100 | ||
1101 | cleanup_return: | |
0c10ed19 | 1102 | free(revs.commit); |
103148aa | 1103 | free_commit_list(bases); |
f08132f8 | 1104 | free(full_refname); |
103148aa PK |
1105 | return ret; |
1106 | } | |
1107 | ||
42d4e1d1 | 1108 | /* |
1109 | * Indexed by hash algorithm identifier. | |
1110 | */ | |
1111 | static const char *gpg_sig_headers[] = { | |
1112 | NULL, | |
1113 | "gpgsig", | |
1114 | "gpgsig-sha256", | |
1115 | }; | |
ba3c69a9 | 1116 | |
937032e1 | 1117 | int sign_with_header(struct strbuf *buf, const char *keyid) |
ba3c69a9 JH |
1118 | { |
1119 | struct strbuf sig = STRBUF_INIT; | |
1120 | int inspos, copypos; | |
3324dd8f | 1121 | const char *eoh; |
42d4e1d1 | 1122 | const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(the_hash_algo)]; |
1123 | int gpg_sig_header_len = strlen(gpg_sig_header); | |
ba3c69a9 JH |
1124 | |
1125 | /* find the end of the header */ | |
3324dd8f JS |
1126 | eoh = strstr(buf->buf, "\n\n"); |
1127 | if (!eoh) | |
1128 | inspos = buf->len; | |
1129 | else | |
1130 | inspos = eoh - buf->buf + 1; | |
ba3c69a9 JH |
1131 | |
1132 | if (!keyid || !*keyid) | |
1133 | keyid = get_signing_key(); | |
1134 | if (sign_buffer(buf, &sig, keyid)) { | |
1135 | strbuf_release(&sig); | |
1136 | return -1; | |
1137 | } | |
1138 | ||
1139 | for (copypos = 0; sig.buf[copypos]; ) { | |
1140 | const char *bol = sig.buf + copypos; | |
1141 | const char *eol = strchrnul(bol, '\n'); | |
1142 | int len = (eol - bol) + !!*eol; | |
1143 | ||
1144 | if (!copypos) { | |
1145 | strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len); | |
1146 | inspos += gpg_sig_header_len; | |
1147 | } | |
a91cc7fa | 1148 | strbuf_insertstr(buf, inspos++, " "); |
ba3c69a9 JH |
1149 | strbuf_insert(buf, inspos, bol, len); |
1150 | inspos += len; | |
1151 | copypos += len; | |
1152 | } | |
1153 | strbuf_release(&sig); | |
1154 | return 0; | |
1155 | } | |
1156 | ||
937032e1 | 1157 | |
1158 | ||
218aa3a6 | 1159 | int parse_signed_commit(const struct commit *commit, |
1fb5cf0d | 1160 | struct strbuf *payload, struct strbuf *signature, |
1161 | const struct git_hash_algo *algop) | |
0c37f1fc JH |
1162 | { |
1163 | unsigned long size; | |
ecb5091f ÆAB |
1164 | const char *buffer = repo_get_commit_buffer(the_repository, commit, |
1165 | &size); | |
937032e1 | 1166 | int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop); |
1167 | ||
ecb5091f | 1168 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
937032e1 | 1169 | return ret; |
1170 | } | |
1171 | ||
1172 | int parse_buffer_signed_by_header(const char *buffer, | |
1173 | unsigned long size, | |
1174 | struct strbuf *payload, | |
1175 | struct strbuf *signature, | |
1176 | const struct git_hash_algo *algop) | |
1177 | { | |
1fb5cf0d | 1178 | int in_signature = 0, saw_signature = 0, other_signature = 0; |
1179 | const char *line, *tail, *p; | |
1180 | const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)]; | |
0c37f1fc JH |
1181 | |
1182 | line = buffer; | |
1183 | tail = buffer + size; | |
0c37f1fc JH |
1184 | while (line < tail) { |
1185 | const char *sig = NULL; | |
218aa3a6 | 1186 | const char *next = memchr(line, '\n', tail - line); |
0c37f1fc JH |
1187 | |
1188 | next = next ? next + 1 : tail; | |
1189 | if (in_signature && line[0] == ' ') | |
1190 | sig = line + 1; | |
1fb5cf0d | 1191 | else if (skip_prefix(line, gpg_sig_header, &p) && |
1192 | *p == ' ') { | |
1193 | sig = line + strlen(gpg_sig_header) + 1; | |
1194 | other_signature = 0; | |
1195 | } | |
1196 | else if (starts_with(line, "gpgsig")) | |
1197 | other_signature = 1; | |
1198 | else if (other_signature && line[0] != ' ') | |
1199 | other_signature = 0; | |
0c37f1fc JH |
1200 | if (sig) { |
1201 | strbuf_add(signature, sig, next - sig); | |
1202 | saw_signature = 1; | |
1203 | in_signature = 1; | |
1204 | } else { | |
1205 | if (*line == '\n') | |
1206 | /* dump the whole remainder of the buffer */ | |
1207 | next = tail; | |
1fb5cf0d | 1208 | if (!other_signature) |
1209 | strbuf_add(payload, line, next - line); | |
0c37f1fc JH |
1210 | in_signature = 0; |
1211 | } | |
1212 | line = next; | |
1213 | } | |
0c37f1fc JH |
1214 | return saw_signature; |
1215 | } | |
1216 | ||
0b05ab6f CC |
1217 | int remove_signature(struct strbuf *buf) |
1218 | { | |
1219 | const char *line = buf->buf; | |
1220 | const char *tail = buf->buf + buf->len; | |
1221 | int in_signature = 0; | |
1fb5cf0d | 1222 | struct sigbuf { |
1223 | const char *start; | |
1224 | const char *end; | |
1225 | } sigs[2], *sigp = &sigs[0]; | |
1226 | int i; | |
1227 | const char *orig_buf = buf->buf; | |
1228 | ||
1229 | memset(sigs, 0, sizeof(sigs)); | |
0b05ab6f CC |
1230 | |
1231 | while (line < tail) { | |
1232 | const char *next = memchr(line, '\n', tail - line); | |
1233 | next = next ? next + 1 : tail; | |
1234 | ||
1235 | if (in_signature && line[0] == ' ') | |
1fb5cf0d | 1236 | sigp->end = next; |
42d4e1d1 | 1237 | else if (starts_with(line, "gpgsig")) { |
1238 | int i; | |
1239 | for (i = 1; i < GIT_HASH_NALGOS; i++) { | |
1240 | const char *p; | |
1241 | if (skip_prefix(line, gpg_sig_headers[i], &p) && | |
1242 | *p == ' ') { | |
1fb5cf0d | 1243 | sigp->start = line; |
1244 | sigp->end = next; | |
42d4e1d1 | 1245 | in_signature = 1; |
1246 | } | |
1247 | } | |
0b05ab6f CC |
1248 | } else { |
1249 | if (*line == '\n') | |
1250 | /* dump the whole remainder of the buffer */ | |
1251 | next = tail; | |
1fb5cf0d | 1252 | if (in_signature && sigp - sigs != ARRAY_SIZE(sigs)) |
1253 | sigp++; | |
0b05ab6f CC |
1254 | in_signature = 0; |
1255 | } | |
1256 | line = next; | |
1257 | } | |
1258 | ||
1fb5cf0d | 1259 | for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--) |
1260 | if (sigs[i].start) | |
1261 | strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start); | |
0b05ab6f | 1262 | |
1fb5cf0d | 1263 | return sigs[0].start != NULL; |
0b05ab6f CC |
1264 | } |
1265 | ||
5231c633 JH |
1266 | static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail) |
1267 | { | |
1268 | struct merge_remote_desc *desc; | |
1269 | struct commit_extra_header *mergetag; | |
1270 | char *buf; | |
482c1191 | 1271 | unsigned long size; |
5231c633 | 1272 | enum object_type type; |
482c1191 | 1273 | struct strbuf payload = STRBUF_INIT; |
1274 | struct strbuf signature = STRBUF_INIT; | |
5231c633 JH |
1275 | |
1276 | desc = merge_remote_util(parent); | |
1277 | if (!desc || !desc->obj) | |
1278 | return; | |
bc726bd0 ÆAB |
1279 | buf = repo_read_object_file(the_repository, &desc->obj->oid, &type, |
1280 | &size); | |
5231c633 JH |
1281 | if (!buf || type != OBJ_TAG) |
1282 | goto free_return; | |
482c1191 | 1283 | if (!parse_signature(buf, size, &payload, &signature)) |
5231c633 JH |
1284 | goto free_return; |
1285 | /* | |
1286 | * We could verify this signature and either omit the tag when | |
1287 | * it does not validate, but the integrator may not have the | |
0e20b229 | 1288 | * public key of the signer of the tag being merged, while a |
5231c633 JH |
1289 | * later auditor may have it while auditing, so let's not run |
1290 | * verify-signed-buffer here for now... | |
1291 | * | |
1292 | * if (verify_signed_buffer(buf, len, buf + len, size - len, ...)) | |
1293 | * warn("warning: signed tag unverified."); | |
1294 | */ | |
ca56dadb | 1295 | CALLOC_ARRAY(mergetag, 1); |
5231c633 JH |
1296 | mergetag->key = xstrdup("mergetag"); |
1297 | mergetag->value = buf; | |
1298 | mergetag->len = size; | |
1299 | ||
1300 | **tail = mergetag; | |
1301 | *tail = &mergetag->next; | |
482c1191 | 1302 | strbuf_release(&payload); |
1303 | strbuf_release(&signature); | |
5231c633 JH |
1304 | return; |
1305 | ||
1306 | free_return: | |
1307 | free(buf); | |
1308 | } | |
1309 | ||
434060ec | 1310 | int check_commit_signature(const struct commit *commit, struct signature_check *sigc) |
ffb6d7d5 SG |
1311 | { |
1312 | struct strbuf payload = STRBUF_INIT; | |
1313 | struct strbuf signature = STRBUF_INIT; | |
434060ec | 1314 | int ret = 1; |
ffb6d7d5 SG |
1315 | |
1316 | sigc->result = 'N'; | |
1317 | ||
1fb5cf0d | 1318 | if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0) |
ffb6d7d5 | 1319 | goto out; |
02769437 | 1320 | |
6393c956 | 1321 | sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT; |
02769437 FS |
1322 | sigc->payload = strbuf_detach(&payload, &sigc->payload_len); |
1323 | ret = check_signature(sigc, signature.buf, signature.len); | |
ffb6d7d5 SG |
1324 | |
1325 | out: | |
ffb6d7d5 SG |
1326 | strbuf_release(&payload); |
1327 | strbuf_release(&signature); | |
434060ec | 1328 | |
1329 | return ret; | |
ffb6d7d5 SG |
1330 | } |
1331 | ||
54887b46 HJI |
1332 | void verify_merge_signature(struct commit *commit, int verbosity, |
1333 | int check_trust) | |
edc4d47d JK |
1334 | { |
1335 | char hex[GIT_MAX_HEXSZ + 1]; | |
1336 | struct signature_check signature_check; | |
54887b46 | 1337 | int ret; |
edc4d47d JK |
1338 | memset(&signature_check, 0, sizeof(signature_check)); |
1339 | ||
54887b46 | 1340 | ret = check_commit_signature(commit, &signature_check); |
edc4d47d | 1341 | |
d850b7a5 ÆAB |
1342 | repo_find_unique_abbrev_r(the_repository, hex, &commit->object.oid, |
1343 | DEFAULT_ABBREV); | |
edc4d47d JK |
1344 | switch (signature_check.result) { |
1345 | case 'G': | |
54887b46 HJI |
1346 | if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL)) |
1347 | die(_("Commit %s has an untrusted GPG signature, " | |
1348 | "allegedly by %s."), hex, signature_check.signer); | |
edc4d47d | 1349 | break; |
edc4d47d JK |
1350 | case 'B': |
1351 | die(_("Commit %s has a bad GPG signature " | |
1352 | "allegedly by %s."), hex, signature_check.signer); | |
1353 | default: /* 'N' */ | |
1354 | die(_("Commit %s does not have a GPG signature."), hex); | |
1355 | } | |
1356 | if (verbosity >= 0 && signature_check.result == 'G') | |
1357 | printf(_("Commit %s has a good GPG signature by %s\n"), | |
1358 | hex, signature_check.signer); | |
ffb6d7d5 | 1359 | |
edc4d47d JK |
1360 | signature_check_clear(&signature_check); |
1361 | } | |
ffb6d7d5 | 1362 | |
5231c633 JH |
1363 | void append_merge_tag_headers(struct commit_list *parents, |
1364 | struct commit_extra_header ***tail) | |
1365 | { | |
1366 | while (parents) { | |
1367 | struct commit *parent = parents->item; | |
1368 | handle_signed_tag(parent, tail); | |
1369 | parents = parents->next; | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | static void add_extra_header(struct strbuf *buffer, | |
1374 | struct commit_extra_header *extra) | |
1375 | { | |
1376 | strbuf_addstr(buffer, extra->key); | |
ed7a42a0 JH |
1377 | if (extra->len) |
1378 | strbuf_add_lines(buffer, " ", extra->value, extra->len); | |
1379 | else | |
1380 | strbuf_addch(buffer, '\n'); | |
1381 | } | |
1382 | ||
c871a1d1 JH |
1383 | struct commit_extra_header *read_commit_extra_headers(struct commit *commit, |
1384 | const char **exclude) | |
ed7a42a0 JH |
1385 | { |
1386 | struct commit_extra_header *extra = NULL; | |
1387 | unsigned long size; | |
ecb5091f ÆAB |
1388 | const char *buffer = repo_get_commit_buffer(the_repository, commit, |
1389 | &size); | |
218aa3a6 | 1390 | extra = read_commit_extra_header_lines(buffer, size, exclude); |
ecb5091f | 1391 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
ed7a42a0 JH |
1392 | return extra; |
1393 | } | |
1394 | ||
fef461ea | 1395 | int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data) |
063da62b CC |
1396 | { |
1397 | struct commit_extra_header *extra, *to_free; | |
fef461ea | 1398 | int res = 0; |
063da62b CC |
1399 | |
1400 | to_free = read_commit_extra_headers(commit, NULL); | |
fef461ea | 1401 | for (extra = to_free; !res && extra; extra = extra->next) { |
063da62b CC |
1402 | if (strcmp(extra->key, "mergetag")) |
1403 | continue; /* not a merge tag */ | |
fef461ea | 1404 | res = fn(commit, extra, data); |
063da62b CC |
1405 | } |
1406 | free_commit_extra_headers(to_free); | |
fef461ea | 1407 | return res; |
063da62b CC |
1408 | } |
1409 | ||
ed7a42a0 JH |
1410 | static inline int standard_header_field(const char *field, size_t len) |
1411 | { | |
b072504c RS |
1412 | return ((len == 4 && !memcmp(field, "tree", 4)) || |
1413 | (len == 6 && !memcmp(field, "parent", 6)) || | |
1414 | (len == 6 && !memcmp(field, "author", 6)) || | |
1415 | (len == 9 && !memcmp(field, "committer", 9)) || | |
1416 | (len == 8 && !memcmp(field, "encoding", 8))); | |
ed7a42a0 JH |
1417 | } |
1418 | ||
c871a1d1 JH |
1419 | static int excluded_header_field(const char *field, size_t len, const char **exclude) |
1420 | { | |
1421 | if (!exclude) | |
1422 | return 0; | |
1423 | ||
1424 | while (*exclude) { | |
1425 | size_t xlen = strlen(*exclude); | |
b072504c | 1426 | if (len == xlen && !memcmp(field, *exclude, xlen)) |
c871a1d1 JH |
1427 | return 1; |
1428 | exclude++; | |
1429 | } | |
1430 | return 0; | |
1431 | } | |
1432 | ||
82a75299 JH |
1433 | static struct commit_extra_header *read_commit_extra_header_lines( |
1434 | const char *buffer, size_t size, | |
1435 | const char **exclude) | |
ed7a42a0 JH |
1436 | { |
1437 | struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL; | |
1438 | const char *line, *next, *eof, *eob; | |
1439 | struct strbuf buf = STRBUF_INIT; | |
1440 | ||
1441 | for (line = buffer, eob = line + size; | |
1442 | line < eob && *line != '\n'; | |
1443 | line = next) { | |
1444 | next = memchr(line, '\n', eob - line); | |
1445 | next = next ? next + 1 : eob; | |
1446 | if (*line == ' ') { | |
1447 | /* continuation */ | |
1448 | if (it) | |
1449 | strbuf_add(&buf, line + 1, next - (line + 1)); | |
1450 | continue; | |
1451 | } | |
1452 | if (it) | |
1453 | it->value = strbuf_detach(&buf, &it->len); | |
1454 | strbuf_reset(&buf); | |
1455 | it = NULL; | |
1456 | ||
50a01cc4 RS |
1457 | eof = memchr(line, ' ', next - line); |
1458 | if (!eof) | |
ed7a42a0 | 1459 | eof = next; |
b072504c RS |
1460 | else if (standard_header_field(line, eof - line) || |
1461 | excluded_header_field(line, eof - line, exclude)) | |
ed7a42a0 JH |
1462 | continue; |
1463 | ||
ca56dadb | 1464 | CALLOC_ARRAY(it, 1); |
ed7a42a0 JH |
1465 | it->key = xmemdupz(line, eof-line); |
1466 | *tail = it; | |
1467 | tail = &it->next; | |
1468 | if (eof + 1 < next) | |
1469 | strbuf_add(&buf, eof + 1, next - (eof + 1)); | |
1470 | } | |
1471 | if (it) | |
1472 | it->value = strbuf_detach(&buf, &it->len); | |
1473 | return extra; | |
5231c633 JH |
1474 | } |
1475 | ||
1476 | void free_commit_extra_headers(struct commit_extra_header *extra) | |
1477 | { | |
1478 | while (extra) { | |
1479 | struct commit_extra_header *next = extra->next; | |
1480 | free(extra->key); | |
1481 | free(extra->value); | |
1482 | free(extra); | |
1483 | extra = next; | |
1484 | } | |
1485 | } | |
1486 | ||
5078f344 PO |
1487 | int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree, |
1488 | struct commit_list *parents, struct object_id *ret, | |
ba3c69a9 | 1489 | const char *author, const char *sign_commit) |
5231c633 JH |
1490 | { |
1491 | struct commit_extra_header *extra = NULL, **tail = &extra; | |
1492 | int result; | |
1493 | ||
1494 | append_merge_tag_headers(parents, &tail); | |
e8cbe211 PW |
1495 | result = commit_tree_extended(msg, msg_len, tree, parents, ret, author, |
1496 | NULL, sign_commit, extra); | |
5231c633 JH |
1497 | free_commit_extra_headers(extra); |
1498 | return result; | |
1499 | } | |
1500 | ||
08a94a14 LT |
1501 | static int find_invalid_utf8(const char *buf, int len) |
1502 | { | |
1503 | int offset = 0; | |
e82bd6cc | 1504 | static const unsigned int max_codepoint[] = { |
1505 | 0x7f, 0x7ff, 0xffff, 0x10ffff | |
1506 | }; | |
08a94a14 LT |
1507 | |
1508 | while (len) { | |
1509 | unsigned char c = *buf++; | |
1510 | int bytes, bad_offset; | |
28110d4b | 1511 | unsigned int codepoint; |
e82bd6cc | 1512 | unsigned int min_val, max_val; |
08a94a14 LT |
1513 | |
1514 | len--; | |
1515 | offset++; | |
1516 | ||
1517 | /* Simple US-ASCII? No worries. */ | |
1518 | if (c < 0x80) | |
1519 | continue; | |
1520 | ||
1521 | bad_offset = offset-1; | |
1522 | ||
1523 | /* | |
1524 | * Count how many more high bits set: that's how | |
1525 | * many more bytes this sequence should have. | |
1526 | */ | |
1527 | bytes = 0; | |
1528 | while (c & 0x40) { | |
1529 | c <<= 1; | |
1530 | bytes++; | |
1531 | } | |
1532 | ||
28110d4b | 1533 | /* |
1534 | * Must be between 1 and 3 more bytes. Longer sequences result in | |
1535 | * codepoints beyond U+10FFFF, which are guaranteed never to exist. | |
1536 | */ | |
1537 | if (bytes < 1 || 3 < bytes) | |
08a94a14 LT |
1538 | return bad_offset; |
1539 | ||
1540 | /* Do we *have* that many bytes? */ | |
1541 | if (len < bytes) | |
1542 | return bad_offset; | |
1543 | ||
e82bd6cc | 1544 | /* |
1545 | * Place the encoded bits at the bottom of the value and compute the | |
1546 | * valid range. | |
1547 | */ | |
28110d4b | 1548 | codepoint = (c & 0x7f) >> bytes; |
e82bd6cc | 1549 | min_val = max_codepoint[bytes-1] + 1; |
1550 | max_val = max_codepoint[bytes]; | |
28110d4b | 1551 | |
08a94a14 LT |
1552 | offset += bytes; |
1553 | len -= bytes; | |
1554 | ||
1555 | /* And verify that they are good continuation bytes */ | |
1556 | do { | |
28110d4b | 1557 | codepoint <<= 6; |
1558 | codepoint |= *buf & 0x3f; | |
08a94a14 LT |
1559 | if ((*buf++ & 0xc0) != 0x80) |
1560 | return bad_offset; | |
1561 | } while (--bytes); | |
1562 | ||
e82bd6cc | 1563 | /* Reject codepoints that are out of range for the sequence length. */ |
1564 | if (codepoint < min_val || codepoint > max_val) | |
28110d4b | 1565 | return bad_offset; |
1566 | /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */ | |
1567 | if ((codepoint & 0x1ff800) == 0xd800) | |
1568 | return bad_offset; | |
81050ac6 | 1569 | /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */ |
dc773a67 | 1570 | if ((codepoint & 0xfffe) == 0xfffe) |
81050ac6 PK |
1571 | return bad_offset; |
1572 | /* So are anything in the range U+FDD0..U+FDEF. */ | |
1573 | if (codepoint >= 0xfdd0 && codepoint <= 0xfdef) | |
28110d4b | 1574 | return bad_offset; |
08a94a14 LT |
1575 | } |
1576 | return -1; | |
1577 | } | |
1578 | ||
1579 | /* | |
1580 | * This verifies that the buffer is in proper utf8 format. | |
1581 | * | |
1582 | * If it isn't, it assumes any non-utf8 characters are Latin1, | |
1583 | * and does the conversion. | |
08a94a14 LT |
1584 | */ |
1585 | static int verify_utf8(struct strbuf *buf) | |
1586 | { | |
1587 | int ok = 1; | |
1588 | long pos = 0; | |
1589 | ||
1590 | for (;;) { | |
1591 | int bad; | |
1592 | unsigned char c; | |
1593 | unsigned char replace[2]; | |
1594 | ||
1595 | bad = find_invalid_utf8(buf->buf + pos, buf->len - pos); | |
1596 | if (bad < 0) | |
1597 | return ok; | |
1598 | pos += bad; | |
1599 | ok = 0; | |
1600 | c = buf->buf[pos]; | |
1601 | strbuf_remove(buf, pos, 1); | |
1602 | ||
1603 | /* We know 'c' must be in the range 128-255 */ | |
1604 | replace[0] = 0xc0 + (c >> 6); | |
1605 | replace[1] = 0x80 + (c & 0x3f); | |
1606 | strbuf_insert(buf, pos, replace, 2); | |
1607 | pos += 2; | |
1608 | } | |
1609 | } | |
1610 | ||
40d52ff7 | 1611 | static const char commit_utf8_warn[] = |
4fa4b315 VA |
1612 | N_("Warning: commit message did not conform to UTF-8.\n" |
1613 | "You may want to amend it after fixing the message, or set the config\n" | |
b4eda05d | 1614 | "variable i18n.commitEncoding to the encoding your project uses.\n"); |
40d52ff7 | 1615 | |
3ffefb54 | 1616 | int commit_tree_extended(const char *msg, size_t msg_len, |
5078f344 PO |
1617 | const struct object_id *tree, |
1618 | struct commit_list *parents, struct object_id *ret, | |
e8cbe211 PW |
1619 | const char *author, const char *committer, |
1620 | const char *sign_commit, | |
ba3c69a9 | 1621 | struct commit_extra_header *extra) |
40d52ff7 JK |
1622 | { |
1623 | int result; | |
1624 | int encoding_is_utf8; | |
1625 | struct strbuf buffer; | |
1626 | ||
e816caa0 | 1627 | assert_oid_type(tree, OBJ_TREE); |
40d52ff7 | 1628 | |
3ffefb54 | 1629 | if (memchr(msg, '\0', msg_len)) |
37576c14 NTND |
1630 | return error("a NUL byte in commit log message not allowed."); |
1631 | ||
40d52ff7 JK |
1632 | /* Not having i18n.commitencoding is the same as having utf-8 */ |
1633 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); | |
1634 | ||
1635 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ | |
5078f344 | 1636 | strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree)); |
40d52ff7 JK |
1637 | |
1638 | /* | |
1639 | * NOTE! This ordering means that the same exact tree merged with a | |
1640 | * different order of parents will be a _different_ changeset even | |
1641 | * if everything else stays the same. | |
1642 | */ | |
1643 | while (parents) { | |
e510ab89 | 1644 | struct commit *parent = pop_commit(&parents); |
40d52ff7 | 1645 | strbuf_addf(&buffer, "parent %s\n", |
f2fd0760 | 1646 | oid_to_hex(&parent->object.oid)); |
40d52ff7 JK |
1647 | } |
1648 | ||
1649 | /* Person/date information */ | |
1650 | if (!author) | |
f9bc573f | 1651 | author = git_author_info(IDENT_STRICT); |
40d52ff7 | 1652 | strbuf_addf(&buffer, "author %s\n", author); |
e8cbe211 PW |
1653 | if (!committer) |
1654 | committer = git_committer_info(IDENT_STRICT); | |
1655 | strbuf_addf(&buffer, "committer %s\n", committer); | |
40d52ff7 JK |
1656 | if (!encoding_is_utf8) |
1657 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); | |
5231c633 JH |
1658 | |
1659 | while (extra) { | |
1660 | add_extra_header(&buffer, extra); | |
1661 | extra = extra->next; | |
1662 | } | |
40d52ff7 JK |
1663 | strbuf_addch(&buffer, '\n'); |
1664 | ||
1665 | /* And add the comment */ | |
3ffefb54 | 1666 | strbuf_add(&buffer, msg, msg_len); |
40d52ff7 JK |
1667 | |
1668 | /* And check the encoding */ | |
08a94a14 | 1669 | if (encoding_is_utf8 && !verify_utf8(&buffer)) |
4fa4b315 | 1670 | fprintf(stderr, _(commit_utf8_warn)); |
40d52ff7 | 1671 | |
937032e1 | 1672 | if (sign_commit && sign_with_header(&buffer, sign_commit)) { |
e505146d RS |
1673 | result = -1; |
1674 | goto out; | |
1675 | } | |
ba3c69a9 | 1676 | |
c80d226a | 1677 | result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret); |
e505146d | 1678 | out: |
40d52ff7 JK |
1679 | strbuf_release(&buffer); |
1680 | return result; | |
1681 | } | |
ae8e4c9c | 1682 | |
e2e5ac23 NTND |
1683 | define_commit_slab(merge_desc_slab, struct merge_remote_desc *); |
1684 | static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab); | |
1685 | ||
1686 | struct merge_remote_desc *merge_remote_util(struct commit *commit) | |
1687 | { | |
1688 | return *merge_desc_slab_at(&merge_desc_slab, commit); | |
1689 | } | |
1690 | ||
beb518c9 RS |
1691 | void set_merge_remote_desc(struct commit *commit, |
1692 | const char *name, struct object *obj) | |
1693 | { | |
1694 | struct merge_remote_desc *desc; | |
5447a76a | 1695 | FLEX_ALLOC_STR(desc, name, name); |
beb518c9 | 1696 | desc->obj = obj; |
e2e5ac23 | 1697 | *merge_desc_slab_at(&merge_desc_slab, commit) = desc; |
beb518c9 RS |
1698 | } |
1699 | ||
ae8e4c9c JH |
1700 | struct commit *get_merge_parent(const char *name) |
1701 | { | |
1702 | struct object *obj; | |
1703 | struct commit *commit; | |
7683e2e6 | 1704 | struct object_id oid; |
d850b7a5 | 1705 | if (repo_get_oid(the_repository, name, &oid)) |
ae8e4c9c | 1706 | return NULL; |
109cd76d | 1707 | obj = parse_object(the_repository, &oid); |
d850b7a5 ÆAB |
1708 | commit = (struct commit *)repo_peel_to_type(the_repository, name, 0, |
1709 | obj, OBJ_COMMIT); | |
e2e5ac23 | 1710 | if (commit && !merge_remote_util(commit)) |
beb518c9 | 1711 | set_merge_remote_desc(commit, name, obj); |
ae8e4c9c JH |
1712 | return commit; |
1713 | } | |
89b5f1d9 RS |
1714 | |
1715 | /* | |
1716 | * Append a commit to the end of the commit_list. | |
1717 | * | |
1718 | * next starts by pointing to the variable that holds the head of an | |
1719 | * empty commit_list, and is updated to point to the "next" field of | |
1720 | * the last item on the list as new commits are appended. | |
1721 | * | |
1722 | * Usage example: | |
1723 | * | |
1724 | * struct commit_list *list; | |
1725 | * struct commit_list **next = &list; | |
1726 | * | |
1727 | * next = commit_list_append(c1, next); | |
1728 | * next = commit_list_append(c2, next); | |
1729 | * assert(commit_list_count(list) == 2); | |
1730 | * return list; | |
1731 | */ | |
1732 | struct commit_list **commit_list_append(struct commit *commit, | |
1733 | struct commit_list **next) | |
1734 | { | |
258c03eb BW |
1735 | struct commit_list *new_commit = xmalloc(sizeof(struct commit_list)); |
1736 | new_commit->item = commit; | |
1737 | *next = new_commit; | |
1738 | new_commit->next = NULL; | |
1739 | return &new_commit->next; | |
89b5f1d9 | 1740 | } |
efc7df45 | 1741 | |
cfc5cf42 JC |
1742 | const char *find_header_mem(const char *msg, size_t len, |
1743 | const char *key, size_t *out_len) | |
fe6eb7f2 JK |
1744 | { |
1745 | int key_len = strlen(key); | |
1746 | const char *line = msg; | |
1747 | ||
cfc5cf42 JC |
1748 | /* |
1749 | * NEEDSWORK: It's possible for strchrnul() to scan beyond the range | |
1750 | * given by len. However, current callers are safe because they compute | |
1751 | * len by scanning a NUL-terminated block of memory starting at msg. | |
1752 | * Nonetheless, it would be better to ensure the function does not look | |
1753 | * at msg beyond the len provided by the caller. | |
1754 | */ | |
1755 | while (line && line < msg + len) { | |
fe6eb7f2 JK |
1756 | const char *eol = strchrnul(line, '\n'); |
1757 | ||
1758 | if (line == eol) | |
1759 | return NULL; | |
1760 | ||
1761 | if (eol - line > key_len && | |
1762 | !strncmp(line, key, key_len) && | |
1763 | line[key_len] == ' ') { | |
1764 | *out_len = eol - line - key_len - 1; | |
1765 | return line + key_len + 1; | |
1766 | } | |
1767 | line = *eol ? eol + 1 : NULL; | |
1768 | } | |
1769 | return NULL; | |
1770 | } | |
0ed8a4e1 | 1771 | |
cfc5cf42 JC |
1772 | const char *find_commit_header(const char *msg, const char *key, size_t *out_len) |
1773 | { | |
1774 | return find_header_mem(msg, strlen(msg), key, out_len); | |
1775 | } | |
8c384589 | 1776 | /* |
710714aa | 1777 | * Inspect the given string and determine the true "end" of the log message, in |
3abd4a67 | 1778 | * order to find where to put a new Signed-off-by trailer. Ignored are |
d76650b8 BM |
1779 | * trailing comment lines and blank lines. To support "git commit -s |
1780 | * --amend" on an existing commit, we also ignore "Conflicts:". To | |
1781 | * support "git commit -v", we truncate at cut lines. | |
8c384589 CC |
1782 | * |
1783 | * Returns the number of bytes from the tail to ignore, to be fed as | |
1784 | * the second parameter to append_signoff(). | |
1785 | */ | |
66e83d9b | 1786 | size_t ignore_non_trailer(const char *buf, size_t len) |
8c384589 | 1787 | { |
66e83d9b JK |
1788 | size_t boc = 0; |
1789 | size_t bol = 0; | |
8c384589 | 1790 | int in_old_conflicts_block = 0; |
d76650b8 | 1791 | size_t cutoff = wt_status_locate_end(buf, len); |
8c384589 | 1792 | |
d76650b8 | 1793 | while (bol < cutoff) { |
710714aa | 1794 | const char *next_line = memchr(buf + bol, '\n', len - bol); |
8c384589 | 1795 | |
710714aa JT |
1796 | if (!next_line) |
1797 | next_line = buf + len; | |
8c384589 CC |
1798 | else |
1799 | next_line++; | |
1800 | ||
710714aa | 1801 | if (buf[bol] == comment_line_char || buf[bol] == '\n') { |
8c384589 CC |
1802 | /* is this the first of the run of comments? */ |
1803 | if (!boc) | |
1804 | boc = bol; | |
1805 | /* otherwise, it is just continuing */ | |
710714aa | 1806 | } else if (starts_with(buf + bol, "Conflicts:\n")) { |
8c384589 CC |
1807 | in_old_conflicts_block = 1; |
1808 | if (!boc) | |
1809 | boc = bol; | |
710714aa | 1810 | } else if (in_old_conflicts_block && buf[bol] == '\t') { |
8c384589 CC |
1811 | ; /* a pathname in the conflicts block */ |
1812 | } else if (boc) { | |
1813 | /* the previous was not trailing comment */ | |
1814 | boc = 0; | |
1815 | in_old_conflicts_block = 0; | |
1816 | } | |
710714aa | 1817 | bol = next_line - buf; |
8c384589 | 1818 | } |
d76650b8 | 1819 | return boc ? len - boc : len - cutoff; |
8c384589 | 1820 | } |
49697cb7 PW |
1821 | |
1822 | int run_commit_hook(int editor_is_used, const char *index_file, | |
a8cc5943 | 1823 | int *invoked_hook, const char *name, ...) |
49697cb7 | 1824 | { |
f443246b | 1825 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
49697cb7 | 1826 | va_list args; |
f443246b | 1827 | const char *arg; |
49697cb7 | 1828 | |
f443246b | 1829 | strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file); |
49697cb7 PW |
1830 | |
1831 | /* | |
1832 | * Let the hook know that no editor will be launched. | |
1833 | */ | |
1834 | if (!editor_is_used) | |
f443246b | 1835 | strvec_push(&opt.env, "GIT_EDITOR=:"); |
49697cb7 PW |
1836 | |
1837 | va_start(args, name); | |
f443246b ES |
1838 | while ((arg = va_arg(args, const char *))) |
1839 | strvec_push(&opt.args, arg); | |
49697cb7 | 1840 | va_end(args); |
49697cb7 | 1841 | |
4369e3a1 | 1842 | opt.invoked_hook = invoked_hook; |
f443246b | 1843 | return run_hooks_opt(name, &opt); |
49697cb7 | 1844 | } |