Skip to content

Instantly share code, notes, and snippets.

@tchap
Last active August 29, 2015 14:06
Show Gist options
  • Save tchap/22e7be8a0e3a9f836a18 to your computer and use it in GitHub Desktop.
Save tchap/22e7be8a0e3a9f836a18 to your computer and use it in GitHub Desktop.
// EDIT: SOLVED!
// The arguments are supposed to be
// [--no-pager log --all --source --abbrev-commit --grep=Story-Id: 12345]
// not
// [--no-pager log --all --source --abbrev-commit --grep='Story-Id: 12345']
// because then it doesn't find the commits and then output is empty.
// The problem is that my function is returning empty stdout buffer
// even though the output is not empty when I run git from CLI.
//
// OS: Mac OS X 10.7.5
//
// Please note that stdout buffer in this particular Git function is returned empty.
// I changed the function a bit to see what is happening. Originally there was just like
//
// cmd.Stdout = stdout
// cmd.Stderr = stderr
// err = cmd.Run()
// return
//
// which was not working.
//
// Demo:
//
// $ test
// [--no-pager log --all --source --abbrev-commit --grep='Story-Id: 12345']
// stdout read returned, bytes=0, err=EOF
//
// $ git --no-pager log --all --source --abbrev-commit --grep='Story-Id: 12345'
// commit 68f8734 refs/heads/develop
// Merge: 394ded9 b841987
// Author: ...
// Date: ...
//
// Finished feature 77965678/test-rb-api
//
// Story-Id: 12345
func Git(args ...string) (stdout, stderr *bytes.Buffer, err error) {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
args = append([]string{"--no-pager"}, args...)
fmt.Println(args)
cmd := exec.Command("git", args...)
outPipe, err := cmd.StdoutPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
buf := make([]byte, 4096)
defer wg.Done()
for {
n, err := outPipe.Read(buf)
fmt.Printf("stdout read returned, bytes=%v, err=%v", n, err)
if err != nil {
return
}
fmt.Println(buf)
}
}()
wg.Wait()
err = cmd.Wait()
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment