commit

package
v0.7.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package commit provides the command to commit changes.

Index

Constants

This section is empty.

Variables

View Source
var CommitCmd = &cobra.Command{
	Use:     "commit -m <msg> [-m <body>]",
	Example: `  contextvibes factory commit -m "feat(auth): Add login" -m "Details about the login logic."`,
	Args:    cobra.NoArgs,
	RunE: func(cmd *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		if len(commitMessages) == 0 {

			return errors.New("commit message is required via -m flag")
		}

		fullMessage := strings.Join(commitMessages, "\n\n")

		subject, _, _ := strings.Cut(fullMessage, "\n")

		validationRule := globals.LoadedAppConfig.Validation.CommitMessage
		validationEnabled := validationRule.Enable == nil || *validationRule.Enable

		if validationEnabled {
			pattern := validationRule.Pattern
			if pattern == "" {
				pattern = config.DefaultCommitMessagePattern
			}
			re, err := regexp.Compile(pattern)
			if err != nil {

				return errors.New("invalid commit message validation regex")
			}
			if !re.MatchString(subject) {
				presenter.Error("Invalid commit subject format.")
				presenter.Detail("Subject: %s", subject)
				presenter.Advice("Subject must match pattern: %s", pattern)

				return errors.New("invalid commit message format")
			}
		}

		gitCfg := git.GitClientConfig{
			Logger:                globals.AppLogger,
			DefaultRemoteName:     globals.LoadedAppConfig.Git.DefaultRemote,
			DefaultMainBranchName: globals.LoadedAppConfig.Git.DefaultMainBranch,
			Executor:              globals.ExecClient.UnderlyingExecutor(),
		}
		client, err := git.NewClient(ctx, ".", gitCfg)
		if err != nil {
			return fmt.Errorf("failed to initialize git client: %w", err)
		}

		if err := client.AddAll(ctx); err != nil {
			return fmt.Errorf("failed to stage changes: %w", err)
		}

		hasStaged, err := client.HasStagedChanges(ctx)
		if err != nil {
			return fmt.Errorf("failed to check staged changes: %w", err)
		}
		if !hasStaged {
			presenter.Info("No changes were staged for commit.")

			return nil
		}

		currentBranch, _ := client.GetCurrentBranchName(ctx)
		statusOutput, _, _ := client.GetStatusShort(ctx)

		presenter.InfoPrefixOnly()

		fmt.Fprintf(presenter.Out(), "  Branch: %s\n", currentBranch)

		fmt.Fprintf(presenter.Out(), "  Subject: %s\n", subject)

		fmt.Fprintf(presenter.Out(), "  Staged Changes:\n%s\n", statusOutput)

		if !globals.AssumeYes {
			confirmed, err := presenter.PromptForConfirmation("Proceed?")
			if err != nil || !confirmed {

				return errors.New("commit aborted")
			}
		}

		return client.Commit(ctx, fullMessage)
	},
}

CommitCmd represents the commit command.

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