Skip to content

Commit 8ac4384

Browse files
committed
feat(app/commit.py, setup.py): enhance commit functionality with concise messages and diff output coverage
1 parent 60bdafe commit 8ac4384

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

app/cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@
77

88

99
def process_query(query: str):
10-
response = query_ollama(query)
10+
11+
history = subprocess.getoutput("history")
12+
prompt = (
13+
f"""Convert the following natural language command to a bash command: {query}
14+
History of CLI commands if it helps:
15+
<history>
16+
{history}
17+
</history>
18+
""",
19+
)
20+
21+
response = query_ollama(prompt)
1122
bash_commands = extract_bash_commands(response)
1223
if len(bash_commands) == 0:
1324
print("No command could be produced")

app/commit.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Generate AI-driven commits from diffs
2+
"""
3+
4+
import subprocess
5+
from app.query_ollama import query_ollama
6+
from textwrap import dedent
7+
8+
9+
def generate_commit_message():
10+
# Get the git diff output
11+
git_diff_output = subprocess.getoutput("git diff")
12+
13+
if not git_diff_output:
14+
print("No changes to commit.")
15+
return None
16+
17+
print(git_diff_output)
18+
# Generate a commit message using the Ollama model
19+
commit_message = query_ollama(
20+
dedent(
21+
f"""
22+
Please generate a git commit message off this. Be concise but cover all files and changes.
23+
Git diff:
24+
{git_diff_output}
25+
26+
Example commit message: ``feat(app/cli.py, query_ollama.py): enhance CLI experience with history and bash command conversion`
27+
28+
Commit message:
29+
"""
30+
)
31+
)
32+
33+
if not commit_message:
34+
print("Failed to generate a commit message.")
35+
return None
36+
37+
return commit_message.strip()
38+
39+
40+
def main():
41+
commit_message = generate_commit_message()
42+
if commit_message:
43+
print("Generated commit message:")
44+
print(commit_message)
45+
# Here you could add code to automatically commit using this message
46+
# For example:
47+
# subprocess.run(["git", "commit", "-am", commit_message])
48+
49+
50+
if __name__ == "__main__":
51+
main()

app/query_ollama.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,29 @@ def init_ollama():
1717
)
1818

1919

20-
def query_ollama(prompt, stream: bool = True):
20+
def query_ollama(prompt: str, stream: bool = True):
2121
init_ollama()
2222
url = os.getenv("HOW_DEFAULT_URL", DEFAULT_URL)
2323
model = os.getenv("HOW_DEFAULT_MODEL", DEFAULT_MODEL)
24-
history = subprocess.getoutput("history")
2524

2625
data = {
2726
"model": model,
28-
"prompt": f"""Convert the following natural language command to a bash command: {prompt}
29-
History of CLI commands if it helps:
30-
<history>
31-
{history}
32-
</history>
33-
""",
27+
"prompt": prompt,
3428
"stream": stream,
3529
}
3630

37-
if stream:
38-
full_response = ""
39-
with requests.post(url, json=data, stream=True) as response:
40-
for line in response.iter_lines():
41-
if line:
42-
json_response = json.loads(line)
43-
chunk = json_response.get("response", "")
44-
full_response += chunk
45-
print(chunk, end="", flush=True)
46-
if json_response.get("done", False):
47-
print() # Print a newline at the end
48-
break
49-
return full_response.strip()
50-
else:
51-
response = requests.post(url, json=data)
52-
if response.status_code == 200:
53-
response_text = response.json()["response"].strip()
54-
print(response_text)
55-
return response_text
56-
else:
57-
error_message = f"Error: Unable to get response from Ollama. Status code: {response.status_code}"
58-
print(error_message)
59-
return error_message
31+
full_response = ""
32+
with requests.post(url, json=data, stream=True) as response:
33+
for line in response.iter_lines():
34+
if line:
35+
json_response = json.loads(line)
36+
chunk = json_response.get("response", "")
37+
full_response += chunk
38+
print(chunk, end="", flush=True)
39+
if json_response.get("done", False):
40+
print() # Print a newline at the end
41+
break
42+
return full_response.strip()
6043

6144

6245
if __name__ == "__main__":

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
entry_points={
3333
"console_scripts": [
3434
"how=app.cli:main",
35+
"how-commit=app.commit:main",
3536
],
3637
},
3738
)

0 commit comments

Comments
 (0)