commit

package
v0.4.1-rc3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 <message>",
	Example: `  contextvibes factory commit -m "feat(auth): Implement OTP login"`,
	Args:    cobra.NoArgs,
	RunE: func(cmd *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		if strings.TrimSpace(commitMessageFlag) == "" {

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

		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(commitMessageFlag) {
				presenter.Error("Invalid commit message format.")
				presenter.Advice("Message 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)
		}

		err = client.AddAll(ctx)
		if 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(), "  Commit Message: %s\n", commitMessageFlag)

		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, commitMessageFlag)
	},
}

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