feat: enhance pre-push hook with changelog preview and user confirmation

- Updated the pre-push hook to display a preview of the changelog and the new version before proceeding with the push.
- Added color-coded output for better visibility and user experience.
- Implemented user confirmation to abort the push if the changelog preview is not approved.
- Cleaned up temporary files created during the changelog update process.
This commit is contained in:
anonpenguin23 2025-11-03 07:11:47 +02:00
parent 69fd6e32f1
commit 6e59b17c6a
2 changed files with 57 additions and 6 deletions

View File

@ -1,29 +1,73 @@
#!/bin/bash
# Colors for output
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NOCOLOR='\033[0m'
# Get the directory where this hook is located
HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$HOOK_DIR/.." && pwd)"
CHANGELOG_SCRIPT="$REPO_ROOT/scripts/update_changelog.sh"
PREVIEW_FILE="$REPO_ROOT/.changelog_preview.tmp"
VERSION_FILE="$REPO_ROOT/.changelog_version.tmp"
# Update changelog before push
if [ -f "$CHANGELOG_SCRIPT" ]; then
echo -e "\nUpdating changelog..."
echo -e "\n${CYAN}Updating changelog...${NOCOLOR}"
bash "$CHANGELOG_SCRIPT"
changelog_status=$?
if [ $changelog_status -ne 0 ]; then
echo "Push aborted: changelog update failed."
echo -e "${RED}Push aborted: changelog update failed.${NOCOLOR}"
exit 1
fi
# Show preview if changelog was updated
if [ -f "$PREVIEW_FILE" ] && [ -f "$VERSION_FILE" ]; then
NEW_VERSION=$(cat "$VERSION_FILE")
PREVIEW_CONTENT=$(cat "$PREVIEW_FILE")
echo ""
echo -e "${BLUE}========================================================================${NOCOLOR}"
echo -e "${CYAN} CHANGELOG PREVIEW${NOCOLOR}"
echo -e "${BLUE}========================================================================${NOCOLOR}"
echo ""
echo -e "${GREEN}New Version: ${YELLOW}$NEW_VERSION${NOCOLOR}"
echo ""
echo -e "${CYAN}Changelog Entry:${NOCOLOR}"
echo -e "${BLUE}────────────────────────────────────────────────────────────────────────${NOCOLOR}"
echo -e "$PREVIEW_CONTENT"
echo -e "${BLUE}────────────────────────────────────────────────────────────────────────${NOCOLOR}"
echo ""
echo -e "${YELLOW}Do you want to proceed with the push? (yes/no):${NOCOLOR} "
read -r confirmation
if [ "$confirmation" != "yes" ]; then
echo -e "${RED}Push aborted by user.${NOCOLOR}"
echo -e "${YELLOW}To revert changes, run:${NOCOLOR}"
echo -e " git checkout CHANGELOG.md Makefile"
# Clean up temp files
rm -f "$PREVIEW_FILE" "$VERSION_FILE"
exit 1
fi
echo -e "${GREEN}Proceeding with push...${NOCOLOR}"
# Clean up temp files
rm -f "$PREVIEW_FILE" "$VERSION_FILE"
fi
else
echo "Warning: changelog update script not found at $CHANGELOG_SCRIPT"
echo -e "${YELLOW}Warning: changelog update script not found at $CHANGELOG_SCRIPT${NOCOLOR}"
fi
echo -e "\nRunning tests:"
echo -e "\n${CYAN}Running tests...${NOCOLOR}"
go test ./... # Runs all tests in your repo
status=$?
if [ $status -ne 0 ]; then
echo "Push aborted: some tests failed."
echo -e "${RED}Push aborted: some tests failed.${NOCOLOR}"
exit 1
else
echo "All tests passed. Proceeding with push."
echo -e "${GREEN}All tests passed. Proceeding with push.${NOCOLOR}"
fi

View File

@ -85,6 +85,8 @@ ${UNPUSHED_DIFF}"
# Check if there are any changes
if [ -z "$(echo "$UNSTAGED_DIFF$STAGED_DIFF$UNPUSHED_DIFF" | tr -d '[:space:]')" ]; then
log "No changes detected (unstaged, staged, or unpushed). Skipping changelog update."
# Clean up any old preview files
rm -f "$REPO_ROOT/.changelog_preview.tmp" "$REPO_ROOT/.changelog_version.tmp"
exit 0
fi
@ -262,6 +264,11 @@ fi
CHANGELOG_ENTRY+="
"
# Save preview to temp file for pre-push hook
PREVIEW_FILE="$REPO_ROOT/.changelog_preview.tmp"
echo "$CHANGELOG_ENTRY" > "$PREVIEW_FILE"
echo "$NEW_VERSION" > "$REPO_ROOT/.changelog_version.tmp"
# Insert after [Unreleased] section using awk (more portable)
# Find the line number after [Unreleased] section (after the "### Fixed" line)
INSERT_LINE=$(awk '/^## \[Unreleased\]/{found=1} found && /^### Fixed$/{print NR+1; exit}' "$CHANGELOG_FILE")