Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efd5a4e

Browse files
author
Vicent Martí
committed
Merge pull request libgit2#1627 from arrbee/iterator-api-improvements
Make iterators use GIT_ITEROVER & smart advance
2 parents 1ed356d + cee695a commit efd5a4e

File tree

11 files changed

+329
-187
lines changed

11 files changed

+329
-187
lines changed

src/checkout.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,9 @@ static int checkout_action(
495495
if (cmp == 0) {
496496
if (wd->mode == GIT_FILEMODE_TREE) {
497497
/* case 2 - entry prefixed by workdir tree */
498-
if ((error = git_iterator_advance_into(&wd, workdir)) < 0) {
499-
if (error != GIT_ENOTFOUND ||
500-
git_iterator_advance(&wd, workdir) < 0)
501-
goto fail;
502-
}
503-
498+
error = git_iterator_advance_into_or_over(&wd, workdir);
499+
if (error && error != GIT_ITEROVER)
500+
goto fail;
504501
*wditem_ptr = wd;
505502
continue;
506503
}
@@ -515,8 +512,10 @@ static int checkout_action(
515512
}
516513

517514
/* case 1 - handle wd item (if it matches pathspec) */
518-
if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0 ||
519-
git_iterator_advance(&wd, workdir) < 0)
515+
if (checkout_action_wd_only(data, workdir, wd, pathspec) < 0)
516+
goto fail;
517+
if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
518+
error != GIT_ITEROVER)
520519
goto fail;
521520

522521
*wditem_ptr = wd;
@@ -539,8 +538,9 @@ static int checkout_action(
539538
if (delta->status == GIT_DELTA_TYPECHANGE) {
540539
if (delta->old_file.mode == GIT_FILEMODE_TREE) {
541540
act = checkout_action_with_wd(data, delta, wd);
542-
if (git_iterator_advance_into(&wd, workdir) < 0)
543-
wd = NULL;
541+
if ((error = git_iterator_advance_into(&wd, workdir)) < 0 &&
542+
error != GIT_ENOTFOUND)
543+
goto fail;
544544
*wditem_ptr = wd;
545545
return act;
546546
}
@@ -550,8 +550,9 @@ static int checkout_action(
550550
delta->old_file.mode == GIT_FILEMODE_COMMIT)
551551
{
552552
act = checkout_action_with_wd(data, delta, wd);
553-
if (git_iterator_advance(&wd, workdir) < 0)
554-
wd = NULL;
553+
if ((error = git_iterator_advance(&wd, workdir)) < 0 &&
554+
error != GIT_ITEROVER)
555+
goto fail;
555556
*wditem_ptr = wd;
556557
return act;
557558
}
@@ -582,6 +583,9 @@ static int checkout_remaining_wd_items(
582583
error = git_iterator_advance(&wd, workdir);
583584
}
584585

586+
if (error == GIT_ITEROVER)
587+
error = 0;
588+
585589
return error;
586590
}
587591

@@ -603,7 +607,8 @@ static int checkout_get_actions(
603607
git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0)
604608
return -1;
605609

606-
if ((error = git_iterator_current(&wditem, workdir)) < 0)
610+
if ((error = git_iterator_current(&wditem, workdir)) < 0 &&
611+
error != GIT_ITEROVER)
607612
goto fail;
608613

609614
deltas = &data->diff->deltas;

src/diff.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,9 @@ static int diff_scan_inside_untracked_dir(
796796
done:
797797
git_buf_free(&base);
798798

799+
if (error == GIT_ITEROVER)
800+
error = 0;
801+
799802
return error;
800803
}
801804

@@ -981,8 +984,11 @@ static int handle_matched_item(
981984
{
982985
int error = 0;
983986

984-
if (!(error = maybe_modified(diff, info)) &&
985-
!(error = git_iterator_advance(&info->oitem, info->old_iter)))
987+
if ((error = maybe_modified(diff, info)) < 0)
988+
return error;
989+
990+
if (!(error = git_iterator_advance(&info->oitem, info->old_iter)) ||
991+
error == GIT_ITEROVER)
986992
error = git_iterator_advance(&info->nitem, info->new_iter);
987993

988994
return error;
@@ -1011,15 +1017,22 @@ int git_diff__from_iterators(
10111017

10121018
/* make iterators have matching icase behavior */
10131019
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE)) {
1014-
if (!(error = git_iterator_set_ignore_case(old_iter, true)))
1015-
error = git_iterator_set_ignore_case(new_iter, true);
1020+
if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 ||
1021+
(error = git_iterator_set_ignore_case(new_iter, true)) < 0)
1022+
goto cleanup;
10161023
}
10171024

10181025
/* finish initialization */
1019-
if (!error &&
1020-
!(error = diff_list_apply_options(diff, opts)) &&
1021-
!(error = git_iterator_current(&info.oitem, old_iter)))
1022-
error = git_iterator_current(&info.nitem, new_iter);
1026+
if ((error = diff_list_apply_options(diff, opts)) < 0)
1027+
goto cleanup;
1028+
1029+
if ((error = git_iterator_current(&info.oitem, old_iter)) < 0 &&
1030+
error != GIT_ITEROVER)
1031+
goto cleanup;
1032+
if ((error = git_iterator_current(&info.nitem, new_iter)) < 0 &&
1033+
error != GIT_ITEROVER)
1034+
goto cleanup;
1035+
error = 0;
10231036

10241037
/* run iterators building diffs */
10251038
while (!error && (info.oitem || info.nitem)) {
@@ -1041,8 +1054,13 @@ int git_diff__from_iterators(
10411054
*/
10421055
else
10431056
error = handle_matched_item(diff, &info);
1057+
1058+
/* because we are iterating over two lists, ignore ITEROVER */
1059+
if (error == GIT_ITEROVER)
1060+
error = 0;
10441061
}
10451062

1063+
cleanup:
10461064
if (!error)
10471065
*diff_ptr = diff;
10481066
else

0 commit comments

Comments
 (0)