0

I have a Go program that run a git grep command to search within a git repo, the git command is:

git grep -I -n TODO -- :'!test'

the go code that calls this command is as the following:

    cmd := "git grep -I -n TODO -- :'!test'"
    cmdParts := strings.Fields(cmd)
    command := exec.CommandContext(c, cmdParts[0], cmdParts[1:]...)
    command.Dir = workingDir

    cmdOutput, err := command.Output()
    if err != nil {
         errMsge := fmt.Errorf("Failed to grep files in (%s). command [%s] returned [%s]", workingDir, cmd, err)
         return errMsg
    }

When i run this code it fails and im getting the following errMsg:

Failed to grep files in (/home/danwgh/myRepo). command [git grep -I -n TODO -- :'!test'] returned [exit status 128]

However, if I run exactly the same program with a git command without to exclude files, that is:

git grep -I -n TODO

the program works fine and show the grep results (but not the result i want, of course...)

Also if i run manually (on the same workingDir) the same command exactly, it does work fine and show the desired output.

Im kind of frustrated by this, it's just the same command, on the same machine and same repo.

Thanks

isherwood
  • 58,414
  • 16
  • 114
  • 157
danwgh
  • 79
  • 6
  • `cmdParts[1:]...` is not the same thing you are passing from the commandline. The error output should explain the problem, what does it say? – JimB Jan 17 '23 at 20:02

0 Answers0