XPlor/ai-commit.sh

37 lines
763 B
Bash
Raw Normal View History

This is a well-structured and functional script that automates committing changes in a Git repository using Ollama to generate commit messages. Here's a breakdown of the script and some potential improvements: **Explanation:** 1. **`#!/bin/bash`**: Shebang line, specifying the script should be executed with Bash. 2. **`set -e`**: This crucial line ensures that the script exits immediately if any command fails. This prevents unexpected behavior and makes debugging easier. 3. **`git add -A`**: Stages all changes in the repository for commit. 4. **`FILES=$(git diff --cached --name-only)`**: This line retrieves a list of staged files from the Git index. `--cached` ensures it only includes staged files, and `--name-only` provides just the filenames. 5. **`if [ -z "$FILES" ]; then ... fi`**: Checks if any files are staged. If not, it prints a message and exits gracefully. This avoids errors when there's nothing to commit. 6. **`for FILE in $FILES; do ... done`**: Iterates through each staged file. 7. **`DIFF=$(git diff --cached -- "$FILE")`**: Gets the diff content of the current file. `--cached` ensures it's the staged diff. 8. **`if [ -z "$DIFF" ]; then continue; fi`**: Checks if there's a diff for the current file. If not (meaning the file hasn't changed), it skips to the next file. 9. **`MSG=$(echo "$DIFF" | ollama run gemma3 ...)`**: This is the core of the script. It pipes the diff content to Ollama to generate a commit message. * `ollama run gemma3`: Executes the `gemma3` model in Ollama. * `"You are a commit bot. Write a clear, concise Git commit message for changes in file: $FILE. Only output the commit message, nothing else. Diff: $DIFF"`: The prompt given to the Ollama model. It clearly instructs the model to create a concise commit message based on the file and its diff content. * The `gemma3` model is used, likely a smaller and faster model. 10. **`git commit -m "$MSG" -- "$FILE"`**: Commits the file with the generated message. * `-m "$MSG"`: Specifies the commit message. * `-- "$FILE"`: Indicates that only this file should be committed. 11. **`echo "✅ Committed $FILE with message:"` and `echo "$MSG"`**: Prints confirmation messages, making the script user-friendly. **Potential Improvements:** * **Error Handling within the Loop:** The script should handle potential errors during the `git commit` command within the loop. If a commit fails for one file, it should continue to process the remaining files. You can add `|| exit 1` after the `git commit` command to achieve this. * **Ollama Model Configuration:** Consider making the Ollama model name (`gemma3`) configurable via a variable. This would allow users to easily switch models. * **More Robust Diff Parsing:** The script assumes the diff content is simple. If the diff is complex (e.g., contains whitespace issues), it might not generate the desired commit message. A more robust solution would involve parsing the diff more carefully. * **Logging:** Add more detailed logging to help with debugging. * **Confirmation Prompt:** Before committing each file, you could ask the user for confirmation (e.g., "Do you want to commit $FILE with message $MSG?"). * **Interactive Diff Viewer:** For more complex changes, using a tool that allows the user to view the diff interactively and then provide the commit message could be beneficial. **Example with Error Handling and Logging:** ```bash #!/bin/bash set -e # Configuration OLLAMA_MODEL="gemma3" # 1. Stage everything git add -A # 2. Get list of staged files FILES=$(git diff --cached --name-only) if [ -z "$FILES" ]; then echo "No changes to commit." exit 0 fi for FILE in $FILES; do # Get diff for this file DIFF=$(git diff --cached -- "$FILE") if [ -z "$DIFF" ]; then continue fi # Ask Ollama for a commit message describing this file change MSG=$(echo "$DIFF" | ollama run "$OLLAMA_MODEL" \ "You are a commit bot. Write a clear, concise Git commit message for changes in file: $FILE. Only output the commit message, nothing else. Diff: $DIFF") # Commit just this file with its message echo "Committing $FILE with message: $MSG" git commit -m "$MSG" -- "$FILE" || { echo "Error committing $FILE. Continuing..." # Handle the error more gracefully here if needed } echo "✅ Committed $FILE with message:" echo "$MSG" done ``` This revised script is more robust and provides better error handling. It's a solid foundation for an automated commit script. Remember to install Ollama and the `gemma3` model before running the script.
2025-09-10 21:54:50 -04:00
#!/bin/bash
set -e
# 1. Stage everything
git add -A
# 2. Get list of staged files
FILES=$(git diff --cached --name-only)
if [ -z "$FILES" ]; then
echo "No changes to commit."
exit 0
fi
# 3. Loop file by file
for FILE in $FILES; do
# Get diff for this file
DIFF=$(git diff --cached -- "$FILE")
if [ -z "$DIFF" ]; then
continue
fi
# Ask Ollama for a commit message describing this file change
MSG=$(echo "$DIFF" | ollama run gemma3 \
"You are a commit bot. Write a SHORT, clear, concise Git commit message for changes in file: $FILE.
This is a well-structured and functional script that automates committing changes in a Git repository using Ollama to generate commit messages. Here's a breakdown of the script and some potential improvements: **Explanation:** 1. **`#!/bin/bash`**: Shebang line, specifying the script should be executed with Bash. 2. **`set -e`**: This crucial line ensures that the script exits immediately if any command fails. This prevents unexpected behavior and makes debugging easier. 3. **`git add -A`**: Stages all changes in the repository for commit. 4. **`FILES=$(git diff --cached --name-only)`**: This line retrieves a list of staged files from the Git index. `--cached` ensures it only includes staged files, and `--name-only` provides just the filenames. 5. **`if [ -z "$FILES" ]; then ... fi`**: Checks if any files are staged. If not, it prints a message and exits gracefully. This avoids errors when there's nothing to commit. 6. **`for FILE in $FILES; do ... done`**: Iterates through each staged file. 7. **`DIFF=$(git diff --cached -- "$FILE")`**: Gets the diff content of the current file. `--cached` ensures it's the staged diff. 8. **`if [ -z "$DIFF" ]; then continue; fi`**: Checks if there's a diff for the current file. If not (meaning the file hasn't changed), it skips to the next file. 9. **`MSG=$(echo "$DIFF" | ollama run gemma3 ...)`**: This is the core of the script. It pipes the diff content to Ollama to generate a commit message. * `ollama run gemma3`: Executes the `gemma3` model in Ollama. * `"You are a commit bot. Write a clear, concise Git commit message for changes in file: $FILE. Only output the commit message, nothing else. Diff: $DIFF"`: The prompt given to the Ollama model. It clearly instructs the model to create a concise commit message based on the file and its diff content. * The `gemma3` model is used, likely a smaller and faster model. 10. **`git commit -m "$MSG" -- "$FILE"`**: Commits the file with the generated message. * `-m "$MSG"`: Specifies the commit message. * `-- "$FILE"`: Indicates that only this file should be committed. 11. **`echo "✅ Committed $FILE with message:"` and `echo "$MSG"`**: Prints confirmation messages, making the script user-friendly. **Potential Improvements:** * **Error Handling within the Loop:** The script should handle potential errors during the `git commit` command within the loop. If a commit fails for one file, it should continue to process the remaining files. You can add `|| exit 1` after the `git commit` command to achieve this. * **Ollama Model Configuration:** Consider making the Ollama model name (`gemma3`) configurable via a variable. This would allow users to easily switch models. * **More Robust Diff Parsing:** The script assumes the diff content is simple. If the diff is complex (e.g., contains whitespace issues), it might not generate the desired commit message. A more robust solution would involve parsing the diff more carefully. * **Logging:** Add more detailed logging to help with debugging. * **Confirmation Prompt:** Before committing each file, you could ask the user for confirmation (e.g., "Do you want to commit $FILE with message $MSG?"). * **Interactive Diff Viewer:** For more complex changes, using a tool that allows the user to view the diff interactively and then provide the commit message could be beneficial. **Example with Error Handling and Logging:** ```bash #!/bin/bash set -e # Configuration OLLAMA_MODEL="gemma3" # 1. Stage everything git add -A # 2. Get list of staged files FILES=$(git diff --cached --name-only) if [ -z "$FILES" ]; then echo "No changes to commit." exit 0 fi for FILE in $FILES; do # Get diff for this file DIFF=$(git diff --cached -- "$FILE") if [ -z "$DIFF" ]; then continue fi # Ask Ollama for a commit message describing this file change MSG=$(echo "$DIFF" | ollama run "$OLLAMA_MODEL" \ "You are a commit bot. Write a clear, concise Git commit message for changes in file: $FILE. Only output the commit message, nothing else. Diff: $DIFF") # Commit just this file with its message echo "Committing $FILE with message: $MSG" git commit -m "$MSG" -- "$FILE" || { echo "Error committing $FILE. Continuing..." # Handle the error more gracefully here if needed } echo "✅ Committed $FILE with message:" echo "$MSG" done ``` This revised script is more robust and provides better error handling. It's a solid foundation for an automated commit script. Remember to install Ollama and the `gemma3` model before running the script.
2025-09-10 21:54:50 -04:00
Only output the commit message, nothing else.
Diff:
$DIFF")
# Commit just this file with its message
git commit -m "$MSG" -- "$FILE"
echo "✅ Committed $FILE with message:"
echo "$MSG"
done