Skip to content

Commit

Permalink
add write method for branch (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 10, 2023
1 parent e82993f commit c5f2421
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 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 @@ -203,15 +212,9 @@ func (r *Refs) UpdateBranchHash(rootGoitPath, branchName string, newHash sha.SHA
branch := r.Heads[n]
branch.hash = newHash

branchPath := filepath.Join(rootGoitPath, "refs", "heads", branchName)
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(newHash.String()); err != nil {
return fmt.Errorf("fail to write hash: %w", err)
// write file
if err := branch.write(rootGoitPath); err != nil {
return fmt.Errorf("fail to write branch: %w", err)
}

return nil
Expand Down

0 comments on commit c5f2421

Please sign in to comment.