Skip to content

Commit

Permalink
add add function (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 16aa862 commit 9b0e71d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
20 changes: 17 additions & 3 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,23 @@ var branchCmd = &cobra.Command{
return fmt.Errorf("fail to get list flag: %w", err)
}

// flag validation
if !((isList && renameOption == "" && deleteOption == "") || (!isList && renameOption != "" && deleteOption == "") || (!isList && renameOption == "" && deleteOption != "")) {
return fmt.Errorf("only one flag is acceptible")
// parameter validation
if !((len(args) == 1 && !isList && renameOption == "" && deleteOption == "") ||
(len(args) == 0 && isList && renameOption == "" && deleteOption == "") ||
(len(args) == 0 && !isList && renameOption != "" && deleteOption == "") ||
(len(args) == 0 && !isList && renameOption == "" && deleteOption != "")) {
return fmt.Errorf("parameters are not valid")
}

// add branch
if len(args) == 1 {
// check if
addBranchName := args[0]
addBranchHash := client.Head.Commit.Hash

if err := client.Refs.AddBranch(addBranchName, addBranchHash); err != nil {
return fmt.Errorf("fail to add branch '%s': %w", addBranchName, err)
}
}

// list branches
Expand Down
63 changes: 46 additions & 17 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,41 @@ import (
"fmt"
"os"
"path/filepath"
"sort"

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

const (
NewBranchFlag = -1
)

type branch struct {
Name string
hash sha.SHA1
}

func newBranch(rootGoitPath, branchName string) (*branch, error) {
branchPath := filepath.Join(rootGoitPath, "refs", "heads", branchName)
func newBranch(name string, hash sha.SHA1) *branch {
return &branch{
Name: name,
hash: hash,
}
}

func (b *branch) loadHash(rootGoitPath string) error {
branchPath := filepath.Join(rootGoitPath, "refs", "heads", b.Name)
hashByte, err := os.ReadFile(branchPath)
if err != nil {
return nil, err
return err
}
hashString := string(hashByte)
hash, err := sha.ReadHash(hashString)
if err != nil {
return nil, err
return err
}
return &branch{
Name: branchName,
hash: hash,
}, nil
b.hash = hash

return nil
}

type Refs struct {
Expand All @@ -45,8 +56,8 @@ func NewRefs(rootGoitPath string) (*Refs, error) {
return nil, err
}
for _, file := range files {
b, err := newBranch(rootGoitPath, file.Name())
if err != nil {
b := newBranch(file.Name(), nil)
if err := b.loadHash(rootGoitPath); err != nil {
return nil, err
}
r.Heads = append(r.Heads, b)
Expand All @@ -69,19 +80,37 @@ func (r *Refs) GetBranch(name string) (*branch, error) {
return nil, fmt.Errorf("fail to find '%s' branch", name)
}

func getBranchPos(branches []*branch, name string) (int, bool) {
for n, branch := range branches {
if branch.Name == name {
return n, true
func (r *Refs) AddBranch(newBranchName string, newBranchHash sha.SHA1) error {
// check if branch already exists
n := r.getBranchPos(newBranchName)
if n != NewBranchFlag {
return fmt.Errorf("fatal: a branch named '%s' already exists", newBranchName)
}

b := newBranch(newBranchName, newBranchHash)
r.Heads = append(r.Heads, b)

// sort heads
sort.Slice(r.Heads, func(i, j int) bool { return r.Heads[i].Name < r.Heads[j].Name })

return nil
}

// return the index of branch in the Refs Heads.
// if not found, return NewBranchFlag which is -1.
func (r *Refs) getBranchPos(branchName string) int {
for n, branch := range r.Heads {
if branch.Name == branchName {
return n
}
}
return -1, false
return NewBranchFlag
}

func (r *Refs) DeleteBranch(rootGoitPath, name string) error {
// delete branch from Refs
p, isBranchFound := getBranchPos(r.Heads, name)
if !isBranchFound {
p := r.getBranchPos(name)
if p == NewBranchFlag {
return fmt.Errorf("branch '%s' not found", name)
}
r.Heads = append(r.Heads[:p], r.Heads[p+1:]...)
Expand Down

0 comments on commit 9b0e71d

Please sign in to comment.