Skip to content

Commit

Permalink
refactoring restore command (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 18, 2023
1 parent 059c37f commit 877b518
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (

"github.com/JunNishimura/Goit/internal/file"
"github.com/JunNishimura/Goit/internal/object"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)

func restoreIndex(tree *object.Tree, path string) error {
func restoreIndex(rootGoitPath, path string, index *store.Index, tree *object.Tree) error {
// get entry
_, entry, isEntryFound := client.Idx.GetEntry([]byte(path))
_, entry, isEntryFound := index.GetEntry([]byte(path))
if !isEntryFound {
return fmt.Errorf("error: pathspec '%s' did not match any file(s) known to goit", path)
}
Expand All @@ -28,7 +29,7 @@ func restoreIndex(tree *object.Tree, path string) error {
// restore index
if isNodeFound { // if node is in the last commit
// change hash
isUpdated, err := client.Idx.Update(client.RootGoitPath, node.Hash, []byte(path))
isUpdated, err := client.Idx.Update(rootGoitPath, node.Hash, []byte(path))
if err != nil {
return fmt.Errorf("fail to update index: %w", err)
}
Expand All @@ -37,21 +38,21 @@ func restoreIndex(tree *object.Tree, path string) error {
}
} else { // if node is not in the last commit
// delete entry
if err := client.Idx.DeleteEntry(client.RootGoitPath, entry); err != nil {
if err := index.DeleteEntry(rootGoitPath, entry); err != nil {
return fmt.Errorf("fail to delete entry: %w", err)
}
}

return nil
}

func restoreWorkingDirectory(path string) error {
_, entry, isEntryFound := client.Idx.GetEntry([]byte(path))
func restoreWorkingDirectory(rootGoitPath, path string, index *store.Index) error {
_, entry, isEntryFound := index.GetEntry([]byte(path))
if !isEntryFound {
return fmt.Errorf("error: pathspec '%s' did not match any file(s) known to goit", path)
}

obj, err := object.GetObject(client.RootGoitPath, entry.Hash)
obj, err := object.GetObject(rootGoitPath, entry.Hash)
if err != nil {
return fmt.Errorf("fail to get object '%s': %w", path, err)
}
Expand Down Expand Up @@ -154,7 +155,7 @@ var restoreCmd = &cobra.Command{
cleanedRelPath := strings.ReplaceAll(relPath, `\`, "/")

// restore index
if err := restoreIndex(tree, cleanedRelPath); err != nil {
if err := restoreIndex(client.RootGoitPath, cleanedRelPath, client.Idx, tree); err != nil {
return err
}
}
Expand All @@ -163,7 +164,7 @@ var restoreCmd = &cobra.Command{
cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/")

// restore index
if err := restoreIndex(tree, cleanedArg); err != nil {
if err := restoreIndex(client.RootGoitPath, cleanedArg, client.Idx, tree); err != nil {
return err
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ var restoreCmd = &cobra.Command{
cleanedRelPath := strings.ReplaceAll(relPath, `\`, "/")

// restore working directory
if err := restoreWorkingDirectory(cleanedRelPath); err != nil {
if err := restoreWorkingDirectory(client.RootGoitPath, cleanedRelPath, client.Idx); err != nil {
return err
}
}
Expand All @@ -206,7 +207,7 @@ var restoreCmd = &cobra.Command{
cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/")

// restore working directory
if err := restoreWorkingDirectory(cleanedArg); err != nil {
if err := restoreWorkingDirectory(client.RootGoitPath, cleanedArg, client.Idx); err != nil {
return err
}
}
Expand Down

0 comments on commit 877b518

Please sign in to comment.