revwalk.c 18.7 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9
#include "revwalk.h"

10
#include "commit.h"
Vicent Marti committed
11
#include "odb.h"
12
#include "pool.h"
13

14
#include "git2/revparse.h"
15
#include "merge.h"
16
#include "vector.h"
17

18 19
static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list);

20 21
git_commit_list_node *git_revwalk__commit_lookup(
	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 27
	if ((commit = git_oidmap_get(walk->commits, oid)) != NULL)
		return commit;
28

29
	commit = git_commit_list_alloc_node(walk);
30 31
	if (commit == NULL)
		return NULL;
32

33
	git_oid_cpy(&commit->oid, oid);
34

35 36
	if ((git_oidmap_set(walk->commits, &commit->oid, commit)) < 0)
		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 59
			return 0;

60
		git_error_set(GIT_ERROR_INVALID, "object is not a committish");
61
		return error;
62
	}
63 64 65 66 67
	if (error < 0)
		return error;

	git_oid_cpy(&commit_id, git_object_id(obj));
	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 74 75 76
	/* A previous hide already told us we don't want this commit  */
	if (commit->uninteresting)
		return 0;

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 87 88
	if ((opts->insert_by_date &&
	    git_commit_list_insert_by_date(commit, &list) == NULL) ||
	    git_commit_list_insert(commit, &list) == NULL) {
89
		git_error_set_oom();
90
		return -1;
91 92
	}

93 94
	walk->user_input = list;

95
	return 0;
96 97
}

98 99
int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
{
100 101
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;

102 103
	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(oid);
104 105

	return git_revwalk__push_commit(walk, oid, &opts);
106
}
107

108

109 110
int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
{
111
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
112 113 114

	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(oid);
115 116 117

	opts.uninteresting = 1;
	return git_revwalk__push_commit(walk, oid, &opts);
118
}
119

120
int git_revwalk__push_ref(git_revwalk *walk, const char *refname, const git_revwalk__push_options *opts)
121
{
122
	git_oid oid;
123

124
	if (git_reference_name_to_id(&oid, walk->repo, refname) < 0)
125 126
		return -1;

127
	return git_revwalk__push_commit(walk, &oid, opts);
128 129
}

130
int git_revwalk__push_glob(git_revwalk *walk, const char *glob, const git_revwalk__push_options *given_opts)
131
{
132
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
133
	int error = 0;
134
	git_str buf = GIT_STR_INIT;
135 136
	git_reference *ref;
	git_reference_iterator *iter;
137
	size_t wildcard;
138

139 140
	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(glob);
141

142 143 144
	if (given_opts)
		memcpy(&opts, given_opts, sizeof(opts));

145
	/* refs/ is implied if not given in the glob */
146
	if (git__prefixcmp(glob, GIT_REFS_DIR) != 0)
147
		git_str_joinpath(&buf, GIT_REFS_DIR, glob);
148
	else
149 150
		git_str_puts(&buf, glob);
	GIT_ERROR_CHECK_ALLOC_STR(&buf);
151 152

	/* If no '?', '*' or '[' exist, we append '/ *' to the glob */
153 154
	wildcard = strcspn(glob, "?*[");
	if (!glob[wildcard])
155
		git_str_put(&buf, "/*", 2);
156

157 158
	if ((error = git_reference_iterator_glob_new(&iter, walk->repo, buf.ptr)) < 0)
		goto out;
159

160
	opts.from_glob = true;
161
	while ((error = git_reference_next(&ref, iter)) == 0) {
162
		error = git_revwalk__push_ref(walk, git_reference_name(ref), &opts);
163 164 165 166 167
		git_reference_free(ref);
		if (error < 0)
			break;
	}
	git_reference_iterator_free(iter);
168

169 170 171
	if (error == GIT_ITEROVER)
		error = 0;
out:
172
	git_str_dispose(&buf);
173
	return error;
174 175 176 177
}

int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
{
178
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
179 180 181

	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(glob);
182 183

	return git_revwalk__push_glob(walk, glob, &opts);
184 185 186 187
}

int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
{
188
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
189 190 191

	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(glob);
192 193 194

	opts.uninteresting = 1;
	return git_revwalk__push_glob(walk, glob, &opts);
195 196
}

197 198
int git_revwalk_push_head(git_revwalk *walk)
{
199
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
200 201

	GIT_ASSERT_ARG(walk);
202 203

	return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts);
204 205 206 207
}

