Skip to content

Commit

Permalink
Merge pull request #60 from hwixley/feature/enhanced-gpt-commit-git-d…
Browse files Browse the repository at this point in the history
…iffs

Feature/enhanced gpt commit git diffs
  • Loading branch information
hwixley committed Jun 30, 2024
2 parents 8fae9c0 + c2795fd commit 7ebc62a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/classes/wgit/wgit.class
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ wgit.wyx_ginit(){
}

wgit.commit() {
git add .
if sys.util.empty "$1" ; then
if [ -f "${WYX_DATA_DIR}/.env" ]; then
if grep -q "OPENAI_API_KEY=" "${WYX_DATA_DIR}/.env" && grep -q "USE_SMART_COMMIT=true" "${WYX_DATA_DIR}/.env" ; then
IFS=$'\n' lines=($(python3 "$WYX_SCRIPT_DIR/services/openai_service.py" "smart"))
git add .
sys.log.h2 "GPT-3 Suggestion"
if sys.shell.zsh; then
sys.log.h2 "Title:${RESET} ${lines[1]}"
Expand All @@ -100,14 +100,17 @@ wgit.commit() {
else
sys.log.info "Provide a commit description: (defaults to 'wyx-cli quick commit')"
read -r description
git add .
git commit -m "${description:-wyx-cli quick commit}"
fi
else
sys.log.info "Provide a commit description: (defaults to 'wyx-cli quick commit')"
read -r description
git add .
git commit -m "${description:-wyx-cli quick commit}"
fi
else
git add .
git commit -m "${1:-wyx-cli quick commit}"
fi
}
Expand Down
12 changes: 8 additions & 4 deletions src/commands/scripts/services/openai_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from openai import OpenAI
from logger import error, info
from termcolor import colored
from typing import Optional


def read_file(file_path):
with open(file_path, "r") as f:
return f.read()

def get_git_diff():
return f"`git diff` output: {os.popen('git diff').read()}, and `git status` output: {os.popen('git status').read()}."
os.popen('git reset')
diff = os.popen('git diff').read()
clean_diff = "\n".join([line for line in diff.split("\n") if line.startswith("+") or line.startswith("-")])
return f"Use the following output of calling `git diff` in the terminal of the repository to inform these changes. Any line that starts with \"+++\" represents the git diff for a given file in the repo, all lines below that until another \"+++\" line is found belong to that given file. Any line that starts with a \"+\" is a line addition, and any line that starts with just a \"-\" is a line deletion in the given file git diff.\n\nOutput:\n\"\"\"\n{clean_diff}\n\"\"\"." #, and `git status` output: {os.popen('git status').read()}."

def format_message(role, message):
return { "role": role, "content": message }
Expand All @@ -25,17 +29,17 @@ def __init__(self):

def gen_title(self) -> str:
return self.prompt_eng(
instr_prefix="Write a 1 line commit message less than or equal to 50 characters technically describing the following bash git outputs."
instr_prefix="Write a 1 line commit message less than or equal to 50 characters technically describing the changes that were made in the given branch."
)

def gen_description(self) -> str:
return self.prompt_eng(
instr_prefix="Write a 2 line commit message technically describing the following bash git outputs.",
instr_prefix="Write a 2 line commit message technically describing the changes that were made in the given branch.",
instr_suffix=f"Do not repeat the title \"{self.title}\"."
)

def prompt_eng(self, instr_prefix: str, instr_suffix: str = "") -> str:
context = f"CONTEXT:\nYou are in a team of developers working on a project. You are using Git source control and need to write succinct yet informative commit messages for the changes that have been made by understanding the passed `git diff` and `git status` outputs."
context = f"CONTEXT:\nYou are in a team of developers working on a project. You are using Git source control and need to write succinct yet informative commit messages for the changes that have been made by understanding the passed `git diff` and `git status` outputs.\n\nFor example, if you saw a notable library (ie. `from argparse import ArgumentParser`) was imported in the git diff this could be used to inform what the commit message should mention (ie. \"setup cli arguments parser\")"
instructions = f"INSTRUCTIONS:\n{instr_prefix} {get_git_diff()}\n\n{instr_suffix} Do not mention anything about the branch these changes were made on, however, you can use the branch name as a hint to for the desired commit message contents if it is relevant. Mention specifically which functions, classes or variables were modified/created/deleted and why."
return context + "\n"*2 + instructions

Expand Down

0 comments on commit 7ebc62a

Please sign in to comment.