Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: output enhancements #549

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions internal/config/available_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ const GhostHookName = "prepare-commit-msg"

// AvailableHooks - list of hooks taken from https://git-scm.com/docs/githooks.
var AvailableHooks = [...]string{
"pre-applypatch",
"applypatch-msg",
"post-applypatch",
"pre-commit",
"pre-push",
"commit-msg",
"applypatch-msg",
"fsmonitor-watchman",
"p4-changelist",
"p4-post-changelist",
"p4-pre-submit",
"p4-prepare-changelist",
"pre-commit",
"post-applypatch",
"post-checkout",
"post-commit",
"pre-receive",
"proc-receive",
"post-receive",
"post-index-change",
"post-merge",
"pre-rebase",
"rebase",
"update",
"post-update",
"post-receive",
"post-rewrite",
"post-checkout",
"post-index-change",
"post-update",
"pre-applypatch",
"pre-auto-gc",
"pre-merge-commit",
"pre-push",
"pre-rebase",
"pre-receive",
"prepare-commit-msg",
"proc-receive",
"push-to-checkout",
"rebase",
"reference-transaction",
"sendemail-validate",
"update",
}

func HookUsesStagedFiles(hook string) bool {
Expand Down
11 changes: 10 additions & 1 deletion internal/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ import (
)

type Exec interface {
SetRootPath(root string)
Cmd(cmd string) (string, error)
CmdArgs(args ...string) (string, error)
CmdLines(cmd string) ([]string, error)
RawCmd(cmd string) (string, error)
}

type OsExec struct{}
type OsExec struct {
root string
}

// NewOsExec returns an object that executes given commands
// in the OS.
func NewOsExec() *OsExec {
return &OsExec{}
}

func (o *OsExec) SetRootPath(root string) {
o.root = root
}

// Cmd runs plain string command. Trims spaces around output.
func (o *OsExec) Cmd(cmd string) (string, error) {
args := strings.Split(cmd, " ")
Expand Down Expand Up @@ -68,9 +75,11 @@ func (o *OsExec) rawExecArgs(args ...string) (string, error) {
log.Debug("[lefthook] cmd: ", args)

cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = o.root
cmd.Env = append(os.Environ(), "LEFTHOOK=0")

out, err := cmd.CombinedOutput()
log.Debug("[lefthook] dir: ", o.root)
log.Debug("[lefthook] err: ", err)
log.Debug("[lefthook] out: ", string(out))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func NewRepository(fs afero.Fs, git Exec) (*Repository, error) {
gitPath = filepath.Join(rootPath, gitPath)
}

git.SetRootPath(rootPath)

return &Repository{
Fs: fs,
Git: git,
Expand Down Expand Up @@ -317,6 +319,9 @@ func (r *Repository) extractFiles(lines []string) ([]string, error) {
}

func (r *Repository) isFile(path string) (bool, error) {
if !strings.HasPrefix(path, r.RootPath) {
path = filepath.Join(r.RootPath, path)
}
stat, err := r.Fs.Stat(path)
if err != nil {
if os.IsNotExist(err) {
Expand Down
2 changes: 2 additions & 0 deletions internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type GitMock struct {
cases map[string]string
}

func (g GitMock) SetRootPath(_root string) {}

func (g GitMock) Cmd(cmd string) (string, error) {
res, err := g.RawCmd(cmd)
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, force bool) error {
return nil
}

log.Info(log.Cyan("SYNCING"))
log.Infof(log.Cyan("sync hooks"))

var success bool
defer func() {
if !success {
log.Info(log.Cyan(": ❌"))
}
}()

checksum, err := l.configChecksum()
if err != nil {
Expand Down Expand Up @@ -148,8 +155,11 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, force bool) error {
return err
}

success = true
if len(hookNames) > 0 {
log.Info(log.Cyan("SERVED HOOKS:"), log.Bold(strings.Join(hookNames, ", ")))
log.Info(log.Cyan(": ✔️"), log.Gray("("+strings.Join(hookNames, ", ")+")"))
} else {
log.Info(log.Cyan(": ✔️ "))
}

return nil
Expand Down
24 changes: 15 additions & 9 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
}

if !logSettings.SkipMeta() {
log.Info(log.Cyan("Lefthook v" + version.Version(false)))
log.Box(
log.Cyan("🥊 lefthook ")+log.Gray(fmt.Sprintf("v%s", version.Version(false))),
log.Gray("hook: ")+log.Bold(hookName),
)
}

// This line controls updating the git hook if config has changed
Expand All @@ -93,10 +96,6 @@ Run 'lefthook install' manually.`,
)
}

if !logSettings.SkipMeta() {
log.Info(log.Cyan("RUNNING HOOK:"), log.Bold(hookName))
}

// Find the hook
hook, ok := cfg.Hooks[hookName]
if !ok {
Expand Down Expand Up @@ -182,14 +181,21 @@ func printSummary(
) {
if len(results) == 0 {
if !logSettings.SkipEmptySummary() {
log.Info(log.Cyan("\nSUMMARY: (SKIP EMPTY)"))
log.Separate(
fmt.Sprintf(
"%s %s %s",
log.Cyan("summary:"),
log.Gray("(skip)"),
log.Yellow("empty"),
),
)
}
return
}

log.Info(log.Cyan(
fmt.Sprintf("\nSUMMARY: (done in %.2f seconds)", duration.Seconds()),
))
log.Separate(
log.Cyan("summary: ") + log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
)

if !logSettings.SkipSuccess() {
for _, result := range results {
Expand Down
35 changes: 22 additions & 13 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"sync/atomic"

"github.com/charmbracelet/lipgloss"
"github.com/spf13/afero"

"github.com/evilmartians/lefthook/internal/config"
Expand All @@ -29,6 +30,7 @@ type status int8
const (
executableFileMode os.FileMode = 0o751
executableMask os.FileMode = 0o111
execLogPadding = 2
)

var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`)
Expand Down Expand Up @@ -139,10 +141,10 @@ func (r *Runner) runLFSHook(ctx context.Context) error {

output := strings.Trim(out.String(), "\n")
if output != "" {
log.Debug("[git-lfs] output: ", output)
log.Debug("[git-lfs] out: ", output)
}
if err != nil {
log.Debug("[git-lfs] error: ", err)
log.Debug("[git-lfs] err: ", err)
}

if err == nil && output != "" {
Expand Down Expand Up @@ -478,14 +480,14 @@ func (r *Runner) logSkip(name, reason string) {
return
}

log.Info(
fmt.Sprintf(
"%s: %s %s",
log.Bold(name),
log.Gray("(skip)"),
log.Yellow(reason),
),
)
log.Styled().
WithLeftBorder(lipgloss.NormalBorder(), log.ColorCyan).
WithPadding(execLogPadding).
Info(
log.Cyan(log.Bold(name)) + " " +
log.Gray("(skip)") + " " +
log.Yellow(reason),
)
}

func (r *Runner) logExecute(name string, err error, out io.Reader) {
Expand All @@ -494,17 +496,24 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
}

var execLog string
var color lipgloss.TerminalColor
switch {
case r.SkipSettings.SkipExecutionInfo():
execLog = ""
case err != nil:
execLog = fmt.Sprint(log.Red("\n EXECUTE > "), log.Bold(name))
execLog = log.Red(fmt.Sprintf("%s ❯ ", name))
color = log.ColorRed
default:
execLog = fmt.Sprint(log.Cyan("\n EXECUTE > "), log.Bold(name))
execLog = log.Cyan(fmt.Sprintf("%s ❯ ", name))
color = log.ColorCyan
}

if execLog != "" {
log.Info(execLog)
log.Styled().
WithLeftBorder(lipgloss.ThickBorder(), color).
WithPadding(execLogPadding).
Info(execLog)
log.Info()
}

if err == nil && r.SkipSettings.SkipExecutionOutput() {
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type GitMock struct {
commands []string
}

func (g *GitMock) SetRootPath(_root string) {}

func (g *GitMock) Cmd(cmd string) (string, error) {
g.mux.Lock()
g.commands = append(g.commands, cmd)
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

type GitMock struct{}

func (g GitMock) SetRootPath(_root string) {}

func (g GitMock) Cmd(_cmd string) (string, error) {
return "", nil
}
Expand Down
Loading
Loading