Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index: introduce git_index_iterator #4884

Merged
merged 1 commit into from
Nov 21, 2018
Merged

index: introduce git_index_iterator #4884

merged 1 commit into from
Nov 21, 2018

Conversation

ethomson
Copy link
Member

Provide a public git_index_iterator API that is backed by an index snapshot. This allows consumers to provide a stable iteration even while manipulating the index during iteration.

@ethomson
Copy link
Member Author

Note that I think this is more accurately re-introduce. I think that we used to have an iterator for the index (likely with callbacks, because that was the iterator pattern of the day way back then) but we got rid of it in favor of expecting consumers to use git_index_get_byindex. However, since we have index snapshots, we can use those to provide callers a stable iteration function that still allows them to manipulate the index during iteration.

seen++;
}

cl_assert_equal_i(expected, seen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails on all platforms:

  1) Failure:
index::tests::can_modify_while_iterating [D:\a\1\s\tests\index\tests.c:1073]
  expected != seen
  109 != 110

This is explained by the off-by-one above

git_index *index);

/**
* Return the next index entry in-order from the iterator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe state what order the caller may expect?

cl_assert_equal_i(entry->file_size, test_entries[i].file_size);
found++;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the iterator would return the same entry ARRAY_SIZE(test_entries) times, then this test would still succeed. If the test entries array was sorted as expected, then you could instead just loop through test_entries and verify that the current entry from test entries matches the one returned by the iterator. Like that, we know that all entries have been found and that the order is the expected one while avoiding this nested loop.

src/index.c Outdated
{
assert(out && it);

if (it->cur > git_vector_length(&it->snap))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an off-by-one here. You will try to access the vector with it->cur == git_vector_length(&it->snap), leading to an OOB

Provide a public git_index_iterator API that is backed by an index
snapshot.  This allows consumers to provide a stable iteration even
while manipulating the index during iteration.
@ethomson
Copy link
Member Author

Oy, thanks for pointing out my arithmetic error. 🙄 Fixed up with your suggestions.

@pks-t pks-t merged commit 0e3e832 into master Nov 21, 2018
@pks-t
Copy link
Member

pks-t commented Nov 21, 2018

Looks good to me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants