Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Warn on use of abbreviated sha1 commit hash #582

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"reflect"
"regexp"
"sort"

"github.com/golang/dep/internal/gps"
Expand Down Expand Up @@ -67,8 +68,20 @@ func validateManifest(s string) ([]error, error) {
for key, value := range v.(map[string]interface{}) {
// Check if the key is valid
switch key {
case "name", "branch", "revision", "version", "source":
case "name", "branch", "version", "source":
// valid key
case "revision":
if valueStr, ok := value.(string); ok {
// Check if sha1 hash is abbreviated
validHash := regexp.MustCompile("[a-f0-9]{40}").MatchString(valueStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's prefer to use encoding/hex.DecodeString() here - faster than compiling a regexp, and also just accurately reflects the data.

if !validHash {
// Check for valid bzr revision-id
validBzr := regexp.MustCompile(".*-[0-9]{14}(-[a-zA-Z0-9]+)?").MatchString(valueStr)
if !validBzr {
errs = append(errs, fmt.Errorf("revision %q should not be in abbreviated form", valueStr))
}
}
}
case "metadata":
// Check if metadata is of Map type
if reflect.TypeOf(value).Kind() != reflect.Map {
Expand Down
26 changes: 25 additions & 1 deletion manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,33 @@ func TestValidateManifest(t *testing.T) {
`,
want: []error{},
},
{
tomlString: `
[[dependencies]]
name = "github.com/foo/bar"
revision = "b86ad16"
`,
want: []error{errors.New("revision \"b86ad16\" should not be in abbreviated form")},
},
{
tomlString: `
[[dependencies]]
name = "bazaar.foobar.com/~bzr/trunk"
revision = "foo@bar.com-12345-wiuilyamo9ian0m7"
`,
want: []error{errors.New("revision \"foo@bar.com-12345-wiuilyamo9ian0m7\" should not be in abbreviated form")},
},
{
tomlString: `
[[dependencies]]
name = "bazaar.foobar.com/~bzr/trunk"
revision = "foo@bar.com-20161116211307-wiuilyamo9ian0m7"
`,
want: []error{},
},
}

// constains for error
// contains for error
contains := func(s []error, e error) bool {
for _, a := range s {
if a.Error() == e.Error() {
Expand Down