Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create prepare-commit-msg #64

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env sh

# This script generates an AI-powered commit message using dotnet-aicommitmessage.
# It can be bypassed by setting the GIT_AICOMMIT_SKIP environment variable.

# Exit immediately if GIT_AICOMMIT_SKIP is set
if [ -n "$GIT_AICOMMIT_SKIP" ]; then
exit 0
fi

# Check if dotnet-aicommitmessage is installed and in PATH
if ! command -v dotnet-aicommitmessage >/dev/null 2>&1; then
echo "Error: dotnet-aicommitmessage is not installed or not in PATH" >&2
echo "Please install it by running 'dotnet tool install -g aicommitmessage'" >&2
exit 1
fi
Comment on lines +11 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Add version compatibility check for dotnet-aicommitmessage.

While the tool presence check is good, it's important to ensure compatibility by verifying the tool version.

 if ! command -v dotnet-aicommitmessage >/dev/null 2>&1; then
     echo "Error: dotnet-aicommitmessage is not installed or not in PATH" >&2
     echo "Please install it by running 'dotnet tool install -g aicommitmessage'" >&2
     exit 1
 fi
+
+# Check tool version
+REQUIRED_VERSION="1.0.0"  # Replace with minimum required version
+CURRENT_VERSION=$(dotnet-aicommitmessage --version 2>/dev/null)
+if [ $? -ne 0 ] || ! echo "$CURRENT_VERSION" | grep -q "^$REQUIRED_VERSION"; then
+    echo "Error: dotnet-aicommitmessage version $REQUIRED_VERSION or higher is required" >&2
+    echo "Current version: $CURRENT_VERSION" >&2
+    exit 1
+fi

Committable suggestion skipped: line range outside the PR's diff.


# Ensure the commit message file is provided
if [ -z "$1" ]; then
echo "Error: Commit message file not provided" >&2
exit 1
fi

COMMIT_MSG_FILE="$1"

# Check if the commit message file exists
if [ ! -f "$COMMIT_MSG_FILE" ]; then
echo "Error: Commit message file '$COMMIT_MSG_FILE' not found" >&2
exit 1
fi
Comment on lines +18 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Add file permission checks.

While the file existence check is good, it's important to verify read/write permissions to prevent failures later.

 if [ ! -f "$COMMIT_MSG_FILE" ]; then
     echo "Error: Commit message file '$COMMIT_MSG_FILE' not found" >&2
     exit 1
 fi
+
+# Check file permissions
+if [ ! -r "$COMMIT_MSG_FILE" ] || [ ! -w "$COMMIT_MSG_FILE" ]; then
+    echo "Error: Insufficient permissions for '$COMMIT_MSG_FILE'" >&2
+    exit 1
+fi
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Ensure the commit message file is provided
if [ -z "$1" ]; then
echo "Error: Commit message file not provided" >&2
exit 1
fi
COMMIT_MSG_FILE="$1"
# Check if the commit message file exists
if [ ! -f "$COMMIT_MSG_FILE" ]; then
echo "Error: Commit message file '$COMMIT_MSG_FILE' not found" >&2
exit 1
fi
# Ensure the commit message file is provided
if [ -z "$1" ]; then
echo "Error: Commit message file not provided" >&2
exit 1
fi
COMMIT_MSG_FILE="$1"
# Check if the commit message file exists
if [ ! -f "$COMMIT_MSG_FILE" ]; then
echo "Error: Commit message file '$COMMIT_MSG_FILE' not found" >&2
exit 1
fi
# Check file permissions
if [ ! -r "$COMMIT_MSG_FILE" ] || [ ! -w "$COMMIT_MSG_FILE" ]; then
echo "Error: Insufficient permissions for '$COMMIT_MSG_FILE'" >&2
exit 1
fi


# Read the current commit message
CURRENT_MESSAGE=$(cat "$COMMIT_MSG_FILE")

