Skip to content

git_repository_open_ext: fix handling of $GIT_NAMESPACE#3992

Merged
pks-t merged 1 commit into
libgit2:masterfrom
joshtriplett:env-namespace
Nov 14, 2016
Merged

git_repository_open_ext: fix handling of $GIT_NAMESPACE#3992
pks-t merged 1 commit into
libgit2:masterfrom
joshtriplett:env-namespace

Conversation

@joshtriplett
Copy link
Copy Markdown
Contributor

The existing code would set a namespace of "" (empty string) with
GIT_NAMESPACE unset. In a repository where refs/heads/namespaces/
exists, that can produce incorrect results. Detect that case and avoid
setting the namespace at all.

Since that makes the last assignment to error, and the previous
assignment can potentially get GIT_ENOTFOUND, set error to 0 explicitly
to prevent the call from incorrectly failing with GIT_ENOTFOUND.

Copy link
Copy Markdown
Member

@pks-t pks-t left a comment

Choose a reason for hiding this comment

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

The fix looks good, but I think previously existing code could be refactored a bit to improve readability here

Comment thread src/repository.c Outdated
if (error < 0)
goto error;
}
error = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks a bit backwards here, as you are in fact not clearing the error of the leading block but instead the error from the previous git__getenv. It would be much clearer if you'd instead clear the error in the previous if (error == GIT_ENOTFOUND) condition, where we already call giterr_clear();.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An additional note, which is not your fault but might be improved when we're already touching this. I think the code would be nicer if you moved the call to git__getenv and setting the namespace together. So you could just move down getting the environment variable and then set the namespace based on the function's return value.

The existing code would set a namespace of "" (empty string) with
GIT_NAMESPACE unset.  In a repository where refs/heads/namespaces/
exists, that can produce incorrect results.  Detect that case and avoid
setting the namespace at all.

Since that makes the last assignment to error conditional, and the
previous assignment can potentially get GIT_ENOTFOUND, set error to 0
explicitly to prevent the call from incorrectly failing with
GIT_ENOTFOUND.
@joshtriplett
Copy link
Copy Markdown
Contributor Author

@pks-t Thanks for the review.

Updated to move the error handling to a clearer location.

@pks-t pks-t merged commit 2d20551 into libgit2:master Nov 14, 2016
@pks-t
Copy link
Copy Markdown
Member

pks-t commented Nov 14, 2016

Looks good now, thanks for this 👍

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