int git_revwalk_hide_head(git_revwalk *walk)
{
208
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
209 210

	GIT_ASSERT_ARG(walk);
211 212 213

	opts.uninteresting = 1;
	return git_revwalk__push_ref(walk, GIT_HEAD_FILE, &opts);
214 215 216 217
}

int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
{
218
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
219 220 221

	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(refname);
222 223

	return git_revwalk__push_ref(walk, refname, &opts);
224 225
}

226 227
int git_revwalk_push_range(git_revwalk *walk, const char *range)
{
228
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
229
	git_revspec revspec;
230 231
	int error = 0;

232
	if ((error = git_revparse(&revspec, walk->repo, range)))
233
		return error;
Vicent Marti committed
234

235
	if (!revspec.to) {
236
		git_error_set(GIT_ERROR_INVALID, "invalid revspec: range not provided");
237 238 239 240
		error = GIT_EINVALIDSPEC;
		goto out;
	}

241
	if (revspec.flags & GIT_REVSPEC_MERGE_BASE) {
242
		/* TODO: support "<commit>...<commit>" */
243
		git_error_set(GIT_ERROR_INVALID, "symmetric differences not implemented in revwalk");
244 245
		error = GIT_EINVALIDSPEC;
		goto out;
246 247
	}

248 249
	opts.uninteresting = 1;
	if ((error = git_revwalk__push_commit(walk, git_object_id(revspec.from), &opts)))
250 251
		goto out;

252 253
	opts.uninteresting = 0;
	error = git_revwalk__push_commit(walk, git_object_id(revspec.to), &opts);
Vicent Marti committed
254 255

out:
256 257
	git_object_free(revspec.from);
	git_object_free(revspec.to);
258 259 260
	return error;
}

261 262
int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
{
263
	git_revwalk__push_options opts = GIT_REVWALK__PUSH_OPTIONS_INIT;
264 265 266 267

	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(refname);

268 269
	opts.uninteresting = 1;
	return git_revwalk__push_ref(walk, refname, &opts);
270 271
}

272
static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit)
273
{
274 275
	return git_pqueue_insert(&walk->iterator_time, commit);
}
276

277
static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit)
278
{
279
	return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1;
280
}
281

282
static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk)
283
{
284
	git_commit_list_node *next;
285

286 287 288 289 290 291
	while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
		/* Some commits might become uninteresting after being added to the list */
		if (!next->uninteresting) {
			*object_out = next;
			return 0;
		}
292
	}
293

294
	git_error_clear();
Russell Belfer committed
295
	return GIT_ITEROVER;
296 297
}

298
static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk)
299
{
300
	int error;
301
	git_commit_list_node *next;
302

303
	while (!(error = get_revision(&next, walk, &walk->iterator_rand))) {
304 305 306 307 308
		/* Some commits might become uninteresting after being added to the list */
		if (!next->uninteresting) {
			*object_out = next;
			return 0;
		}
309
	}
310

311
	return error;
312
}
313

314
static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk)
315
{
316
	int error;
317
	git_commit_list_node *next;
318

319
	while (!(error = get_revision(&next, walk, &walk->iterator_topo))) {
320 321 322 323 324
		/* Some commits might become uninteresting after being added to the list */
		if (!next->uninteresting) {
			*object_out = next;
			return 0;
		}
325
	}
326

327
	return error;
328 329
}

330
static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk)
331
{
332
	*object_out = git_commit_list_pop(&walk->iterator_reverse);
Russell Belfer committed
333
	return *object_out ? 0 : GIT_ITEROVER;
334
}
335

336
static void mark_parents_uninteresting(git_commit_list_node *commit)
337
{
338 339
	unsigned short i;
	git_commit_list *parents = NULL;
340

341 342
	for (i = 0; i < commit->out_degree; i++)
		git_commit_list_insert(commit->parents[i], &parents);
343

344 345

	while (parents) {
346
		commit = git_commit_list_pop(&parents);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

		while (commit) {
			if (commit->uninteresting)
				break;

			commit->uninteresting = 1;
			/*
			 * If we've reached this commit some other way
			 * already, we need to mark its parents uninteresting
			 * as well.
			 */
			if (!commit->parents)
				break;

			for (i = 0; i < commit->out_degree; i++)
				git_commit_list_insert(commit->parents[i], &parents);
			commit = commit->parents[0];
		}
	}
366 367
}

