1717# Exit on error
1818set -e
1919
20+ # Get absolute path to the script directory
21+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
22+ MONOREPO_ROOT=" $( dirname " $SCRIPT_DIR " ) "
23+
2024# Ensure git-filter-repo is available in PATH
2125if ! command -v git-filter-repo > /dev/null 2>&1 ; then
26+ LOCAL_FILTER_REPO_DIR=" ${SCRIPT_DIR} /.git-filter-repo"
27+ LOCAL_FILTER_REPO=" ${LOCAL_FILTER_REPO_DIR} /git-filter-repo"
28+
29+ if [ ! -f " $LOCAL_FILTER_REPO " ]; then
30+ echo " git-filter-repo not found locally or in PATH. Downloading version v2.45.0..."
31+ mkdir -p " $LOCAL_FILTER_REPO_DIR "
32+ curl -sSL -o " $LOCAL_FILTER_REPO " " https://raw.githubusercontent.com/newren/git-filter-repo/v2.45.0/git-filter-repo"
33+ chmod +x " $LOCAL_FILTER_REPO "
34+ fi
35+
2236 echo " Injecting local .git-filter-repo into PATH..."
23- export PATH=" $( pwd ) /.git-filter-repo :$PATH "
37+ export PATH=" ${LOCAL_FILTER_REPO_DIR} :$PATH "
2438fi
2539
2640# Function to check if a command exists
@@ -49,13 +63,12 @@ CODEOWNER="${CODEOWNER:-}"
4963SOURCE_REPO_NAME=" ${SOURCE_REPO_URL##*/ } "
5064MONOREPO_NAME=" ${MONOREPO_URL##*/ } "
5165
52- # Use a temporary working directory sibling to the current monorepo
53- WORKING_DIR=" ../../migration-work"
66+ # Use a temporary working directory sibling to the current monorepo, anchored to script location
67+ WORKING_DIR=" $( cd " ${SCRIPT_DIR} / ../.." && pwd ) /migration-work"
5468SOURCE_DIR=" $WORKING_DIR /$SOURCE_REPO_NAME -source"
5569TARGET_DIR=" $WORKING_DIR /$MONOREPO_NAME -target"
5670
57- # Get absolute path to the transformation script before any cd
58- TRANSFORM_SCRIPT_DIR=" $( cd " $( dirname " $0 " ) " && pwd) "
71+ TRANSFORM_SCRIPT_DIR=" ${SCRIPT_DIR} "
5972TRANSFORM_SCRIPT=" $TRANSFORM_SCRIPT_DIR /transform_workflow.py"
6073MODERNIZE_POM_SCRIPT=" $TRANSFORM_SCRIPT_DIR /modernize_pom.py"
6174UPDATE_ROOT_POM_SCRIPT=" $TRANSFORM_SCRIPT_DIR /update_root_pom.py"
@@ -75,20 +88,20 @@ echo "Starting migration using git read-tree with isolated clones..."
7588# 0. Create working directory
7689mkdir -p " $WORKING_DIR "
7790
78- MIGRATION_HEAD_BRANCH=" ${MIGRATION_HEAD_BRANCH :- main} "
91+ MIGRATION_HEAD_BRANCH=" main"
7992echo " Basing migration branch on: ${MIGRATION_HEAD_BRANCH} "
8093
8194# 1. Clone the source repository
8295if [ ! -d " $SOURCE_DIR " ]; then
8396 echo " Cloning source repo: $SOURCE_REPO_URL into $SOURCE_DIR "
84- git clone " $SOURCE_REPO_URL " " $SOURCE_DIR "
97+ git clone --branch main --single-branch " $SOURCE_REPO_URL " " $SOURCE_DIR "
8598elif [ " ${SKIP_SOURCE_UPDATE:- false} " = " true" ]; then
8699 echo " Skipping source repository update..."
87100else
88101 echo " Source directory $SOURCE_DIR already exists. Ensuring it is clean and up-to-date..."
89102 cd " $SOURCE_DIR "
90103 git remote add origin " $SOURCE_REPO_URL " 2> /dev/null || git remote set-url origin " $SOURCE_REPO_URL "
91- git fetch origin
104+ git fetch origin main
92105 git checkout -f " main"
93106 git reset --hard origin/main
94107 git clean -fd
136149# 2. Clone the target monorepo (the "isolated clone")
137150if [ ! -d " $TARGET_DIR " ]; then
138151 echo " Cloning target monorepo: $MONOREPO_URL into $TARGET_DIR "
139- git clone " $MONOREPO_URL " " $TARGET_DIR "
140- git checkout -f " ${MIGRATION_HEAD_BRANCH} "
141- git reset --hard origin/${MIGRATION_HEAD_BRANCH}
152+ git clone --branch main --single-branch --depth 1 " $MONOREPO_URL " " $TARGET_DIR "
142153else
143154 echo " Target directory $TARGET_DIR already exists. Ensuring it is clean and up-to-date..."
144155 cd " $TARGET_DIR "
145- git fetch origin
146- git checkout -f " ${MIGRATION_HEAD_BRANCH} "
147- git reset --hard origin/${MIGRATION_HEAD_BRANCH}
156+ git fetch --depth 1 origin main
157+ git checkout -f " main "
158+ git reset --hard origin/main
148159 git clean -fd
149160 cd - > /dev/null
150161fi
151162
152163cd " $TARGET_DIR "
164+ if [ " $( pwd) " = " $MONOREPO_ROOT " ]; then
165+ echo " CRITICAL ERROR: Script failed to change directory or attempted to run destructive Git operations inside the active workspace!" >&2
166+ exit 1
167+ fi
153168
154169# Ensure we are on a clean main branch in the target clone
155170echo " Ensuring clean state in target monorepo..."
156- git fetch origin
171+ git fetch --depth 1 origin main
157172git reset --hard HEAD
158173git clean -fd
159- git checkout -f " ${MIGRATION_HEAD_BRANCH} "
160- git reset --hard origin/${MIGRATION_HEAD_BRANCH}
174+ git checkout -f " main "
175+ git reset --hard origin/main
161176git clean -fdx
162177
163178# Check if the repository is already migrated
0 commit comments