refactor: Enhance cleanup script for my-aliases.zsh tracking with improved color output and confirmation prompts

This commit is contained in:
Peter Wood
2025-05-28 16:29:38 -04:00
parent 4398aa7bf8
commit d75c7c5a94

View File

@@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
# filepath: cleanup-alias-tracking.sh
# Cleanup script for removing my-aliases.zsh from git tracking # Cleanup script for removing my-aliases.zsh from git tracking
# This script handles the repository cleanup needed after the alias file # This script handles the repository cleanup needed after the alias file
@@ -6,25 +7,31 @@
set -e set -e
# Colors for output # Colors for output - use printf instead of echo -e for better compatibility
GREEN='\033[0;32m' print_color() {
YELLOW='\033[1;33m' local color="$1"
RED='\033[0;31m' local message="$2"
BLUE='\033[0;34m' case "$color" in
NC='\033[0m' # No Color "green") printf '\033[0;32m%s\033[0m\n' "$message" ;;
"yellow") printf '\033[1;33m%s\033[0m\n' "$message" ;;
"red") printf '\033[0;31m%s\033[0m\n' "$message" ;;
"blue") printf '\033[0;34m%s\033[0m\n' "$message" ;;
*) printf '%s\n' "$message" ;;
esac
}
# Script directory and repository root # Script directory and repository root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$SCRIPT_DIR" REPO_ROOT="$SCRIPT_DIR"
echo -e "${BLUE}=== Alias File Tracking Cleanup Script ===${NC}" print_color "blue" "=== Alias File Tracking Cleanup Script ==="
echo -e "${YELLOW}This script will remove my-aliases.zsh from git tracking${NC}" print_color "yellow" "This script will remove my-aliases.zsh from git tracking"
echo -e "${YELLOW}and update .gitignore to prevent future conflicts.${NC}" print_color "yellow" "and update .gitignore to prevent future conflicts."
echo "" echo ""
# Verify we're in a git repository # Verify we're in a git repository
if [ ! -d "$REPO_ROOT/.git" ]; then if [ ! -d "$REPO_ROOT/.git" ]; then
echo -e "${RED}Error: Not in a git repository${NC}" print_color "red" "Error: Not in a git repository"
echo "Please run this script from the root of your shell repository" echo "Please run this script from the root of your shell repository"
exit 1 exit 1
fi fi
@@ -33,30 +40,30 @@ fi
cd "$REPO_ROOT" cd "$REPO_ROOT"
# Check current git status # Check current git status
echo -e "${BLUE}=== Current Git Status ===${NC}" print_color "blue" "=== Current Git Status ==="
git status --porcelain git status --porcelain
# Check if my-aliases.zsh is currently tracked # Check if my-aliases.zsh is currently tracked
if git ls-files --error-unmatch dotfiles/my-aliases.zsh >/dev/null 2>&1; then if git ls-files --error-unmatch dotfiles/my-aliases.zsh >/dev/null 2>&1; then
echo -e "${YELLOW}my-aliases.zsh is currently tracked by git${NC}" print_color "yellow" "my-aliases.zsh is currently tracked by git"
ALIASES_TRACKED=true ALIASES_TRACKED=true
else else
echo -e "${GREEN}my-aliases.zsh is already untracked${NC}" print_color "green" "my-aliases.zsh is already untracked"
ALIASES_TRACKED=false ALIASES_TRACKED=false
fi fi
# Check if .gitignore already has the entry # Check if .gitignore already has the entry
if grep -q "dotfiles/my-aliases.zsh" .gitignore 2>/dev/null; then if grep -q "dotfiles/my-aliases.zsh" .gitignore 2>/dev/null; then
echo -e "${GREEN}.gitignore already contains entry for my-aliases.zsh${NC}" print_color "green" ".gitignore already contains entry for my-aliases.zsh"
GITIGNORE_UPDATED=true GITIGNORE_UPDATED=true
else else
echo -e "${YELLOW}.gitignore needs to be updated${NC}" print_color "yellow" ".gitignore needs to be updated"
GITIGNORE_UPDATED=false GITIGNORE_UPDATED=false
fi fi
# Prompt for confirmation # Prompt for confirmation
echo "" echo ""
echo -e "${YELLOW}Actions that will be performed:${NC}" print_color "yellow" "Actions that will be performed:"
if [ "$ALIASES_TRACKED" = true ]; then if [ "$ALIASES_TRACKED" = true ]; then
echo " 1. Remove dotfiles/my-aliases.zsh from git tracking" echo " 1. Remove dotfiles/my-aliases.zsh from git tracking"
fi fi
@@ -73,50 +80,50 @@ echo ""
read -p "Continue? (y/N): " -n 1 -r read -p "Continue? (y/N): " -n 1 -r
echo echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Cleanup cancelled${NC}" print_color "yellow" "Cleanup cancelled"
exit 0 exit 0
fi fi
# Step 1: Remove from git tracking if needed # Step 1: Remove from git tracking if needed
if [ "$ALIASES_TRACKED" = true ]; then if [ "$ALIASES_TRACKED" = true ]; then
echo -e "${BLUE}=== Removing my-aliases.zsh from git tracking ===${NC}" print_color "blue" "=== Removing my-aliases.zsh from git tracking ==="
# Check if file exists and remove from tracking # Check if file exists and remove from tracking
if [ -f "dotfiles/my-aliases.zsh" ]; then if [ -f "dotfiles/my-aliases.zsh" ]; then
git rm --cached dotfiles/my-aliases.zsh git rm --cached dotfiles/my-aliases.zsh
echo -e "${GREEN}✓ Removed dotfiles/my-aliases.zsh from git tracking${NC}" print_color "green" "✓ Removed dotfiles/my-aliases.zsh from git tracking"
else else
# File doesn't exist but is tracked - remove from index anyway # File doesn't exist but is tracked - remove from index anyway
git rm --cached dotfiles/my-aliases.zsh 2>/dev/null || true git rm --cached dotfiles/my-aliases.zsh 2>/dev/null || true
echo -e "${GREEN}✓ Removed non-existent dotfiles/my-aliases.zsh from git index${NC}" print_color "green" "✓ Removed non-existent dotfiles/my-aliases.zsh from git index"
fi fi
fi fi
# Step 2: Update .gitignore if needed # Step 2: Update .gitignore if needed
if [ "$GITIGNORE_UPDATED" = false ]; then if [ "$GITIGNORE_UPDATED" = false ]; then
echo -e "${BLUE}=== Updating .gitignore ===${NC}" print_color "blue" "=== Updating .gitignore ==="
# Check if the "Generated dotfiles" section already exists # Check if the "Generated dotfiles" section already exists
if grep -q "Generated dotfiles" .gitignore 2>/dev/null; then if grep -q "Generated dotfiles" .gitignore 2>/dev/null; then
# Add to existing section # Add to existing section
if ! grep -q "dotfiles/my-aliases.zsh" .gitignore; then if ! grep -q "dotfiles/my-aliases.zsh" .gitignore; then
sed -i '/# Generated dotfiles/a dotfiles/my-aliases.zsh' .gitignore sed -i '/# Generated dotfiles/a dotfiles/my-aliases.zsh' .gitignore
echo -e "${GREEN}✓ Added entry to existing Generated dotfiles section${NC}" print_color "green" "✓ Added entry to existing Generated dotfiles section"
fi fi
else else
# Create new section # Create new section
echo "" >> .gitignore echo "" >> .gitignore
echo "# Generated dotfiles - these are created dynamically by bootstrap process" >> .gitignore echo "# Generated dotfiles - these are created dynamically by bootstrap process" >> .gitignore
echo "dotfiles/my-aliases.zsh" >> .gitignore echo "dotfiles/my-aliases.zsh" >> .gitignore
echo -e "${GREEN}✓ Added new Generated dotfiles section to .gitignore${NC}" print_color "green" "✓ Added new Generated dotfiles section to .gitignore"
fi fi
fi fi
# Step 3: Check if we have changes to commit # Step 3: Check if we have changes to commit
if git diff --cached --quiet && git diff --quiet; then if git diff --cached --quiet && git diff --quiet; then
echo -e "${GREEN}=== No changes to commit - cleanup already complete ===${NC}" print_color "green" "=== No changes to commit - cleanup already complete ==="
else else
echo -e "${BLUE}=== Committing changes ===${NC}" print_color "blue" "=== Committing changes ==="
# Add .gitignore changes if any # Add .gitignore changes if any
git add .gitignore git add .gitignore
@@ -130,47 +137,47 @@ else
# Commit the changes # Commit the changes
git commit -m "$COMMIT_MSG" git commit -m "$COMMIT_MSG"
echo -e "${GREEN}✓ Changes committed successfully${NC}" print_color "green" "✓ Changes committed successfully"
fi fi
# Step 4: Verify the cleanup # Step 4: Verify the cleanup
echo -e "${BLUE}=== Verification ===${NC}" print_color "blue" "=== Verification ==="
# Check that file is no longer tracked # Check that file is no longer tracked
if ! git ls-files --error-unmatch dotfiles/my-aliases.zsh >/dev/null 2>&1; then if ! git ls-files --error-unmatch dotfiles/my-aliases.zsh >/dev/null 2>&1; then
echo -e "${GREEN}✓ dotfiles/my-aliases.zsh is no longer tracked${NC}" print_color "green" "✓ dotfiles/my-aliases.zsh is no longer tracked"
else else
echo -e "${RED}✗ dotfiles/my-aliases.zsh is still tracked${NC}" print_color "red" "✗ dotfiles/my-aliases.zsh is still tracked"
fi fi
# Check that .gitignore contains the entry # Check that .gitignore contains the entry
if grep -q "dotfiles/my-aliases.zsh" .gitignore; then if grep -q "dotfiles/my-aliases.zsh" .gitignore; then
echo -e "${GREEN}✓ .gitignore contains entry for dotfiles/my-aliases.zsh${NC}" print_color "green" "✓ .gitignore contains entry for dotfiles/my-aliases.zsh"
else else
echo -e "${RED}✗ .gitignore missing entry for dotfiles/my-aliases.zsh${NC}" print_color "red" "✗ .gitignore missing entry for dotfiles/my-aliases.zsh"
fi fi
# Show final git status # Show final git status
echo -e "${BLUE}=== Final Git Status ===${NC}" print_color "blue" "=== Final Git Status ==="
git status --porcelain git status --porcelain
# Check if we need to push # Check if we need to push
if git log --oneline origin/main..HEAD 2>/dev/null | grep -q "Remove my-aliases.zsh"; then if git log --oneline origin/main..HEAD 2>/dev/null | grep -q "Remove my-aliases.zsh"; then
echo "" echo ""
echo -e "${YELLOW}=== Push Required ===${NC}" print_color "yellow" "=== Push Required ==="
echo -e "${YELLOW}You have local commits that need to be pushed to the remote repository.${NC}" print_color "yellow" "You have local commits that need to be pushed to the remote repository."
echo "Run: ${BLUE}git push origin main${NC}" printf "Run: \033[0;34mgit push origin main\033[0m\n"
elif ! git remote >/dev/null 2>&1; then elif ! git remote >/dev/null 2>&1; then
echo -e "${YELLOW}No remote repository configured${NC}" print_color "yellow" "No remote repository configured"
else else
echo -e "${GREEN}Repository is up to date with remote${NC}" print_color "green" "Repository is up to date with remote"
fi fi
echo "" echo ""
echo -e "${GREEN}=== Cleanup Complete! ===${NC}" print_color "green" "=== Cleanup Complete! ==="
echo -e "${GREEN}The my-aliases.zsh file is now properly configured as a generated file.${NC}" print_color "green" "The my-aliases.zsh file is now properly configured as a generated file."
echo "" echo ""
echo -e "${YELLOW}Next steps:${NC}" print_color "yellow" "Next steps:"
echo "1. Push changes if needed: ${BLUE}git push origin main${NC}" printf "1. Push changes if needed: \033[0;34mgit push origin main\033[0m\n"
echo "2. Run bootstrap/setup on this system to regenerate aliases" echo "2. Run bootstrap/setup on this system to regenerate aliases"
echo "3. The aliases file will now be created dynamically without git conflicts" echo "3. The aliases file will now be created dynamically without git conflicts"