-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Submodules-API should report .gitmodules parse errors instead of ignoring them #4522
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
Submodules-API should report .gitmodules parse errors instead of ignoring them #4522
Conversation
src/submodule.c
Outdated
@@ -91,7 +91,7 @@ __KHASH_IMPL( | |||
|
|||
static int submodule_alloc(git_submodule **out, git_repository *repo, const char *name); | |||
static git_config_backend *open_gitmodules(git_repository *repo, int gitmod); | |||
static git_config *gitmodules_snapshot(git_repository *repo); | |||
static int gitmodules_snapshot(git_repository *repo, git_config **snap); |
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 out
parameter is typically first
src/submodule.c
Outdated
@@ -509,7 +509,7 @@ int git_submodule__map(git_repository *repo, git_strmap *map) | |||
data.map = map; | |||
data.repo = repo; | |||
|
|||
if ((mods = gitmodules_snapshot(repo)) == NULL) | |||
if ((error = gitmodules_snapshot(repo, &mods)) < 0 || mods == NULL) |
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.
I feel like this interface has become a bit more brittle by having to check for mods == NULL
, as well. What about returning for example ENOTFOUND
in case there are no submodules?
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.
I haven't done that in the first place as I then have to reset errors... First lemme see whether this approach will work at all...
src/submodule.c
Outdated
if (git_config_open_ondisk(&mods, path.ptr) < 0) | ||
mods = NULL; | ||
} | ||
int error = git_config_open_ondisk(&mods, path.ptr); |
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.
Might be I'm misreading the diff, but isn't this introducing a declaration after instructions?
cl_git_rewritefile("submod2/.gitmodules", | ||
"[submodule \"Test_App\"\n" | ||
" path = Test_App\n" | ||
" url = ../Test_App\n"); |
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.
👍
tests/submodule/lookup.c
Outdated
@@ -445,3 +445,20 @@ void test_submodule_lookup__foreach_in_bare_repository_fails(void) | |||
|
|||
cl_git_fail(git_submodule_foreach(g_repo, foreach_cb, NULL)); | |||
} | |||
|
|||
void test_submodule_lookup__fail_invalid_gitignores(void) |
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.
gitignores
? Don't you mean gitmodules
?
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.
Yeah a type, this is still WIP as I can't test locally ATM...
@pks-t All change requests should be addressed now. |
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.
Thanks for the fixups. I think the interface is a lot easier to use like this, as you can assume that mods
will never be NULL
in case the function returns success. The only non-stylistic thing that bothers me is the git_buf_joinpath
error being ignored, which should definitly be fixed (even though you obviously just go with the previous behavior).
src/submodule.c
Outdated
@@ -509,8 +509,12 @@ int git_submodule__map(git_repository *repo, git_strmap *map) | |||
data.map = map; | |||
data.repo = repo; | |||
|
|||
if ((mods = gitmodules_snapshot(repo)) == NULL) | |||
if ((error = gitmodules_snapshot(&mods, repo)) < 0) | |||
{ |
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 opening "{" is on the same line for single-line if-statements.
src/submodule.c
Outdated
if ((error = gitmodules_snapshot(&mods, repo)) < 0) | ||
{ | ||
if (error == GIT_ENOTFOUND) | ||
error = 0; |
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.
In my opinion, it looks much more readable like this
src/submodule.c
Outdated
@@ -1916,31 +1921,32 @@ static int submodule_load_from_wd_lite(git_submodule *sm) | |||
|
|||
/** | |||
* Returns a snapshot of $WORK_TREE/.gitmodules. |
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.
I'd like to see a short comment about GIT_ENOTFOUND
here such that people know to expect it in case there are no submodules.
src/submodule.c
Outdated
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0) | ||
return NULL; | ||
return 0; |
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.
Hum. Shouldn't this bubble up the error? git_buf_joinpath
failing indicates that there was a memory allocation error, which definitly shouldn't be ignored.
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) < 0)
return -1;
src/submodule.c
Outdated
{ | ||
const char *workdir = git_repository_workdir(repo); | ||
git_config *mods = NULL, *snap = NULL; | ||
git_buf path = GIT_BUF_INIT; | ||
|
||
if (workdir != NULL) { |
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 whole indentation could be avoided if you return early in case workdir == NULL
. That's a matter of taste, though -- your call if you want to do that instead.
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.
Yeah I also thought that, however, I did not want to make too big changes...
@pks-t All change requests should be addressed now. |
src/submodule.c
Outdated
|
||
git_buf_free(&path); | ||
|
||
if (mods) { | ||
git_config_snapshot(&snap, mods); | ||
git_config_snapshot(snap, mods); |
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.
Sorry for not catching that earlier, but now that you report errors you should definitly also check the return value of git_config_snapshot
.
src/submodule.c
Outdated
git_config_free(mods); | ||
return 0; |
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.
We usually try to avoid returning early for non-trivial code. I'd rather go with something like
...
*snap = NULL;
...
if ((error = git_config_open_ondisk(...)) < 0)
goto out;
if (!mods) {
error = -1;
goto out;
}
if ((error = git_config_snapshot(mods, config)) < 0)
goto out;
out:
git_config_free(mods);
if (error)
git_config_free(*snap);
return error;
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.
goto
is better? - Shall I change all the code for this? You asked to exit early before for !worktree
.
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.
Sorry 😅 Seeing that your code already improves the function by quite a lot, I'm satisfied if you check the return code of git_config_snapshot
, without converting the function to goto
style.
@pks-t Any reason why lots of PRs are not merged (such as this one)? |
@csware because we’re busy. We appreciate your contributions but we are all volunteers and do not always have as much time to review pull requests as we would like. We appreciate your patience. |
Also, we’ve locked down for 0.27. This is eligible for merging to master after that release, but this is not a change we’d take before then. |
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.
v0.27.0 is finally out the door, so we're back to our usual business again.
src/submodule.c
Outdated
mods = NULL; | ||
} | ||
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0) | ||
return -1; |
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.
This should probably be if ((error = git_buf_joinpath(&path, workdir, GIT_MODULES_FILE)) < 0) return error;
src/submodule.c
Outdated
if (git_buf_joinpath(&path, workdir, GIT_MODULES_FILE) != 0) | ||
return -1; | ||
|
||
error = git_config_open_ondisk(&mods, path.ptr); |
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.
You should probably also check the return value here. While an error will cause mods
to be NULL
, which is catched in a few lines, it feels a bit more robust to check directly. You could avoid multiple free's by adding a goto out
. Additional kudos if you also convert the other error handling branches to goto out
with a unified section for freeing up memory in case error != 0
.
tests/submodule/lookup.c
Outdated
|
||
cl_git_fail(git_submodule_foreach(g_repo, sm_lookup_cb, &data)); | ||
} | ||
|
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.
There's a trailing newline here
Signed-off-by: Sven Strickroth <[email protected]>
Thanks for the contribution, @csware! |
Issue mentioned in issue #4517 and issue #4516.
Git for Windows does report parse errors in
.gitmodules
, so should libgit2.