Skip to content

Commit

Permalink
fix: always force install to sync files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 7, 2023
1 parent 91caa53 commit 9935549
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 80 deletions.
19 changes: 13 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,34 @@ import (
"github.com/spf13/cobra"

"github.com/evilmartians/lefthook/internal/lefthook"
"github.com/evilmartians/lefthook/internal/log"
)

func newInstallCmd(opts *lefthook.Options) *cobra.Command {
args := lefthook.InstallArgs{}
var a, force bool

installCmd := cobra.Command{
Use: "install",
Short: "Write basic configuration file in your project repository. Or initialize existed config",
RunE: func(cmd *cobra.Command, _args []string) error {
return lefthook.Install(opts, &args)
return lefthook.Install(opts, force)
},
}

// To be dropped in next releases.
installCmd.Flags().BoolVarP(
&args.Force, "force", "f", false,
"reinstall hooks without checking config version",
&force, "force", "f", false,
"overwrite .old hooks",
)
installCmd.Flags().BoolVarP(
&args.Aggressive, "aggressive", "a", false,
"remove all hooks from .git/hooks dir and install lefthook hooks",
&a, "aggressive", "a", false,
"use --force flag instead",
)

err := installCmd.Flags().MarkDeprecated("aggressive", "use --force flag instead")
if err != nil {
log.Warn("Unexpected error:", err)
}

return &installCmd
}
14 changes: 12 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"

"github.com/evilmartians/lefthook/internal/lefthook"
"github.com/evilmartians/lefthook/internal/log"
)

var commands = [...]func(*lefthook.Options) *cobra.Command{
Expand Down Expand Up @@ -41,14 +42,23 @@ func newRootCmd() *cobra.Command {
&options.NoColors, "no-colors", false, "disable colored output",
)

// To be dropped in next releases.
rootCmd.Flags().BoolVarP(
&options.Force, "force", "f", false,
"DEPRECATED: reinstall hooks without checking config version",
"use command-specific --force option",
)
rootCmd.Flags().BoolVarP(
&options.Aggressive, "aggressive", "a", false,
"DEPRECATED: remove all hooks from .git/hooks dir and install lefthook hooks",
"use --force flag instead",
)
err := rootCmd.Flags().MarkDeprecated("aggressive", "use command-specific --force option")
if err != nil {
log.Warn("Unexpected error:", err)
}
err = rootCmd.Flags().MarkDeprecated("force", "use command-specific --force option")
if err != nil {
log.Warn("Unexpected error:", err)
}

for _, subcommand := range commands {
rootCmd.AddCommand(subcommand(&options))
Expand Down
17 changes: 6 additions & 11 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@ var (
errNoConfig = fmt.Errorf("no lefthook config found")
)

type InstallArgs struct {
Force, Aggressive bool
}

// Install installs the hooks from config file to the .git/hooks.
func Install(opts *Options, args *InstallArgs) error {
func Install(opts *Options, force bool) error {
lefthook, err := initialize(opts)
if err != nil {
return err
}

return lefthook.Install(args)
return lefthook.Install(force)
}

func (l *Lefthook) Install(args *InstallArgs) error {
func (l *Lefthook) Install(force bool) error {
cfg, err := l.readOrCreateConfig()
if err != nil {
return err
Expand All @@ -66,8 +62,7 @@ func (l *Lefthook) Install(args *InstallArgs) error {
}
}

return l.createHooksIfNeeded(cfg,
args.Force || args.Aggressive || l.Options.Force || l.Options.Aggressive)
return l.createHooksIfNeeded(cfg, false, force)
}

func (l *Lefthook) readOrCreateConfig() (*config.Config, error) {
Expand Down Expand Up @@ -111,8 +106,8 @@ func (l *Lefthook) createConfig(path string) error {
return nil
}

func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, force bool) error {
if !force && l.hooksSynchronized() {
func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, checkHashSum, force bool) error {
if checkHashSum && l.hooksSynchronized() {
return nil
}

Expand Down
Loading

0 comments on commit 9935549

Please sign in to comment.