]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
"git-apply --check" should not report "fixed"
[thirdparty/git.git] / commit.c
CommitLineData
8f1d2e6f 1#include "cache.h"
961784ee 2#include "tag.h"
175785e5 3#include "commit.h"
ed09aef0 4#include "pkt-line.h"
52883fbd 5#include "utf8.h"
199c45bf
JH
6#include "diff.h"
7#include "revision.h"
175785e5 8
60ab26de
LT
9int save_commit_buffer = 1;
10
175785e5
DB
11const char *commit_type = "commit";
12
f76412ed
JH
13static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
15 int quiet)
961784ee 16{
1974632c 17 if (obj->type != OBJ_COMMIT) {
f76412ed
JH
18 if (!quiet)
19 error("Object %s is a %s, not a commit",
885a86ab 20 sha1_to_hex(sha1), typename(obj->type));
961784ee
LT
21 return NULL;
22 }
23 return (struct commit *) obj;
24}
25
f76412ed
JH
26struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
27 int quiet)
961784ee 28{
9534f40b 29 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
30
31 if (!obj)
32 return NULL;
f76412ed
JH
33 return check_commit(obj, sha1, quiet);
34}
35
36struct commit *lookup_commit_reference(const unsigned char *sha1)
37{
38 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
39}
40
5d6ccf5c 41struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
42{
43 struct object *obj = lookup_object(sha1);
100c5f3b
LT
44 if (!obj)
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
d1af002d 46 if (!obj->type)
1974632c 47 obj->type = OBJ_COMMIT;
f76412ed 48 return check_commit(obj, sha1, 0);
175785e5
DB
49}
50
51static unsigned long parse_commit_date(const char *buf)
52{
53 unsigned long date;
54
55 if (memcmp(buf, "author", 6))
56 return 0;
57 while (*buf++ != '\n')
58 /* nada */;
59 if (memcmp(buf, "committer", 9))
60 return 0;
61 while (*buf++ != '>')
62 /* nada */;
63 date = strtoul(buf, NULL, 10);
64 if (date == ULONG_MAX)
65 date = 0;
66 return date;
67}
68
5040f17e 69static struct commit_graft **commit_graft;
5da5c8f4
JH
70static int commit_graft_alloc, commit_graft_nr;
71
72static int commit_graft_pos(const unsigned char *sha1)
73{
74 int lo, hi;
75 lo = 0;
76 hi = commit_graft_nr;
77 while (lo < hi) {
78 int mi = (lo + hi) / 2;
79 struct commit_graft *graft = commit_graft[mi];
a89fccd2 80 int cmp = hashcmp(sha1, graft->sha1);
5da5c8f4
JH
81 if (!cmp)
82 return mi;
83 if (cmp < 0)
84 hi = mi;
85 else
86 lo = mi + 1;
87 }
88 return -lo - 1;
89}
90
5040f17e
JH
91int register_commit_graft(struct commit_graft *graft, int ignore_dups)
92{
93 int pos = commit_graft_pos(graft->sha1);
a6080a0a 94
5040f17e
JH
95 if (0 <= pos) {
96 if (ignore_dups)
97 free(graft);
98 else {
99 free(commit_graft[pos]);
100 commit_graft[pos] = graft;
101 }
102 return 1;
103 }
104 pos = -pos - 1;
105 if (commit_graft_alloc <= ++commit_graft_nr) {
106 commit_graft_alloc = alloc_nr(commit_graft_alloc);
107 commit_graft = xrealloc(commit_graft,
108 sizeof(*commit_graft) *
109 commit_graft_alloc);
110 }
111 if (pos < commit_graft_nr)
112 memmove(commit_graft + pos + 1,
113 commit_graft + pos,
114 (commit_graft_nr - pos - 1) *
115 sizeof(*commit_graft));
116 commit_graft[pos] = graft;
117 return 0;
118}
119
120struct commit_graft *read_graft_line(char *buf, int len)
121{
122 /* The format is just "Commit Parent1 Parent2 ...\n" */
123 int i;
124 struct commit_graft *graft = NULL;
125
126 if (buf[len-1] == '\n')
127 buf[--len] = 0;
360204c3 128 if (buf[0] == '#' || buf[0] == '\0')
5bc4ce58 129 return NULL;
5040f17e
JH
130 if ((len + 1) % 41) {
131 bad_graft_data:
132 error("bad graft data: %s", buf);
133 free(graft);
134 return NULL;
135 }
136 i = (len + 1) / 41 - 1;
137 graft = xmalloc(sizeof(*graft) + 20 * i);
138 graft->nr_parent = i;
139 if (get_sha1_hex(buf, graft->sha1))
140 goto bad_graft_data;
141 for (i = 40; i < len; i += 41) {
142 if (buf[i] != ' ')
143 goto bad_graft_data;
144 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
145 goto bad_graft_data;
146 }
147 return graft;
148}
149
150int read_graft_file(const char *graft_file)
5da5c8f4 151{
5da5c8f4
JH
152 FILE *fp = fopen(graft_file, "r");
153 char buf[1024];
5040f17e
JH
154 if (!fp)
155 return -1;
5da5c8f4
JH
156 while (fgets(buf, sizeof(buf), fp)) {
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
158 int len = strlen(buf);
5040f17e 159 struct commit_graft *graft = read_graft_line(buf, len);
5bc4ce58
JH
160 if (!graft)
161 continue;
5040f17e 162 if (register_commit_graft(graft, 1))
5da5c8f4 163 error("duplicate graft data: %s", buf);
5da5c8f4
JH
164 }
165 fclose(fp);
5040f17e
JH
166 return 0;
167}
168
169static void prepare_commit_graft(void)
170{
171 static int commit_graft_prepared;
172 char *graft_file;
173
174 if (commit_graft_prepared)
175 return;
176 graft_file = get_graft_file();
177 read_graft_file(graft_file);
ed09aef0
JS
178 /* make sure shallows are read */
179 is_repository_shallow();
5040f17e 180 commit_graft_prepared = 1;
5da5c8f4
JH
181}
182
183static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
184{
185 int pos;
5040f17e 186 prepare_commit_graft();
5da5c8f4
JH
187 pos = commit_graft_pos(sha1);
188 if (pos < 0)
189 return NULL;
190 return commit_graft[pos];
191}
192
ed09aef0
JS
193int write_shallow_commits(int fd, int use_pack_protocol)
194{
195 int i, count = 0;
196 for (i = 0; i < commit_graft_nr; i++)
197 if (commit_graft[i]->nr_parent < 0) {
198 const char *hex =
199 sha1_to_hex(commit_graft[i]->sha1);
200 count++;
201 if (use_pack_protocol)
202 packet_write(fd, "shallow %s", hex);
203 else {
93822c22
AW
204 if (write_in_full(fd, hex, 40) != 40)
205 break;
206 if (write_in_full(fd, "\n", 1) != 1)
207 break;
ed09aef0
JS
208 }
209 }
210 return count;
211}
212
f53514bc
JS
213int unregister_shallow(const unsigned char *sha1)
214{
215 int pos = commit_graft_pos(sha1);
216 if (pos < 0)
217 return -1;
218 if (pos + 1 < commit_graft_nr)
219 memcpy(commit_graft + pos, commit_graft + pos + 1,
220 sizeof(struct commit_graft *)
221 * (commit_graft_nr - pos - 1));
222 commit_graft_nr--;
223 return 0;
224}
225
bd2c39f5 226int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 227{
3b44f15a 228 char *tail = buffer;
f7cc77d7 229 char *bufptr = buffer;
175785e5 230 unsigned char parent[20];
6c88be16 231 struct commit_list **pptr;
5da5c8f4 232 struct commit_graft *graft;
27dedf0c 233 unsigned n_refs = 0;
bd2c39f5 234
175785e5
DB
235 if (item->object.parsed)
236 return 0;
237 item->object.parsed = 1;
3b44f15a
JH
238 tail += size;
239 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
f7cc77d7 240 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
3b44f15a 241 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
bd2afde8
JH
242 return error("bad tree pointer in commit %s",
243 sha1_to_hex(item->object.sha1));
175785e5 244 item->tree = lookup_tree(parent);
235ac407 245 if (item->tree)
27dedf0c 246 n_refs++;
175785e5 247 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 248 pptr = &item->parents;
5da5c8f4
JH
249
250 graft = lookup_commit_graft(item->object.sha1);
3b44f15a 251 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
f7cc77d7
LT
252 struct commit *new_parent;
253
3b44f15a
JH
254 if (tail <= bufptr + 48 ||
255 get_sha1_hex(bufptr + 7, parent) ||
256 bufptr[47] != '\n')
f7cc77d7 257 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
258 bufptr += 48;
259 if (graft)
260 continue;
f7cc77d7 261 new_parent = lookup_commit(parent);
235ac407 262 if (new_parent) {
6c88be16 263 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 264 n_refs++;
235ac407 265 }
5da5c8f4
JH
266 }
267 if (graft) {
268 int i;
269 struct commit *new_parent;
270 for (i = 0; i < graft->nr_parent; i++) {
271 new_parent = lookup_commit(graft->parent[i]);
272 if (!new_parent)
273 continue;
274 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 275 n_refs++;
5da5c8f4 276 }
175785e5
DB
277 }
278 item->date = parse_commit_date(bufptr);
27dedf0c
JH
279
280 if (track_object_refs) {
281 unsigned i = 0;
282 struct commit_list *p;
283 struct object_refs *refs = alloc_object_refs(n_refs);
284 if (item->tree)
285 refs->ref[i++] = &item->tree->object;
286 for (p = item->parents; p; p = p->next)
287 refs->ref[i++] = &p->item->object;
288 set_object_refs(&item->object, refs);
289 }
290
175785e5
DB
291 return 0;
292}
293
bd2c39f5
NP
294int parse_commit(struct commit *item)
295{
21666f1a 296 enum object_type type;
bd2c39f5
NP
297 void *buffer;
298 unsigned long size;
299 int ret;
300
301 if (item->object.parsed)
302 return 0;
21666f1a 303 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
304 if (!buffer)
305 return error("Could not read %s",
306 sha1_to_hex(item->object.sha1));
21666f1a 307 if (type != OBJ_COMMIT) {
bd2c39f5
NP
308 free(buffer);
309 return error("Object %s not a commit",
310 sha1_to_hex(item->object.sha1));
311 }
312 ret = parse_commit_buffer(item, buffer, size);
60ab26de 313 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
314 item->buffer = buffer;
315 return 0;
316 }
bd2c39f5
NP
317 free(buffer);
318 return ret;
319}
320
ac5155ef 321struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 322{
812666c8 323 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
324 new_list->item = item;
325 new_list->next = *list_p;
326 *list_p = new_list;
ac5155ef 327 return new_list;
dd97f850
DB
328}
329
175785e5
DB
330void free_commit_list(struct commit_list *list)
331{
332 while (list) {
333 struct commit_list *temp = list;
334 list = temp->next;
335 free(temp);
336 }
337}
dd97f850 338
f755494c 339struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
340{
341 struct commit_list **pp = list;
342 struct commit_list *p;
343 while ((p = *pp) != NULL) {
344 if (p->item->date < item->date) {
345 break;
346 }
347 pp = &p->next;
348 }
f755494c 349 return commit_list_insert(item, pp);
dd97f850
DB
350}
351
a6080a0a 352
dd97f850
DB
353void sort_by_date(struct commit_list **list)
354{
355 struct commit_list *ret = NULL;
356 while (*list) {
f755494c 357 insert_by_date((*list)->item, &ret);
dd97f850
DB
358 *list = (*list)->next;
359 }
360 *list = ret;
361}
362
58e28af6
DB
363struct commit *pop_most_recent_commit(struct commit_list **list,
364 unsigned int mark)
dd97f850
DB
365{
366 struct commit *ret = (*list)->item;
367 struct commit_list *parents = ret->parents;
368 struct commit_list *old = *list;
369
370 *list = (*list)->next;
371 free(old);
372
373 while (parents) {
4056c091 374 struct commit *commit = parents->item;
58e28af6
DB
375 parse_commit(commit);
376 if (!(commit->object.flags & mark)) {
377 commit->object.flags |= mark;
f755494c 378 insert_by_date(commit, list);
4056c091 379 }
dd97f850
DB
380 parents = parents->next;
381 }
382 return ret;
383}
e3bc7a3b 384
f8f9c73c
JH
385void clear_commit_marks(struct commit *commit, unsigned int mark)
386{
60fcc2e6
JS
387 while (commit) {
388 struct commit_list *parents;
f8f9c73c 389
60fcc2e6
JS
390 if (!(mark & commit->object.flags))
391 return;
58ecf5c1 392
60fcc2e6
JS
393 commit->object.flags &= ~mark;
394
395 parents = commit->parents;
396 if (!parents)
397 return;
398
399 while ((parents = parents->next))
400 clear_commit_marks(parents->item, mark);
401
402 commit = commit->parents->item;
f8f9c73c
JH
403 }
404}
405
a3437b8c
JS
406struct commit *pop_commit(struct commit_list **stack)
407{
408 struct commit_list *top = *stack;
409 struct commit *item = top ? top->item : NULL;
410
411 if (top) {
412 *stack = top->next;
413 free(top);
414 }
415 return item;
416}
417
ab580ace
JS
418/*
419 * Performs an in-place topological sort on the list supplied.
420 */
4c8725f1 421void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2 422{
23c17d4a
LT
423 struct commit_list *next, *orig = *list;
424 struct commit_list *work, **insert;
425 struct commit_list **pptr;
a6080a0a 426
23c17d4a 427 if (!orig)
7d6fb370 428 return;
23c17d4a
LT
429 *list = NULL;
430
431 /* Mark them and clear the indegree */
432 for (next = orig; next; next = next->next) {
433 struct commit *commit = next->item;
434 commit->object.flags |= TOPOSORT;
435 commit->indegree = 0;
ab580ace 436 }
23c17d4a 437
ab580ace 438 /* update the indegree */
23c17d4a 439 for (next = orig; next; next = next->next) {
ab580ace
JS
440 struct commit_list * parents = next->item->parents;
441 while (parents) {
23c17d4a 442 struct commit *parent = parents->item;
6b6dcfc2 443
23c17d4a
LT
444 if (parent->object.flags & TOPOSORT)
445 parent->indegree++;
446 parents = parents->next;
ab580ace 447 }
ab580ace 448 }
23c17d4a 449
a6080a0a 450 /*
674d1727
PH
451 * find the tips
452 *
453 * tips are nodes not reachable from any other node in the list
454 *
455 * the tips serve as a starting set for the work queue.
456 */
23c17d4a 457 work = NULL;
2ed02887 458 insert = &work;
23c17d4a
LT
459 for (next = orig; next; next = next->next) {
460 struct commit *commit = next->item;
ab580ace 461
23c17d4a
LT
462 if (!commit->indegree)
463 insert = &commit_list_insert(commit, insert)->next;
ab580ace 464 }
4c8725f1 465
ab580ace 466 /* process the list in topological order */
4c8725f1
JH
467 if (!lifo)
468 sort_by_date(&work);
23c17d4a
LT
469
470 pptr = list;
471 *list = NULL;
ab580ace 472 while (work) {
23c17d4a
LT
473 struct commit *commit;
474 struct commit_list *parents, *work_item;
ab580ace 475
23c17d4a
LT
476 work_item = work;
477 work = work_item->next;
478 work_item->next = NULL;
479
480 commit = work_item->item;
481 for (parents = commit->parents; parents ; parents = parents->next) {
482 struct commit *parent=parents->item;
483
484 if (!(parent->object.flags & TOPOSORT))
485 continue;
486
487 /*
488 * parents are only enqueued for emission
489 * when all their children have been emitted thereby
490 * guaranteeing topological order.
491 */
492 if (!--parent->indegree) {
493 if (!lifo)
494 insert_by_date(parent, &work);
495 else
496 commit_list_insert(parent, &work);
ab580ace 497 }
ab580ace
JS
498 }
499 /*
674d1727
PH
500 * work_item is a commit all of whose children
501 * have already been emitted. we can emit it now.
502 */
23c17d4a
LT
503 commit->object.flags &= ~TOPOSORT;
504 *pptr = work_item;
505 pptr = &work_item->next;
ab580ace 506 }
ab580ace 507}
7c6f8aaf 508
1295228d 509/* merge-base stuff */
7c6f8aaf 510
577ed5c2
JH
511/* bits #0..15 in revision.h */
512#define PARENT1 (1u<<16)
513#define PARENT2 (1u<<17)
514#define STALE (1u<<18)
515#define RESULT (1u<<19)
7c6f8aaf 516
1295228d
JH
517static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
518
7c6f8aaf
JS
519static struct commit *interesting(struct commit_list *list)
520{
521 while (list) {
522 struct commit *commit = list->item;
523 list = list->next;
542ccefe 524 if (commit->object.flags & STALE)
7c6f8aaf
JS
525 continue;
526 return commit;
527 }
528 return NULL;
529}
530
f3249438 531static struct commit_list *merge_bases(struct commit *one, struct commit *two)
7c6f8aaf
JS
532{
533 struct commit_list *list = NULL;
534 struct commit_list *result = NULL;
7c6f8aaf 535
f3249438
JH
536 if (one == two)
537 /* We do not mark this even with RESULT so we do not
538 * have to clean it up.
539 */
540 return commit_list_insert(one, &result);
7c6f8aaf 541
f3249438
JH
542 parse_commit(one);
543 parse_commit(two);
7c6f8aaf 544
f3249438
JH
545 one->object.flags |= PARENT1;
546 two->object.flags |= PARENT2;
547 insert_by_date(one, &list);
548 insert_by_date(two, &list);
7c6f8aaf
JS
549
550 while (interesting(list)) {
f3249438 551 struct commit *commit;
7c6f8aaf 552 struct commit_list *parents;
f3249438
JH
553 struct commit_list *n;
554 int flags;
7c6f8aaf 555
f3249438
JH
556 commit = list->item;
557 n = list->next;
558 free(list);
559 list = n;
7c6f8aaf 560
f3249438
JH
561 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
562 if (flags == (PARENT1 | PARENT2)) {
563 if (!(commit->object.flags & RESULT)) {
564 commit->object.flags |= RESULT;
565 insert_by_date(commit, &result);
566 }
542ccefe
JH
567 /* Mark parents of a found merge stale */
568 flags |= STALE;
7c6f8aaf
JS
569 }
570 parents = commit->parents;
571 while (parents) {
572 struct commit *p = parents->item;
573 parents = parents->next;
574 if ((p->object.flags & flags) == flags)
575 continue;
576 parse_commit(p);
577 p->object.flags |= flags;
578 insert_by_date(p, &list);
579 }
580 }
581
f3249438 582 /* Clean up the result to remove stale ones */
1295228d 583 free_commit_list(list);
f3249438
JH
584 list = result; result = NULL;
585 while (list) {
586 struct commit_list *n = list->next;
587 if (!(list->item->object.flags & STALE))
588 insert_by_date(list->item, &result);
589 free(list);
590 list = n;
591 }
592 return result;
593}
7c6f8aaf 594
f3249438 595struct commit_list *get_merge_bases(struct commit *one,
674d1727 596 struct commit *two, int cleanup)
f3249438 597{
f3249438
JH
598 struct commit_list *list;
599 struct commit **rslt;
600 struct commit_list *result;
601 int cnt, i, j;
602
603 result = merge_bases(one, two);
604 if (one == two)
605 return result;
606 if (!result || !result->next) {
607 if (cleanup) {
608 clear_commit_marks(one, all_flags);
609 clear_commit_marks(two, all_flags);
7c6f8aaf 610 }
f3249438 611 return result;
7c6f8aaf
JS
612 }
613
f3249438
JH
614 /* There are more than one */
615 cnt = 0;
616 list = result;
617 while (list) {
618 list = list->next;
619 cnt++;
620 }
621 rslt = xcalloc(cnt, sizeof(*rslt));
622 for (list = result, i = 0; list; list = list->next)
623 rslt[i++] = list->item;
624 free_commit_list(result);
625
626 clear_commit_marks(one, all_flags);
627 clear_commit_marks(two, all_flags);
628 for (i = 0; i < cnt - 1; i++) {
629 for (j = i+1; j < cnt; j++) {
630 if (!rslt[i] || !rslt[j])
631 continue;
632 result = merge_bases(rslt[i], rslt[j]);
633 clear_commit_marks(rslt[i], all_flags);
634 clear_commit_marks(rslt[j], all_flags);
635 for (list = result; list; list = list->next) {
636 if (rslt[i] == list->item)
637 rslt[i] = NULL;
638 if (rslt[j] == list->item)
639 rslt[j] = NULL;
640 }
641 }
58ecf5c1 642 }
7c6f8aaf 643
f3249438
JH
644 /* Surviving ones in rslt[] are the independent results */
645 result = NULL;
646 for (i = 0; i < cnt; i++) {
647 if (rslt[i])
648 insert_by_date(rslt[i], &result);
649 }
650 free(rslt);
7c6f8aaf
JS
651 return result;
652}
2ecd2bbc 653
03840fc3 654int in_merge_bases(struct commit *commit, struct commit **reference, int num)
2ecd2bbc
JH
655{
656 struct commit_list *bases, *b;
657 int ret = 0;
658
03840fc3
JH
659 if (num == 1)
660 bases = get_merge_bases(commit, *reference, 1);
661 else
662 die("not yet");
2ecd2bbc 663 for (b = bases; b; b = b->next) {
03840fc3 664 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
2ecd2bbc
JH
665 ret = 1;
666 break;
667 }
668 }
669
670 free_commit_list(bases);
671 return ret;
672}