Skip to content

Commit

Permalink
Merge pull request #110 from JunNishimura/#109
Browse files Browse the repository at this point in the history
replace updateReference function with refs method
  • Loading branch information
JunNishimura committed Jun 10, 2023
2 parents 8d1f43c + 21041ae commit adb6bdb
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func commit() error {
}

// update branch
if err := updateReference(branchPath, commit.Hash); err != nil {
return fmt.Errorf("fail to update branch %s: %w", branchPath, err)
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)
}

return nil
Expand Down
33 changes: 14 additions & 19 deletions cmd/updateRef.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/JunNishimura/Goit/internal/sha"
"github.com/spf13/cobra"
)

func updateReference(refPath string, hash sha.SHA1) error {
f, err := os.Create(refPath)
if err != nil {
return fmt.Errorf("%w: %s", ErrIOHandling, refPath)
}
defer f.Close()

_, err = f.WriteString(hash.String())
if err != nil {
return fmt.Errorf("fail to write hash(%s) to %s", hash.String(), refPath)
}

return nil
}
var (
branchRegexp = regexp.MustCompile("refs/heads/.+")
)

// updateRefCmd represents the updateRef command
var updateRefCmd = &cobra.Command{
Expand All @@ -44,7 +35,11 @@ var updateRefCmd = &cobra.Command{
}

// get reference path
refPath := filepath.Join(client.RootGoitPath, args[0])
if ok := branchRegexp.MatchString(args[0]); !ok {
return fmt.Errorf("invalid branch path %s", args[0])
}
branchSplit := strings.Split(args[0], "/")
branchName := branchSplit[len(branchSplit)-1]

// hash validation
hashString := args[1]
Expand All @@ -53,15 +48,15 @@ var updateRefCmd = &cobra.Command{
}
hashPath := filepath.Join(client.RootGoitPath, "objects", hashString[:2], hashString[2:])
if _, err := os.Stat(hashPath); os.IsNotExist(err) {
return fmt.Errorf("fatal: trying to write ref '%s' with nonexistent object %s", refPath, hashString)
return fmt.Errorf("fatal: trying to write ref '%s' with nonexistent object %s", args[0], hashString)
}
hash, err := sha.ReadHash(hashString)
newHash, err := sha.ReadHash(hashString)
if err != nil {
return ErrInvalidHash
}

if err := updateReference(refPath, hash); err != nil {
return fmt.Errorf("fail to update reference %s: %w", refPath, err)
if err := client.Refs.UpdateBranchHash(client.RootGoitPath, branchName, newHash); err != nil {
return fmt.Errorf("fail to update reference %s: %w", args[0], err)
}

return nil
Expand Down
42 changes: 34 additions & 8 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ func (b *branch) loadHash(rootGoitPath string) error {
return nil
}

func (b *branch) write(rootGoitPath string) error {
branchPath := filepath.Join(rootGoitPath, "refs", "heads", b.Name)
f, err := os.Create(branchPath)
if err != nil {
return fmt.Errorf("fail to create %s: %w", branchPath, err)
}
defer f.Close()

if _, err := f.WriteString(b.hash.String()); err != nil {
return fmt.Errorf("fail to write hash(%s): %w", b.hash, err)
}

return nil
}

type Refs struct {
Heads []*branch
}
Expand Down Expand Up @@ -94,14 +109,8 @@ func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.S
r.Heads = append(r.Heads, b)

// write file
branchPath := filepath.Join(rootGoitPath, "refs", "heads", newBranchName)
f, err := os.Create(branchPath)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(newBranchHash.String()); err != nil {
return err
if err := b.write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write branch: %w", err)
}

// sort heads
Expand Down Expand Up @@ -193,3 +202,20 @@ func (r *Refs) DeleteBranch(rootGoitPath, headBranchName, deleteBranchName strin

return nil
}

func (r *Refs) UpdateBranchHash(rootGoitPath, branchName string, newHash sha.SHA1) error {
n := r.getBranchPos(branchName)
if n == NewBranchFlag {
return fmt.Errorf("branch '%s' does not exist", branchName)
}

branch := r.Heads[n]
branch.hash = newHash

// write file
if err := branch.write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write branch: %w", err)
}

return nil
}
192 changes: 192 additions & 0 deletions internal/store/refs_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package store

import (
"encoding/hex"
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/JunNishimura/Goit/internal/sha"
Expand Down Expand Up @@ -103,6 +105,74 @@ func TestLoadHash(t *testing.T) {
}
}

func TestBranchWrite(t *testing.T) {
type fields struct {
name string
hash sha.SHA1
}
type test struct {
name string
fields fields
wantFileName string
wantFileContent string
wantErr bool
}
tests := []*test{
func() *test {
hash, _ := hex.DecodeString("87f3c49bccf2597484ece08746d3ee5defaba335")

return &test{
name: "success",
fields: fields{
name: "main",
hash: sha.SHA1(hash),
},
wantFileName: "main",
wantFileContent: "87f3c49bccf2597484ece08746d3ee5defaba335",
wantErr: false,
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
// .goit initialization
goitDir := filepath.Join(tmpDir, ".goit")
if err := os.Mkdir(goitDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, goitDir)
}
// make .goit/refs directory
refsDir := filepath.Join(goitDir, "refs")
if err := os.Mkdir(refsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, refsDir)
}
// make .goit/refs/heads directory
headsDir := filepath.Join(refsDir, "heads")
if err := os.Mkdir(headsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, headsDir)
}
// write branch
b := newBranch(tt.fields.name, tt.fields.hash)
if err := b.write(goitDir); (err != nil) != tt.wantErr {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}

branchPath := filepath.Join(headsDir, tt.wantFileName)
if _, err := os.Stat(branchPath); os.IsNotExist(err) {
t.Errorf("fail to find branch '%s': %v", tt.wantFileName, err)
}
hashBytes, err := os.ReadFile(branchPath)
if err != nil {
t.Errorf("fail to read file '%s': %v", branchPath, err)
}

if string(hashBytes) != tt.wantFileContent {
t.Errorf("got = %s, want = %s", string(hashBytes), tt.wantFileContent)
}
})
}
}

