Skip to content

Commit fa58fb1

Browse files
committed
Revert "fix: Add .DS_Store file"
This reverts commit bd325f1.
1 parent bd325f1 commit fa58fb1

3 files changed

Lines changed: 100 additions & 5 deletions

File tree

.DS_Store

-6 KB
Binary file not shown.

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ If ollama server is running, run the script using the command
3333
python3 auto_commit.py
3434
```
3535

36-
Create commit for each file
37-
38-
```
39-
python3 auto_commit.py single_file
40-
```
4136

4237

4338

auto_commit_single_file.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import subprocess
2+
from ollama import chat
3+
from ollama import ChatResponse
4+
5+
model = "gemma3:4b"
6+
prompt = f"""
7+
Given the following Git diff and the list of changed files (with file types), suggest a single concise and relevant commit message that best summarizes all the changes made. Use a conventional commit style (e.g., feat:, fix:, chore:, docs:, refactor:). The message should be no longer than 72 characters.
8+
Just return the commit messages without any additional text or explanation, without any Markdown formatting.
9+
Input:
10+
Git Diff:
11+
[Git Diff]
12+
13+
Changed Files and Types:
14+
[Changed Files and Types]
15+
16+
Instructions:
17+
1. Analyze the diff and the list of changed files/types.
18+
2. Summarize all changes into a single logical commit.
19+
3. Write a concise commit message (max 72 characters) in the conventional commit style
20+
"""
21+
22+
def get_changed_files():
23+
# Git add all
24+
subprocess.run(
25+
["git", "add", "."],
26+
capture_output=True, text=True
27+
)
28+
# Get all staged and unstaged files (excluding untracked)
29+
result = subprocess.run(
30+
["git", "diff", "--name-only"],
31+
capture_output=True, text=True
32+
)
33+
unstaged = set(result.stdout.splitlines())
34+
result = subprocess.run(
35+
["git", "diff", "--name-only", "--staged"],
36+
capture_output=True, text=True
37+
)
38+
staged = set(result.stdout.splitlines())
39+
# Union of both sets
40+
return sorted(unstaged | staged)
41+
42+
def get_diff_for_file(filename, staged=False):
43+
cmd = ["git", "diff"]
44+
if staged:
45+
cmd.append("--staged")
46+
cmd.append("--")
47+
cmd.append(filename)
48+
result = subprocess.run(cmd, capture_output=True, text=True)
49+
return result.stdout
50+
51+
def get_commit_messages(diff, files):
52+
# Use the Ollama chat model to get commit messages
53+
if len(diff) == 0 or len(files) == 0:
54+
return ""
55+
try:
56+
response: ChatResponse = chat(model=model, messages=[
57+
{
58+
'role': 'user',
59+
'content': prompt.replace("[Git Diff]", diff).replace("[Changed Files and Types]", files),
60+
},
61+
])
62+
return response['message']['content']
63+
except Exception:
64+
return ""
65+
66+
def diff_single_file(file):
67+
commit_messages = []
68+
unstaged_diff = get_diff_for_file(file, staged=False).strip()
69+
staged_diff = get_diff_for_file(file, staged=True).strip()
70+
messages_staged_diff = get_commit_messages(staged_diff, file).strip()
71+
messages_unstaged_diff = get_commit_messages(unstaged_diff, file).strip()
72+
if messages_staged_diff:
73+
commit_messages.append(messages_staged_diff)
74+
if messages_unstaged_diff:
75+
commit_messages.append(messages_unstaged_diff)
76+
return commit_messages
77+
""
78+
def git_commit_everything(message):
79+
"""
80+
Stages all changes (including new, modified, deleted files), commits with the given message,
81+
and pushes the commit to the current branch on the default remote ('origin').
82+
"""
83+
# Stage all changes (new, modified, deleted)
84+
subprocess.run(['git', 'add', '-A'], check=True)
85+
# Commit with the provided message
86+
subprocess.run(['git', 'commit', '-m', message], check=True)
87+
88+
def main():
89+
files = get_changed_files()
90+
if not files:
91+
print("No changes detected.")
92+
return
93+
for file in files:
94+
commit_messages = diff_single_file(file)
95+
single_message = "\n".join(commit_messages)
96+
print(f"{file}\n{single_message}")
97+
git_commit_everything(single_message)
98+
99+
if __name__ == "__main__":
100+
main()

0 commit comments

Comments
 (0)