-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Allow code_style.py
to work from a git hook
#9233
Allow code_style.py
to work from a git hook
#9233
Conversation
When running a git hook, git sets certain environment variables (such as GIT_INDEX_FILE) which force git to look at the main repository, overriding other options. This trips up code_style.py whenever it tries to run a git command on the framework submodule. Fix this by explicitly clearing git-related environment-variables before running git commands on the framework. This is recommended by git's documentation[1]: > Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are > exported so that Git commands run by the hook can correctly locate > the repository. If your hook needs to invoke Git commands in a > foreign repository or in a different working tree of the same > repository, then it should clear these environment variables so > they do not interfere with Git operations at the foreign location. [1] https://git-scm.com/docs/githooks Signed-off-by: David Horstmann <[email protected]>
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.
LGTM except for a variable reuse.
scripts/code_style.py
Outdated
git_env_vars = git_env_vars.split() | ||
# Remove the vars from the environment | ||
for var in git_env_vars: |
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.
Changing the type of a variable mid-function is confusing both to readers and to mypy.
git_env_vars = git_env_vars.split() | |
# Remove the vars from the environment | |
for var in git_env_vars: | |
# Remove the vars from the environment | |
for var in git_env_vars.split(): |
No need for a changelog entry. This only affects contributors, not users. |
Signed-off-by: David Horstmann <[email protected]>
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.
LGTM
code_style.py
to work from a git hook
# (i.e. prevent us from performing commands on the framework repo). | ||
# Create an environment without these variables for running commands on the | ||
# framework repo. | ||
framework_env = os.environ.copy() |
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.
Just a comment that we may want to do that in check_files.py as well. Not necessarily in this PR though.
# Remove the vars from the environment | ||
for var in git_env_vars.split(): | ||
framework_env.pop(var, None) | ||
|
||
output = subprocess.check_output(["git", "-C", "framework", "ls-files"] |
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 think the solution here is actually on the wrong track, although it's an improvement so I won't block it. When this script is called with --since
, it looks for the given revision name in the submodule. Reported as Mbed-TLS/mbedtls-framework#24. I suspect the right solution would be to never call git -C framework
and instead rely on --recurse-submodules
throughout.
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.
Using --recurse-submodules
comes with some issues as well: see 7661aa0 commit message.
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 this PR! LGTM!
When running a git hook, git sets certain environment variables (such as
GIT_INDEX_FILE
) which force git to look at the main repository, overriding other options. This trips up code_style.py whenever it tries to run a git command on the framework submodule.Fix this by explicitly clearing git-related environment-variables before running git commands on the framework. This is recommended by git's documentation:
PR checklist
Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")
code_style.py
to work from a git hook #9234