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