From b4a33a295faec88e9c8625d72f3282241edf102d Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sun, 21 May 2023 20:08:44 +0900 Subject: [PATCH] fix isCommitNecessary function (#49) Commit bug happens because checking hash string process was not implemented. --- cmd/commit.go | 11 +++++++---- object/tree.go | 28 +++++++++++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cmd/commit.go b/cmd/commit.go index c0bb3eb..bf973be 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -76,17 +76,20 @@ func isCommitNecessary(commitObj *object.Commit) (bool, error) { } // get entries from tree object - paths, err := treeObject.ExtractFilePaths(client.RootGoitPath, "") + entries, err := treeObject.ExtractEntries(client.RootGoitPath, "") if err != nil { return false, fmt.Errorf("fail to get filepath from tree object: %w", err) } // compare entries extraceted from tree object with index - if len(paths) != int(client.Idx.EntryNum) { + if len(entries) != int(client.Idx.EntryNum) { return true, nil } - for i := 0; i < len(paths); i++ { - if paths[i] != string(client.Idx.Entries[i].Path) { + for i := 0; i < len(entries); i++ { + if string(entries[i].Path) != string(client.Idx.Entries[i].Path) { + return true, nil + } + if entries[i].Hash.String() != client.Idx.Entries[i].Hash.String() { return true, nil } } diff --git a/object/tree.go b/object/tree.go index 8cac224..eacb5d3 100644 --- a/object/tree.go +++ b/object/tree.go @@ -89,9 +89,10 @@ func WriteTreeObject(rootGoitPath string, entries []*index.Entry) (*Object, erro return treeObject, nil } -func (to *Object) ExtractFilePaths(rootGoitPath, rootDir string) ([]string, error) { - var paths []string +func (to *Object) ExtractEntries(rootGoitPath, rootDir string) ([]*index.Entry, error) { + var entries []*index.Entry var dirName string + var filePath string buf := bytes.NewReader(to.Data) for { @@ -128,14 +129,25 @@ func (to *Object) ExtractFilePaths(rootGoitPath, rootDir string) ([]string, erro } else { path = fmt.Sprintf("%s/%s", rootDir, dirName) } - getPaths, err := treeObject.ExtractFilePaths(rootGoitPath, path) + getEntries, err := treeObject.ExtractEntries(rootGoitPath, path) if err != nil { return nil, err } - paths = append(paths, getPaths...) + entries = append(entries, getEntries...) dirName = "" } + if filePath != "" { + hashString := hex.EncodeToString([]byte(lineSplit[0])) + hash, err := sha.ReadHash(hashString) + if err != nil { + return nil, err + } + entry := index.NewEntry(hash, []byte(filePath)) + entries = append(entries, entry) + filePath = "" + } + if len(lineSplit) == 1 { // last line break } else { @@ -152,17 +164,15 @@ func (to *Object) ExtractFilePaths(rootGoitPath, rootDir string) ([]string, erro if fileMode == "040000" { dirName = fileName } else if fileMode == "100644" { - var path string if rootDir == "" { - path = fileName + filePath = fileName } else { - path = fmt.Sprintf("%s/%s", rootDir, fileName) + filePath = fmt.Sprintf("%s/%s", rootDir, fileName) } - paths = append(paths, path) } } } - return paths, nil + return entries, nil } func (to *Object) ConvertDataToString() (string, error) {