diff --git a/cmd/add.go b/cmd/add.go index f06b94c..ed3b05a 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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) @@ -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) } @@ -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) } @@ -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 } } diff --git a/cmd/commit.go b/cmd/commit.go index b4c115d..54e1f06 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -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 @@ -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) } @@ -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) } @@ -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) } @@ -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) } } diff --git a/cmd/log.go b/cmd/log.go index cf21e17..5f3e50f 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -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{}{} @@ -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 } @@ -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 { diff --git a/cmd/restore.go b/cmd/restore.go index fe7ad82..d7744b5 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -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) } @@ -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) } @@ -37,7 +38,7 @@ 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) } } @@ -45,13 +46,13 @@ func restoreIndex(tree *object.Tree, path string) error { 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) } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } diff --git a/cmd/revParse.go b/cmd/revParse.go index 327c450..819eaa5 100644 --- a/cmd/revParse.go +++ b/cmd/revParse.go @@ -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 { @@ -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 }