Skip to content

Commit

Permalink
Merge pull request #131 from JunNishimura/#130
Browse files Browse the repository at this point in the history
refactoring cmd directory
  • Loading branch information
JunNishimura committed Jun 18, 2023
2 parents 4d0cfc7 + 8318d92 commit 7798370
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
11 changes: 6 additions & 5 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

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

func add(path string) error {
func add(rootGoitPath, path string, index *store.Index) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("%w: %s", ErrIOHandling, path)
Expand All @@ -40,7 +41,7 @@ func add(path string) error {
byteRelPath := []byte(cleanedRelPath)

// update index
isUpdated, err := client.Idx.Update(client.RootGoitPath, object.Hash, byteRelPath)
isUpdated, err := index.Update(rootGoitPath, object.Hash, byteRelPath)
if err != nil {
return fmt.Errorf("fail to update index: %w", err)
}
Expand All @@ -49,7 +50,7 @@ func add(path string) error {
}

// write object to file
if err := object.Write(client.RootGoitPath); err != nil {
if err := object.Write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write object: %w", err)
}

Expand Down Expand Up @@ -117,12 +118,12 @@ var addCmd = &cobra.Command{
return fmt.Errorf("fail to get file path under directory: %w", err)
}
for _, filePath := range filePaths {
if err := add(filePath); err != nil {
if err := add(client.RootGoitPath, filePath, client.Idx); err != nil {
return err
}
}
} else {
if err := add(path); err != nil {
if err := add(client.RootGoitPath, path, client.Idx); err != nil {
return err
}
}
Expand Down
42 changes: 21 additions & 21 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ to set your account's default identity.
ErrNothingToCommit = errors.New("nothing to commit, working tree clean")
)

func commit() error {
func commit(rootGoitPath string, index *store.Index, head *store.Head, conf *store.Config, refs *store.Refs) error {
// make and write tree object
treeObject, err := writeTreeObject(client.RootGoitPath, client.Idx.Entries)
treeObject, err := writeTreeObject(rootGoitPath, index.Entries)
if err != nil {
return err
}

// make and write commit object
var data []byte
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", client.Head.Reference)
branchPath := filepath.Join(rootGoitPath, "refs", "heads", head.Reference)
branchBytes, err := os.ReadFile(branchPath)
author := object.NewSign(client.Conf.GetUserName(), client.Conf.GetEmail())
author := object.NewSign(conf.GetUserName(), conf.GetEmail())
committer := author
if err != nil {
// no branch means that this is the initial commit
Expand All @@ -62,36 +62,36 @@ func commit() error {
if err != nil {
return fmt.Errorf("fail to make commit object: %w", err)
}
if err := commit.Write(client.RootGoitPath); err != nil {
if err := commit.Write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write commit object: %w", err)
}

// create/update branch
var from sha.SHA1
if client.Refs.IsBranchExist(client.Head.Reference) {
if refs.IsBranchExist(head.Reference) {
// update
if err := client.Refs.UpdateBranchHash(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to update branch %s: %w", client.Head.Reference, err)
if err := refs.UpdateBranchHash(rootGoitPath, head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to update branch %s: %w", head.Reference, err)
}
from = client.Head.Commit.Hash
from = head.Commit.Hash
} else {
// create
if err := client.Refs.AddBranch(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to create branch %s: %w", client.Head.Reference, err)
if err := refs.AddBranch(rootGoitPath, head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to create branch %s: %w", head.Reference, err)
}
from = nil
}
// log
record := log.NewRecord(log.CommitRecord, from, commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), message)
record := log.NewRecord(log.CommitRecord, from, commit.Hash, conf.GetUserName(), conf.GetEmail(), time.Now(), message)
if err := gLogger.WriteHEAD(record); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(record, client.Head.Reference); err != nil {
if err := gLogger.WriteBranch(record, head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}

// update HEAD
if err := client.Head.Update(client.Refs, client.RootGoitPath, client.Head.Reference); err != nil {
if err := head.Update(refs, rootGoitPath, head.Reference); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}

Expand Down Expand Up @@ -119,21 +119,21 @@ func isIndexDifferentFromTree(index *store.Index, tree *object.Tree) (bool, erro
return false, nil
}

func isCommitNecessary(commitObj *object.Commit) (bool, error) {
func isCommitNecessary(rootGoitPath string, index *store.Index, commitObj *object.Commit) (bool, error) {
// get tree object
treeObject, err := object.GetObject(client.RootGoitPath, commitObj.Tree)
treeObject, err := object.GetObject(rootGoitPath, commitObj.Tree)
if err != nil {
return false, fmt.Errorf("fail to get tree object: %w", err)
}

// get tree
tree, err := object.NewTree(client.RootGoitPath, treeObject)
tree, err := object.NewTree(rootGoitPath, treeObject)
if err != nil {
return false, fmt.Errorf("fail to get tree: %w", err)
}

// compare index with tree
isDiff, err := isIndexDifferentFromTree(client.Idx, tree)
isDiff, err := isIndexDifferentFromTree(index, tree)
if err != nil {
return false, fmt.Errorf("fail to compare index with tree: %w", err)
}
Expand Down Expand Up @@ -170,12 +170,12 @@ var commitCmd = &cobra.Command{
}

// commit
if err := commit(); err != nil {
if err := commit(client.RootGoitPath, client.Idx, client.Head, client.Conf, client.Refs); err != nil {
return err
}
} else {
// compare last commit with index
isDiff, err := isCommitNecessary(client.Head.Commit)
isDiff, err := isCommitNecessary(client.RootGoitPath, client.Idx, client.Head.Commit)
if err != nil {
return fmt.Errorf("fail to compare last commit with index: %w", err)
}
Expand All @@ -184,7 +184,7 @@ var commitCmd = &cobra.Command{
}

// commit
if err := commit(); err != nil {
if err := commit(client.RootGoitPath, client.Idx, client.Head, client.Conf, client.Refs); err != nil {
return fmt.Errorf("fail to commit: %w", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (

type WalkFunc func(commit *object.Commit) error

func walkHistory(hash sha.SHA1, walkFunc WalkFunc) error {
func walkHistory(rootGoitPath string, hash sha.SHA1, walkFunc WalkFunc) error {
queue := []sha.SHA1{hash}
visitMap := map[string]struct{}{}

Expand All @@ -37,7 +37,7 @@ func walkHistory(hash sha.SHA1, walkFunc WalkFunc) error {
}
visitMap[currentHash.String()] = struct{}{}

commitObject, err := object.GetObject(client.RootGoitPath, currentHash)
commitObject, err := object.GetObject(rootGoitPath, currentHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,7 +80,7 @@ var logCmd = &cobra.Command{
}

// print log
if err := walkHistory(client.Head.Commit.Hash, func(commit *object.Commit) error {
if err := walkHistory(client.RootGoitPath, client.Head.Commit.Hash, func(commit *object.Commit) error {
fmt.Println(commit)
return nil
}); err != nil {
Expand Down
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
9 changes: 5 additions & 4 deletions cmd/revParse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"path/filepath"
"strings"

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

func revParse(refNames ...string) error {
func revParse(rootGoitPath string, head *store.Head, refNames ...string) error {
for _, refName := range refNames {
var refPath string
if strings.ToLower(refName) == "head" {
refPath = filepath.Join(client.RootGoitPath, "refs", "heads", client.Head.Reference)
refPath = filepath.Join(rootGoitPath, "refs", "heads", head.Reference)
} else {
refPath = filepath.Join(client.RootGoitPath, "refs", "heads", refName)
refPath = filepath.Join(rootGoitPath, "refs", "heads", refName)
}
hashBytes, err := os.ReadFile(refPath)
if err != nil {
Expand All @@ -42,7 +43,7 @@ var revParseCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := revParse(args...); err != nil {
if err := revParse(client.RootGoitPath, client.Head, args...); err != nil {
return err
}

Expand Down

0 comments on commit 7798370

Please sign in to comment.