Class: Ai::Commit::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ai/commit/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/ai/commit/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#commitObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ai/commit/cli.rb', line 69

def commit
  unless Config.api_key_configured?
    puts "❌ No API key configured. Please run 'smart-commit setup' first."
    exit 1
  end
  
  unless Git.in_git_repo?
    puts "❌ Not in a git repository"
    exit 1
  end
  
  unless Git.has_staged_changes?
    puts "❌ No staged changes found. Stage your changes with 'git add' first."
    exit 1
  end
  
  puts "🤖 Generating commit message..."
  
  begin
    diff = Git.staged_diff
    client = ClaudeClient.new(Config.api_key)
    message = client.generate_commit_message(diff)
    
    puts "\n✨ Generated commit message:"
    puts "=" * 50
    puts message
    puts "=" * 50
    
    print "\nCommit with this message? (y/N/e to edit/r to regenerate): "
    response = STDIN.gets.chomp.downcase
    
    if response == 'y' || response == 'yes'
      puts "\n🚀 Committing..."
      result = system("git", "commit", "-m", message)
      
      if result
        puts "✅ Successfully committed!"
      else
        puts "❌ Failed to commit"
        exit 1
      end
    elsif response == 'e' || response == 'edit'
      edited_message = edit_message(message)
      if edited_message && !edited_message.strip.empty?
        puts "\n🚀 Committing with edited message..."
        result = system("git", "commit", "-m", edited_message)
        
        if result
          puts "✅ Successfully committed!"
        else
          puts "❌ Failed to commit"
          exit 1
        end
      else
        puts "❌ Commit cancelled."
        exit 0
      end
    elsif response == 'r' || response == 'regenerate'
      regenerate_with_feedback(message, diff, client)
    else
      puts "❌ Commit cancelled."
      exit 0
    end
    
  rescue Error => e
    puts "❌ Error: #{e.message}"
    exit 1
  rescue => e
    puts "❌ Unexpected error: #{e.message}"
    exit 1
  end
end

#configObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ai/commit/cli.rb', line 143

def config
  if Config.api_key_configured?
    masked_key = Config.api_key[0..10] + "..." if Config.api_key
    puts "✓ API key configured: #{masked_key}"
  else
    puts "❌ No API key configured"
  end
  
  if Config.has_custom_prompts?
    puts "✓ Custom prompts found: #{Config.custom_prompts.length} prompt(s)"
    Config.custom_prompts.each_with_index do |prompt, i|
      puts "  #{i + 1}. #{prompt}"
    end
  else
    puts "ℹ️  No custom prompts found"
    puts "   Create a .commit-prompts file in your project root to add custom context"
  end
end

#generateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ai/commit/cli.rb', line 31

def generate
  unless Config.api_key_configured?
    puts "❌ No API key configured. Please run 'smart-commit setup' first."
    exit 1
  end
  
  unless Git.in_git_repo?
    puts "❌ Not in a git repository"
    exit 1
  end
  
  unless Git.has_staged_changes?
    puts "❌ No staged changes found. Stage your changes with 'git add' first."
    exit 1
  end
  
  puts "🤖 Generating commit message..."
  
  begin
    diff = Git.staged_diff
    client = ClaudeClient.new(Config.api_key)
    message = client.generate_commit_message(diff)
    
    puts "\n✨ Generated commit message:"
    puts "#{message}"
    puts "\nTo commit with this message, run:"
    puts "git commit -m \"#{message}\""
    
  rescue Error => e
    puts "❌ Error: #{e.message}"
    exit 1
  rescue => e
    puts "❌ Unexpected error: #{e.message}"
    exit 1
  end
end

#init_promptsObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ai/commit/cli.rb', line 163

def init_prompts
  prompts_file = Config::PROMPTS_FILE
  
  if File.exist?(prompts_file)
    puts "❌ .commit-prompts file already exists"
    exit 1
  end
  
  sample_content = "    # Custom commit message prompts\n    # Add your project-specific context and preferences here\n    # Lines starting with # are ignored\n    \n    # Example prompts:\n    # - Always include the JIRA ticket number in the scope\n    # - Use 'feat' for new features, 'fix' for bug fixes\n    # - Keep descriptions under 50 characters\n    # - Prefer active voice and present tense\n    # - Include breaking change indicators when applicable\n    \n    # Your custom prompts go here:\n    \n  PROMPTS\n  \n  File.write(prompts_file, sample_content)\n  puts \"\u2705 Created \#{prompts_file} with sample content\"\n  puts \"\u{1F4DD} Edit the file to add your custom prompts\"\nend\n"

#setupObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ai/commit/cli.rb', line 14

def setup
  puts "Welcome to AI Commit setup!"
  print "Enter your Anthropic API key: "
  api_key = STDIN.noecho(&:gets).chomp
  puts
  
  if api_key.empty?
    puts "No API key provided. Setup cancelled."
    exit 1
  end
  
  Config.set_api_key(api_key)
  puts "✓ API key saved successfully!"
  puts "You can now use 'smart-commit generate' to create commit messages."
end