From 15551129992cb45563dea76c4b67ae518ef5453f Mon Sep 17 00:00:00 2001 From: Mahboob Hasan Date: Thu, 9 Apr 2026 02:27:03 +0530 Subject: [PATCH 1/2] feat: custom commit message on submit --- cmd/codecrafters/main.go | 18 +++++++++++++++++- internal/commands/submit.go | 5 ++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index e88504e..53829f4 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -82,7 +82,23 @@ func run() error { return commands.TestCommand(*shouldTestPrevious) case "submit": - return commands.SubmitCommand() + submitCmd := flag.NewFlagSet("submit", flag.ExitOnError) + msgShort := submitCmd.String("m", "", "commit message") + msgLong := submitCmd.String("message", "", "commit message") + submitCmd.Parse(flag.Args()[1:]) + if submitCmd.NArg() > 0 { + red := color.New(color.FgRed).SprintFunc() + fmt.Fprintf(os.Stderr, "%s\n", red("Error: custom commit message must be passed using -m or --message")) + os.Exit(1) + } + msg := *msgShort + if msg == "" { + msg = *msgLong + } + if msg == "" { + msg = "codecrafters submit [skip ci]" + } + return commands.SubmitCommand(msg) case "task": taskCmd := flag.NewFlagSet("task", flag.ExitOnError) stageSlug := taskCmd.String("stage", "", "view instructions for a specific stage (slug, +N, or -N)") diff --git a/internal/commands/submit.go b/internal/commands/submit.go index a7e5756..7153968 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -12,7 +12,7 @@ import ( "github.com/getsentry/sentry-go" ) -func SubmitCommand() (err error) { +func SubmitCommand(message string) (err error) { utils.Logger.Debug().Msg("submit command starts") defer func() { @@ -70,8 +70,7 @@ func SubmitCommand() (err error) { } utils.Logger.Debug().Msgf("committing changes to %s", defaultBranchName) - - commitSha, err := commitChanges(repoDir, "codecrafters submit [skip ci]") + commitSha, err := commitChanges(repoDir, message) if err != nil { return fmt.Errorf("commit changes: %w", err) } From e346585cc2e2d457e92a5e3b7de03116a780bfb7 Mon Sep 17 00:00:00 2001 From: Andy Li <1450947+andy1li@users.noreply.github.com> Date: Mon, 20 Apr 2026 08:03:59 +0900 Subject: [PATCH 2/2] Enhance submit command to require a non-empty custom commit message and update usage instructions for clarity. --- cmd/codecrafters/main.go | 27 ++++++++++++++------------- internal/commands/submit.go | 4 ++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index 53829f4..a95f447 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -25,6 +25,7 @@ USAGE EXAMPLES $ codecrafters submit # Commit changes & run tests + $ codecrafters submit -m "msg" # Commit changes & run tests with a custom commit message $ codecrafters test # Run tests without committing changes $ codecrafters test --previous # Run tests for all previous stages and the current stage without committing changes @@ -77,28 +78,28 @@ func run() error { switch cmd { case "test": testCmd := flag.NewFlagSet("test", flag.ExitOnError) - shouldTestPrevious := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") + shouldTestPrevious := testCmd.Bool("previous", false, "Run tests for all previous stages and the current stage without committing changes") testCmd.Parse(flag.Args()[1:]) // parse the args after the test command return commands.TestCommand(*shouldTestPrevious) case "submit": submitCmd := flag.NewFlagSet("submit", flag.ExitOnError) - msgShort := submitCmd.String("m", "", "commit message") - msgLong := submitCmd.String("message", "", "commit message") + + var commitMessage string + defaultCommitMessage := "codecrafters submit" + usage := "Commit changes & run tests with a custom commit message" + submitCmd.StringVar(&commitMessage, "m", defaultCommitMessage, usage) + submitCmd.StringVar(&commitMessage, "message", defaultCommitMessage, usage) + submitCmd.Parse(flag.Args()[1:]) if submitCmd.NArg() > 0 { - red := color.New(color.FgRed).SprintFunc() - fmt.Fprintf(os.Stderr, "%s\n", red("Error: custom commit message must be passed using -m or --message")) - os.Exit(1) + return fmt.Errorf("Unexpected arguments: use -m or --message to set a custom commit message.") } - msg := *msgShort - if msg == "" { - msg = *msgLong + if commitMessage == "" { + return fmt.Errorf("Cannot submit with an empty commit message.") } - if msg == "" { - msg = "codecrafters submit [skip ci]" - } - return commands.SubmitCommand(msg) + + return commands.SubmitCommand(commitMessage + " [skip ci]") case "task": taskCmd := flag.NewFlagSet("task", flag.ExitOnError) stageSlug := taskCmd.String("stage", "", "view instructions for a specific stage (slug, +N, or -N)") diff --git a/internal/commands/submit.go b/internal/commands/submit.go index 7153968..981849e 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -12,7 +12,7 @@ import ( "github.com/getsentry/sentry-go" ) -func SubmitCommand(message string) (err error) { +func SubmitCommand(commitMessage string) (err error) { utils.Logger.Debug().Msg("submit command starts") defer func() { @@ -70,7 +70,7 @@ func SubmitCommand(message string) (err error) { } utils.Logger.Debug().Msgf("committing changes to %s", defaultBranchName) - commitSha, err := commitChanges(repoDir, message) + commitSha, err := commitChanges(repoDir, commitMessage) if err != nil { return fmt.Errorf("commit changes: %w", err) }