368
static int add_parents_to_list(git_revwalk *walk, git_commit_list_node *commit, git_commit_list **list)
369 370
{
	unsigned short i;
371
	int error;
372

373 374
	if (commit->added)
		return 0;
375

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	commit->added = 1;

	/*
	 * Go full on in the uninteresting case as we want to include
	 * as many of these as we can.
	 *
	 * Usually we haven't parsed the parent of a parent, but if we
	 * have it we reached it via other means so we want to mark
	 * its parents recursively too.
	 */
	if (commit->uninteresting) {
		for (i = 0; i < commit->out_degree; i++) {
			git_commit_list_node *p = commit->parents[i];
			p->uninteresting = 1;

			/* git does it gently here, but we don't like missing objects */
			if ((error = git_commit_list_parse(walk, p)) < 0)
				return error;

			if (p->parents)
				mark_parents_uninteresting(p);

			p->seen = 1;
			git_commit_list_insert_by_date(p, list);
		}

		return 0;
403 404
	}

405 406 407 408 409 410 411
	/*
	 * Now on to what we do if the commit is indeed
	 * interesting. Here we do want things like first-parent take
	 * effect as this is what we'll be showing.
	 */
	for (i = 0; i < commit->out_degree; i++) {
		git_commit_list_node *p = commit->parents[i];
412

413 414
		if ((error = git_commit_list_parse(walk, p)) < 0)
			return error;
415

416
		if (walk->hide_cb && walk->hide_cb(&p->oid, walk->hide_cb_payload))
417
			continue;
418

419 420 421 422 423 424 425 426 427 428
		if (!p->seen) {
			p->seen = 1;
			git_commit_list_insert_by_date(p, list);
		}

		if (walk->first_parent)
			break;
	}
	return 0;
}
429

Dimitris Apostolou committed
430
/* How many uninteresting commits we want to look at after we run out of interesting ones */
431 432 433 434 435 436 437 438
#define SLOP 5

static int still_interesting(git_commit_list *list, int64_t time, int slop)
{
	/* The empty list is pretty boring */
	if (!list)
		return 0;

439 440 441 442 443 444 445
	/*
	 * If the destination list has commits with an earlier date than our
	 * source, we want to reset the slop counter as we're not done.
	 */
	if (time <= list->item->time)
		return SLOP;

446 447
	for (; list; list = list->next) {
		/*
448
		 * If the destination list still contains interesting commits we
449 450 451 452 453
		 * want to continue looking.
		 */
		if (!list->item->uninteresting || list->item->time > time)
			return SLOP;
	}
454 455 456 457 458 459 460 461

	/* Everything's uninteresting, reduce the count */
	return slop - 1;
}

static int limit_list(git_commit_list **out, git_revwalk *walk, git_commit_list *commits)
{
	int error, slop = SLOP;
462
	int64_t time = INT64_MAX;
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	git_commit_list *list = commits;
	git_commit_list *newlist = NULL;
	git_commit_list **p = &newlist;

	while (list) {
		git_commit_list_node *commit = git_commit_list_pop(&list);

		if ((error = add_parents_to_list(walk, commit, &list)) < 0)
			return error;

		if (commit->uninteresting) {
			mark_parents_uninteresting(commit);

			slop = still_interesting(list, time, slop);
			if (slop)
478 479
				continue;

480
			break;
481
		}
482

483 484
		if (walk->hide_cb && walk->hide_cb(&commit->oid, walk->hide_cb_payload))
			continue;
485 486 487

		time = commit->time;
		p = &git_commit_list_insert(commit, p)->next;
488 489
	}

490
	git_commit_list_free(&list);
491 492
	*out = newlist;
	return 0;
493 494
}

495 496 497 498 499
static int get_revision(git_commit_list_node **out, git_revwalk *walk, git_commit_list **list)
{
	int error;
	git_commit_list_node *commit;

500 501
	commit = git_commit_list_pop(list);
	if (!commit) {
502
		git_error_clear();
503 504 505 506 507 508 509 510 511
		return GIT_ITEROVER;
	}

	/*
	 * If we did not run limit_list and we must add parents to the
	 * list ourselves.
	 */
	if (!walk->limited) {
		if ((error = add_parents_to_list(walk, commit, list)) < 0)
512
			return error;
513 514 515 516
	}

	*out = commit;
	return 0;
517 518
}

