Skip to content

Commit

Permalink
write branch to a file (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 9b0e71d commit 03c3f2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 1 addition & 2 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ var branchCmd = &cobra.Command{

// 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 {
if err := client.Refs.AddBranch(client.RootGoitPath, addBranchName, addBranchHash); err != nil {
return fmt.Errorf("fail to add branch '%s': %w", addBranchName, err)
}
}
Expand Down
13 changes: 12 additions & 1 deletion internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *Refs) GetBranch(name string) (*branch, error) {
return nil, fmt.Errorf("fail to find '%s' branch", name)
}

func (r *Refs) AddBranch(newBranchName string, newBranchHash sha.SHA1) error {
func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.SHA1) error {
// check if branch already exists
n := r.getBranchPos(newBranchName)
if n != NewBranchFlag {
Expand All @@ -90,6 +90,17 @@ func (r *Refs) AddBranch(newBranchName string, newBranchHash sha.SHA1) error {
b := newBranch(newBranchName, newBranchHash)
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
}

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

Expand Down

0 comments on commit 03c3f2d

Please sign in to comment.