From 330680e9fe24f3bd0aa65eacf3e657ccf539a562 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 6 Jun 2024 15:25:10 +0100 Subject: [PATCH 1/2] Allow code_style.py to work from a git hook 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 --- scripts/code_style.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 9e3c75142af9..937e4648683d 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -75,8 +75,25 @@ def get_src_files(since: Optional[str]) -> List[str]: output = subprocess.check_output(["git", "ls-files"] + file_patterns, universal_newlines=True) src_files = output.split() + + # When this script is called from a git hook, some environment variables + # are set by default which force all git commands to use the main repository + # (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() + # Get a list of environment vars that git sets + git_env_vars = subprocess.check_output(["git", "rev-parse", "--local-env-vars"], + universal_newlines=True) + git_env_vars = git_env_vars.split() + # Remove the vars from the environment + for var in git_env_vars: + framework_env.pop(var, None) + output = subprocess.check_output(["git", "-C", "framework", "ls-files"] - + file_patterns, universal_newlines=True) + + file_patterns, + universal_newlines=True, + env=framework_env) framework_src_files = output.split() if since: @@ -89,7 +106,8 @@ def get_src_files(since: Optional[str]) -> List[str]: # ... the framework submodule cmd = ["git", "-C", "framework", "log", since + "..HEAD", "--name-only", "--pretty=", "--"] + framework_src_files - output = subprocess.check_output(cmd, universal_newlines=True) + output = subprocess.check_output(cmd, universal_newlines=True, + env=framework_env) committed_changed_files += ["framework/" + s for s in output.split()] # and also get all files with uncommitted changes in ... @@ -100,7 +118,8 @@ def get_src_files(since: Optional[str]) -> List[str]: # ... the framework submodule cmd = ["git", "-C", "framework", "diff", "--name-only", "--"] + \ framework_src_files - output = subprocess.check_output(cmd, universal_newlines=True) + output = subprocess.check_output(cmd, universal_newlines=True, + env=framework_env) uncommitted_changed_files += ["framework/" + s for s in output.split()] src_files = committed_changed_files + uncommitted_changed_files From f8bbc2d53212aff06ffe73635e2815a956196e04 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Thu, 6 Jun 2024 16:16:31 +0100 Subject: [PATCH 2/2] Remove multi-type variable Signed-off-by: David Horstmann --- scripts/code_style.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/code_style.py b/scripts/code_style.py index 937e4648683d..d3f89d913013 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -85,9 +85,8 @@ def get_src_files(since: Optional[str]) -> List[str]: # Get a list of environment vars that git sets git_env_vars = subprocess.check_output(["git", "rev-parse", "--local-env-vars"], universal_newlines=True) - git_env_vars = git_env_vars.split() # Remove the vars from the environment - for var in git_env_vars: + for var in git_env_vars.split(): framework_env.pop(var, None) output = subprocess.check_output(["git", "-C", "framework", "ls-files"]