Skip to content

Commit

Permalink
Merge pull request #117 from JunNishimura/#108
Browse files Browse the repository at this point in the history
add logger
  • Loading branch information
JunNishimura committed Jun 13, 2023
2 parents 3828cc4 + e93e96a commit 9543fd5
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package cmd

import (
"fmt"
"time"

"github.com/JunNishimura/Goit/internal/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,6 +50,9 @@ var branchCmd = &cobra.Command{
if err := client.Refs.AddBranch(client.RootGoitPath, addBranchName, addBranchHash); err != nil {
return fmt.Errorf("fail to add branch '%s': %w", addBranchName, err)
}
if err := gLogger.WriteBranch(log.NewRecord(log.BranchRecord, nil, addBranchHash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("Created from %s", client.Head.Reference)), addBranchName); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

// list branches
Expand All @@ -57,19 +62,39 @@ 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 != "" {
if err := client.Refs.DeleteBranch(client.RootGoitPath, client.Head.Reference, deleteOption); err != nil {
return fmt.Errorf("fail to delete branch: %w", err)
}
if err := gLogger.DeleteBranch(deleteOption); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

return nil
Expand Down
16 changes: 16 additions & 0 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"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 @@ -64,17 +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)
}
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)
}
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
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"runtime/debug"

"github.com/JunNishimura/Goit/internal/file"
"github.com/JunNishimura/Goit/internal/log"
"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)

var (
gLogger *log.GoitLogger
client *store.Client
goitVersion = ""
)
Expand Down Expand Up @@ -77,6 +79,8 @@ func init() {
}
client = store.NewClient(config, index, head, r, rootGoitPath)

gLogger = log.NewGoitLogger(client.RootGoitPath)

rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolP("version", "v", false, "Show Goit version")
}
13 changes: 13 additions & 0 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package cmd
import (
"errors"
"fmt"
"time"

"github.com/JunNishimura/Goit/internal/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,18 +40,29 @@ var switchCmd = &cobra.Command{

// switch branch == update HEAD
if len(args) == 1 {
prevBranch := client.Head.Reference
if err := client.Head.Update(client.Refs, client.RootGoitPath, args[0]); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
if err := gLogger.WriteHEAD(log.NewRecord(log.CheckoutRecord, client.Head.Commit.Hash, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("moving from %s to %s", prevBranch, client.Head.Reference))); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

if createOption != "" {
prevBranch := client.Head.Reference
if err := client.Refs.AddBranch(client.RootGoitPath, createOption, client.Head.Commit.Hash); err != nil {
return fmt.Errorf("fail to create new branch %s: %w", createOption, err)
}
if err := client.Head.Update(client.Refs, client.RootGoitPath, createOption); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
if err := gLogger.WriteHEAD(log.NewRecord(log.CheckoutRecord, client.Head.Commit.Hash, client.Head.Commit.Hash, client.Conf.GetUserName(), client.Conf.GetEmail(), time.Now(), fmt.Sprintf("moving from %s to %s", prevBranch, client.Head.Reference))); 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)), createOption); err != nil {
return fmt.Errorf("log error: %w", err)
}
}

return nil
Expand Down
152 changes: 152 additions & 0 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package log

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

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

type recordType int

const (
CommitRecord recordType = iota
CheckoutRecord
BranchRecord
)

func (t recordType) String() string {
switch t {
case CommitRecord:
return "commit"
case CheckoutRecord:
return "checkout"
case BranchRecord:
return "branch"
default:
return "undefined"
}
}

type record struct {
recType recordType
from sha.SHA1
to sha.SHA1
name string
email string
unixtime string
timeDiff string
message string
}

func NewRecord(recType recordType, from, to sha.SHA1, name, email string, t time.Time, message string) *record {
unixtime := fmt.Sprint(t.Unix())
_, offset := t.Zone()
offsetMinutes := offset / 60
timeDiff := fmt.Sprintf("%+03d%02d", offsetMinutes/60, offsetMinutes%60)

return &record{
recType: recType,
from: from,
to: to,
name: name,
email: email,
unixtime: unixtime,
timeDiff: timeDiff,
message: message,
}
}

func (r *record) String() string {
var fromStr, toStr string
if r.from == nil {
fromStr = strings.Repeat("0", 40)
} else {
fromStr = r.from.String()
}
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 {
rootGoitPath string
}

func NewGoitLogger(rootGoitPath string) *GoitLogger {
return &GoitLogger{
rootGoitPath: rootGoitPath,
}
}

func (l *GoitLogger) WriteHEAD(r *record) error {
// init logs
logsPath := filepath.Join(l.rootGoitPath, "logs")
if _, err := os.Stat(logsPath); os.IsNotExist(err) {
if err := os.Mkdir(logsPath, os.ModePerm); err != nil {
return fmt.Errorf("fail to make dir %s: %w", logsPath, err)
}
}

// write HEAD log
headPath := filepath.Join(logsPath, "HEAD")
f, err := os.OpenFile(headPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return fmt.Errorf("fail to create %s: %w", headPath, err)
}
defer f.Close()

if _, err := f.WriteString(r.String()); err != nil {
return fmt.Errorf("fail to write %s: %w", headPath, err)
}

return nil
}

func (l *GoitLogger) WriteBranch(r *record, branchName string) error {
// init logs
logsPath := filepath.Join(l.rootGoitPath, "logs")
if _, err := os.Stat(logsPath); os.IsNotExist(err) {
if err := os.Mkdir(logsPath, os.ModePerm); err != nil {
return fmt.Errorf("fail to make dir %s: %w", logsPath, err)
}
}
logsRefsPath := filepath.Join(logsPath, "refs")
if _, err := os.Stat(logsRefsPath); os.IsNotExist(err) {
if err := os.Mkdir(logsRefsPath, os.ModePerm); err != nil {
return fmt.Errorf("fail to make dir %s: %w", logsRefsPath, err)
}
logsHeadsPath := filepath.Join(logsRefsPath, "heads")
if err := os.Mkdir(logsHeadsPath, os.ModePerm); err != nil {
return fmt.Errorf("fail to make dir %s: %w", logsHeadsPath, err)
}
}

// write branch log
branchPath := filepath.Join(logsRefsPath, "heads", branchName)
f, err := os.OpenFile(branchPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return fmt.Errorf("fail to create %s: %w", branchPath, err)
}
defer f.Close()

if _, err := f.WriteString(r.String()); err != nil {
return fmt.Errorf("fail to write %s: %w", branchPath, err)
}

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
}
Loading

0 comments on commit 9543fd5

Please sign in to comment.