From 0ffe5352354117c55493af3094eee66a403f2f6c Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Wed, 3 Jul 2024 13:48:14 -0700 Subject: [PATCH] git: Add option for no_config in git shell This causes git commands to run without considering any system or user configuration. In some cases it's easier to ban all overrides rather than have to override things on a flag by flag basis. --- revup/git.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/revup/git.py b/revup/git.py index d2d4ca2..9ee9c07 100644 --- a/revup/git.py +++ b/revup/git.py @@ -55,6 +55,11 @@ "-U1", ] +GIT_ENV_NOCONFIG = { + "HOME": "/dev/null", + "GIT_CONFIG_NOSYSTEM": "1", +} + COMMON_MAIN_BRANCHES = ["main", "master"] # Below logic assumes 2 values here @@ -269,7 +274,13 @@ def get_scratch_dir(self) -> str: """ return f"{self.repo_root}/.revup" if self.keep_temp else self.temp_dir.name - async def git(self, *args: str, **kwargs: Any) -> Tuple[int, str]: + async def git( + self, + *args: str, + no_config: bool = False, + env: Optional[Dict[str, str]] = None, + **kwargs: Any, + ) -> Tuple[int, str]: """ Run a git command. The returned stdout has trailing newlines stripped. @@ -277,11 +288,16 @@ async def git(self, *args: str, **kwargs: Any) -> Tuple[int, str]: *args: Arguments to git **kwargs: Any valid kwargs for sh() """ + git_env = {} + if no_config: + git_env.update(GIT_ENV_NOCONFIG) + if env is not None: + git_env.update(env) def _maybe_rstrip(s: Tuple[int, str]) -> Tuple[int, str]: return (s[0], s[1].rstrip()) - return _maybe_rstrip(await self.sh.sh(*((self.git_path,) + args), **kwargs)) + return _maybe_rstrip(await self.sh.sh(*((self.git_path,) + args), env=git_env, **kwargs)) async def git_return_code(self, *args: str, **kwargs: Any) -> int: return (await self.git(raiseonerror=False, *args, **kwargs))[0] @@ -596,6 +612,8 @@ async def get_patch_id( await self.sh.piped_sh( patch_source, [self.git_path, "patch-id", "--verbatim"], + env1=GIT_ENV_NOCONFIG, + env2=GIT_ENV_NOCONFIG, ) )[1].split() # If the diff is empty, patch id will return nothing. We just use that as the patch-id since @@ -610,7 +628,9 @@ async def get_diff_summary( """ Return the summary of the diff (files and lines changed) """ - return (await self.git_stdout("diff", "--shortstat", parent, commit)).rstrip() + return ( + await self.git_stdout("diff", "--shortstat", parent, commit, no_config=True) + ).rstrip() async def merge_tree_commit( self, @@ -628,7 +648,7 @@ async def merge_tree_commit( args.extend(["--merge-base", merge_base]) args.extend([branch1, branch2]) - ret, stdout = await self.git(*args, raiseonerror=False) + ret, stdout = await self.git(*args, no_config=True, raiseonerror=False) # See man page for git merge-tree for a complete breakdown of the output format sections = stdout.split("\0\0")