Calls git with the given arguments, returns stdout as a string
(
command: List[str],
timeout: int = COMMAND_TIMEOUT,
check: bool = True,
cwd: Optional[Union[str, Path]] = None,
log_stderr: bool = True,
ignore_git_config: bool = True,
)
| 191 | |
| 192 | |
| 193 | def git( |
| 194 | command: List[str], |
| 195 | timeout: int = COMMAND_TIMEOUT, |
| 196 | check: bool = True, |
| 197 | cwd: Optional[Union[str, Path]] = None, |
| 198 | log_stderr: bool = True, |
| 199 | ignore_git_config: bool = True, |
| 200 | ) -> str: |
| 201 | """Calls git with the given arguments, returns stdout as a string""" |
| 202 | env = os.environ.copy() |
| 203 | # Ensure git messages are in English |
| 204 | env["LANG"] = "C" |
| 205 | # Ensure git behavior is not affected by the user git configuration, but give us a |
| 206 | # way to set some configuration (useful for safe.directory) |
| 207 | if ignore_git_config: |
| 208 | env["GIT_CONFIG_GLOBAL"] = os.getenv("GG_GIT_CONFIG", "") |
| 209 | env["GIT_CONFIG_SYSTEM"] = "" |
| 210 | |
| 211 | if cwd is None: |
| 212 | cwd = Path.cwd() |
| 213 | |
| 214 | try: |
| 215 | logger.debug("command=%s timeout=%d", command, timeout) |
| 216 | result = subprocess.run( |
| 217 | ( |
| 218 | [ |
| 219 | _get_git_path(), |
| 220 | "-c", |
| 221 | "core.quotePath=false", |
| 222 | "-c", |
| 223 | "safe.directory=*", |
| 224 | ] |
| 225 | + ( |
| 226 | ["-c", "core.longpaths=true"] |
| 227 | if platform.system() == "Windows" |
| 228 | else [] |
| 229 | ) |
| 230 | + command |
| 231 | ), |
| 232 | check=check, |
| 233 | capture_output=True, |
| 234 | timeout=timeout, |
| 235 | env=env, |
| 236 | cwd=str(cwd), |
| 237 | ) |
| 238 | if result.stderr and log_stderr: |
| 239 | logger.warning( |
| 240 | "command=%s, stderr=%s", |
| 241 | command, |
| 242 | result.stderr.decode("utf-8", errors="ignore"), |
| 243 | ) |
| 244 | return result.stdout.decode("utf-8", errors="ignore").rstrip() |
| 245 | except subprocess.CalledProcessError as exc: |
| 246 | stderr = exc.stderr.decode("utf-8", errors="ignore") |
| 247 | if log_stderr: |
| 248 | logger.error("command=%s, stderr=%s", command, stderr) |
| 249 | if "detected dubious ownership in repository" in stderr: |
| 250 | raise GitError( |