Skip to content

Commit

Permalink
move getHead to head.go (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 4, 2023
1 parent 2d635c4 commit 6ac66b9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
31 changes: 1 addition & 30 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"

"github.com/JunNishimura/Goit/internal/object"
"github.com/JunNishimura/Goit/internal/sha"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -150,29 +149,6 @@ func isCommitNecessary(commitObj *object.Commit) (bool, error) {
return isDiff, nil
}

// get HEAD commit
func getHeadCommit() (*object.Commit, error) {
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", string(client.Head))
hashBytes, err := os.ReadFile(branchPath)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrIOHandling, branchPath)
}
hashString := string(hashBytes)
hash, err := sha.ReadHash(hashString)
if err != nil {
return nil, fmt.Errorf("fail to decode hash string: %w", err)
}
commitObject, err := object.GetObject(client.RootGoitPath, hash)
if err != nil {
return nil, fmt.Errorf("fail to get last commit object: %w", err)
}
commit, err := object.NewCommit(commitObject)
if err != nil {
return nil, fmt.Errorf("fail to get last commit: %w", err)
}
return commit, nil
}

// commitCmd represents the commit command
var commitCmd = &cobra.Command{
Use: "commit",
Expand Down Expand Up @@ -211,13 +187,8 @@ var commitCmd = &cobra.Command{
return fmt.Errorf("fail to delete untracked files: %w", err)
}

headCommit, err := getHeadCommit()
if err != nil {
return fmt.Errorf("fail to get HEAD commit: %w", err)
}

// compare last commit with index
isCommitNecessary, err := isCommitNecessary(headCommit)
isCommitNecessary, err := isCommitNecessary(client.Head.Commit)
if err != nil {
return fmt.Errorf("fail to compare last commit with index: %w", err)
}
Expand Down
10 changes: 2 additions & 8 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,13 @@ var restoreCmd = &cobra.Command{
if isStaged {
// restore --stage is comparing index with commit object pointed by HEAD
// so, at lease one commit is needed
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", string(client.Head))
branchPath := filepath.Join(client.RootGoitPath, "refs", "heads", client.Head.Reference)
if _, err := os.Stat(branchPath); os.IsNotExist(err) {
return errors.New("fatal: could not resolve HEAD")
}

// get HEAD commit
headCommit, err := getHeadCommit()
if err != nil {
return fmt.Errorf("fail to get HEAD commit: %w", err)
}

// get tree from HEAD commit
treeObject, err := object.GetObject(client.RootGoitPath, headCommit.Tree)
treeObject, err := object.GetObject(client.RootGoitPath, client.Head.Commit.Tree)
if err != nil {
return fmt.Errorf("fail to get tree object from commit HEAD: %w", err)
}
Expand Down

0 comments on commit 6ac66b9

Please sign in to comment.