-
Notifications
You must be signed in to change notification settings - Fork 2.5k
reset: Allow the selective reset of pathspecs #1190
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
Conversation
/cc @jamill This should help pushing the |
Hmm. Travis doesn't like this PR very much
|
The travis errors are not related to the PR. Let's try re-triggering the build... |
Rebased, force pushed and now Travis is happy! |
break; | ||
} | ||
|
||
git_index_entry__free(entry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeatedly allocating and freeing the git_index_entry
here seems wasteful to me. Could this loop not read, instead:
for (i = 0; i < git_diff_num_deltas(diff); i++) {
git_index_entry entry;
if ((error = git_diff_get_patch(NULL, &delta, diff, i)) < 0)
goto cleanup;
memset(&entry, 0, sizeof(entry));
entry->mode = delta->new_file.mode;
entry->oid = delta->new_file.oid;
entry->path = delta->new_file.path;
switch (delta->status) {
...
}
}
Since the entry only has to exist for a moment in order to call git_index_add()
, this would save two unnecessary alloc/frees per iteration of the loop. Additionally, if you were to do it this way, all the git_index_entry__free()
exports could go away and the overall patch size would be reduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual code I wrote is not correct, btw. That needs to be entry.mode
etc. when setting the values and the git_index_add()
calls would need &entry
, too.
So, I notice that the code path for That makes me think that Would you consider adding a new API instead: GIT_EXTERN(int) git_reset_default(
git_repository *repo,
git_object *target,
git_strarray* pathspec); where the I mostly ask this because it seems like the new option is functionally orthogonal to the old options and intermingling the two function bodies makes it harder to follow. |
@arrbee Thanks a lot for the review! I'll update the PR accordingly. |
@arrbee The PR has been fixed as requested. |
} | ||
|
||
opts.pathspec = *pathspecs; | ||
opts.flags = GIT_DIFF_REVERSE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For efficiency, you should probably add GIT_DIFF_SKIP_BINARY_CHECK
here as well, otherwise when you call git_diff_get_patch()
below, the diff code may actually load the files into memory so that it can set the binary
flag correctly on the diff delta.
Alternatively, since this is internal to libgit2, you could just peek directly into the internal vector of git_diff_delta
data inside the git_diff_list
and not use git_diff_get_patch()
for access.
Hey, so it appears to me that this code is creating two empty size_t i;
git_diff_delta *delta;
git_index_entry entry = {0};
...
if ((error = git_diff_tree_to_index(&diff, repo, tree, index, &opts)) < 0)
goto cleanup;
git_vector_foreach(&diff->deltas, i, delta) {
if ((error = git_index_conflict_remove(index, delta->old_file.path)) < 0)
goto cleanup;
if (delta->status == GIT_DELTA_DELETED) {
if ((error = git_index_remove(index, delta->old_file.path, 0)) < 0)
goto cleanup;
} else {
entry.mode = delta->new_file.mode;
git_oid_cpy(&entry.oid, &delta->new_file.oid);
entry.path = (char *)delta->new_file.path;
if ((error = git_index_add(to_be_included, &entry)) < 0)
goto cleanup;
}
}
... Given the options being passed to diff, there should never be any records that are not deleted, added, or modified. You could probably add an assertion to that effect in the else clause if you were concerned about it. I apologize if I've missed something and the indirection is needed! |
@arrbee The PR has been fixed according to your recommendations. Thanks to you the code is now shorter and clearer. Great lesson! BTW, I've eventually
|
✨ Thanks! |
reset: Allow the selective reset of pathspecs
reset: Allow the selective reset of pathspecs
Not done yet. But I'd really like some early feedback on the API and proposed behavior