Skip to content

Commit

Permalink
Merge pull request #123 from JunNishimura/#122
Browse files Browse the repository at this point in the history
add Compare method
  • Loading branch information
JunNishimura authored Jun 17, 2023
2 parents b189c64 + 557e647 commit 53876b0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func isIndexDifferentFromTree(index *store.Index, tree *object.Tree) (bool, erro
if string(gotEntries[i].Path) != string(index.Entries[i].Path) {
return true, nil
}
if gotEntries[i].Hash.String() != index.Entries[i].Hash.String() {
if !gotEntries[i].Hash.Compare(index.Entries[i].Hash) {
return true, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/object/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestNewTree(t *testing.T) {
for i := 0; i < len(got.Children); i++ {
gotChild := got.Children[i]
wantChild := tt.want.Children[i]
if gotChild.Hash.String() != wantChild.Hash.String() {
if !gotChild.Hash.Compare(wantChild.Hash) {
t.Errorf("got = %v, want = %v", gotChild.Hash.String(), wantChild.Hash.String())
}
if gotChild.Name != wantChild.Name {
Expand Down
8 changes: 6 additions & 2 deletions internal/sha/sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ var (
ErrInvalidHash = errors.New("invalid hash")
)

func (sha1 SHA1) String() string {
return hex.EncodeToString(sha1)
func (s SHA1) String() string {
return hex.EncodeToString(s)
}

func (s SHA1) Compare(other SHA1) bool {
return s.String() == other.String()
}

func ReadHash(hashString string) (SHA1, error) {
Expand Down
56 changes: 56 additions & 0 deletions internal/sha/sha_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
package sha

import (
"encoding/hex"
"errors"
"strings"
"testing"
)

func TestCompare(t *testing.T) {
type args struct {
sha SHA1
}
type fields struct {
sha SHA1
}
type test struct {
name string
args args
fields fields
want bool
}
tests := []*test{
func() *test {
s, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")

return &test{
name: "success: true",
args: args{
sha: s,
},
fields: fields{
sha: s,
},
want: true,
}
}(),
func() *test {
s, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")
s2, _ := hex.DecodeString(strings.Repeat("0", 40))

return &test{
name: "success: false",
args: args{
sha: s,
},
fields: fields{
sha: s2,
},
want: false,
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fields.sha.Compare(tt.args.sha)
if got != tt.want {
t.Errorf("got = %v, want = %v", got, tt.want)
}
})
}
}

func TestReadHash(t *testing.T) {
type args struct {
hashString string
Expand Down
2 changes: 1 addition & 1 deletion internal/store/reflog.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *Reflog) load(rootGoitPath string, head *Head, refs *Refs) error {
record.hash = hash

// references
if head.Commit.Hash.String() == sp1[1] {
if head.Commit.Hash.Compare(hash) {
record.isHead = true
}
branches := refs.getBranchesByHash(hash)
Expand Down
2 changes: 1 addition & 1 deletion internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (r *Refs) getBranchPos(branchName string) int {
func (r *Refs) getBranchesByHash(hash sha.SHA1) []*branch {
var branches []*branch
for _, branch := range r.Heads {
if branch.hash.String() == hash.String() {
if branch.hash.Compare(hash) {
branches = append(branches, branch)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/store/refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestLoadHash(t *testing.T) {
if err := b.loadHash(goitDir); !errors.Is(err, tt.wantErr) {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}
if b.hash.String() != tt.want.String() {
if !b.hash.Compare(tt.want) {
t.Errorf("got = %s, want = %s", b.hash, tt.want)
}
})
Expand Down

0 comments on commit 53876b0

Please sign in to comment.