From c7eb168dc273cec1e2d933d9210fb918517c41a7 Mon Sep 17 00:00:00 2001 From: Pho Tran Date: Sun, 18 May 2025 11:50:38 +0700 Subject: [PATCH 1/5] feat: Update ollama integration with async client --- auto_commit.py | 67 ++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/auto_commit.py b/auto_commit.py index cbc0c88..6cd9775 100644 --- a/auto_commit.py +++ b/auto_commit.py @@ -1,7 +1,7 @@ +import asyncio import subprocess import sys -from ollama import chat -from ollama import ChatResponse +from ollama import AsyncClient model = "gemma3:4b" prompt = f""" @@ -19,9 +19,11 @@ 2. Summarize all changes into a single logical commit. 3. Write a concise commit message (max 72 characters) in the conventional commit style """ +client = AsyncClient() -def get_changed_files(): - # Git add all + +async def get_changed_files(): + # Git add all subprocess.run( ["git", "add", "."], capture_output=True, text=True @@ -40,7 +42,8 @@ def get_changed_files(): # Union of both sets return sorted(unstaged | staged) -def get_diff_for_file(filename, staged=False): + +async def get_diff_for_file(filename, staged=False): cmd = ["git", "diff"] if staged: cmd.append("--staged") @@ -49,34 +52,38 @@ def get_diff_for_file(filename, staged=False): result = subprocess.run(cmd, capture_output=True, text=True) return result.stdout -def get_commit_messages(diff, files): + +async def get_commit_messages(diff, files): # Use the Ollama chat model to get commit messages if len(diff) == 0 or len(files) == 0: return "" try: - response: ChatResponse = chat(model=model, messages=[ - { - 'role': 'user', - 'content': prompt.replace("[Git Diff]", diff).replace("[Changed Files and Types]", files), - }, - ]) + messages = [ + { + 'role': 'user', + 'content': prompt.replace("[Git Diff]", diff).replace("[Changed Files and Types]", files), + }, + ] + response = await client.chat(model=model, messages=messages) return response['message']['content'] except Exception: return "" - -def diff_single_file(file): + + +async def diff_single_file(file): commit_messages = [] - unstaged_diff = get_diff_for_file(file, staged=False).strip() - staged_diff = get_diff_for_file(file, staged=True).strip() - messages_staged_diff = get_commit_messages(staged_diff, file).strip() - messages_unstaged_diff = get_commit_messages(unstaged_diff, file).strip() + unstaged_diff = (await get_diff_for_file(file, staged=False)).strip() + staged_diff = (await get_diff_for_file(file, staged=True)).strip() + messages_staged_diff = (await get_commit_messages(staged_diff, file)).strip() + messages_unstaged_diff = (await get_commit_messages(unstaged_diff, file)).strip() if messages_staged_diff: commit_messages.append(messages_staged_diff) if messages_unstaged_diff: commit_messages.append(messages_unstaged_diff) return commit_messages - "" -def git_commit_everything(message): + + +async def git_commit_everything(message): """ Stages all changes (including new, modified, deleted files), commits with the given message, and pushes the commit to the current branch on the default remote ('origin'). @@ -87,10 +94,12 @@ def git_commit_everything(message): subprocess.run(['git', 'add', '-A'], check=True) # Commit with the provided message subprocess.run(['git', 'commit', '-m', message], check=True) - -def main(): - commit_single_file = True if (len(sys.argv) > 1 and sys.argv[1] == "single_file") else False - files = get_changed_files() + + +async def main(): + commit_single_file = True if ( + len(sys.argv) > 1 and sys.argv[1] == "single_file") else False + files = await get_changed_files() if not files: print("No changes detected.") return @@ -98,15 +107,15 @@ def main(): all_commit_messages = [] for file in files: print(f"{file}") - commit_messages = diff_single_file(file) + commit_messages = await diff_single_file(file) commit_messages_text = "\n".join(commit_messages) print(f"{commit_messages_text}") if commit_single_file: - git_commit_everything(commit_messages_text) - else: + await git_commit_everything(commit_messages_text) + else: all_commit_messages.extend(commit_messages) if all_commit_messages and not commit_single_file: - git_commit_everything("\n".join(all_commit_messages)) + await git_commit_everything("\n".join(all_commit_messages)) if __name__ == "__main__": - main() \ No newline at end of file + asyncio.run(main()) From dd9b35cf78968167d37e48bfb225bd5546dbabd3 Mon Sep 17 00:00:00 2001 From: Pho Tran Date: Sun, 18 May 2025 11:52:13 +0700 Subject: [PATCH 2/5] feat: Add Ollama installation instructions --- file2.md | 16 ++++++++++++++++ file_1.txt | 7 +++++++ 2 files changed, 23 insertions(+) create mode 100644 file2.md create mode 100644 file_1.txt diff --git a/file2.md b/file2.md new file mode 100644 index 0000000..fa36f32 --- /dev/null +++ b/file2.md @@ -0,0 +1,16 @@ +### Installation + +- [Olama](https://ollama.com/download) + +- [Ollama Model](https://ollama.com/library/gemma3) + +This script currently uses the `gemma3:4b` model. + +``` +ollama run gemma3:4b +``` + +- [Python Ollama](https://github.com/ollama/ollama-python) +``` +pip install ollama +``` \ No newline at end of file diff --git a/file_1.txt b/file_1.txt new file mode 100644 index 0000000..2f229b4 --- /dev/null +++ b/file_1.txt @@ -0,0 +1,7 @@ +Contributions and ideas are welcome, such as: + +- Modifying the prompt, model, or LLM provider. + +- Generating commit messages for individual files or a single message for all files. + +- Optimize or refactor... From 21642a5f34a54cba8a975eabb037885346c7522f Mon Sep 17 00:00:00 2001 From: Pho Tran Date: Sun, 18 May 2025 11:54:04 +0700 Subject: [PATCH 3/5] fix: Add commit messages for each file --- auto_commit.py | 5 +++-- file2.md | 1 - file_1.txt | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/auto_commit.py b/auto_commit.py index 6cd9775..84a76db 100644 --- a/auto_commit.py +++ b/auto_commit.py @@ -106,12 +106,13 @@ async def main(): all_commit_messages = [] for file in files: - print(f"{file}") + # print(f"{file}") commit_messages = await diff_single_file(file) commit_messages_text = "\n".join(commit_messages) - print(f"{commit_messages_text}") + # print(f"{commit_messages_text}") if commit_single_file: await git_commit_everything(commit_messages_text) + print(f"{file} --- {commit_messages_text}") else: all_commit_messages.extend(commit_messages) if all_commit_messages and not commit_single_file: diff --git a/file2.md b/file2.md index fa36f32..c66446a 100644 --- a/file2.md +++ b/file2.md @@ -1,6 +1,5 @@ ### Installation -- [Olama](https://ollama.com/download) - [Ollama Model](https://ollama.com/library/gemma3) diff --git a/file_1.txt b/file_1.txt index 2f229b4..891b32e 100644 --- a/file_1.txt +++ b/file_1.txt @@ -3,5 +3,3 @@ Contributions and ideas are welcome, such as: - Modifying the prompt, model, or LLM provider. - Generating commit messages for individual files or a single message for all files. - -- Optimize or refactor... From 429b67a83e92e3622c712ba5e15e7e71e0aa8596 Mon Sep 17 00:00:00 2001 From: Pho Tran Date: Sun, 18 May 2025 11:55:36 +0700 Subject: [PATCH 4/5] feat: Update Ollama model instructions in file2.md --- file2.md | 15 --------------- file_1.txt | 5 ----- 2 files changed, 20 deletions(-) delete mode 100644 file2.md delete mode 100644 file_1.txt diff --git a/file2.md b/file2.md deleted file mode 100644 index c66446a..0000000 --- a/file2.md +++ /dev/null @@ -1,15 +0,0 @@ -### Installation - - -- [Ollama Model](https://ollama.com/library/gemma3) - -This script currently uses the `gemma3:4b` model. - -``` -ollama run gemma3:4b -``` - -- [Python Ollama](https://github.com/ollama/ollama-python) -``` -pip install ollama -``` \ No newline at end of file diff --git a/file_1.txt b/file_1.txt deleted file mode 100644 index 891b32e..0000000 --- a/file_1.txt +++ /dev/null @@ -1,5 +0,0 @@ -Contributions and ideas are welcome, such as: - -- Modifying the prompt, model, or LLM provider. - -- Generating commit messages for individual files or a single message for all files. From 95f2fd21bd4aa15789b077c6fa49f25f3553aa14 Mon Sep 17 00:00:00 2001 From: Pho Tran Date: Sun, 18 May 2025 11:57:27 +0700 Subject: [PATCH 5/5] docs: Update README with auto-commit instructions --- README.md | 6 ------ auto_commit.py | 15 +++------------ 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2cbf79d..ddda63f 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,6 @@ If ollama server is running, run the script using the command python3 auto_commit.py ``` -Create a commit for each file. - -``` -python3 auto_commit.py single_file -``` - ### Miscellaneous diff --git a/auto_commit.py b/auto_commit.py index 84a76db..ed818c2 100644 --- a/auto_commit.py +++ b/auto_commit.py @@ -97,26 +97,17 @@ async def git_commit_everything(message): async def main(): - commit_single_file = True if ( - len(sys.argv) > 1 and sys.argv[1] == "single_file") else False files = await get_changed_files() if not files: print("No changes detected.") return - all_commit_messages = [] for file in files: - # print(f"{file}") + print(f"{file}") commit_messages = await diff_single_file(file) commit_messages_text = "\n".join(commit_messages) - # print(f"{commit_messages_text}") - if commit_single_file: - await git_commit_everything(commit_messages_text) - print(f"{file} --- {commit_messages_text}") - else: - all_commit_messages.extend(commit_messages) - if all_commit_messages and not commit_single_file: - await git_commit_everything("\n".join(all_commit_messages)) + print(f"{commit_messages_text}") + await git_commit_everything(commit_messages_text) if __name__ == "__main__": asyncio.run(main())