Module: AIGit::Commands::Default
- Defined in:
- lib/ai_git/commands/default.rb
Class Method Summary collapse
- .build_prompt(diff) ⇒ Object
- .call(argv = []) ⇒ Object
- .check_base_url!(options) ⇒ Object
- .check_secrets!(staged, diff, options) ⇒ Object
- .commit_and_push(message, branch, options) ⇒ Object
- .confirm?(options) ⇒ Boolean
- .dry_run_summary(branch, options) ⇒ Object
- .edited_message(message) ⇒ Object
- .empty_response_error ⇒ Object
- .generate_commit_message(diff, model_name, temperature: 0.3) ⇒ Object
- .normalize_message(message) ⇒ Object
- .print_header(provider, model_name, staged, branch) ⇒ Object
- .print_message(message) ⇒ Object
- .resolve_message(diff, model_name, options) ⇒ Object
- .run(staged, options) ⇒ Object
- .standard_prompt(diff) ⇒ Object
Class Method Details
.build_prompt(diff) ⇒ Object
192 193 194 |
# File 'lib/ai_git/commands/default.rb', line 192 def build_prompt(diff) standard_prompt(diff) end |
.call(argv = []) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/ai_git/commands/default.rb', line 65 def call(argv = []) = AIGit::Options.parse(argv) return puts(AIGit::USAGE) if .help? staged = AIGit::Git.staged_files raise "No staged files. Use `git add` first." if staged.strip.empty? run(staged, ) end |
.check_base_url!(options) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ai_git/commands/default.rb', line 148 def check_base_url!() return if AIGit::Config.loopback_base_url? host = AIGit::Config.base_uri.host if AIGit::Config.insecure_remote_base_url? && !.force? raise "Refusing to send the staged diff unencrypted to #{host} over http. " \ "Use https, a loopback address, or pass --force." end AIGit::UI.warning("Warning: the full staged diff will be sent to #{AIGit::Config.base_url} (not this machine).") end |
.check_secrets!(staged, diff, options) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ai_git/commands/default.rb', line 161 def check_secrets!(staged, diff, ) findings = AIGit::Secrets.scan(staged, diff) findings[:warnings].each { |finding| AIGit::UI.warning("Warning: #{finding}.") } blocking = findings[:blocking] return if blocking.empty? blocking.each { |finding| AIGit::UI.warning("Possible secret: #{finding}.") } return if .force? raise "Refusing to send possible secrets to #{AIGit::Config.provider}. " \ "Unstage these files or pass --force." end |
.commit_and_push(message, branch, options) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ai_git/commands/default.rb', line 123 def commit_and_push(, branch, ) AIGit::Git.() AIGit::UI.success("Committed.") return AIGit::UI.info(AIGit::UI.dim("Skipped push (--no-push).")) unless .push? return AIGit::UI.warning("Detached HEAD: skipping push. Commit is local only.") if branch.nil? AIGit::Git.push_current_branch AIGit::UI.success("Pushed to origin/#{branch}.") end |
.confirm?(options) ⇒ Boolean
111 112 113 |
# File 'lib/ai_git/commands/default.rb', line 111 def confirm?() !.dry_run? && !.assume_yes? && AIGit::Prompt.interactive? end |
.dry_run_summary(branch, options) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ai_git/commands/default.rb', line 135 def dry_run_summary(branch, ) outcome = if !.push? "would commit only" elsif branch.nil? "would commit only (detached HEAD)" else "would commit and push to origin/#{branch}" end AIGit::UI.info(AIGit::UI.dim("Dry run: nothing changed, #{outcome}.")) end |
.edited_message(message) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/ai_git/commands/default.rb', line 115 def () edited = (AIGit::Prompt.edit()) raise "Aborted: the edited commit message is empty." if edited.empty? (edited) edited end |
.empty_response_error ⇒ Object
51 52 53 54 |
# File 'lib/ai_git/commands/default.rb', line 51 def empty_response_error "#{AIGit::Config.provider} returned an empty commit message. " \ "Check the model name and that the server is loaded (see `ai_git config`), then retry." end |
.generate_commit_message(diff, model_name, temperature: 0.3) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ai_git/commands/default.rb', line 36 def (diff, model_name, temperature: 0.3) raise "No staged changes to generate commit message for" if diff.to_s.strip.empty? = AIClient.complete( prompt: build_prompt(diff), model_name: model_name, temperature: temperature ) = () raise empty_response_error if .empty? end |
.normalize_message(message) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/ai_git/commands/default.rb', line 56 def () text = .to_s.gsub(/\n{3,}/, "\n\n").strip lines = text.lines.map(&:chomp) return text if lines.length < 2 lines.insert(1, "") unless lines[1].empty? lines.join("\n").gsub(/\n{3,}/, "\n\n").strip end |
.print_header(provider, model_name, staged, branch) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/ai_git/commands/default.rb', line 176 def print_header(provider, model_name, staged, branch) AIGit::UI.kv("AI Provider", provider) AIGit::UI.kv("Model", model_name) AIGit::UI.kv("Staged Files", staged.to_s.strip.gsub("\n", ", ")) AIGit::UI.kv("Branch", branch || "(detached HEAD)") puts end |
.print_message(message) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/ai_git/commands/default.rb', line 184 def () puts puts AIGit::UI.bold("Commit message:") puts puts puts end |
.resolve_message(diff, model_name, options) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ai_git/commands/default.rb', line 95 def (diff, model_name, ) loop do AIGit::UI.info(AIGit::UI.bold("Generating commit message…")) = (diff, model_name) () return unless confirm?() case AIGit::Prompt.ask_action when :accept then return when :edit then return () when :abort then return nil end end end |
.run(staged, options) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ai_git/commands/default.rb', line 75 def run(staged, ) diff = AIGit::Git.diff branch = AIGit::Git.current_branch model_name = AIGit::Config.model_name check_base_url!() check_secrets!(staged, diff, ) print_header(AIGit::Config.provider, model_name, staged, branch) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) = (diff, model_name, ) return AIGit::UI.info("Aborted. Nothing committed.") if .nil? return dry_run_summary(branch, ) if .dry_run? commit_and_push(, branch, ) AIGit::UI.kv("Done in", format("%.1fs", Process.clock_gettime(Process::CLOCK_MONOTONIC) - started)) end |
.standard_prompt(diff) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/ai_git/commands/default.rb', line 196 def standard_prompt(diff) <<~PROMPT You are an expert Git commit message writer. Output ONLY the commit message — no explanations, no markdown, no backticks, no preamble. Here are the changes: #{diff} STRICT OUTPUT FORMAT (follow exactly): <short imperative title, max 72 chars, summarizing the main change> <blank line> <body written as plain prose paragraphs, no headers, no bullet points, no tags. Explain why the change is necessary and what problem it solves, how it addresses the issue at a high level (do not recount line-by-line code changes, those are visible in the diff), and any side effects or tradeoffs reviewers should know about.> RULES: - Title line: short, specific, imperative mood (e.g. "Add JWT login with refresh token support"). Avoid vague titles like "Update stuff" or "Fix bug". - Body: plain prose only. No markdown headers, no bullet points, no bold/italics, no tags. - Body: explain WHY the change is necessary and WHAT problem it solves, then HOW it addresses the issue at a high level. Do not describe the code line by line. - Body: mention side effects or tradeoffs only if they exist and matter to a reviewer. - No filler phrases ("this commit", "this PR", "as per discussion"). - No line should exceed 72 characters. EXAMPLES OF GOOD OUTPUT: Add JWT-based login with refresh token support Users were being logged out on every page reload because sessions were not persisted across requests. This made the app unusable for any workflow longer than a single page view. Introduces a login endpoint that issues short-lived access tokens alongside long-lived refresh tokens, plus a refresh route that rotates tokens on use. Refresh tokens are stored in encrypted HTTP-only cookies so the client never handles them directly. Adds a new middleware layer on private routes, so any route not yet wired to it remains unauthenticated until migrated. --- Prevent nil crash when user preferences are missing Reports were raising NoMethodError in production for accounts created before the preferences feature shipped, since those accounts have no preferences record at all. Falls back to system defaults whenever a user's preferences are absent, so report generation no longer assumes the record exists. --- Now generate the commit message: PROMPT end |