Skip to content

Commit

Permalink
Merge pull request #91 from kevinburke/split-output
Browse files Browse the repository at this point in the history
Return an error if Version() prints to stderr
  • Loading branch information
mattfarina authored Feb 25, 2019
2 parents df81cd8 + 0afcb75 commit ecf366d
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 ecf366d

Please sign in to comment.