Skip to content

Commit

Permalink
add logging for rename branch (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 12, 2023
1 parent e410005 commit 4fe5aea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ var branchCmd = &cobra.Command{

// rename current branch
if renameOption != "" {
prevBranch := client.Head.Reference
if err := client.Refs.RenameBranch(client.RootGoitPath, client.Head.Reference, renameOption); err != nil {
return fmt.Errorf("fail to rename branch: %w", err)
}
// update HEAD
if err := client.Head.Update(client.Refs, client.RootGoitPath, renameOption); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
// log
if err := gLogger.WriteHEAD(log.NewRecord(log.BranchRecord, client.Head.Commit.Hash, nil, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s to refs/heads/%s", prevBranch, client.Head.Reference))); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteHEAD(log.NewRecord(log.BranchRecord, nil, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s to refs/heads/%s", prevBranch, client.Head.Reference))); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.DeleteBranch(prevBranch); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, nil, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("Created from %s", prevBranch)), client.Head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, client.Head.Commit.Hash, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("renamed refs/heads/%s refs/heads/%s", prevBranch, client.Head.Reference)), client.Head.Reference); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

if deleteOption != "" {
Expand Down
17 changes: 15 additions & 2 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ func NewRecord(recType recordType, from, to sha.SHA1, name, email string, t time
}

func (r *record) String() string {
var fromStr string
var fromStr, toStr string
if r.from == nil {
fromStr = strings.Repeat("0", 40)
} else {
fromStr = r.from.String()
}
return fmt.Sprintf("%s %s %s <%s> %s %s\t%s: %s\n", fromStr, r.to, r.name, r.email, r.unixtime, r.timeDiff, r.recType, r.message)
if r.to == nil {
toStr = strings.Repeat("0", 40)
} else {
toStr = r.to.String()
}
return fmt.Sprintf("%s %s %s <%s> %s %s\t%s: %s\n", fromStr, toStr, r.name, r.email, r.unixtime, r.timeDiff, r.recType, r.message)
}

type GoitLogger struct {
Expand Down Expand Up @@ -137,3 +142,11 @@ func (l *GoitLogger) WriteBranch(r *record, branchName string) error {

return nil
}

func (l *GoitLogger) DeleteBranch(branchName string) error {
branchPath := filepath.Join(l.rootGoitPath, "logs", "refs", "heads", branchName)
if err := os.Remove(branchPath); err != nil {
return fmt.Errorf("fail to delete %s: %w", branchPath, err)
}
return nil
}

0 comments on commit 4fe5aea

Please sign in to comment.