commit wip

This commit is contained in:
Paul Gauthier 2023-05-08 11:41:51 -07:00
parent b0d2fafdd5
commit 0f040949c4
2 changed files with 67 additions and 12 deletions

View file

@ -17,6 +17,7 @@ from tqdm import tqdm
from pathlib import Path from pathlib import Path
import os import os
import pygit2
import openai import openai
from dump import dump from dump import dump
@ -233,12 +234,12 @@ class Coder:
interrupted = False interrupted = False
try: try:
if show_progress: if show_progress is not None:
self.show_send_progress(completion, show_progress) self.show_send_progress(completion, show_progress)
elif self.pretty: elif self.pretty and show_progress:
self.show_send_output_color(completion) self.show_send_output_color(completion)
else: else:
self.show_send_output_plain(completion) self.show_send_output_plain(completion, False)
except KeyboardInterrupt: except KeyboardInterrupt:
interrupted = True interrupted = True
@ -259,7 +260,7 @@ class Coder:
pbar.update(show_progress) pbar.update(show_progress)
pbar.close() pbar.close()
def show_send_output_plain(self, completion): def show_send_output_plain(self, completion, show_output=True):
self.resp = "" self.resp = ""
for chunk in completion: for chunk in completion:
@ -271,6 +272,7 @@ class Coder:
except AttributeError: except AttributeError:
continue continue
if show_output:
sys.stdout.write(text) sys.stdout.write(text)
sys.stdout.flush() sys.stdout.flush()
@ -402,11 +404,64 @@ class Coder:
return res return res
def commit(self, message_history): def commit(self, message_history, prefix=None):
# _messages = [ repo_paths = set(pygit2.discover_repository(fname) for fname in self.fnames)
# dict(role="system", content=prompts.commit_system),
# ] if len(repo_paths) > 1:
pass repo_paths = " ".join(repo_paths)
raise ValueError(f"Files must all be in one git repo, not: {repo_paths}")
repo = pygit2.Repository(repo_paths.pop())
"""
index = repo.index
index.read()
for patch in index.diff_to_workdir():
print(patch.text)
"""
patches = repo.diff("HEAD")
if not len(patches):
return
context = ""
if message_history:
context += "# Context:\n"
for msg in message_history:
context += msg["role"].upper() + ": " + msg["content"] + "\n"
diffs = "# Diffs:\n"
for diff in patches:
dump(dir(diff))
dump(diff.delta.new_file.path)
diffs += diff.text
diffs += "\n\n"
if not diffs:
return
messages = [
dict(role="system", content=prompts.commit_system),
dict(role="user", content=context + diffs),
]
self.show_messages(messages, "commit")
commit_message, interrupted = self.send(
messages,
model="gpt-3.5-turbo",
show_progress=None,
)
commit_message = commit_message.strip()
if interrupted:
raise KeyboardInterrupt
if prefix:
commit_message = prefix + commit_message
print(commit_message)
def main(): def main():

View file

@ -105,7 +105,7 @@ ONLY OUTPUT {fname} !!!
# COMMIT # COMMIT
commit_system = """You are an expert software engineer. commit_system = """You are an expert software engineer.
Review the provided diffs which are about to be committed to a git repo. Review the provided context and diffs which are about to be committed to a git repo.
Generate a 1 line, 1-2 sentence commit message. Generate a 1 line, 1-2 sentence commit message.
Reply with JUST the commit message, without comments, questions, etc. Reply with JUST the commit message, without comments, questions, etc.
""" """