func TestNewRefs(t *testing.T) {
type fields struct {
name string
Expand Down Expand Up @@ -504,3 +574,125 @@ func TestDeleteBranch(t *testing.T) {
})
}
}

func TestUpdateBranchHash(t *testing.T) {
type args struct {
branchName string
newHash sha.SHA1
}
type fields struct {
name string
hash sha.SHA1
}
tests := []struct {
name string
args args
fieldsList []*fields
want *Refs
wantErr bool
}{
{
name: "success",
args: args{
branchName: "main",
newHash: sha.SHA1([]byte(strings.Repeat("4", 40))),
},
fieldsList: []*fields{
{
name: "main",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
{
name: "test",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
},
want: &Refs{
Heads: []*branch{
{
Name: "main",
hash: sha.SHA1([]byte(strings.Repeat("4", 40))),
},
{
Name: "test",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
},
},
wantErr: false,
},
{
name: "failure",
args: args{
branchName: "xxxx",
newHash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
fieldsList: []*fields{
{
name: "main",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
{
name: "test",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
},
want: &Refs{
Heads: []*branch{
{
Name: "main",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
{
Name: "test",
hash: sha.SHA1([]byte("87f3c49bccf2597484ece08746d3ee5defaba335")),
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
// .goit initialization
goitDir := filepath.Join(tmpDir, ".goit")
if err := os.Mkdir(goitDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, goitDir)
}
// make .goit/refs directory
refsDir := filepath.Join(goitDir, "refs")
if err := os.Mkdir(refsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, refsDir)
}
// make .goit/refs/heads directory
headsDir := filepath.Join(refsDir, "heads")
if err := os.Mkdir(headsDir, os.ModePerm); err != nil {
t.Logf("%v: %s", err, headsDir)
}
for _, field := range tt.fieldsList {
// make main branch
branchPath := filepath.Join(headsDir, field.name)
f, err := os.Create(branchPath)
if err != nil {
t.Logf("%v: %s", err, branchPath)
}
if _, err := f.WriteString(field.hash.String()); err != nil {
t.Log(err)
}
f.Close()
}

r, err := NewRefs(goitDir)
if err != nil {
t.Log(err)
}
if err := r.UpdateBranchHash(goitDir, tt.args.branchName, tt.args.newHash); (err != nil) != tt.wantErr {
t.Errorf("got = %v, want = %v", err, tt.wantErr)
}
if !reflect.DeepEqual(r, tt.want) {
t.Errorf("got = %v, want = %v", r, tt.want)
}
})
}
}

0 comments on commit adb6bdb

Please sign in to comment.