Skip to content

Commit 02733fb

Browse files
committed
chore(migration): optimize paths, add workspace protection guard and depth constraints
1 parent 4eaccad commit 02733fb

4 files changed

Lines changed: 121 additions & 17 deletions

File tree

monorepo-migration/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.git-filter-repo/
2+
__pycache__/
3+
*.pyc
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Exit on error
17+
set -e
18+
19+
# Get absolute paths
20+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21+
MONOREPO_ROOT="$(dirname "$SCRIPT_DIR")"
22+
23+
echo "========================================================"
24+
echo " Staging java-bigtable migration"
25+
echo "========================================================"
26+
27+
# 1. Configure environment for the base migrate.sh script
28+
export SOURCE_REPO_URL="https://github.com/googleapis/java-bigtable"
29+
export MIGRATION_HEAD_BRANCH="main"
30+
export SQUASH_COMMITS="false"
31+
export CODEOWNER="@googleapis/bigtable-team"
32+
33+
# 2. Execute the central migration script
34+
"${SCRIPT_DIR}/migrate.sh"
35+
36+
echo ""
37+
echo "========================================================"
38+
echo "Migration staged successfully!"
39+
echo "Results are available in the isolated clone:"
40+
echo " migration-work/google-cloud-java-target"
41+
echo "Current Branch: migrate-java-bigtable"
42+
echo "Next Steps: cd migration-work/google-cloud-java-target && mvn clean install -DskipTests"
43+
echo "========================================================"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Exit on error
17+
set -e
18+
19+
# Get absolute paths
20+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21+
MONOREPO_ROOT="$(dirname "$SCRIPT_DIR")"
22+
23+
echo "========================================================"
24+
echo " Staging java-firestore migration"
25+
echo "========================================================"
26+
27+
# 1. Configure environment for the base migrate.sh script
28+
export SOURCE_REPO_URL="https://github.com/googleapis/java-firestore"
29+
export MIGRATION_HEAD_BRANCH="main"
30+
export SQUASH_COMMITS="false"
31+
export CODEOWNER="@googleapis/firestore-team"
32+
33+
# 2. Execute the central migration script
34+
"${SCRIPT_DIR}/migrate.sh"
35+
36+
echo ""
37+
echo "========================================================"
38+
echo "Migration staged successfully!"
39+
echo "Results are available in the isolated clone:"
40+
echo " migration-work/google-cloud-java-target"
41+
echo "Current Branch: migrate-java-firestore"
42+
echo "Next Steps: cd migration-work/google-cloud-java-target && mvn clean install -DskipTests"
43+
echo "========================================================"

monorepo-migration/migrate.sh

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,24 @@
1717
# Exit on error
1818
set -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
2125
if ! 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"
2438
fi
2539

2640
# Function to check if a command exists
@@ -49,13 +63,12 @@ CODEOWNER="${CODEOWNER:-}"
4963
SOURCE_REPO_NAME="${SOURCE_REPO_URL##*/}"
5064
MONOREPO_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"
5468
SOURCE_DIR="$WORKING_DIR/$SOURCE_REPO_NAME-source"
5569
TARGET_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}"
5972
TRANSFORM_SCRIPT="$TRANSFORM_SCRIPT_DIR/transform_workflow.py"
6073
MODERNIZE_POM_SCRIPT="$TRANSFORM_SCRIPT_DIR/modernize_pom.py"
6174
UPDATE_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
7689
mkdir -p "$WORKING_DIR"
7790

78-
MIGRATION_HEAD_BRANCH="${MIGRATION_HEAD_BRANCH:-main}"
91+
MIGRATION_HEAD_BRANCH="main"
7992
echo "Basing migration branch on: ${MIGRATION_HEAD_BRANCH}"
8093

8194
# 1. Clone the source repository
8295
if [ ! -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"
8598
elif [ "${SKIP_SOURCE_UPDATE:-false}" = "true" ]; then
8699
echo "Skipping source repository update..."
87100
else
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
@@ -136,28 +149,30 @@ fi
136149
# 2. Clone the target monorepo (the "isolated clone")
137150
if [ ! -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"
142153
else
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
150161
fi
151162

152163
cd "$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
155170
echo "Ensuring clean state in target monorepo..."
156-
git fetch origin
171+
git fetch --depth 1 origin main
157172
git reset --hard HEAD
158173
git 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
161176
git clean -fdx
162177

163178
# Check if the repository is already migrated

0 commit comments

Comments
 (0)