#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}Running pre-push checks...${NC}"

# Get current branch
current_branch=$(git rev-parse --abbrev-ref HEAD)

# Read push information from stdin
while read local_ref local_sha remote_ref remote_sha; do
    # Extract remote branch name
    remote_branch=$(echo "$remote_ref" | sed 's/refs\/heads\///')
    
    echo -e "${YELLOW}Pushing '$current_branch' to '$remote_branch'${NC}"
    
    # Check if trying to push to a different branch than current
    if [[ "$current_branch" != "$remote_branch" ]]; then
        echo -e "${YELLOW}⚠️  Note: You are on branch '$current_branch' but pushing to '$remote_branch'${NC}"
    fi
    
    # Warn about force pushes
    if [[ $(git log --oneline "$remote_sha..$local_sha" 2>/dev/null | wc -l) -eq 0 ]] && [[ "$local_sha" != "0000000000000000000000000000000000000000" ]]; then
        echo -e "${YELLOW}⚠️  Warning: This appears to be a force push or rewrite${NC}"
        echo -e "${YELLOW}Make sure this is intentional and won't affect other developers${NC}"
    fi
done

echo -e "${GREEN}✅ Pre-push checks passed!${NC}"
exit 0 