Skip to content

Commit

Permalink
Merge pull request #50 from JunNishimura/#49
Browse files Browse the repository at this point in the history
fix isCommitNecessary function
  • Loading branch information
JunNishimura authored May 21, 2023
2 parents c42acdf + b4a33a2 commit bb8fe9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
11 changes: 7 additions & 4 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
28 changes: 19 additions & 9 deletions object/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit bb8fe9e

Please sign in to comment.