Skip to content

Commit

Permalink
Merge pull request golang#560 from darkowlzz/552-backup-vendor-at-init
Browse files Browse the repository at this point in the history
Backup vendor at init
  • Loading branch information
sdboyer committed May 12, 2017
2 parents 4e09d39 + 47302cf commit 109db8e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

"github.com/golang/dep"
"github.com/golang/dep/internal"
Expand Down Expand Up @@ -72,6 +73,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {

mf := filepath.Join(root, dep.ManifestName)
lf := filepath.Join(root, dep.LockName)
vpath := filepath.Join(root, "vendor")

mok, err := dep.IsRegular(mf)
if err != nil {
Expand Down Expand Up @@ -190,6 +192,15 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {

l.Memo = s.HashInputs()

// Pass timestamp (yyyyMMddHHmmss format) as suffix to backup name.
vendorbak, err := dep.BackupVendor(vpath, time.Now().Format("20060102150405"))
if err != nil {
return err
}
if vendorbak != "" {
ctx.Loggers.Err.Printf("Old vendor backed up to %v", vendorbak)
}

if ctx.Loggers.Verbose {
ctx.Loggers.Err.Println("dep: Writing manifest and lock files.")
}
Expand Down
26 changes: 26 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var errProjectNotFound = fmt.Errorf("could not find project %s, use dep init to initiate a manifest", ManifestName)
var errVendorBackupFailed = fmt.Errorf("Failed to create vendor backup. File with same name exists.")

// findProjectRoot searches from the starting directory upwards looking for a
// manifest file until we get to the root of the filesystem.
Expand Down Expand Up @@ -64,3 +65,28 @@ func (p *Project) MakeParams() gps.SolveParameters {

return params
}

// BackupVendor looks for existing vendor directory and if it's not empty,
// creates a backup of it to a new directory with the provided suffix.
func BackupVendor(vpath, suffix string) (string, error) {
// Check if there's a non-empty vendor directory
vendorExists, err := IsNonEmptyDir(vpath)
if err != nil {
return "", err
}
if vendorExists {
vendorbak := vpath + "-" + suffix
// Check if a directory with same name exists
if _, err = os.Stat(vendorbak); os.IsNotExist(err) {
// Rename existing vendor to vendor-{suffix}
if err := renameWithFallback(vpath, vendorbak); err != nil {
return "", err
}
return vendorbak, nil
} else {
return "", errVendorBackupFailed
}
}

return "", nil
}
65 changes: 65 additions & 0 deletions project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,68 @@ func TestSlashedGOPATH(t *testing.T) {
t.Fatal(err)
}
}

func TestBackupVendor(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

pc := NewTestProjectContext(h, "vendorbackupproject")
defer pc.Release()

dummyFile := filepath.Join("vendor", "badinput_fileroot")
pc.CopyFile(dummyFile, "txn_writer/badinput_fileroot")
pc.Load()

if err := pc.VendorShouldExist(); err != nil {
t.Fatal(err)
}

// Create a backup
wantName := "vendor-sfx"
vendorbak, err := BackupVendor("vendor", "sfx")
if err != nil {
t.Fatal(err)
}

if vendorbak != wantName {
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, wantName)
}

if err = pc.h.ShouldExist(vendorbak); err != nil {
t.Fatal(err)
}

if err = pc.h.ShouldExist(vendorbak + string(filepath.Separator) + "badinput_fileroot"); err != nil {
t.Fatal(err)
}

// Create another vendor directory. Previous vendor moved as backup.
os.MkdirAll("vendor", 0777)
pc.CopyFile(dummyFile, "txn_writer/badinput_fileroot")

// Should return error on creating backup with existing filename
vendorbak, err = BackupVendor("vendor", "sfx")

if err != errVendorBackupFailed {
t.Fatalf("Vendor backup error is not as expected: \n\t(GOT) %v\n\t(WNT) %v", err, errVendorBackupFailed)
}

if vendorbak != "" {
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "")
}

// Delete vendor
if err = os.RemoveAll("vendor"); err != nil {
t.Fatal(err)
}

// Should return empty backup file name when no vendor exists
vendorbak, err = BackupVendor("vendor", "sfx")
if err != nil {
t.Fatal(err)
}

if vendorbak != "" {
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "")
}
}

0 comments on commit 109db8e

Please sign in to comment.