Skip to content

Commit

Permalink
fix(checkout): checkout fails early if link exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Apr 10, 2020
1 parent 9d6e812 commit b8f697f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
38 changes: 30 additions & 8 deletions fsi/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,51 @@ func (fsi *FSI) LinkedRefs(offset, limit int) ([]reporef.DatasetRef, error) {
return refs, nil
}

// CreateLink links a working directory to a dataset. Returns the reference alias, and a
// rollback function if no error occurs
func (fsi *FSI) CreateLink(dirPath, refStr string) (alias string, rollback func(), err error) {
rollback = func() {}

// HasLink checks if a refString already has an existing link on the file system
func (fsi *FSI) HasLink(refStr string) (hasLink bool, err error) {
ref, err := repo.ParseDatasetRef(refStr)
if err != nil {
return "", rollback, err
return false, err
}
err = repo.CanonicalizeDatasetRef(fsi.repo, &ref)
if err != nil && err != repo.ErrNotFound && err != repo.ErrNoHistory {
return ref.String(), rollback, err
return false, err
}

return fsi.RefHasLink(ref)
}

// RefHasLink checks if a ref already has an existing link on the file system
func (fsi *FSI) RefHasLink(ref reporef.DatasetRef) (hasLink bool, err error) {
if stored, err := fsi.repo.GetRef(ref); err == nil {
if stored.FSIPath != "" {
// There is already a link for this dataset, see if that link still exists.
targetPath := filepath.Join(stored.FSIPath, QriRefFilename)
if _, err := os.Stat(targetPath); err == nil {
return "", rollback, fmt.Errorf("'%s' is already linked to %s", ref.AliasString(), stored.FSIPath)
return true, fmt.Errorf("'%s' is already linked to %s", ref.AliasString(), stored.FSIPath)
}
}
}
return false, nil
}

// CreateLink links a working directory to a dataset. Returns the reference alias, and a
// rollback function if no error occurs
func (fsi *FSI) CreateLink(dirPath, refStr string) (alias string, rollback func(), err error) {
rollback = func() {}

ref, err := repo.ParseDatasetRef(refStr)
if err != nil {
return "", rollback, err
}
err = repo.CanonicalizeDatasetRef(fsi.repo, &ref)
if err != nil && err != repo.ErrNotFound && err != repo.ErrNoHistory {
return ref.String(), rollback, err
}

if _, err := fsi.RefHasLink(ref); err != nil {
return "", rollback, err
}

// Link the FSIPath to the reference before putting it into the repo
log.Debugf("fsi.CreateLink: linking ref=%q, FSIPath=%q", ref, dirPath)
Expand Down
5 changes: 5 additions & 0 deletions lib/fsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (m *FSIMethods) Checkout(p *CheckoutParams, out *string) (err error) {
}
log.Debugf("Checkout loaded dataset %q", ref)

// Fail early if link already exists
if _, err := m.inst.fsi.HasLink(p.Ref); err != nil {
return err
}

// Create a directory.
if err := os.Mkdir(p.Dir, os.ModePerm); err != nil {
log.Debugf("Checkout, Mkdir failed, error: %s", ref)
Expand Down

0 comments on commit b8f697f

Please sign in to comment.