Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Common options:
- `--prompt-file VALUE` Path to a custom prompt file
- `--engine VALUE` Override engine name
- `--amend` Amend the previous commit
- `-d`, `--diff` Show staged diff in the editor (implies `--edit`)
- `-e`, `--edit` Open the generated commit message in an editor before committing
- `-a`, `--all` Stage modified and deleted files before generating the message
- `-i`, `--include VALUE` Stage specific files before generating the message
Expand Down
7 changes: 7 additions & 0 deletions cmd/git-ai-commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type options struct {
amend bool
addAll bool
edit bool
diff bool
includeFiles []string
excludeFiles []string
debugPrompt bool
Expand Down Expand Up @@ -53,6 +54,7 @@ func main() {
opts.amend,
opts.addAll,
opts.edit,
opts.diff,
opts.includeFiles,
opts.excludeFiles,
opts.debugPrompt,
Expand Down Expand Up @@ -93,6 +95,8 @@ func parseArgs(args []string) (options, error) {
}
case "amend":
opts.amend = true
case "diff":
opts.diff = true
case "edit":
opts.edit = true
case "all":
Expand Down Expand Up @@ -198,6 +202,8 @@ func parseShortOptions(opts *options, arg string, args []string, index *int) err
return fmt.Errorf("missing value for -x")
}
opts.excludeFiles = append(opts.excludeFiles, value)
case 'd':
opts.diff = true
case 'e':
opts.edit = true
case 'h':
Expand All @@ -220,6 +226,7 @@ func printUsage(out *os.File) {
fmt.Fprintln(out, " --prompt-file VALUE Path to a custom prompt file")
fmt.Fprintln(out, " --engine VALUE LLM engine name override")
fmt.Fprintln(out, " --amend Amend the previous commit")
fmt.Fprintln(out, " -d, --diff Show staged diff in the editor (implies --edit)")
fmt.Fprintln(out, " -e, --edit Open the generated commit message in an editor before committing")
fmt.Fprintln(out, " -a, --all Stage modified and deleted files before generating the message")
fmt.Fprintln(out, " -i, --include VALUE Stage specific files before generating the message")
Expand Down
33 changes: 33 additions & 0 deletions cmd/git-ai-commit/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ func TestParseArgs_ExcludeShortCluster(t *testing.T) {
}
}

func TestParseArgs_DiffLong(t *testing.T) {
opts, err := parseArgs([]string{"--diff"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !opts.diff {
t.Error("expected diff to be true")
}
}

func TestParseArgs_DiffShort(t *testing.T) {
opts, err := parseArgs([]string{"-d"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !opts.diff {
t.Error("expected diff to be true")
}
}

func TestParseArgs_DiffShortCluster(t *testing.T) {
opts, err := parseArgs([]string{"-de"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !opts.diff {
t.Error("expected diff to be true")
}
if !opts.edit {
t.Error("expected edit to be true")
}
}

func TestParseArgs_EditLong(t *testing.T) {
opts, err := parseArgs([]string{"--edit"})
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"git-ai-commit/internal/prompt"
)

func Run(context, contextFile, promptName, promptFile, engineName string, amend, addAll, edit bool, includeFiles, excludeFiles []string, debugPrompt, debugCommand bool) (err error) {
func Run(context, contextFile, promptName, promptFile, engineName string, amend, addAll, edit, showDiff bool, includeFiles, excludeFiles []string, debugPrompt, debugCommand bool) (err error) {
cfg, err := config.Load()
if err != nil {
return err
Expand Down Expand Up @@ -88,7 +88,10 @@ func Run(context, contextFile, promptName, promptFile, engineName string, amend,
return fmt.Errorf("empty commit message from engine")
}

if err := git.CommitWithMessage(message, amend, edit); err != nil {
if showDiff {
edit = true
}
if err := git.CommitWithMessage(message, amend, edit, showDiff); err != nil {
return err
}
return nil
Expand Down
9 changes: 6 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func HasHeadCommit() (bool, error) {
return true, nil
}

func CommitWithMessage(message string, amend, edit bool) error {
func CommitWithMessage(message string, amend, edit, diff bool) error {
if edit {
return commitWithEdit(message, amend)
return commitWithEdit(message, amend, diff)
}
args := []string{"commit", "-F", "-"}
if amend {
Expand All @@ -113,7 +113,7 @@ func CommitWithMessage(message string, amend, edit bool) error {
return nil
}

func commitWithEdit(message string, amend bool) error {
func commitWithEdit(message string, amend, diff bool) error {
f, err := os.CreateTemp("", "git-ai-commit-*.txt")
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
Expand All @@ -132,6 +132,9 @@ func commitWithEdit(message string, amend bool) error {
if amend {
args = append(args, "--amend")
}
if diff {
args = append(args, "--verbose")
}
cmd := exec.Command("git", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand Down