Skip to content

Instantly share code, notes, and snippets.

@sarthaksarthak9
Last active March 21, 2025 19:56
Show Gist options
  • Select an option

  • Save sarthaksarthak9/7336aa5597ee708dda1d876431d5ddb8 to your computer and use it in GitHub Desktop.

Select an option

Save sarthaksarthak9/7336aa5597ee708dda1d876431d5ddb8 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e

# This script performs a 3-way merge between:
# 1. Original Kubebuilder template (base)
# 2. Current project state (ours)
# 3. Latest Kubebuilder template (theirs)

# Set up variables
CURRENT_VERSION="$1"
TARGET_VERSION="$2"
DOMAIN="$3"
REPO="$4"

echo "Current Kubebuilder version: $CURRENT_VERSION"
echo "Target Kubebuilder version: $TARGET_VERSION"
echo "Project domain: $DOMAIN"
echo "Project repo: $REPO"

# Create temporary directories
ORIGINAL_TEMPLATE="/tmp/kb-original"
LATEST_TEMPLATE="/tmp/kb-latest"
CURRENT_PROJECT="$(pwd)"

mkdir -p "$ORIGINAL_TEMPLATE"
mkdir -p "$LATEST_TEMPLATE"

# Create original template project
pushd "$ORIGINAL_TEMPLATE" > /dev/null
kubebuilder init --domain "$DOMAIN" --repo "$REPO" --plugins="$CURRENT_VERSION"
popd > /dev/null

# Create latest template project
pushd "$LATEST_TEMPLATE" > /dev/null
kubebuilder init --domain "$DOMAIN" --repo "$REPO" --plugins="$TARGET_VERSION"
popd > /dev/null

# List of files to check for merging
FILES=(
  "Makefile"
  "PROJECT"
  "go.mod"
  ".github/workflows/ci.yaml"
  "hack/boilerplate.go.txt"
)

# Perform 3-way merge for each file
for file in "${FILES[@]}"; do
  echo "Processing file: $file"
  
  # Check if file exists in all three locations
  if [[ -f "$ORIGINAL_TEMPLATE/$file" && -f "$LATEST_TEMPLATE/$file" && -f "$CURRENT_PROJECT/$file" ]]; then
    # Create directory for the file if needed
    mkdir -p "$(dirname "$CURRENT_PROJECT/$file.merged")"
    
    # Perform 3-way merge
    git merge-file -p \
      "$CURRENT_PROJECT/$file" \
      "$ORIGINAL_TEMPLATE/$file" \
      "$LATEST_TEMPLATE/$file" > "$CURRENT_PROJECT/$file.merged"
      
    # Check if there were conflicts
    if [ $? -eq 0 ]; then
      echo "Successfully merged $file without conflicts"
      mv "$CURRENT_PROJECT/$file.merged" "$CURRENT_PROJECT/$file"
    
    else
     
     echo "Conflicts detected in $file"
      # Keep the merged file with conflict markers
      mv "$CURRENT_PROJECT/$file.merged" "$CURRENT_PROJECT/$file"
      echo "$file" >> conflict_files.txt
    
    fi
  
  elif [[ ! -f "$ORIGINAL_TEMPLATE/$file" && -f "$LATEST_TEMPLATE/$file" && ! -f "$CURRENT_PROJECT/$file" ]]; then
    # New file in latest template that doesn't exist in original or current project
    echo "Adding new file from template: $file"
    mkdir -p "$(dirname "$CURRENT_PROJECT/$file")"
    cp "$LATEST_TEMPLATE/$file" "$CURRENT_PROJECT/$file"
  
  elif [[ -f "$ORIGINAL_TEMPLATE/$file" && ! -f "$LATEST_TEMPLATE/$file" && -f "$CURRENT_PROJECT/$file" ]]; then
    # File removed in latest template
    echo "File removed in latest template: $file"
    # For now, we keep the user's file but notify
    echo "$file" >> removed_files.txt
  
  fi

done

echo "Merge process completed."

# Summary
if [ -f conflict_files.txt ]; then
  echo "Conflicts were detected in the following files:"
  cat conflict_files.txt
  echo "Please resolve these conflicts manually."
fi

if [ -f removed_files.txt ]; then
  echo "The following files were removed in the latest template but kept in your project:"
  cat removed_files.txt
  echo "You may want to review these files to see if they should be removed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment