зеркало из https://github.com/golang/dep.git
fix obvious readability problems
This commit is contained in:
Родитель
21f357ac6c
Коммит
f81d6f5e26
12
context.go
12
context.go
|
@ -131,11 +131,9 @@ func (c *ctx) absoluteProjectRoot(path string) (string, error) {
|
|||
if err != nil {
|
||||
return "", errors.Wrapf(err, "checking if %s is a directory", posspath)
|
||||
}
|
||||
|
||||
if !dirOK {
|
||||
return "", fmt.Errorf("%s does not exist", posspath)
|
||||
}
|
||||
|
||||
return posspath, nil
|
||||
}
|
||||
|
||||
|
@ -160,14 +158,14 @@ func (c *ctx) versionInWorkspace(root gps.ProjectRoot) (gps.Version, error) {
|
|||
return nil, errors.Wrapf(err, "getting repo version for root: %s", pr)
|
||||
}
|
||||
|
||||
// first look through tags
|
||||
// First look through tags.
|
||||
tags, err := repo.Tags()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "getting repo tags for root: %s", pr)
|
||||
}
|
||||
// try to match the current version to a tag
|
||||
// Try to match the current version to a tag.
|
||||
if contains(tags, ver) {
|
||||
// assume semver if it starts with a v
|
||||
// Assume semver if it starts with a v.
|
||||
if strings.HasPrefix(ver, "v") {
|
||||
return gps.NewVersion(ver).Is(gps.Revision(rev)), nil
|
||||
}
|
||||
|
@ -175,12 +173,12 @@ func (c *ctx) versionInWorkspace(root gps.ProjectRoot) (gps.Version, error) {
|
|||
return nil, fmt.Errorf("version for root %s does not start with a v: %q", pr, ver)
|
||||
}
|
||||
|
||||
// look for the current branch
|
||||
// Look for the current branch.
|
||||
branches, err := repo.Branches()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "getting repo branch for root: %s")
|
||||
}
|
||||
// try to match the current version to a branch
|
||||
// Try to match the current version to a branch.
|
||||
if contains(branches, ver) {
|
||||
return gps.NewBranch(ver).Is(gps.Revision(rev)), nil
|
||||
}
|
||||
|
|
10
ensure.go
10
ensure.go
|
@ -364,7 +364,7 @@ func renameWithFallback(src, dest string) error {
|
|||
|
||||
// Windows cannot use syscall.Rename to rename a directory
|
||||
if runtime.GOOS == "windows" && fi.IsDir() {
|
||||
if err := copyFolder(src, dest); err != nil {
|
||||
if err := copyDir(src, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.RemoveAll(src)
|
||||
|
@ -388,7 +388,7 @@ func renameWithFallback(src, dest string) error {
|
|||
if terr.Err == syscall.EXDEV {
|
||||
vlogf("Cross link err (is temp dir on same partition as project?); falling back to manual copy: %s", terr)
|
||||
if fi.IsDir() {
|
||||
cerr = copyFolder(src, dest)
|
||||
cerr = copyDir(src, dest)
|
||||
} else {
|
||||
cerr = copyFile(src, dest)
|
||||
}
|
||||
|
@ -414,9 +414,9 @@ func renameWithFallback(src, dest string) error {
|
|||
return os.RemoveAll(src)
|
||||
}
|
||||
|
||||
// copyFolder takes in a directory and copies its contents to the destination.
|
||||
// copyDir takes in a directory and copies its contents to the destination.
|
||||
// It preserves the file mode on files as well.
|
||||
func copyFolder(src string, dest string) error {
|
||||
func copyDir(src string, dest string) error {
|
||||
fi, err := os.Lstat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -447,7 +447,7 @@ func copyFolder(src string, dest string) error {
|
|||
destfile := filepath.Join(dest, obj.Name())
|
||||
|
||||
if obj.IsDir() {
|
||||
err = copyFolder(srcfile, destfile)
|
||||
err = copyDir(srcfile, destfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ func TestDeduceConstraint(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCopyFolder(t *testing.T) {
|
||||
func TestCopyDir(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "dep")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -212,7 +212,7 @@ func TestCopyFolder(t *testing.T) {
|
|||
srcf.Close()
|
||||
|
||||
destdir := filepath.Join(dir, "dest")
|
||||
if err := copyFolder(srcdir, destdir); err != nil {
|
||||
if err := copyDir(srcdir, destdir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,7 @@ func (cmd *hashinCommand) ShortHelp() string { return "" }
|
|||
func (cmd *hashinCommand) LongHelp() string { return "" }
|
||||
func (cmd *hashinCommand) Hidden() bool { return true }
|
||||
|
||||
func (cmd *hashinCommand) Register(fs *flag.FlagSet) {
|
||||
}
|
||||
func (cmd *hashinCommand) Register(fs *flag.FlagSet) {}
|
||||
|
||||
type hashinCommand struct{}
|
||||
|
||||
|
|
8
main.go
8
main.go
|
@ -39,7 +39,6 @@ type command interface {
|
|||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// Build the list of available commands.
|
||||
commands := []command{
|
||||
&initCommand{},
|
||||
|
@ -143,9 +142,7 @@ func resetUsage(fs *flag.FlagSet, name, args, longHelp string) {
|
|||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errProjectNotFound = errors.New("could not find project manifest.json, use dep init to initiate a manifest")
|
||||
)
|
||||
var errProjectNotFound = errors.New("could not find project manifest.json, use dep init to initiate a manifest")
|
||||
|
||||
func findProjectRootFromWD() (string, error) {
|
||||
path, err := os.Getwd()
|
||||
|
@ -155,8 +152,7 @@ func findProjectRootFromWD() (string, error) {
|
|||
return findProjectRoot(path)
|
||||
}
|
||||
|
||||
// search upwards looking for a manifest file until we get to the root of the
|
||||
// filesystem
|
||||
// search upwards looking for a manifest file until we get to the root of the filesystem.
|
||||
func findProjectRoot(from string) (string, error) {
|
||||
for {
|
||||
mp := filepath.Join(from, manifestName)
|
||||
|
|
44
main_test.go
44
main_test.go
|
@ -5,7 +5,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
@ -19,48 +18,41 @@ func TestFindRoot(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expect := filepath.Join(wd, "_testdata", "rootfind")
|
||||
|
||||
got1, err := findProjectRoot(expect)
|
||||
want := filepath.Join(wd, "_testdata", "rootfind")
|
||||
got1, err := findProjectRoot(want)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error while finding root: %s", err)
|
||||
} else if expect != got1 {
|
||||
t.Errorf("findProjectRoot directly on root dir should have found %s, got %s", expect, got1)
|
||||
} else if want != got1 {
|
||||
t.Errorf("findProjectRoot directly on root dir should have found %s, got %s", want, got1)
|
||||
}
|
||||
|
||||
got2, err := findProjectRoot(filepath.Join(expect, "subdir"))
|
||||
got2, err := findProjectRoot(filepath.Join(want, "subdir"))
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error while finding root: %s", err)
|
||||
} else if expect != got2 {
|
||||
t.Errorf("findProjectRoot on subdir should have found %s, got %s", expect, got2)
|
||||
} else if want != got2 {
|
||||
t.Errorf("findProjectRoot on subdir should have found %s, got %s", want, got2)
|
||||
}
|
||||
|
||||
got3, err := findProjectRoot(filepath.Join(expect, "nonexistent"))
|
||||
got3, err := findProjectRoot(filepath.Join(want, "nonexistent"))
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error while finding root: %s", err)
|
||||
} else if expect != got3 {
|
||||
t.Errorf("findProjectRoot on nonexistent subdir should still work and give %s, got %s", expect, got3)
|
||||
} else if want != got3 {
|
||||
t.Errorf("findProjectRoot on nonexistent subdir should still work and give %s, got %s", want, got3)
|
||||
}
|
||||
|
||||
root := "/"
|
||||
returnedPath, projectRootErr := findProjectRoot(root)
|
||||
if returnedPath != "" {
|
||||
t.Errorf("findProjectRoot with path %s returned non empty string: %s", root, returnedPath)
|
||||
p, err := findProjectRoot(root)
|
||||
if p != "" {
|
||||
t.Errorf("findProjectRoot with path %s returned non empty string: %s", root, p)
|
||||
}
|
||||
if projectRootErr == nil {
|
||||
t.Errorf("findProjectRoot with path %s should return error", root)
|
||||
}
|
||||
errStr := fmt.Sprintf("%v", projectRootErr.Error())
|
||||
expectedStr := "could not find project manifest.json, use dep init to initiate a manifest"
|
||||
if errStr != expectedStr {
|
||||
t.Errorf("Incorrect errProjectNotFound error. Found: %s. Expected: %s", errStr, expectedStr)
|
||||
if err != errProjectNotFound {
|
||||
t.Errorf("findProjectRoot want: %#v got: %#v", errProjectNotFound, err)
|
||||
}
|
||||
|
||||
// the following test does not work on windows because syscall.Stat does not
|
||||
// return a "not a directory" error
|
||||
// The following test does not work on windows because syscall.Stat does not
|
||||
// return a "not a directory" error.
|
||||
if runtime.GOOS != "windows" {
|
||||
got4, err := findProjectRoot(filepath.Join(expect, manifestName))
|
||||
got4, err := findProjectRoot(filepath.Join(want, manifestName))
|
||||
if err == nil {
|
||||
t.Errorf("Should have err'd when trying subdir of file, but returned %s", got4)
|
||||
}
|
||||
|
|
|
@ -49,7 +49,6 @@ func readManifest(r io.Reader) (*manifest, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := &manifest{
|
||||
Dependencies: make(gps.ProjectConstraints, len(rm.Dependencies)),
|
||||
Ovr: make(gps.ProjectConstraints, len(rm.Overrides)),
|
||||
|
|
|
@ -129,8 +129,8 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
}
|
||||
|
||||
if writeV {
|
||||
// prefer the nl, but take the l if only that's available, as could be the
|
||||
// case if true was passed for forceVendor
|
||||
// Prefer the nl, but take the l if only that's available, as could be the
|
||||
// case if true was passed for forceVendor.
|
||||
l := sw.nl
|
||||
if l == nil {
|
||||
l = sw.l
|
||||
|
@ -142,7 +142,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
}
|
||||
|
||||
// Move the existing files and dirs to the temp dir while we put the new
|
||||
// ones in, to provide insurance against errors for as long as possible
|
||||
// ones in, to provide insurance against errors for as long as possible.
|
||||
type pathpair struct {
|
||||
from, to string
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
|
||||
if writeM {
|
||||
if _, err := os.Stat(mpath); err == nil {
|
||||
// move out the old one
|
||||
// Move out the old one.
|
||||
tmploc := filepath.Join(td, manifestName+".orig")
|
||||
failerr = renameWithFallback(mpath, tmploc)
|
||||
if failerr != nil {
|
||||
|
@ -161,7 +161,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
restore = append(restore, pathpair{from: tmploc, to: mpath})
|
||||
}
|
||||
|
||||
// move in the new one
|
||||
// Move in the new one.
|
||||
failerr = renameWithFallback(filepath.Join(td, manifestName), mpath)
|
||||
if failerr != nil {
|
||||
goto fail
|
||||
|
@ -170,7 +170,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
|
||||
if writeL {
|
||||
if _, err := os.Stat(lpath); err == nil {
|
||||
// move out the old one
|
||||
// Move out the old one.
|
||||
tmploc := filepath.Join(td, lockName+".orig")
|
||||
|
||||
failerr = renameWithFallback(lpath, tmploc)
|
||||
|
@ -180,7 +180,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
restore = append(restore, pathpair{from: tmploc, to: lpath})
|
||||
}
|
||||
|
||||
// move in the new one
|
||||
// Move in the new one.
|
||||
failerr = renameWithFallback(filepath.Join(td, lockName), lpath)
|
||||
if failerr != nil {
|
||||
goto fail
|
||||
|
@ -189,13 +189,13 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
|
||||
if writeV {
|
||||
if _, err := os.Stat(vpath); err == nil {
|
||||
// move out the old vendor dir. just do it into an adjacent dir, to
|
||||
// Move out the old vendor dir. just do it into an adjacent dir, to
|
||||
// try to mitigate the possibility of a pointless cross-filesystem
|
||||
// move with a temp dir
|
||||
// move with a temp directory.
|
||||
vendorbak = vpath + ".orig"
|
||||
if _, err := os.Stat(vendorbak); err == nil {
|
||||
// If the adjacent dir already exists, bite the bullet and move
|
||||
// to a proper tempdir
|
||||
// to a proper tempdir.
|
||||
vendorbak = filepath.Join(td, "vendor.orig")
|
||||
}
|
||||
|
||||
|
@ -206,7 +206,7 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
restore = append(restore, pathpair{from: vendorbak, to: vpath})
|
||||
}
|
||||
|
||||
// move in the new one
|
||||
// Move in the new one.
|
||||
failerr = renameWithFallback(filepath.Join(td, "vendor"), vpath)
|
||||
if failerr != nil {
|
||||
goto fail
|
||||
|
@ -215,7 +215,6 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
|
||||
// Renames all went smoothly. The deferred os.RemoveAll will get the temp
|
||||
// dir, but if we wrote vendor, we have to clean that up directly
|
||||
|
||||
if writeV {
|
||||
// Nothing we can really do about an error at this point, so ignore it
|
||||
os.RemoveAll(vendorbak)
|
||||
|
@ -224,9 +223,9 @@ func (sw safeWriter) writeAllSafe(forceVendor bool) error {
|
|||
return nil
|
||||
|
||||
fail:
|
||||
// If we failed at any point, move all the things back into place, then bail
|
||||
// If we failed at any point, move all the things back into place, then bail.
|
||||
for _, pair := range restore {
|
||||
// Nothing we can do on err here, as we're already in recovery mode
|
||||
// Nothing we can do on err here, as we're already in recovery mode.
|
||||
renameWithFallback(pair.from, pair.to)
|
||||
}
|
||||
return failerr
|
||||
|
|
Загрузка…
Ссылка в новой задаче