I'm translating our Cli tool written in Ruby (using Thor) to Go.
Up until now I've had no issues with passing git commands and capturing the output I want.
The general code I use when I want an output is:
func output(c string, args []string, printOut bool) (string, error) {
cmd := exec.Command(c, args...)
if verbose {
text.Command(c, args)
}
if printOut {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
output, err := cmd.Output()
if err != nil {
reportError()
return "", err
}
reportOk()
return string(output), nil
}
I'm having some issues with the following command though:
git --no-pager log --merges --grep='^merge.*(hotfix|release)|(.*from.*(hotfix|release))' --regexp-ignore-case --perl-regexp --pretty=format:%H -n 1
I'm calling the output like this:
args := []string{
"--no-pager", "log", "--merges", "--grep='^merge.*(hotfix|release)|(.*from.*(hotfix|release))'", "--regexp-ignore-case", "--perl-regexp", "--pretty=format:%H", "-n", "1",
}
out, err := output(cmd, args, false)
For some context, this command is used to find the hash of the last release/hotfix merge, in order to find all relevant commits to add to the changelog, from that commit until 'now'.
If I remove --grep='^merge.*(hotfix|release)|(.*from.*(hotfix|release))' --regexp-ignore-case --perl-regexp
, I do get a commit hash output, but I need the regex to make sure its a merge I want.
The thing is cmd.Output()
does not return an error when I pass the full command, it simply returns an empty string.
The full command works when executed on the terminal.
Does anyone has any idea what I might be doing wrong?
Thanks a lot!
question from:
https://stackoverflow.com/questions/65928534/go-exec-does-not-capture-git-log-output