Skip to content

Commit

Permalink
git: Add option for no_config in git shell
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jerry-skydio committed Aug 19, 2024
1 parent 37f685b commit 4e46364
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions revup/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -269,19 +274,30 @@ 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.
Args:
*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]
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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")
Expand Down

0 comments on commit 4e46364

Please sign in to comment.