cmd

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Root = &cli.Command{
	Name:      "commitlint-scope",
	Usage:     "Lint commit scopes against changed files.",
	UsageText: "commitlint-scope --from <sha> --to <sha>",
	Description: `Validate that scopes declared in commit messages correspond to actually changed files.

The command inspects a range of commits (from exclusive, to inclusive) and reports any scope that does not match the files modified in that commit.

Examples:
  commitlint-scope --from main --to feature-branch
  commitlint-scope --from HEAD~5 --to HEAD
`,

	Suggest: true,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:        "from",
			Aliases:     []string{"f"},
			Usage:       "Start of the commit range (exclusive)",
			Required:    true,
			Destination: &flagFrom,
		},
		&cli.StringFlag{
			Name:        "to",
			Aliases:     []string{"t"},
			Usage:       "End of the commit range (inclusive)",
			Required:    true,
			Destination: &flagTo,
		},
		&cli.BoolFlag{
			Name:        "verbose",
			Aliases:     []string{"v"},
			Usage:       "Verbose output",
			Required:    false,
			Destination: &flagVerbose,
		},
		&cli.BoolFlag{
			Name:        "no-color",
			Usage:       "Disable color output",
			Required:    false,
			Destination: &flagNoColor,
		},
		&cli.BoolFlag{
			Name:        "json",
			Usage:       "Show output in JSON format",
			Required:    false,
			Destination: &flagJSON,
		},
	},

	Action: func(ctx context.Context, cmd *cli.Command) error {
		color.NoColor = flagNoColor

		configureLogger(os.Stderr, flagVerbose)

		cfg, err := validator.LoadConfig()
		if err != nil {
			return fmt.Errorf("loading config: %w", err)
		}

		vld, err := validator.NewValidator(cfg, validator.Options{
			Logger:         slog.Default(),
			SHALength:      7,
			Git:            nil,
			OutsiderFinder: nil,
			ScopeParser:    nil,
		})
		if err != nil {
			return fmt.Errorf("creating validator: %w", err)
		}

		violations, err := vld.Validate(ctx, flagFrom, flagTo)
		if err != nil {
			return fmt.Errorf("validation failed: %w", err)
		}

		if len(violations) == 0 {
			return nil
		}

		msg := fmt.Sprintf("%d violation(s) found:", len(violations))
		_, _ = color.New(color.FgRed, color.Bold).Fprintln(cmd.ErrWriter, msg)

		if flagJSON {
			err := jsonOutput(cmd.Writer, violations)
			if err != nil {
				return err
			}
		} else {
			textOutput(cmd.Writer, violations)
		}

		return nil
	},
}

Root is the entry point command for commitlint-scope.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL