#!/bin/bash # CTO AI Delegation - Git-Native Auto-Merge Setup # Uses git hooks for automatic merging to INTEGRATION-WIP set -e echo "๐Ÿ”ง CTO AI Delegation - Git-Native Auto-Merge Setup" echo "=================================================" echo "Setting up automatic merging to INTEGRATION-WIP using git hooks" # Validate we're in a git repository if [ ! -d ".git" ]; then echo "โŒ Error: Not in a git repository" echo "๐Ÿ”ง Run this from your project root directory" exit 1 fi # Check if INTEGRATION-WIP branch exists if ! git show-ref --quiet refs/heads/INTEGRATION-WIP; then echo "โŒ Error: INTEGRATION-WIP branch not found!" echo "๐Ÿ”ง Run the bootstrap process first from docs/AGENT-LLM.MD" exit 1 fi # Create hooks directory if it doesn't exist mkdir -p .git/hooks echo "๐Ÿ”ง Setting up git hooks for automatic integration..." # Create post-commit hook for auto-merge to INTEGRATION-WIP cat > .git/hooks/post-commit << 'EOF' #!/bin/bash # CTO AI Delegation - Auto-merge to INTEGRATION-WIP # Runs after every commit on feature branches or bootstrap CURRENT_BRANCH=$(git branch --show-current) # Only auto-merge from feature branches and bootstrap if [[ "$CURRENT_BRANCH" == feature/* ]] || [[ "$CURRENT_BRANCH" == "bootstrap" ]]; then echo "๐Ÿ”„ Auto-merging $CURRENT_BRANCH to INTEGRATION-WIP..." # Save current branch ORIGINAL_BRANCH="$CURRENT_BRANCH" # Switch to INTEGRATION-WIP and merge if git checkout INTEGRATION-WIP 2>/dev/null; then if git merge "$ORIGINAL_BRANCH" --no-ff -m "๐Ÿ”„ Auto-Integration: $ORIGINAL_BRANCH โ†’ INTEGRATION-WIP Branch: $ORIGINAL_BRANCH Commit: $(git rev-parse HEAD) Timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC') ๐Ÿค– CTO AI Delegation Framework - Git-Native Auto-Merge"; then echo "โœ… Successfully auto-merged $ORIGINAL_BRANCH to INTEGRATION-WIP" # Switch back to original branch git checkout "$ORIGINAL_BRANCH" else echo "โŒ Auto-merge failed due to conflicts" echo "๐Ÿšจ Manual resolution required:" echo " 1. Resolve conflicts in INTEGRATION-WIP" echo " 2. Complete the merge manually" echo " 3. Return to your feature branch: git checkout $ORIGINAL_BRANCH" # Abort the merge and return to original branch git merge --abort 2>/dev/null || true git checkout "$ORIGINAL_BRANCH" 2>/dev/null || true fi else echo "โŒ Could not switch to INTEGRATION-WIP branch" fi fi EOF # Make post-commit hook executable chmod +x .git/hooks/post-commit # Create post-merge hook to handle pulled changes cat > .git/hooks/post-merge << 'EOF' #!/bin/bash # CTO AI Delegation - Post-merge auto-integration # Handles integration after pull/merge operations CURRENT_BRANCH=$(git branch --show-current) # Only process feature branches and bootstrap if [[ "$CURRENT_BRANCH" == feature/* ]] || [[ "$CURRENT_BRANCH" == "bootstrap" ]]; then # Check if we're not already on INTEGRATION-WIP if [[ "$CURRENT_BRANCH" != "INTEGRATION-WIP" ]]; then echo "๐Ÿ”„ Post-merge: Updating INTEGRATION-WIP with $CURRENT_BRANCH changes..." # Save current branch ORIGINAL_BRANCH="$CURRENT_BRANCH" # Auto-merge to INTEGRATION-WIP if git checkout INTEGRATION-WIP 2>/dev/null; then if git merge "$ORIGINAL_BRANCH" --no-ff -m "๐Ÿ”„ Post-Merge Integration: $ORIGINAL_BRANCH โ†’ INTEGRATION-WIP Branch: $ORIGINAL_BRANCH Trigger: Post-merge hook Timestamp: $(date -u '+%Y-%m-%d %H:%M:%S UTC') ๐Ÿค– CTO AI Delegation Framework - Git-Native Auto-Merge"; then echo "โœ… Successfully updated INTEGRATION-WIP with $ORIGINAL_BRANCH changes" else echo "โš ๏ธ Integration conflicts detected - manual resolution may be needed" git merge --abort 2>/dev/null || true fi # Return to original branch git checkout "$ORIGINAL_BRANCH" 2>/dev/null || true fi fi fi EOF # Make post-merge hook executable chmod +x .git/hooks/post-merge # Create a push hook to ensure INTEGRATION-WIP is also pushed cat > .git/hooks/pre-push << 'EOF' #!/bin/bash # CTO AI Delegation - Ensure INTEGRATION-WIP is pushed # Automatically push INTEGRATION-WIP when feature branches are pushed CURRENT_BRANCH=$(git branch --show-current) # If pushing a feature branch or bootstrap, also push INTEGRATION-WIP if [[ "$CURRENT_BRANCH" == feature/* ]] || [[ "$CURRENT_BRANCH" == "bootstrap" ]]; then # Check if INTEGRATION-WIP has unpushed commits if git diff --quiet origin/INTEGRATION-WIP INTEGRATION-WIP 2>/dev/null; then echo "๐Ÿ“‹ INTEGRATION-WIP is up to date" else echo "๐Ÿ”„ Auto-pushing updated INTEGRATION-WIP..." git push origin INTEGRATION-WIP || { echo "โš ๏ธ Could not push INTEGRATION-WIP - may need manual push later" } fi fi EOF # Make pre-push hook executable chmod +x .git/hooks/pre-push echo "" echo "โœ… Git-native auto-merge setup complete!" echo "" echo "๐Ÿ“‹ How it works:" echo " 1. Every commit on feature branches or bootstrap automatically merges to INTEGRATION-WIP" echo " 2. Conflicts are handled gracefully with clear error messages" echo " 3. INTEGRATION-WIP is automatically pushed when you push feature branches" echo " 4. No external workflows or runners required - pure git!" echo "" echo "๐Ÿงช Test the setup:" echo " 1. Create a test commit on a feature branch" echo " 2. Check INTEGRATION-WIP branch for the auto-merge" echo " 3. Push your feature branch and verify INTEGRATION-WIP is also pushed" echo "" echo "๐ŸŽฏ Git-native integration automation ready!" # Update AGENT-LLM.MD to reflect git-native approach if grep -q "INTEGRATION AUTOMATION" docs/AGENT-LLM.MD; then # Replace existing automation section sed -i '/## INTEGRATION AUTOMATION/,/^##/{/^##/!d;}' docs/AGENT-LLM.MD sed -i '/## INTEGRATION AUTOMATION/d' docs/AGENT-LLM.MD fi # Add updated integration info cat >> docs/AGENT-LLM.MD << 'EOF' ## INTEGRATION AUTOMATION (GIT-NATIVE) **Automatic process - no action required by AI agents:** - Every commit on feature branches automatically merges to INTEGRATION-WIP - Uses git hooks (no external workflows required) - Conflicts handled gracefully with clear error messages - INTEGRATION-WIP automatically pushed when feature branches are pushed - Pure git-native solution - no Gitea Actions needed EOF echo "" echo "โœ… Updated docs/AGENT-LLM.MD with git-native integration info" echo "" echo "๐Ÿš€ Ready for automatic git-native integration!"