Skip to content

[Feature] Retrieve the actual command in case of failure #222

@itingliu

Description

@itingliu

I have several helper functions to call into git through Subprocess like this

    func getCurrentCommitHash(
        at repoPath: FilePath
    ) async throws -> String {
        let gitResult = try await Subprocess.run(
            .at("/usr/local/bin/git"),
            arguments: ["rev-parse", "HEAD"],
            workingDirectory: repoPath
        )
        guard gitResult.terminationStatus.isSuccess else {
            let stderr = String(data: gitResult.standardError, encoding: .utf8)
            throw MyError.commandFailed(
                "git rev-parse HEAD",
                stderr
            )
        }

        return String(decoding: gitResult.standardOutput, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)
    }

For each of these I am manually constructing an Error to throw when the underlying git command fails. The main body of the error is the actual command that's being run, git rev-parse HEAD in this case.

It would be nice if I can get the underlying command populated up through Subprocess.run. This function already knows the exact command being executed, so it should be possible to also surface this up to the caller.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions