Skip to content

Commit

Permalink
Return an error if Version() prints to stderr
Browse files Browse the repository at this point in the history
We call `hg --debug identify` to get the version, but this command can
print warnings to stderr. Previously we would read the first line as
the version, which would try to parse whatever warning was printed as
the version.

Instead parse stdout and stderr separately and return an error if
anything gets sent to stderr. This should at least alert people to the
problem and avoid percolating problems further downstream.

Fixes #90.
  • Loading branch information
kevinburke committed Aug 19, 2018
1 parent df81cd8 commit 0afcb75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 16 additions & 6 deletions hg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package vcs

import (
"bytes"
"encoding/xml"
"errors"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -110,12 +112,20 @@ func (s *HgRepo) UpdateVersion(version string) error {

// Version retrieves the current version.
func (s *HgRepo) Version() (string, error) {
out, err := s.RunFromDir("hg", "--debug", "identify")
if err != nil {
return "", NewLocalError("Unable to retrieve checked out version", err, string(out))
}

parts := strings.SplitN(string(out), " ", 2)
c := s.CmdFromDir("hg", "--debug", "identify")
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
c.Stdout = stdout
c.Stderr = stderr
if err := c.Run(); err != nil {
return "", NewLocalError("Unable to retrieve checked out version", err, stderr.String())
}
if stderr.Len() > 0 {
// "hg --debug identify" can print out errors before it actually prints
// the version.
// https://github.com/Masterminds/vcs/issues/90
return "", NewLocalError("Unable to retrieve checked out version", errors.New("Error output printed before identify"), stderr.String())
}
parts := strings.SplitN(stdout.String(), " ", 2)
sha := parts[0]
return strings.TrimSpace(sha), nil
}
Expand Down
5 changes: 2 additions & 3 deletions hg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package vcs

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
//"log"
"os"
"testing"
"time"
)

// Canary test to ensure HgRepo implements the Repo interface.
Expand Down

0 comments on commit 0afcb75

Please sign in to comment.