Skip to content

Commit

Permalink
run_git_captured: don't capture stderr along with stdout
Browse files Browse the repository at this point in the history
If running:

   rc, out = run_git_captured(repo_path, "status", "--porcelain")

You don't want the error message in the `out` variable.

Fixes #381, among other things
  • Loading branch information
dmerejkowsky committed Dec 16, 2023
1 parent 2eb6507 commit a2599ac
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tsrc/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class GitError(Error):

class GitCommandError(GitError):
def __init__(
self, working_path: Path, cmd: Iterable[str], *, output: Optional[str] = None
self,
working_path: Path,
cmd: Iterable[str],
*,
output: Optional[str] = None,
error: Optional[str] = None,
) -> None:
self.cmd = cmd
self.working_path = working_path
Expand All @@ -29,6 +34,8 @@ def __init__(
message = f"`git {cmd_str}` from {working_path} failed"
if output:
message += "\n" + output
if error:
message += "\n" + error
super().__init__(message)


Expand Down Expand Up @@ -238,18 +245,18 @@ def run_git_captured(

options: Dict[str, Any] = {}
options["stdout"] = subprocess.PIPE
options["stderr"] = subprocess.STDOUT
options["stderr"] = subprocess.PIPE
options["text"] = True

ui.debug(ui.lightgray, working_path, "$", ui.reset, *git_cmd)
process = subprocess.Popen(git_cmd, cwd=working_path, **options)
out, _ = process.communicate()
out = out.decode("utf-8")
out, err = process.communicate()
if out.endswith("\n"):
out = out.strip("\n")
returncode = process.returncode
ui.debug(ui.lightgray, "[", returncode, "]", ui.reset, out)
if check and returncode != 0:
raise GitCommandError(working_path, cmd, output=out)
raise GitCommandError(working_path, cmd, output=out, error=err)
return returncode, out


Expand Down

0 comments on commit a2599ac

Please sign in to comment.