# Backup the commit message file
cp "$COMMIT_MSG_FILE" "${COMMIT_MSG_FILE}.bak"

# Generate the AI commit message
if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" 2>/dev/null); then
echo "Error: Failed to generate AI commit message. Using original message." >&2
exit 0
fi
Comment on lines +32 to +42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Enhance error handling for the AI message generation.

The current implementation could be more robust by explicitly checking the tool's exit code and providing more detailed error information.

-if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" 2>/dev/null); then
+# Capture both output and exit code
+AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" 2>&1)
+EXIT_CODE=$?
+if [ $EXIT_CODE -ne 0 ]; then
     echo "Error: Failed to generate AI commit message. Using original message." >&2
+    echo "Details: $AI_MESSAGE" >&2
     exit 0
 fi
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Read the current commit message
CURRENT_MESSAGE=$(cat "$COMMIT_MSG_FILE")
# Backup the commit message file
cp "$COMMIT_MSG_FILE" "${COMMIT_MSG_FILE}.bak"
# Generate the AI commit message
if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" 2>/dev/null); then
echo "Error: Failed to generate AI commit message. Using original message." >&2
exit 0
fi
# Read the current commit message
CURRENT_MESSAGE=$(cat "$COMMIT_MSG_FILE")
# Backup the commit message file
cp "$COMMIT_MSG_FILE" "${COMMIT_MSG_FILE}.bak"
# Generate the AI commit message
# Capture both output and exit code
AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "Error: Failed to generate AI commit message. Using original message." >&2
echo "Details: $AI_MESSAGE" >&2
exit 0
fi


# Check if the generated message is empty
if [ -z "$AI_MESSAGE" ] || echo "$AI_MESSAGE" | grep -q '^[[:space:]]*$'; then
echo "Error: Generated commit message is empty." >&2
exit 1
fi

# Write the new commit message back to the file
if ! echo "$AI_MESSAGE" > "$COMMIT_MSG_FILE" 2>/dev/null; then
echo "Error: Failed to write new commit message" >&2
cp "${COMMIT_MSG_FILE}.bak" "$COMMIT_MSG_FILE"
rm "${COMMIT_MSG_FILE}.bak"
exit 1
fi

# Remove the backup file
rm "${COMMIT_MSG_FILE}.bak"
exit 0
Comment on lines +1 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’‘ Codebase verification

Git hooks setup process needs to be implemented

The repository lacks an automated Git hooks installation process. Neither the installation scripts (install.sh, install.ps1) nor the README contain any mention of Git hooks setup.

  • The .githooks/prepare-commit-msg hook won't be automatically installed for developers.
  • No documentation exists about manual hook installation.
  • No automated setup process found for configuring Git hooks.

Consider implementing one of these solutions:

  1. Add git config core.hooksPath .githooks to both installation scripts
  2. Add documentation in README.md about manual hook installation
  3. Use a Git hooks management tool like Husky
πŸ”— Analysis chain

Verify Git hook installation process.

The script is placed in .githooks/, but we should verify if there's a process to automatically install/update this hook for all developers.

Consider adding one of these options to automate hook installation:

  1. Add hook installation to the project's setup/install script
  2. Use git config core.hooksPath .githooks in the project setup
  3. Document the manual installation process in the README
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if there's any automated hook installation process
rg -l "prepare-commit-msg" --type sh

Length of output: 36


Script:

#!/bin/bash
# Let's check for any setup scripts, package.json, or README that might mention git hooks setup
fd -t f "^(package\.json|README\.md|setup.*|install.*|init\..*|configure\..*|husky\..*|\.husky.*|\.git.*)"

# Also check for any files mentioning git hooks configuration
rg -l "git.*hooks?" --type-not lock

Length of output: 179


Script:

#!/bin/bash
# Let's check the content of install scripts and README for git hooks setup
cat install.sh
cat install.ps1
cat README.md

Length of output: 2764

Loading