|
#!/usr/bin/env bash |
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
|
|
|
# Define colors |
|
GREEN=$(tput setaf 2) |
|
RED=$(tput setaf 1) |
|
YELLOW=$(tput setaf 3) |
|
BLUE=$(tput setaf 4) |
|
BOLD=$(tput bold) |
|
NC=$(tput sgr0) # No Color |
|
|
|
# Default values |
|
target_branch="master" |
|
patch_file="pr.patch" |
|
|
|
# Print usage |
|
usage() { |
|
echo "Usage: $0 -s <source-branch> [-t <target-branch>] [-p <patch-file>]" |
|
echo " -s: Source branch (required)" |
|
echo " -t: Target branch (default: master)" |
|
echo " -p: Patch file (default: pr.patch)" |
|
exit 1 |
|
} |
|
|
|
# Parse command line arguments |
|
while getopts "s:t:p:h" opt; do |
|
case $opt in |
|
s) source_branch="$OPTARG" ;; |
|
t) target_branch="$OPTARG" ;; |
|
p) patch_file="$OPTARG" ;; |
|
h) usage ;; |
|
?) usage ;; |
|
esac |
|
done |
|
|
|
# Check if source branch was provided |
|
if [ -z "$source_branch" ]; then |
|
echo "${RED}Error: Source branch (-s) is required${NC}" |
|
usage |
|
fi |
|
|
|
# Find the remote tracking branch for target branch |
|
remote_master=$(git rev-parse --abbrev-ref ${target_branch}@{upstream}) |
|
if [ -z "$remote_master" ]; then |
|
echo "${RED}Error: Could not determine remote tracking branch for $target_branch${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Extract the remote name (e.g., "origin" from "origin/master") |
|
remote_name=${remote_master%%/*} |
|
if [ -z "$remote_name" ]; then |
|
echo "${RED}Error: Could not determine remote name${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Fetch from the remote |
|
echo "${BLUE}Fetching from $remote_name...${NC}" |
|
if ! git fetch "$remote_name"; then |
|
echo "${RED}Error: Failed to fetch from $remote_name${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Step 1: Checkout the source branch |
|
echo "${BLUE}Checking out source branch: $source_branch${NC}" |
|
if ! git checkout "$source_branch"; then |
|
echo "${RED}Error: Failed to checkout branch $source_branch${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Step 2: Create patch file using merge-base |
|
echo "${BLUE}Creating patch file from $source_branch against merge-base with $target_branch${NC}" |
|
if ! git format-patch $(git merge-base HEAD $remote_master)..HEAD --stdout > "$patch_file"; then |
|
echo "${RED}Error: Failed to create patch file${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Step 3: Run rename-paths.sh |
|
echo "${BLUE}Running rename-paths.sh on $patch_file${NC}" |
|
if ! $SCRIPT_DIR/rename-paths.sh "$patch_file"; then |
|
echo "${RED}Error: Failed to process patch paths${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Step 4: Create and checkout new branch |
|
new_branch="monorepo-conversion/${source_branch}" |
|
echo "${BLUE}Creating new branch $new_branch from $remote_master${NC}" |
|
if ! git checkout -b "$new_branch" "$remote_master"; then |
|
echo "${RED}Error: Failed to create new branch${NC}" |
|
exit 1 |
|
fi |
|
|
|
# Step 5: Apply the patch |
|
echo "Applying patch..." |
|
if git apply --reject --whitespace=fix "$patch_file" 2> /tmp/patch_error; then |
|
echo "${GREEN}Patch applied successfully!${NC}" |
|
rm -f "$patch_file" |
|
else |
|
echo "${YELLOW}Warning: There were some issues applying the patch" |
|
echo "Check the following files for rejected hunks:" |
|
find . -name "*.rej" -type f |
|
echo "Original error output:" |
|
cat /tmp/patch_error |
|
rm -f /tmp/patch_error |
|
echo "${NC}" |
|
exit 1 |
|
fi |