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