Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Last active March 25, 2024 02:20
Show Gist options
  • Select an option

  • Save PurpleBooth/974e84f2801e42e81b3f07d80b321afd to your computer and use it in GitHub Desktop.

Select an option

Save PurpleBooth/974e84f2801e42e81b3f07d80b321afd to your computer and use it in GitHub Desktop.

Revisions

  1. PurpleBooth revised this gist Mar 5, 2023. 1 changed file with 74 additions and 48 deletions.
    122 changes: 74 additions & 48 deletions write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -1,52 +1,79 @@
    #!/usr/bin/env python
    import json
    import os
    import logging
    import subprocess
    import textwrap
    import sys
    from pprint import pprint
    import textwrap
    from typing import Literal, Sequence, TypedDict, Union
    from urllib.error import HTTPError
    from urllib.request import Request, urlopen

    api_key = "replaceme"


    def ask_openai(summary: str, diff: str) -> str:
    url = "https://api.openai.com/v1/chat/completions"
    class Message(TypedDict):
    role: Union[Literal["system"], Literal["assistant"], Literal["user"]]
    content: str


    body = json.dumps(
    def get_subject(summary: str, diff: str) -> tuple[str, list[Message]]:
    messages: list[Message] = [
    {
    "model": "gpt-3.5-turbo",
    "messages": [
    {
    "role": "system",
    "content": "Write a well formatted commit message, without trailers, do not write a conventional commit",
    },
    {
    "role": "assistant",
    "content": "Sure, do you have any information other than the diff?",
    },
    {"role": "user", "content": summary},
    {"role": "assistant", "content": "Can you provide me the diff?"},
    {"role": "user", "content": diff},
    {
    "role": "assistant",
    "content": "Thanks, the next message has the text you would put in your editor",
    },
    ],
    }
    )
    "role": "system",
    "content": "You are a tool that explains code changes",
    },
    {"role": "user", "content": "Can you explain change?"},
    {
    "role": "assistant",
    "content": "Sure, do you have any information other than the diff?",
    },
    {"role": "user", "content": summary},
    {"role": "assistant", "content": "Can you provide me the diff?"},
    {"role": "user", "content": diff},
    {
    "role": "assistant",
    "content": "Here is a terse, 50-character summary of the changes in the imperative tense",
    },
    ]
    result = do_request(messages)
    return result, messages + [{"role": "assistant", "content": result}]


    def get_body(previous: Sequence[Message]) -> str:
    messages: list[Message] = list(previous) + [
    {
    "role": "user",
    "content": "Can you give me a longer description of the changes?",
    },
    {
    "role": "assistant",
    "content": "Sure, what follows is a longer description of the changes in the imperative tense",
    },
    ]
    return do_request(messages)


    def do_request(messages: Sequence[Message]) -> str:
    request = {
    "model": "gpt-3.5-turbo",
    "messages": messages,
    }
    body = json.dumps(request)
    httprequest = Request(
    url,
    "https://api.openai.com/v1/chat/completions",
    method="POST",
    headers={
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}",
    },
    )

    with urlopen(httprequest, data=body.encode("utf-8")) as response:
    body = response.read().decode()
    return json.loads(body)["choices"][0]["message"]["content"]
    try:
    with urlopen(httprequest, data=body.encode("utf-8")) as response:
    response_body = response.read().decode()
    return json.loads(response_body)["choices"][0]["message"]["content"]
    except HTTPError as e:
    logging.exception(e.read().decode())
    raise e from e


    hint = "No"
    @@ -59,27 +86,26 @@ def ask_openai(summary: str, diff: str) -> str:
    )
    diff_output.check_returncode()

    suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n")
    subject, messages = get_subject(hint, diff_output.stdout.decode())
    subject = subject.rstrip(".")
    body = get_body(messages).split("\n\n")
    wrapped_subject = [subject]

    subject = suggestion[0]
    if len(subject) > 72:
    body = [subject] + body
    subject = textwrap.shorten(subject, width=50)
    wrapped_subject = [subject]

    if len(subject) > 50:
    suggestion = [textwrap.shorten(subject, width=50), subject] + suggestion[1:]
    wrapped_paragraphs = [
    textwrap.fill(
    s,
    width=72,
    )
    for s in body
    ]

    wrapped_text = "\n\n".join(
    [
    suggestion[0],
    *[
    textwrap.fill(
    s,
    width=72,
    drop_whitespace=False,
    replace_whitespace=False,
    expand_tabs=False,
    )
    for s in suggestion[1:]
    ],
    ]
    wrapped_subject + wrapped_paragraphs,
    )

    print(wrapped_text)
  2. PurpleBooth revised this gist Mar 3, 2023. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,12 @@ def ask_openai(summary: str, diff: str) -> str:
    diff_output.check_returncode()

    suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n")

    subject = suggestion[0]

    if len(subject) > 50:
    suggestion = [textwrap.shorten(subject, width=50), subject] + suggestion[1:]

    wrapped_text = "\n\n".join(
    [
    suggestion[0],
  3. PurpleBooth revised this gist Mar 2, 2023. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,19 @@ def ask_openai(summary: str, diff: str) -> str:

    suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n")
    wrapped_text = "\n\n".join(
    [suggestion[0], *[textwrap.fill(s, width=72) for s in suggestion[1:]]]
    [
    suggestion[0],
    *[
    textwrap.fill(
    s,
    width=72,
    drop_whitespace=False,
    replace_whitespace=False,
    expand_tabs=False,
    )
    for s in suggestion[1:]
    ],
    ]
    )

    print(wrapped_text)
  4. PurpleBooth revised this gist Mar 2, 2023. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    import json
    import os
    import subprocess
    import textwrap
    import sys
    from pprint import pprint
    from urllib.request import Request, urlopen
    @@ -58,4 +59,9 @@ def ask_openai(summary: str, diff: str) -> str:
    )
    diff_output.check_returncode()

    print(ask_openai(hint, diff_output.stdout.decode()))
    suggestion = ask_openai(hint, diff_output.stdout.decode()).split("\n\n")
    wrapped_text = "\n\n".join(
    [suggestion[0], *[textwrap.fill(s, width=72) for s in suggestion[1:]]]
    )

    print(wrapped_text)
  5. PurpleBooth revised this gist Mar 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ def ask_openai(summary: str, diff: str) -> str:
    hint = "No"

    if len(sys.argv) > 1:
    hint = " ".join(sys.argv())
    hint = " ".join(sys.argv[1:])

    diff_output = subprocess.run(
    ["git", "diff", "--patch", "--cached"], capture_output=True
  6. PurpleBooth revised this gist Mar 2, 2023. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -18,24 +18,29 @@ def ask_openai(summary: str, diff: str) -> str:
    "messages": [
    {
    "role": "system",
    "content": "Write a well formatted commit message, without trailers, do not write a conventional commit"
    }, {
    "content": "Write a well formatted commit message, without trailers, do not write a conventional commit",
    },
    {
    "role": "assistant",
    "content": "Sure, do you have any information other than the diff?"
    }, {"role": "user", "content": summary},
    "content": "Sure, do you have any information other than the diff?",
    },
    {"role": "user", "content": summary},
    {"role": "assistant", "content": "Can you provide me the diff?"},
    {"role": "user", "content": diff},
    {"role": "assistant", "content": "Thanks, the next message has the text you would put in your editor"},
    ]
    {
    "role": "assistant",
    "content": "Thanks, the next message has the text you would put in your editor",
    },
    ],
    }
    )
    httprequest = Request(
    url,
    method="POST",
    headers={
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
    }
    "Authorization": f"Bearer {api_key}",
    },
    )

    with urlopen(httprequest, data=body.encode("utf-8")) as response:
    @@ -48,7 +53,9 @@ def ask_openai(summary: str, diff: str) -> str:
    if len(sys.argv) > 1:
    hint = " ".join(sys.argv())

    diff_output = subprocess.run(["git", "diff", "--patch", "--cached"], capture_output=True)
    diff_output = subprocess.run(
    ["git", "diff", "--patch", "--cached"], capture_output=True
    )
    diff_output.check_returncode()

    print(ask_openai(hint, diff_output.stdout.decode()))
  7. PurpleBooth revised this gist Mar 2, 2023. 2 changed files with 54 additions and 80 deletions.
    54 changes: 54 additions & 0 deletions write-me-a-commit-message.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/usr/bin/env python
    import json
    import os
    import subprocess
    import sys
    from pprint import pprint
    from urllib.request import Request, urlopen

    api_key = "replaceme"


    def ask_openai(summary: str, diff: str) -> str:
    url = "https://api.openai.com/v1/chat/completions"

    body = json.dumps(
    {
    "model": "gpt-3.5-turbo",
    "messages": [
    {
    "role": "system",
    "content": "Write a well formatted commit message, without trailers, do not write a conventional commit"
    }, {
    "role": "assistant",
    "content": "Sure, do you have any information other than the diff?"
    }, {"role": "user", "content": summary},
    {"role": "assistant", "content": "Can you provide me the diff?"},
    {"role": "user", "content": diff},
    {"role": "assistant", "content": "Thanks, the next message has the text you would put in your editor"},
    ]
    }
    )
    httprequest = Request(
    url,
    method="POST",
    headers={
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
    }
    )

    with urlopen(httprequest, data=body.encode("utf-8")) as response:
    body = response.read().decode()
    return json.loads(body)["choices"][0]["message"]["content"]


    hint = "No"

    if len(sys.argv) > 1:
    hint = " ".join(sys.argv())

    diff_output = subprocess.run(["git", "diff", "--patch", "--cached"], capture_output=True)
    diff_output.check_returncode()

    print(ask_openai(hint, diff_output.stdout.decode()))
    80 changes: 0 additions & 80 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -1,80 +0,0 @@
    #!/usr/bin/env bash

    set -euo pipefail

    api_key="replaceme"
    truncate_at=4092

    ask_openai() {
    local prompt="$1"

    max_tokens=2048
    request="$(jq --null-input \
    --argjson max_tokens "$max_tokens" \
    --arg "prompt" "$prompt" \
    '{"model": "gpt-3.5-turbo", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    response="$(curl https://api.openai.com/v1/completions \
    --silent \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $api_key" \
    --data "$request" \
    --output -)"

    text="$(echo "$response" | jq --raw-output .choices[0].text)"

    if [ -z "$text" ] || [ "null" == "$text" ]; then
    echo ""
    else
    python -c "import sys; print( sys.argv[1].strip() )" "$text"
    fi
    }

    hint="No"

    if [ $# -ne 0 ]; then
    hint="$@"
    fi

    prompt=$(
    cat <<-END
    Please write me a commit message
    Sure, do you have any information other than the diff?
    $hint
    Can you provide me the diff?
    $(git diff -p --cached | head --bytes="$truncate_at")
    Please write a short, 50 characters or less, commit message for these changes?
    END
    )

    subject="$(ask_openai "$prompt")"

    prompt_for_body=$(
    cat <<-END
    $prompt
    $subject
    Now describe the change in detail in a longer commit message
    END
    )

    body="$(ask_openai "$prompt_for_body")"
    echo "$subject"

    if [ -n "$body" ] && [ "$subject" != "$body" ]; then
    echo
    echo "$body" | fold -s -w 72
    fi
  8. PurpleBooth revised this gist Mar 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ ask_openai() {
    request="$(jq --null-input \
    --argjson max_tokens "$max_tokens" \
    --arg "prompt" "$prompt" \
    '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"
    '{"model": "gpt-3.5-turbo", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    response="$(curl https://api.openai.com/v1/completions \
    --silent \
  9. PurpleBooth revised this gist Feb 20, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ prompt_for_body=$(
    $subject
    Now describe the change in detail
    Now describe the change in detail in a longer commit message
    END
    )

  10. PurpleBooth revised this gist Feb 19, 2023. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -57,23 +57,20 @@ prompt=$(
    END
    )



    subject="$(ask_openai "$prompt" | head --bytes=50)"
    subject="$(ask_openai "$prompt")"

    prompt_for_body=$(
    cat <<-END
    $prompt
    $subject
    $subject
    Now describe the change in detail
    END
    )


    body="$(ask_openai "$prompt_for_body")"
    echo "$subject"

  11. PurpleBooth revised this gist Feb 18, 2023. 1 changed file with 51 additions and 18 deletions.
    69 changes: 51 additions & 18 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,33 @@
    set -euo pipefail

    api_key="replaceme"

    max_tokens=2048
    truncate_at=4092

    ask_openai() {
    local prompt="$1"

    max_tokens=2048
    request="$(jq --null-input \
    --argjson max_tokens "$max_tokens" \
    --arg "prompt" "$prompt" \
    '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    response="$(curl https://api.openai.com/v1/completions \
    --silent \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $api_key" \
    --data "$request" \
    --output -)"

    text="$(echo "$response" | jq --raw-output .choices[0].text)"

    if [ -z "$text" ] || [ "null" == "$text" ]; then
    echo ""
    else
    python -c "import sys; print( sys.argv[1].strip() )" "$text"
    fi
    }

    hint="No"

    if [ $# -ne 0 ]; then
    @@ -29,22 +53,31 @@ prompt=$(
    $(git diff -p --cached | head --bytes="$truncate_at")
    Here is the commit message:
    Please write a short, 50 characters or less, commit message for these changes?
    END
    )

    request="$(jq --null-input \
    --argjson max_tokens "$max_tokens" \
    --arg "prompt" "$prompt" \
    '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    response="$(curl https://api.openai.com/v1/completions \
    --silent \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $api_key" \
    --data "$request" \
    --output -)"

    echo "$response" |
    jq --raw-output .choices[0].text |
    python -c "import sys; print( sys.stdin.read().strip())"


    subject="$(ask_openai "$prompt" | head --bytes=50)"

    prompt_for_body=$(
    cat <<-END
    $prompt
    $subject
    Now describe the change in detail
    END
    )


    body="$(ask_openai "$prompt_for_body")"
    echo "$subject"

    if [ -n "$body" ] && [ "$subject" != "$body" ]; then
    echo
    echo "$body" | fold -s -w 72
    fi
  12. PurpleBooth revised this gist Feb 17, 2023. 1 changed file with 2 additions and 18 deletions.
    20 changes: 2 additions & 18 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -17,23 +17,7 @@ prompt=$(
    Please write me a commit message
    Sure, what does a good commit message look like?
    Capitalized, short (50 chars or less) summary
    More detailed explanatory text, if necessary. Wrap it to about 72
    characters or so. In some contexts, the first line is treated as the
    subject of an email and the rest of the text as the body. The blank
    line separating the summary from the body is critical (unless you omit
    the body entirely); tools like rebase can get confused if you run the
    two together.
    Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
    or "Fixes bug." This convention matches up with commit messages generated
    by commands like git merge and git revert.
    Do you have any additional information that should included?
    Sure, do you have any information other than the diff?
    $hint
    @@ -45,7 +29,7 @@ prompt=$(
    $(git diff -p --cached | head --bytes="$truncate_at")
    A good commit message would be:
    Here is the commit message:
    END
    )

  13. PurpleBooth revised this gist Feb 15, 2023. 1 changed file with 11 additions and 13 deletions.
    24 changes: 11 additions & 13 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,12 @@ fi
    prompt=$(
    cat <<-END
    Please write me a commit message
    Sure, what does a good commit message look like?
    Capitalized, short (50 chars or less) summary
    Capitalized, short (50 chars or less) summary
    More detailed explanatory text, if necessary. Wrap it to about 72
    characters or so. In some contexts, the first line is treated as the
    subject of an email and the rest of the text as the body. The blank
    @@ -32,23 +31,22 @@ prompt=$(
    Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
    or "Fixes bug." This convention matches up with commit messages generated
    by commands like git merge and git revert.
    Do you have any additional information that should included?
    $hint
    Can you provide me the diff?
    $(git diff -p --cached | head --bytes="$truncate_at")
    A good commit message would be
    END
    A good commit message would be:
    END
    )

    request="$(jq --null-input \
  14. PurpleBooth revised this gist Feb 15, 2023. 1 changed file with 17 additions and 7 deletions.
    24 changes: 17 additions & 7 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -6,17 +6,19 @@ api_key="replaceme"

    max_tokens=2048
    truncate_at=4092
    hint=""
    hint="No"

    if [ $# -ne 0 ]; then
    hint="It is very important to include this information: $@"
    hint="$@"
    fi

    prompt=$(
    cat <<-END
    Summarize these changes for a commit message
    Please write me a commit message
    Use the syle in this example below
    Sure, what does a good commit message look like?
    Capitalized, short (50 chars or less) summary
    @@ -30,13 +32,21 @@ prompt=$(
    Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
    or "Fixes bug." This convention matches up with commit messages generated
    by commands like git merge and git revert.
    Do you have any additional information that should included?
    $hint
    Below is a diff of the changes. Do not include it verbaitem
    Can you provide me the diff?
    $(git diff -p --cached | head --bytes="$truncate_at")
    A good commit message would be
    END
    )
  15. PurpleBooth revised this gist Feb 13, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -55,5 +55,4 @@ response="$(curl https://api.openai.com/v1/completions \

    echo "$response" |
    jq --raw-output .choices[0].text |
    sed -e's/[[:space:]]*$//' |
    sed -e's/^[[:space:]]*//'
    python -c "import sys; print( sys.stdin.read().strip())"
  16. PurpleBooth revised this gist Feb 12, 2023. 1 changed file with 7 additions and 15 deletions.
    22 changes: 7 additions & 15 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    set -euo pipefail

    api_key="sk-hahaha"
    api_key="replaceme"

    max_tokens=2048
    truncate_at=4092
    @@ -14,9 +14,9 @@ fi

    prompt=$(
    cat <<-END
    Summarize this diff for a commit message
    Summarize these changes for a commit message
    The following is a good commit message
    Use the syle in this example below
    Capitalized, short (50 chars or less) summary
    @@ -31,19 +31,10 @@ prompt=$(
    or "Fixes bug." This convention matches up with commit messages generated
    by commands like git merge and git revert.
    Further paragraphs come after blank lines.
    - Bullet points are okay, too
    $hint
    - Typically a hyphen or asterisk is used for the bullet, followed by a
    single space, with blank lines in between, but conventions vary here
    - Use a hanging indent
    $@
    Below is a diff of changes, lines beginning with "-" are deletions, and lines beginning with "+" are additions
    Below is a diff of the changes. Do not include it verbaitem
    $(git diff -p --cached | head --bytes="$truncate_at")
    END
    @@ -64,4 +55,5 @@ response="$(curl https://api.openai.com/v1/completions \

    echo "$response" |
    jq --raw-output .choices[0].text |
    xargs
    sed -e's/[[:space:]]*$//' |
    sed -e's/^[[:space:]]*//'
  17. PurpleBooth revised this gist Feb 12, 2023. 1 changed file with 32 additions and 6 deletions.
    38 changes: 32 additions & 6 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -5,23 +5,47 @@ set -euo pipefail
    api_key="sk-hahaha"

    max_tokens=2048
    truncate_at=4092
    hint=""

    if [ $# -ne 0 ]; then
    hint="Hint about the intention of this commit: $@"
    hint="It is very important to include this information: $@"
    fi

    prompt=$(
    cat <<-END
    Summarize this diff for a commit message
    Make the first line a summary of 50 characters or less, then a blank line, remaining text should be wrapped at 72 characters and include additional details
    The following is a good commit message
    Capitalized, short (50 chars or less) summary
    More detailed explanatory text, if necessary. Wrap it to about 72
    characters or so. In some contexts, the first line is treated as the
    subject of an email and the rest of the text as the body. The blank
    line separating the summary from the body is critical (unless you omit
    the body entirely); tools like rebase can get confused if you run the
    two together.
    Write your commit message in the imperative: "Fix bug" and not "Fixed bug"
    or "Fixes bug." This convention matches up with commit messages generated
    by commands like git merge and git revert.
    Further paragraphs come after blank lines.
    - Bullet points are okay, too
    - Typically a hyphen or asterisk is used for the bullet, followed by a
    single space, with blank lines in between, but conventions vary here
    - Use a hanging indent
    $@
    A diff of the change:
    Below is a diff of changes, lines beginning with "-" are deletions, and lines beginning with "+" are additions
    $(git diff -p --cached)
    $(git diff -p --cached | head --bytes="$truncate_at")
    END
    )
    @@ -31,11 +55,13 @@ request="$(jq --null-input \
    --arg "prompt" "$prompt" \
    '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    curl https://api.openai.com/v1/completions \
    response="$(curl https://api.openai.com/v1/completions \
    --silent \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $api_key" \
    --data "$request" \
    --output - |
    --output -)"

    echo "$response" |
    jq --raw-output .choices[0].text |
    xargs
  18. PurpleBooth created this gist Feb 12, 2023.
    41 changes: 41 additions & 0 deletions write-me-a-commit-message.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env bash

    set -euo pipefail

    api_key="sk-hahaha"

    max_tokens=2048
    hint=""

    if [ $# -ne 0 ]; then
    hint="Hint about the intention of this commit: $@"
    fi

    prompt=$(
    cat <<-END
    Summarize this diff for a commit message
    Make the first line a summary of 50 characters or less, then a blank line, remaining text should be wrapped at 72 characters and include additional details
    $@
    A diff of the change:
    $(git diff -p --cached)
    END
    )

    request="$(jq --null-input \
    --argjson max_tokens "$max_tokens" \
    --arg "prompt" "$prompt" \
    '{"model": "text-davinci-003", "prompt": $prompt, "temperature": 0, "max_tokens": $max_tokens }')"

    curl https://api.openai.com/v1/completions \
    --silent \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $api_key" \
    --data "$request" \
    --output - |
    jq --raw-output .choices[0].text |
    xargs