tidy

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package tidy provides the command to clean up merged branches.

Index

Constants

This section is empty.

Variables

View Source
var TidyCmd = &cobra.Command{
	Use: "tidy",
	RunE: func(cmd *cobra.Command, _ []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		presenter.Summary("--- Finishing Merged Branch Workflow ---")

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

		mainBranch := gitClient.MainBranchName()
		currentBranch, err := gitClient.GetCurrentBranchName(ctx)
		if err != nil {
			return fmt.Errorf("failed to get current branch: %w", err)
		}
		if currentBranch == mainBranch {

			return errors.New("you are already on the main branch; there is no branch to finish")
		}

		prompt := fmt.Sprintf(
			"This will delete your local branch '%s' and switch to '%s'. Are you sure it has been merged?",
			currentBranch,
			mainBranch,
		)
		confirmed, err := presenter.PromptForConfirmation(prompt)
		if err != nil {
			return fmt.Errorf("confirmation failed: %w", err)
		}
		if !confirmed {
			presenter.Info("Aborted by user.")

			return nil
		}

		err = gitClient.SwitchBranch(ctx, mainBranch)
		if err != nil {
			return fmt.Errorf("failed to switch to main branch: %w", err)
		}

		err = gitClient.PullRebase(ctx, mainBranch)
		if err != nil {
			return fmt.Errorf("failed to pull rebase main branch: %w", err)
		}

		err = globals.ExecClient.Execute(ctx, ".", "git", "branch", "-d", currentBranch)
		if err != nil {
			presenter.Warning(
				"Could not delete branch with '-d' (likely not fully merged). Trying '-D'...",
			)
			errForce := globals.ExecClient.Execute(ctx, ".", "git", "branch", "-D", currentBranch)
			if errForce != nil {
				return fmt.Errorf("failed to force delete branch: %w", errForce)
			}
		}
		presenter.Success(
			"Successfully cleaned up '%s' and updated '%s'.",
			currentBranch,
			mainBranch,
		)

		return nil
	},
}

TidyCmd represents the tidy 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