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

Allow code_style.py to work from a git hook #9233

Conversation

davidhorstmann-arm
Copy link
Contributor

@davidhorstmann-arm davidhorstmann-arm commented Jun 6, 2024

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:

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.

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

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]>
@davidhorstmann-arm davidhorstmann-arm added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) needs-ci Needs to pass CI tests labels Jun 6, 2024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a 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.

Comment on lines 88 to 90
git_env_vars = git_env_vars.split()
# Remove the vars from the environment
for var in git_env_vars:
Copy link
Contributor

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.

Suggested change
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():

@gilles-peskine-arm gilles-peskine-arm added needs-work needs-backports Backports are missing or are pending review and approval. priority-medium Medium priority - this can be reviewed as time permits and removed needs-reviewer This PR needs someone to pick it up for review labels Jun 6, 2024
@gilles-peskine-arm
Copy link
Contributor

No need for a changelog entry. This only affects contributors, not users.

Signed-off-by: David Horstmann <[email protected]>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@davidhorstmann-arm davidhorstmann-arm changed the title Allow code_style.py to work from a git hook Allow code_style.py to work from a git hook Jun 6, 2024
@davidhorstmann-arm davidhorstmann-arm removed the needs-backports Backports are missing or are pending review and approval. label Jun 6, 2024
# (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()
Copy link
Contributor

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"]
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

@valeriosetti valeriosetti left a 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!

@davidhorstmann-arm davidhorstmann-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests labels Jun 7, 2024
@ronald-cron-arm ronald-cron-arm added this pull request to the merge queue Jun 7, 2024
Merged via the queue into Mbed-TLS:development with commit 4ac0182 Jun 7, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports priority-medium Medium priority - this can be reviewed as time permits size-s Estimated task size: small (~2d)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants