Skip to content

Commit

Permalink
refactoring index DeleteEntry method (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 20, 2023
1 parent 034545a commit 81cb297
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func restoreIndex(rootGoitPath, path string, index *store.Index, tree *object.Tree) error {
// get entry
_, entry, isEntryFound := index.GetEntry([]byte(path))
_, _, 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 @@ -38,7 +38,7 @@ func restoreIndex(rootGoitPath, path string, index *store.Index, tree *object.Tr
}
} else { // if node is not in the last commit
// delete entry
if err := index.DeleteEntry(rootGoitPath, entry); err != nil {
if err := index.DeleteEntry(rootGoitPath, []byte(path)); err != nil {
return fmt.Errorf("fail to delete entry: %w", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/store/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func (idx *Index) Update(rootGoitPath string, hash sha.SHA1, path []byte) (bool,
return true, nil
}

func (idx *Index) DeleteEntry(rootGoitPath string, entry *Entry) error {
pos, _, isFound := idx.GetEntry(entry.Path)
func (idx *Index) DeleteEntry(rootGoitPath string, path []byte) error {
pos, _, isFound := idx.GetEntry(path)
if !isFound {
return fmt.Errorf("'%s' is not registered in index, so fail to delete", entry.Path)
return fmt.Errorf("'%s' is not registered in index, so fail to delete", path)
}

// delete target entry
Expand Down
12 changes: 4 additions & 8 deletions internal/store/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func TestDeleteEntry(t *testing.T) {
path []byte
}
type args struct {
entry *Entry
path []byte
}
type test struct {
name string
Expand All @@ -399,16 +399,14 @@ func TestDeleteEntry(t *testing.T) {
hash = sha.SHA1(hash)
path := []byte("cmd/main.go")

entry := NewEntry(hash, path)

return &test{
name: "success",
fields: fields{
hash: hash,
path: path,
},
args: args{
entry: entry,
path: path,
},
wantErr: false,
}
Expand All @@ -418,16 +416,14 @@ func TestDeleteEntry(t *testing.T) {
hash = sha.SHA1(hash)
path := []byte("cmd/main.go")

dummyEntry := NewEntry([]byte{}, []byte("not_exist.txt"))

return &test{
name: "not found",
fields: fields{
hash: hash,
path: path,
},
args: args{
entry: dummyEntry,
path: []byte("not_exist.txt"),
},
wantErr: true,
}
Expand Down Expand Up @@ -490,7 +486,7 @@ func TestDeleteEntry(t *testing.T) {
t.Log(err)
}

if err := index.DeleteEntry(goitDir, tt.args.entry); (err != nil) != tt.wantErr {
if err := index.DeleteEntry(goitDir, tt.args.path); (err != nil) != tt.wantErr {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}
})
Expand Down

0 comments on commit 81cb297

Please sign in to comment.