519
static int sort_in_topological_order(git_commit_list **out, git_revwalk *walk, git_commit_list *list)
520
{
521
	git_commit_list *ll = NULL, *newlist, **pptr;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	git_commit_list_node *next;
	git_pqueue queue;
	git_vector_cmp queue_cmp = NULL;
	unsigned short i;
	int error;

	if (walk->sorting & GIT_SORT_TIME)
		queue_cmp = git_commit_list_time_cmp;

	if ((error = git_pqueue_init(&queue, 0, 8, queue_cmp)))
		return error;

	/*
	 * Start by resetting the in-degree to 1 for the commits in
	 * our list. We want to go through this list again, so we
	 * store it in the commit list as we extract it from the lower
	 * machinery.
	 */
540 541
	for (ll = list; ll; ll = ll->next) {
		ll->item->in_degree = 1;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	}

	/*
	 * Count up how many children each commit has. We limit
	 * ourselves to those commits in the original list (in-degree
	 * of 1) avoiding setting it for any parent that was hidden.
	 */
	for(ll = list; ll; ll = ll->next) {
		for (i = 0; i < ll->item->out_degree; ++i) {
			git_commit_list_node *parent = ll->item->parents[i];
			if (parent->in_degree)
				parent->in_degree++;
		}
	}

	/*
	 * Now we find the tips i.e. those not reachable from any other node
	 * i.e. those which still have an in-degree of 1.
	 */
	for(ll = list; ll; ll = ll->next) {
		if (ll->item->in_degree == 1) {
			if ((error = git_pqueue_insert(&queue, ll->item)))
				goto cleanup;
		}
	}

	/*
	 * We need to output the tips in the order that they came out of the
	 * traversal, so if we're not doing time-sorting, we need to reverse the
	 * pqueue in order to get them to come out as we inserted them.
	 */
	if ((walk->sorting & GIT_SORT_TIME) == 0)
		git_pqueue_reverse(&queue);


	pptr = &newlist;
	newlist = NULL;
	while ((next = git_pqueue_pop(&queue)) != NULL) {
		for (i = 0; i < next->out_degree; ++i) {
			git_commit_list_node *parent = next->parents[i];
			if (parent->in_degree == 0)
				continue;

			if (--parent->in_degree == 1) {
				if ((error = git_pqueue_insert(&queue, parent)))
					goto cleanup;
			}
		}

		/* All the children of 'item' have been emitted (since we got to it via the priority queue) */
		next->in_degree = 0;

		pptr = &git_commit_list_insert(next, pptr)->next;
	}

	*out = newlist;
	error = 0;

cleanup:
	git_pqueue_free(&queue);
	return error;
}

605 606
static int prepare_walk(git_revwalk *walk)
{
607
	int error = 0;
608
	git_commit_list *list, *commits = NULL;
609 610
	git_commit_list_node *next;

611 612
	/* If there were no pushes, we know that the walk is already over */
	if (!walk->did_push) {
613
		git_error_clear();
614 615 616
		return GIT_ITEROVER;
	}

617 618 619 620 621 622 623 624 625 626 627 628 629 630
	for (list = walk->user_input; list; list = list->next) {
		git_commit_list_node *commit = list->item;
		if ((error = git_commit_list_parse(walk, commit)) < 0)
			return error;

		if (commit->uninteresting)
			mark_parents_uninteresting(commit);

		if (!commit->seen) {
			commit->seen = 1;
			git_commit_list_insert(commit, &commits);
		}
	}

631
	if (walk->limited && (error = limit_list(&commits, walk, commits)) < 0)
632
		return error;
633

634
	if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
635 636 637 638
		error = sort_in_topological_order(&walk->iterator_topo, walk, commits);
		git_commit_list_free(&commits);

		if (error < 0)
639
			return error;
640

641
		walk->get_next = &revwalk_next_toposort;
642 643 644 645 646 647 648 649 650 651 652
	} else if (walk->sorting & GIT_SORT_TIME) {
		for (list = commits; list && !error; list = list->next)
			error = walk->enqueue(walk, list->item);

		git_commit_list_free(&commits);

		if (error < 0)
			return error;
	} else {
		walk->iterator_rand = commits;
		walk->get_next = revwalk_next_unsorted;
653
	}
654

655
	if (walk->sorting & GIT_SORT_REVERSE) {
656

657
		while ((error = walk->get_next(&next, walk)) == 0)
658
			if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL)
659
				return -1;
660

Russell Belfer committed
661
		if (error != GIT_ITEROVER)
662
			return error;
663

664 665
		walk->get_next = &revwalk_next_reverse;
	}
666

667
	walk->walking = 1;
668
	return 0;
669
}
670 671


