Created
January 21, 2025 00:30
-
-
Save rjpower/cd75b255a416f3fa93d73b167ee3a94a to your computer and use it in GitHub Desktop.
auto-squash aider commits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Users/power/bin/.venv/bin/python | |
"""Automatically squash all commits not in the origin. | |
Nice to compact down aider commits. | |
""" | |
import subprocess | |
from litellm import completion | |
def get_commits_not_in_origin(): | |
commits = ( | |
subprocess.check_output( | |
["git", "log", "--stat", "origin/main..HEAD"], text=True | |
) | |
.strip() | |
) | |
return commits | |
def generate_commit_message(commits): | |
prompt = f"""Given the following commit blocks for many individual small commits suggest a good combined message. | |
A good message should read as should highlight important changes to the user | |
behavior, refactorings, test improvements, etc. | |
Target 10 lines. | |
Ignore commits which cancel each other out, or are obvious placeholders. e.g. | |
(added "x", removed "x"). Output only the new commit message, with no other | |
content. | |
Commits start now. | |
{'\n'.join(commits)} | |
""" | |
response = completion( | |
model="anthropic/claude-3-5-sonnet-20240620", | |
messages=[{"role": "user", "content": prompt}], | |
max_tokens=200, | |
) | |
content = response.choices[0].message.content.strip() | |
return content | |
def main(): | |
commits = get_commits_not_in_origin() | |
if not commits: | |
print("No commits to squash.") | |
return | |
combined_message = generate_commit_message(commits) | |
print("Suggested commit message:") | |
print(combined_message) | |
user_input = input("Do you want to proceed with this commit message? (y/n): ") | |
if user_input.lower() != "y": | |
print("Aborting squash.") | |
return | |
with open("/tmp/squash-message", "w") as f: | |
f.write(combined_message) | |
subprocess.check_call(["git", "reset", "--soft", "origin/main"]) | |
subprocess.check_call(["git", "commit", "--file", "/tmp/squash-message"]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment