]> git.proxmox.com Git - libgit2.git/blob - src/revwalk.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / revwalk.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "revwalk.h"
9
10 #include "commit.h"
11 #include "odb.h"
12 #include "pool.h"
13
14 #include "git2/revparse.h"
15 #include "merge.h"
16 #include "vector.h"
17
18 static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list);
19
20 git_commit_list_node *git_revwalk__commit_lookup(
21 git_revwalk *walk, const git_oid *oid)
22 {
23 git_commit_list_node *commit;
24
25 /* lookup and reserve space if not already present */
26 if ((commit = git_oidmap_get(walk->commits, oid)) != NULL)
27 return commit;
28
29 commit = git_commit_list_alloc_node(walk);
30 if (commit == NULL)
31 return NULL;
32
33 git_oid_cpy(&commit->oid, oid);
34
35 if ((git_oidmap_set(walk->commits, &commit->oid, commit)) < 0)
36 return NULL;
37
38 return commit;
39 }
40
41 int git_revwalk__push_commit(git_revwalk *walk, const git_oid *oid, const git_revwalk__push_options *opts)
42 {
43 git_oid commit_id;
44 int error;
45 git_object *obj, *oobj;
46 git_commit_list_node *commit;
47 git_commit_list *list;
48
49 if ((error = git_object_lookup(&oobj, walk->repo, oid, GIT_OBJECT_ANY)) < 0)
50 return error;
51
52 error = git_object_peel(&obj, oobj, GIT_OBJECT_COMMIT);
53 git_object_free(oobj);
54
55 if (error == GIT_ENOTFOUND || error == GIT_EINVALIDSPEC || error == GIT_EPEEL) {
56 /* If this comes from e.g. push_glob("tags"), ignore this */
57 if (opts->from_glob)
58 return 0;
59
60 git_error_set(GIT_ERROR_INVALID, "object is not a committish");
61 return error;
62 }
63 if (error < 0)
64 return error;
65
66 git_oid_cpy(&commit_id, git_object_id(obj));
67 git_object_free(obj);
68
69 commit = git_revwalk__commit_lookup(walk, &commit_id);
70 if (commit == NULL)
71 return -1; /* error already reported by failed lookup */
72
73 /* A previous hide already told us we don't want this commit */
74 if (commit->uninteresting)
75 return 0;
76
77 if (opts->uninteresting) {
78 walk->limited = 1;
79 walk->did_hide = 1;
80 } else {
81 walk->did_push = 1;
82 }
83
84 commit->uninteresting = opts->uninteresting;
85 list = walk->user_input;
86 if ((opts->insert_by_date &&
87 git_commit_list_insert_by_date(commit, &list) == NULL) ||
88 git_commit_list_insert(commit, &list) == NULL) {
89 git_error_set_oom();
90 return -1;
91 }
92
93 walk->user_input = list;
94
95 return 0;
96 }
97
98 int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
99 {
100 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
101
102 assert(walk && oid);
103
104 return git_revwalk__push_commit(walk, oid, &opts);
105 }
106
107
108 int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
109 {
110 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
111 assert(walk && oid);
112
113 opts.uninteresting = 1;
114 return git_revwalk__push_commit(walk, oid, &opts);
115 }
116
117 int git_revwalk__push_ref(git_revwalk *walk, const char *refname, const git_revwalk__push_options *opts)
118 {
119 git_oid oid;
120
121 if (git_reference_name_to_id(&oid, walk->repo, refname) < 0)
122 return -1;
123
124 return git_revwalk__push_commit(walk, &oid, opts);
125 }
126
127 int git_revwalk__push_glob(git_revwalk *walk, const char *glob, const git_revwalk__push_options *given_opts)
128 {
129 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
130 int error = 0;
131 git_buf buf = GIT_BUF_INIT;
132 git_reference *ref;
133 git_reference_iterator *iter;
134 size_t wildcard;
135
136 assert(walk && glob);
137
138 if (given_opts)
139 memcpy(&opts, given_opts, sizeof(opts));
140
141 /* refs/ is implied if not given in the glob */
142 if (git__prefixcmp(glob, GIT_REFS_DIR) != 0)
143 git_buf_joinpath(&buf, GIT_REFS_DIR, glob);
144 else
145 git_buf_puts(&buf, glob);
146 GIT_ERROR_CHECK_ALLOC_BUF(&buf);
147
148 /* If no '?', '*' or '[' exist, we append '/ *' to the glob */
149 wildcard = strcspn(glob, "?*[");
150 if (!glob[wildcard])
151 git_buf_put(&buf, "/*", 2);
152
153 if ((error = git_reference_iterator_glob_new(&iter, walk->repo, buf.ptr)) < 0)
154 goto out;
155
156 opts.from_glob = true;
157 while ((error = git_reference_next(&ref, iter)) == 0) {
158 error = git_revwalk__push_ref(walk, git_reference_name(ref), &opts);
159 git_reference_free(ref);
160 if (error < 0)
161 break;
162 }
163 git_reference_iterator_free(iter);
164
165 if (error == GIT_ITEROVER)
166 error = 0;
167 out:
168 git_buf_dispose(&buf);
169 return error;
170 }
171
172 int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
173 {
174 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
175 assert(walk && glob);
176
177 return git_revwalk__push_glob(walk, glob, &opts);
178 }
179
180 int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
181 {
182 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
183 assert(walk && glob);
184
185 opts.uninteresting = 1;
186 return git_revwalk__push_glob(walk, glob, &opts);
187 }
188
189 int git_revwalk_push_head(git_revwalk *walk)
190 {
191 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
192 assert(walk);
193
194 return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts);
195 }
196
197 int git_revwalk_hide_head(git_revwalk *walk)
198 {
199 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
200 assert(walk);
201
202 opts.uninteresting = 1;
203 return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts);
204 }
205
206 int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
207 {
208 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
209 assert(walk && refname);
210
211 return git_revwalk__push_ref(walk, refname, &opts);
212 }
213
214 int git_revwalk_push_range(git_revwalk *walk, const char *range)
215 {
216 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
217 git_revspec revspec;
218 int error = 0;
219
220 if ((error = git_revparse(&revspec, walk->repo, range)))
221 return error;
222
223 if (!revspec.to) {
224 git_error_set(GIT_ERROR_INVALID, "invalid revspec: range not provided");
225 error = GIT_EINVALIDSPEC;
226 goto out;
227 }
228
229 if (revspec.flags & GIT_REVPARSE_MERGE_BASE) {
230 /* TODO: support "<commit>...<commit>" */
231 git_error_set(GIT_ERROR_INVALID, "symmetric differences not implemented in revwalk");
232 error = GIT_EINVALIDSPEC;
233 goto out;
234 }
235
236 opts.uninteresting = 1;
237 if ((error = git_revwalk__push_commit(walk, git_object_id(revspec.from), &opts)))
238 goto out;
239
240 opts.uninteresting = 0;
241 error = git_revwalk__push_commit(walk, git_object_id(revspec.to), &opts);
242
243 out:
244 git_object_free(revspec.from);
245 git_object_free(revspec.to);
246 return error;
247 }
248
249 int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
250 {
251 git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
252 assert(walk && refname);
253 opts.uninteresting = 1;
254 return git_revwalk__push_ref(walk, refname, &opts);
255 }
256
257 static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit)
258 {
259 return git_pqueue_insert(&walk->iterator_time, commit);
260 }
261
262 static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit)
263 {
264 return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1;
265 }
266
267 static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk)
268 {
269 git_commit_list_node *next;
270
271 while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
272 /* Some commits might become uninteresting after being added to the list */
273 if (!next->uninteresting) {
274 *object_out = next;
275 return 0;
276 }
277 }
278
279 git_error_clear();
280 return GIT_ITEROVER;
281 }
282
283 static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk)
284 {
285 int error;
286 git_commit_list_node *next;
287
288 while (!(error = get_revision(&next, walk, &walk->iterator_rand))) {
289 /* Some commits might become uninteresting after being added to the list */
290 if (!next->uninteresting) {
291 *object_out = next;
292 return 0;
293 }
294 }
295
296 return error;
297 }
298
299 static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk)
300 {
301 int error;
302 git_commit_list_node *next;
303
304 while (!(error = get_revision(&next, walk, &walk->iterator_topo))) {
305 /* Some commits might become uninteresting after being added to the list */
306 if (!next->uninteresting) {
307 *object_out = next;
308 return 0;
309 }
310 }
311
312 return error;
313 }
314
315 static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk)
316 {
317 *object_out = git_commit_list_pop(&walk->iterator_reverse);
318 return *object_out ? 0 : GIT_ITEROVER;
319 }
320
321 static void mark_parents_uninteresting(git_commit_list_node *commit)
322 {
323 unsigned short i;
324 git_commit_list *parents = NULL;
325
326 for (i = 0; i < commit->out_degree; i++)
327 git_commit_list_insert(commit->parents[i], &parents);
328
329
330 while (parents) {
331 commit = git_commit_list_pop(&parents);
332
333 while (commit) {
334 if (commit->uninteresting)
335 break;
336
337 commit->uninteresting = 1;
338 /*
339 * If we've reached this commit some other way
340 * already, we need to mark its parents uninteresting
341 * as well.
342 */
343 if (!commit->parents)
344 break;
345
346 for (i = 0; i < commit->out_degree; i++)
347 git_commit_list_insert(commit->parents[i], &parents);
348 commit = commit->parents[0];
349 }
350 }
351 }
352
353 static int add_parents_to_list(git_revwalk *walk, git_commit_list_node *commit, git_commit_list **list)
354 {
355 unsigned short i;
356 int error;
357
358 if (commit->added)
359 return 0;
360
361 commit->added = 1;
362
363 /*
364 * Go full on in the uninteresting case as we want to include
365 * as many of these as we can.
366 *
367 * Usually we haven't parsed the parent of a parent, but if we
368 * have it we reached it via other means so we want to mark
369 * its parents recursively too.
370 */
371 if (commit->uninteresting) {
372 for (i = 0; i < commit->out_degree; i++) {
373 git_commit_list_node *p = commit->parents[i];
374 p->uninteresting = 1;
375
376 /* git does it gently here, but we don't like missing objects */
377 if ((error = git_commit_list_parse(walk, p)) < 0)
378 return error;
379
380 if (p->parents)
381 mark_parents_uninteresting(p);
382
383 p->seen = 1;
384 git_commit_list_insert_by_date(p, list);
385 }
386
387 return 0;
388 }
389
390 /*
391 * Now on to what we do if the commit is indeed
392 * interesting. Here we do want things like first-parent take
393 * effect as this is what we'll be showing.
394 */
395 for (i = 0; i < commit->out_degree; i++) {
396 git_commit_list_node *p = commit->parents[i];
397
398 if ((error = git_commit_list_parse(walk, p)) < 0)
399 return error;
400
401 if (walk->hide_cb && walk->hide_cb(&p->oid, walk->hide_cb_payload))
402 continue;
403
404 if (!p->seen) {
405 p->seen = 1;
406 git_commit_list_insert_by_date(p, list);
407 }
408
409 if (walk->first_parent)
410 break;
411 }
412 return 0;
413 }
414
415 /* How many unintersting commits we want to look at after we run out of interesting ones */
416 #define SLOP 5
417
418 static int still_interesting(git_commit_list *list, int64_t time, int slop)
419 {
420 /* The empty list is pretty boring */
421 if (!list)
422 return 0;
423
424 /*
425 * If the destination list has commits with an earlier date than our
426 * source, we want to reset the slop counter as we're not done.
427 */
428 if (time <= list->item->time)
429 return SLOP;
430
431 for (; list; list = list->next) {
432 /*
433 * If the destination list still contains interesting commits we
434 * want to continue looking.
435 */
436 if (!list->item->uninteresting || list->item->time > time)
437 return SLOP;
438 }
439
440 /* Everything's uninteresting, reduce the count */
441 return slop - 1;
442 }
443
444 static int limit_list(git_commit_list **out, git_revwalk *walk, git_commit_list *commits)
445 {
446 int error, slop = SLOP;
447 int64_t time = INT64_MAX;
448 git_commit_list *list = commits;
449 git_commit_list *newlist = NULL;
450 git_commit_list **p = &newlist;
451
452 while (list) {
453 git_commit_list_node *commit = git_commit_list_pop(&list);
454
455 if ((error = add_parents_to_list(walk, commit, &list)) < 0)
456 return error;
457
458 if (commit->uninteresting) {
459 mark_parents_uninteresting(commit);
460
461 slop = still_interesting(list, time, slop);
462 if (slop)
463 continue;
464
465 break;
466 }
467
468 if (walk->hide_cb && walk->hide_cb(&commit->oid, walk->hide_cb_payload))
469 continue;
470
471 time = commit->time;
472 p = &git_commit_list_insert(commit, p)->next;
473 }
474
475 git_commit_list_free(&list);
476 *out = newlist;
477 return 0;
478 }
479
480 static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list)
481 {
482 int error;
483 git_commit_list_node *commit;
484
485 commit = git_commit_list_pop(list);
486 if (!commit) {
487 git_error_clear();
488 return GIT_ITEROVER;
489 }
490
491 /*
492 * If we did not run limit_list and we must add parents to the
493 * list ourselves.
494 */
495 if (!walk->limited) {
496 if ((error = add_parents_to_list(walk, commit, list)) < 0)
497 return error;
498 }
499
500 *out = commit;
501 return 0;
502 }
503
504 static int sort_in_topological_order(git_commit_list **out, git_revwalk *walk, git_commit_list *list)
505 {
506 git_commit_list *ll = NULL, *newlist, **pptr;
507 git_commit_list_node *next;
508 git_pqueue queue;
509 git_vector_cmp queue_cmp = NULL;
510 unsigned short i;
511 int error;
512
513 if (walk->sorting & GIT_SORT_TIME)
514 queue_cmp = git_commit_list_time_cmp;
515
516 if ((error = git_pqueue_init(&queue, 0, 8, queue_cmp)))
517 return error;
518
519 /*
520 * Start by resetting the in-degree to 1 for the commits in
521 * our list. We want to go through this list again, so we
522 * store it in the commit list as we extract it from the lower
523 * machinery.
524 */
525 for (ll = list; ll; ll = ll->next) {
526 ll->item->in_degree = 1;
527 }
528
529 /*
530 * Count up how many children each commit has. We limit
531 * ourselves to those commits in the original list (in-degree
532 * of 1) avoiding setting it for any parent that was hidden.
533 */
534 for(ll = list; ll; ll = ll->next) {
535 for (i = 0; i < ll->item->out_degree; ++i) {
536 git_commit_list_node *parent = ll->item->parents[i];
537 if (parent->in_degree)
538 parent->in_degree++;
539 }
540 }
541
542 /*
543 * Now we find the tips i.e. those not reachable from any other node
544 * i.e. those which still have an in-degree of 1.
545 */
546 for(ll = list; ll; ll = ll->next) {
547 if (ll->item->in_degree == 1) {
548 if ((error = git_pqueue_insert(&queue, ll->item)))
549 goto cleanup;
550 }
551 }
552
553 /*
554 * We need to output the tips in the order that they came out of the
555 * traversal, so if we're not doing time-sorting, we need to reverse the
556 * pqueue in order to get them to come out as we inserted them.
557 */
558 if ((walk->sorting & GIT_SORT_TIME) == 0)
559 git_pqueue_reverse(&queue);
560
561
562 pptr = &newlist;
563 newlist = NULL;
564 while ((next = git_pqueue_pop(&queue)) != NULL) {
565 for (i = 0; i < next->out_degree; ++i) {
566 git_commit_list_node *parent = next->parents[i];
567 if (parent->in_degree == 0)
568 continue;
569
570 if (--parent->in_degree == 1) {
571 if ((error = git_pqueue_insert(&queue, parent)))
572 goto cleanup;
573 }
574 }
575
576 /* All the children of 'item' have been emitted (since we got to it via the priority queue) */
577 next->in_degree = 0;
578
579 pptr = &git_commit_list_insert(next, pptr)->next;
580 }
581
582 *out = newlist;
583 error = 0;
584
585 cleanup:
586 git_pqueue_free(&queue);
587 return error;
588 }
589
590 static int prepare_walk(git_revwalk *walk)
591 {
592 int error = 0;
593 git_commit_list *list, *commits = NULL;
594 git_commit_list_node *next;
595
596 /* If there were no pushes, we know that the walk is already over */
597 if (!walk->did_push) {
598 git_error_clear();
599 return GIT_ITEROVER;
600 }
601
602 for (list = walk->user_input; list; list = list->next) {
603 git_commit_list_node *commit = list->item;
604 if ((error = git_commit_list_parse(walk, commit)) < 0)
605 return error;
606
607 if (commit->uninteresting)
608 mark_parents_uninteresting(commit);
609
610 if (!commit->seen) {
611 commit->seen = 1;
612 git_commit_list_insert(commit, &commits);
613 }
614 }
615
616 if (walk->limited && (error = limit_list(&commits, walk, commits)) < 0)
617 return error;
618
619 if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
620 error = sort_in_topological_order(&walk->iterator_topo, walk, commits);
621 git_commit_list_free(&commits);
622
623 if (error < 0)
624 return error;
625
626 walk->get_next = &revwalk_next_toposort;
627 } else if (walk->sorting & GIT_SORT_TIME) {
628 for (list = commits; list && !error; list = list->next)
629 error = walk->enqueue(walk, list->item);
630
631 git_commit_list_free(&commits);
632
633 if (error < 0)
634 return error;
635 } else {
636 walk->iterator_rand = commits;
637 walk->get_next = revwalk_next_unsorted;
638 }
639
640 if (walk->sorting & GIT_SORT_REVERSE) {
641
642 while ((error = walk->get_next(&next, walk)) == 0)
643 if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL)
644 return -1;
645
646 if (error != GIT_ITEROVER)
647 return error;
648
649 walk->get_next = &revwalk_next_reverse;
650 }
651
652 walk->walking = 1;
653 return 0;
654 }
655
656
657 int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
658 {
659 git_revwalk *walk = git__calloc(1, sizeof(git_revwalk));
660 GIT_ERROR_CHECK_ALLOC(walk);
661
662 if (git_oidmap_new(&walk->commits) < 0 ||
663 git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 ||
664 git_pool_init(&walk->commit_pool, COMMIT_ALLOC) < 0)
665 return -1;
666
667 walk->get_next = &revwalk_next_unsorted;
668 walk->enqueue = &revwalk_enqueue_unsorted;
669
670 walk->repo = repo;
671
672 if (git_repository_odb(&walk->odb, repo) < 0) {
673 git_revwalk_free(walk);
674 return -1;
675 }
676
677 *revwalk_out = walk;
678 return 0;
679 }
680
681 void git_revwalk_free(git_revwalk *walk)
682 {
683 if (walk == NULL)
684 return;
685
686 git_revwalk_reset(walk);
687 git_odb_free(walk->odb);
688
689 git_oidmap_free(walk->commits);
690 git_pool_clear(&walk->commit_pool);
691 git_pqueue_free(&walk->iterator_time);
692 git__free(walk);
693 }
694
695 git_repository *git_revwalk_repository(git_revwalk *walk)
696 {
697 assert(walk);
698 return walk->repo;
699 }
700
701 int git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode)
702 {
703 assert(walk);
704
705 if (walk->walking)
706 git_revwalk_reset(walk);
707
708 walk->sorting = sort_mode;
709
710 if (walk->sorting & GIT_SORT_TIME) {
711 walk->get_next = &revwalk_next_timesort;
712 walk->enqueue = &revwalk_enqueue_timesort;
713 } else {
714 walk->get_next = &revwalk_next_unsorted;
715 walk->enqueue = &revwalk_enqueue_unsorted;
716 }
717
718 if (walk->sorting != GIT_SORT_NONE)
719 walk->limited = 1;
720
721 return 0;
722 }
723
724 int git_revwalk_simplify_first_parent(git_revwalk *walk)
725 {
726 walk->first_parent = 1;
727 return 0;
728 }
729
730 int git_revwalk_next(git_oid *oid, git_revwalk *walk)
731 {
732 int error;
733 git_commit_list_node *next;
734
735 assert(walk && oid);
736
737 if (!walk->walking) {
738 if ((error = prepare_walk(walk)) < 0)
739 return error;
740 }
741
742 error = walk->get_next(&next, walk);
743
744 if (error == GIT_ITEROVER) {
745 git_revwalk_reset(walk);
746 git_error_clear();
747 return GIT_ITEROVER;
748 }
749
750 if (!error)
751 git_oid_cpy(oid, &next->oid);
752
753 return error;
754 }
755
756 int git_revwalk_reset(git_revwalk *walk)
757 {
758 git_commit_list_node *commit;
759
760 assert(walk);
761
762 git_oidmap_foreach_value(walk->commits, commit, {
763 commit->seen = 0;
764 commit->in_degree = 0;
765 commit->topo_delay = 0;
766 commit->uninteresting = 0;
767 commit->added = 0;
768 commit->flags = 0;
769 });
770
771 git_pqueue_clear(&walk->iterator_time);
772 git_commit_list_free(&walk->iterator_topo);
773 git_commit_list_free(&walk->iterator_rand);
774 git_commit_list_free(&walk->iterator_reverse);
775 git_commit_list_free(&walk->user_input);
776 walk->first_parent = 0;
777 walk->walking = 0;
778 walk->limited = 0;
779 walk->did_push = walk->did_hide = 0;
780 walk->sorting = GIT_SORT_NONE;
781
782 return 0;
783 }
784
785 int git_revwalk_add_hide_cb(
786 git_revwalk *walk,
787 git_revwalk_hide_cb hide_cb,
788 void *payload)
789 {
790 assert(walk);
791
792 if (walk->walking)
793 git_revwalk_reset(walk);
794
795 walk->hide_cb = hide_cb;
796 walk->hide_cb_payload = payload;
797
798 if (hide_cb)
799 walk->limited = 1;
800
801 return 0;
802 }
803