Skip to content

Commit 076ead4

Browse files
authored
add git status (#3)
* create single commit message Create * feat: Add single commit option in auto_commit.py * refactor: Simplify git commit function * fix: Update VERSION to 1.0.1 * docs: Update VERSION file to 1.0.1 * fix: Update VERSION to 1.0.1 * fix: Generate commit messages for single files * fix: Update auto_commit.py with Git Diff content * fix: Version updated to 1.0.2
1 parent 0ea9fe1 commit 076ead4

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

auto_commit.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ async def get_diff_for_file(filename, staged=False):
5353
return result.stdout
5454

5555

56-
async def get_commit_messages(diff, files):
56+
async def get_commit_messages(diff, file_with_type):
5757
# Use the Ollama chat model to get commit messages
58-
if len(diff) == 0 or len(files) == 0:
58+
if len(diff) == 0 or len(file_with_type) == 0:
5959
return ""
6060
try:
6161
messages = [
6262
{
6363
'role': 'user',
64-
'content': prompt.replace("[Git Diff]", diff).replace("[Changed Files and Types]", files),
64+
'content': prompt.replace("[Git Diff]", diff).replace("[Changed Files and Types]", file_with_type),
6565
},
6666
]
6767
response = await client.chat(model=model, messages=messages)
@@ -70,12 +70,43 @@ async def get_commit_messages(diff, files):
7070
return ""
7171

7272

73+
def status_file(file_path):
74+
"""
75+
Creates a descriptive commit message for changes to a single file,
76+
detecting if it was added, modified, or deleted.
77+
"""
78+
try:
79+
# Check if the file is new (not tracked yet)
80+
status_new_process = subprocess.run(
81+
['git', 'status', '--porcelain', file_path],
82+
capture_output=True, text=True, check=True
83+
)
84+
if status_new_process.stdout.strip().startswith("??"):
85+
return "Add"
86+
87+
# Check if the file was deleted
88+
status_deleted_process = subprocess.run(
89+
['git', 'diff', '--staged', '--name-status', file_path],
90+
capture_output=True, text=True, check=True,
91+
)
92+
if status_deleted_process.stdout.strip().startswith("D"):
93+
return "Remove"
94+
95+
# If not new or deleted, assume it's modified
96+
return "Update"
97+
98+
except:
99+
return ""
100+
101+
73102
async def diff_single_file(file):
74103
commit_messages = []
104+
status = status_file(file).strip()
105+
file_with_type = f"{file} : {status}"
75106
unstaged_diff = (await get_diff_for_file(file, staged=False)).strip()
76107
staged_diff = (await get_diff_for_file(file, staged=True)).strip()
77-
messages_staged_diff = (await get_commit_messages(staged_diff, file)).strip()
78-
messages_unstaged_diff = (await get_commit_messages(unstaged_diff, file)).strip()
108+
messages_staged_diff = (await get_commit_messages(staged_diff, file_with_type)).strip()
109+
messages_unstaged_diff = (await get_commit_messages(unstaged_diff, file_with_type)).strip()
79110
if messages_staged_diff:
80111
commit_messages.append(messages_staged_diff)
81112
if messages_unstaged_diff:

0 commit comments

Comments
 (0)