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