Module: AIGit::Commands::Default

Defined in:
lib/ai_git/commands/default.rb

Class Method Summary collapse

Class Method Details

.build_prompt(diff) ⇒ Object



82
83
84
# File 'lib/ai_git/commands/default.rb', line 82

def build_prompt(diff)
  standard_prompt(diff)
end

.call(_argv = []) ⇒ Object



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
# File 'lib/ai_git/commands/default.rb', line 38

def call(_argv = [])
  provider = AIGit::Config.provider
  model_name = AIGit::Config.model_name

  staged = AIGit::Git.staged_files
  abort "Error: No staged files. Use `git add` first." if staged.to_s.strip.empty?

  diff = AIGit::Git.diff
  branch = AIGit::Git.current_branch

  print_header(provider, model_name, staged, branch)

  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  AIGit::UI.info(AIGit::UI.bold("Generating commit message…"))
  message = generate_commit_message(diff, model_name)
  print_message(message)

  AIGit::Git.commit_with_message(message)
  AIGit::UI.success("Committed.")

  AIGit::Git.push_current_branch
  AIGit::UI.success("Pushed to origin/#{branch}.")

  elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
  AIGit::UI.kv("Done in", format("%.1fs", elapsed))
end

.generate_commit_message(diff, model_name, temperature: 0.3) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ai_git/commands/default.rb', line 13

def generate_commit_message(diff, model_name, temperature: 0.3)
  raise "No staged changes to generate commit message for" if diff.to_s.strip.empty?

  message = AIClient.complete(
    prompt: build_prompt(diff),
    model_name: model_name,
    temperature: temperature
  )

  message = normalize_message(message)
  message.empty? ? "chore: update code" : message
end

.normalize_message(message) ⇒ Object

Keep the commit body readable: collapse runs of blank lines to a single one and guarantee a blank line after the title so git sees a proper subject/body split (otherwise git log --oneline mashes them together).



29
30
31
32
33
34
35
36
# File 'lib/ai_git/commands/default.rb', line 29

def normalize_message(message)
  text = message.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


66
67
68
69
70
71
72
# File 'lib/ai_git/commands/default.rb', line 66

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)
  puts
end


74
75
76
77
78
79
80
# File 'lib/ai_git/commands/default.rb', line 74

def print_message(message)
  puts
  puts AIGit::UI.bold("Commit message:")
  puts
  puts message
  puts
end

.standard_prompt(diff) ⇒ Object



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
141
142
# File 'lib/ai_git/commands/default.rb', line 86

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>

    <blank line>

    ## Summary
    <2–4 bullet points covering the most important changes. Each bullet starts with a verb.>

    ## Why
    <1–3 sentences explaining the motivation or context behind the change. Omit if the reason is obvious.>

    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".
    - Summary bullets: describe WHAT changed, not HOW the code looks. Focus on behaviour and impact.
    - Why section: explain the problem being solved or the goal being achieved. Skip if it adds no value.
    - 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

    ## Summary
    - Implement login endpoint with access and refresh token issuance
    - Add token refresh route with rotation and expiry validation
    - Protect private routes via middleware that verifies access tokens
    - Store refresh tokens using encrypted HTTP-only cookies

    ## Why
    Users were being logged out on every page reload. Refresh tokens allow
    sessions to persist securely without requiring re-authentication.

    ---

    Prevent nil crash when user preferences are missing

    ## Summary
    - Add nil guard in ReportGenerator#process before accessing preferences
    - Fall back to system defaults when preferences object is absent

    ## Why
    Reports were raising NoMethodError in production for users created
    before the preferences feature shipped.

    ---

    Now generate the commit message:
  PROMPT
end