Skip to content

Instantly share code, notes, and snippets.

@gobilo
Forked from karpathy/add_to_zshrc.sh
Last active August 26, 2024 08:03
Show Gist options
  • Select an option

  • Save gobilo/2af3b7e5480b77f5901be2ab860fccc0 to your computer and use it in GitHub Desktop.

Select an option

Save gobilo/2af3b7e5480b77f5901be2ab860fccc0 to your computer and use it in GitHub Desktop.

Revisions

  1. gobilo revised this gist Aug 26, 2024. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions add_to_zshrc.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,19 @@
    # -----------------------------------------------------------------------------
    # AI-powered Git Commit Function by Andrej Karpathy. Edited by Stefan Goebel for use with local instance of ollama.
    # AI-powered Git Commit Function by Andrej Karpathy. Edited by Stefan Goebel for use with a local instance of OLLAMA.
    # Check the MODEL variable and adjust it to the model you want to use.
    # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. Source it first to test it.
    # It:
    # 1) gets the current staged changed diff
    # 2) sends them to an LLM to write the git commit message
    # 2) sends it to Ollama to write the git commit message
    # 3) allows you to easily accept, edit, regenerate, cancel
    # But - just read and edit the code however you like
    # the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/
    # Read and edit the code however you like.
    # Remark: Andrej's orignal version of the script uses the `llm` CLI util which you can get here: https://llm.datasette.io/en/stable/

    gcm() {
    # Function to generate commit message
    generate_commit_message() {
    MODEL="llama3:latest"

    DIFF=$(git diff --cached)

    ollama run llama3:latest "
  2. gobilo revised this gist Aug 26, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion add_to_zshrc.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # -----------------------------------------------------------------------------
    # AI-powered Git Commit Function by Andrej Karpathy. Edited by Stefan Goebel for use with ollama.
    # AI-powered Git Commit Function by Andrej Karpathy. Edited by Stefan Goebel for use with local instance of ollama.
    # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. Source it first to test it.
    # It:
    # 1) gets the current staged changed diff
  3. gobilo revised this gist Aug 26, 2024. 1 changed file with 13 additions and 8 deletions.
    21 changes: 13 additions & 8 deletions add_to_zshrc.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # -----------------------------------------------------------------------------
    # AI-powered Git Commit Function
    # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. It:
    # AI-powered Git Commit Function by Andrej Karpathy. Edited by Stefan Goebel for use with ollama.
    # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. Source it first to test it.
    # It:
    # 1) gets the current staged changed diff
    # 2) sends them to an LLM to write the git commit message
    # 3) allows you to easily accept, edit, regenerate, cancel
    @@ -10,16 +11,20 @@
    gcm() {
    # Function to generate commit message
    generate_commit_message() {
    git diff --cached | llm "
    DIFF=$(git diff --cached)

    ollama run llama3:latest "
    Below is a diff of all staged changes, coming from the command:
    \`\`\`
    git diff --cached
    \`\`\`
    Please generate a concise, one-line commit message for these changes."
    }
    Please generate a concise, one-line commit message for these changes. Only return the commit message and nothing else.
    " + "${DIFF}"
    }

    # Function to read user input compatibly with both Bash and Zsh
    read_input() {
    if [ -n "$ZSH_VERSION" ]; then
    @@ -31,12 +36,12 @@ Please generate a concise, one-line commit message for these changes."
    }

    # Main script
    echo "Generating AI-powered commit message..."
    echo -e "Generating AI-powered commit message...\n"
    commit_message=$(generate_commit_message)

    while true; do
    echo -e "\nProposed commit message:"
    echo "$commit_message"
    # echo -e "\nProposed commit message:"
    echo -e "$commit_message\n"

    read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
    choice=$REPLY
  4. @karpathy karpathy created this gist Aug 25, 2024.
    78 changes: 78 additions & 0 deletions add_to_zshrc.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    # -----------------------------------------------------------------------------
    # AI-powered Git Commit Function
    # Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command. It:
    # 1) gets the current staged changed diff
    # 2) sends them to an LLM to write the git commit message
    # 3) allows you to easily accept, edit, regenerate, cancel
    # But - just read and edit the code however you like
    # the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/

    gcm() {
    # Function to generate commit message
    generate_commit_message() {
    git diff --cached | llm "
    Below is a diff of all staged changes, coming from the command:
    \`\`\`
    git diff --cached
    \`\`\`
    Please generate a concise, one-line commit message for these changes."
    }

    # Function to read user input compatibly with both Bash and Zsh
    read_input() {
    if [ -n "$ZSH_VERSION" ]; then
    echo -n "$1"
    read -r REPLY
    else
    read -p "$1" -r REPLY
    fi
    }

    # Main script
    echo "Generating AI-powered commit message..."
    commit_message=$(generate_commit_message)

    while true; do
    echo -e "\nProposed commit message:"
    echo "$commit_message"

    read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
    choice=$REPLY

    case "$choice" in
    a|A )
    if git commit -m "$commit_message"; then
    echo "Changes committed successfully!"
    return 0
    else
    echo "Commit failed. Please check your changes and try again."
    return 1
    fi
    ;;
    e|E )
    read_input "Enter your commit message: "
    commit_message=$REPLY
    if [ -n "$commit_message" ] && git commit -m "$commit_message"; then
    echo "Changes committed successfully with your message!"
    return 0
    else
    echo "Commit failed. Please check your message and try again."
    return 1
    fi
    ;;
    r|R )
    echo "Regenerating commit message..."
    commit_message=$(generate_commit_message)
    ;;
    c|C )
    echo "Commit cancelled."
    return 1
    ;;
    * )
    echo "Invalid choice. Please try again."
    ;;
    esac
    done
    }