Skip to content

Commit

Permalink
add error check (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 11, 2023
1 parent 9202e4f commit d1a0e3d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/JunNishimura/Goit/internal/log"
"github.com/JunNishimura/Goit/internal/object"
"github.com/JunNishimura/Goit/internal/sha"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -66,25 +67,30 @@ func commit() error {
}

// create/update branch
var from sha.SHA1
if client.Refs.IsBranchExist(client.Head.Reference) {
// update
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)
}
// log
record := log.NewRecord(log.CommitRecord, client.Head.Commit.Hash, commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), message)
gLogger.WriteHEAD(record)
gLogger.WriteBranch(record, client.Head.Reference)
from = client.Head.Commit.Hash
} else {
// create
if err := client.Refs.AddBranch(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to create branch %s: %w", client.Head.Reference, err)
}
// log
record := log.NewRecord(log.CommitRecord, nil, commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), message)
gLogger.WriteHEAD(record)
gLogger.WriteBranch(record, client.Head.Reference)
from = nil
}
// log
record := log.NewRecord(log.CommitRecord, from, commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), message)
if err := gLogger.WriteHEAD(record); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(record, client.Head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}

// update HEAD
if err := client.Head.Update(client.Refs, client.RootGoitPath, client.Head.Reference); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
Expand Down

0 comments on commit d1a0e3d

Please sign in to comment.