672 673
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
{
674
	git_revwalk *walk = git__calloc(1, sizeof(git_revwalk));
675
	GIT_ERROR_CHECK_ALLOC(walk);
676

677 678 679
	if (git_oidmap_new(&walk->commits) < 0 ||
	    git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 ||
	    git_pool_init(&walk->commit_pool, COMMIT_ALLOC) < 0)
680
		return -1;
681 682 683 684 685 686

	walk->get_next = &revwalk_next_unsorted;
	walk->enqueue = &revwalk_enqueue_unsorted;

	walk->repo = repo;

687
	if (git_repository_odb(&walk->odb, repo) < 0) {
688
		git_revwalk_free(walk);
689
		return -1;
690 691
	}

692
	*revwalk_out = walk;
693
	return 0;
694 695 696 697 698 699 700 701
}

void git_revwalk_free(git_revwalk *walk)
{
	if (walk == NULL)
		return;

	git_revwalk_reset(walk);
702
	git_odb_free(walk->odb);
703

704
	git_oidmap_free(walk->commits);
705
	git_pool_clear(&walk->commit_pool);
706
	git_pqueue_free(&walk->iterator_time);
707
	git__free(walk);
708 709 710 711
}

git_repository *git_revwalk_repository(git_revwalk *walk)
{
712 713
	GIT_ASSERT_ARG_WITH_RETVAL(walk, NULL);

714 715 716
	return walk->repo;
}

717
int git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode)
718
{
719
	GIT_ASSERT_ARG(walk);
720 721 722 723 724 725 726 727 728 729 730 731 732

	if (walk->walking)
		git_revwalk_reset(walk);

	walk->sorting = sort_mode;

	if (walk->sorting & GIT_SORT_TIME) {
		walk->get_next = &revwalk_next_timesort;
		walk->enqueue = &revwalk_enqueue_timesort;
	} else {
		walk->get_next = &revwalk_next_unsorted;
		walk->enqueue = &revwalk_enqueue_unsorted;
	}
733

734
	if (walk->sorting != GIT_SORT_NONE)
735
		walk->limited = 1;
736 737

	return 0;
738 739
}

740
int git_revwalk_simplify_first_parent(git_revwalk *walk)
741 742
{
	walk->first_parent = 1;
743
	return 0;
744 745
}

746 747 748
int git_revwalk_next(git_oid *oid, git_revwalk *walk)
{
	int error;
749
	git_commit_list_node *next;
750

751 752
	GIT_ASSERT_ARG(walk);
	GIT_ASSERT_ARG(oid);
753

754
	if (!walk->walking) {
755
		if ((error = prepare_walk(walk)) < 0)
756
			return error;
757
	}
758

759
	error = walk->get_next(&next, walk);
760

Russell Belfer committed
761
	if (error == GIT_ITEROVER) {
762
		git_revwalk_reset(walk);
763
		git_error_clear();
Russell Belfer committed
764
		return GIT_ITEROVER;
765
	}
766

767 768
	if (!error)
		git_oid_cpy(oid, &next->oid);
769

770
	return error;
771 772
}

773
int git_revwalk_reset(git_revwalk *walk)
774
{
775
	git_commit_list_node *commit;
776

777
	GIT_ASSERT_ARG(walk);
778

779
	git_oidmap_foreach_value(walk->commits, commit, {
780 781 782
		commit->seen = 0;
		commit->in_degree = 0;
		commit->topo_delay = 0;
783
		commit->uninteresting = 0;
784
		commit->added = 0;
785
		commit->flags = 0;
786
		});
787

788
	git_pqueue_clear(&walk->iterator_time);
789 790 791
	git_commit_list_free(&walk->iterator_topo);
	git_commit_list_free(&walk->iterator_rand);
	git_commit_list_free(&walk->iterator_reverse);
792
	git_commit_list_free(&walk->user_input);
793
	walk->first_parent = 0;
794
	walk->walking = 0;
795
	walk->limited = 0;
796
	walk->did_push = walk->did_hide = 0;
797
	walk->sorting = GIT_SORT_NONE;
798 799

	return 0;
800
}
801

802 803 804 805 806
int git_revwalk_add_hide_cb(
	git_revwalk *walk,
	git_revwalk_hide_cb hide_cb,
	void *payload)
{
807
	GIT_ASSERT_ARG(walk);
808 809 810 811 812 813

	if (walk->walking)
		git_revwalk_reset(walk);

	walk->hide_cb = hide_cb;
	walk->hide_cb_payload = payload;
814 815 816

	if (hide_cb)
		walk->limited = 1;
817 818 819 820

	return 0;
}