Skip to content

Commit 45a0854

Browse files
committed
optionally squash commits
1 parent a7a73aa commit 45a0854

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

monorepo-migration/migrate.sh

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ UPDATE_GENERATION_CONFIG_SCRIPT="$TRANSFORM_SCRIPT_DIR/update_generation_config.
5757
UPDATE_OWLBOT_HERMETIC_SCRIPT="$TRANSFORM_SCRIPT_DIR/update_owlbot_hermetic.py"
5858
TRANSFORM_OWLBOT_SCRIPT="$TRANSFORM_SCRIPT_DIR/update_owlbot.py"
5959

60+
# Track number of commits made by this script
61+
COMMIT_COUNT=0
62+
6063
echo "Starting migration using git read-tree with isolated clones..."
6164

6265
# 0. Create working directory
@@ -185,6 +188,7 @@ find "$SOURCE_REPO_NAME" -maxdepth 1 -name "*.md" ! -name "CHANGELOG.md" ! -name
185188
# 7. Commit the migration
186189
echo "Committing migration..."
187190
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): migrate $SOURCE_REPO_NAME into monorepo"
191+
((COMMIT_COUNT++))
188192

189193
# 7.1 Update CODEOWNERS
190194
if [ -n "$CODEOWNER" ]; then
@@ -195,6 +199,7 @@ if [ -n "$CODEOWNER" ]; then
195199
echo "Committing CODEOWNERS update..."
196200
git add .github/CODEOWNERS
197201
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): add code owners for $SOURCE_REPO_NAME"
202+
((COMMIT_COUNT++))
198203
fi
199204

200205
# 7.2 Update root pom.xml modules
@@ -204,6 +209,7 @@ python3 "$UPDATE_ROOT_POM_SCRIPT" "pom.xml" "$SOURCE_REPO_NAME"
204209
echo "Committing root pom.xml modules update..."
205210
git add pom.xml
206211
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): add module to root pom.xml"
212+
((COMMIT_COUNT++))
207213

208214

209215
# 7.5 Migrate GitHub Actions workflows
@@ -240,6 +246,7 @@ if [ -d "$SOURCE_REPO_NAME/.github/workflows" ]; then
240246
echo "Committing workflow migration..."
241247
git add .github/workflows
242248
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): migrate and adapt GitHub Actions workflows"
249+
((COMMIT_COUNT++))
243250
fi
244251

245252
# 7.6 Update generation_config.yaml
@@ -254,6 +261,7 @@ if [ -f "$SOURCE_CONFIG" ]; then
254261
echo "Committing generation_config.yaml update..."
255262
git add generation_config.yaml "$SOURCE_CONFIG"
256263
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): add library to generation_config.yaml"
264+
((COMMIT_COUNT++))
257265
fi
258266

259267
# 7.7 Consolidate versions.txt
@@ -269,6 +277,7 @@ if [ -f "$SOURCE_VERSIONS" ]; then
269277
echo "Committing versions.txt update..."
270278
git add versions.txt "$SOURCE_VERSIONS"
271279
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): consolidate versions.txt into root"
280+
((COMMIT_COUNT++))
272281
fi
273282

274283
# 7.8 Migrate .OwlBot-hermetic.yaml
@@ -286,6 +295,7 @@ if [ -n "$SOURCE_OWLBOT" ]; then
286295
echo "Committing .OwlBot-hermetic.yaml migration..."
287296
git add "$TARGET_OWLBOT"
288297
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): migrate .OwlBot-hermetic.yaml"
298+
((COMMIT_COUNT++))
289299
fi
290300

291301

@@ -299,6 +309,7 @@ if [ -f "$SOURCE_DIR/owlbot.py" ]; then
299309
echo "Committing owlbot.py migration..."
300310
git add "$TARGET_OWLBOT"
301311
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): migrate owlbot.py"
312+
((COMMIT_COUNT++))
302313
fi
303314

304315
# 7.9 Fix copyright headers in Java files
@@ -308,6 +319,7 @@ python3 "$FIX_COPYRIGHT_SCRIPT" "$SOURCE_REPO_NAME"
308319
echo "Committing copyright header fixes..."
309320
git add "$SOURCE_REPO_NAME"
310321
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): update copyright headers to 2026 Google LLC"
322+
((COMMIT_COUNT++))
311323

312324
# 7.11 Modernize root pom.xml
313325
echo "Modernizing root pom.xml..."
@@ -317,24 +329,49 @@ python3 "$MODERNIZE_POM_SCRIPT" "$SOURCE_REPO_NAME/pom.xml" "$PARENT_VERSION" "$
317329
echo "Committing root pom.xml modernization..."
318330
git add "$SOURCE_REPO_NAME/pom.xml"
319331
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): modernize root pom.xml"
332+
((COMMIT_COUNT++))
320333

321334
# 7.12 Modernize BOM pom.xml
322335
echo "Modernizing BOM pom.xml..."
323336
# Find potential BOM POMs (usually in a subdirectory ending with -bom)
324-
find "$SOURCE_REPO_NAME" -name "pom.xml" | grep "\-bom/pom.xml" | grep -v "samples" | while read -r bom_pom; do
337+
# Use process substitution or just a loop over the output of find to avoid subshell issues with counters
338+
while read -r bom_pom; do
325339
echo "Modernizing BOM: $bom_pom"
326340
# BOMs should inherit from google-cloud-pom-parent
327341
python3 "$MODERNIZE_POM_SCRIPT" "$bom_pom" "$PARENT_VERSION" "$SOURCE_REPO_NAME" "google-cloud-pom-parent" "../../google-cloud-pom-parent/pom.xml"
328342

329343
echo "Committing BOM pom.xml modernization for $bom_pom..."
330344
git add "$bom_pom"
331345
git commit -n --no-gpg-sign -m "chore($SOURCE_REPO_NAME): modernize BOM pom.xml"
332-
done
346+
((COMMIT_COUNT++))
347+
done < <(find "$SOURCE_REPO_NAME" -name "pom.xml" | grep "\-bom/pom.xml" | grep -v "samples")
333348

334349
# 7.11 Verify compilation
335350
echo "Verifying compilation..."
336351
(cd "$SOURCE_REPO_NAME" && mvn compile -DskipTests -T 1C)
337352

353+
# 7.13 Squash commits
354+
if [ "${SQUASH_COMMITS:-false}" = "true" ]; then
355+
echo "Squashing $COMMIT_COUNT commits..."
356+
if [ "$COMMIT_COUNT" -gt 1 ]; then
357+
# Reset soft to the first commit of the migration
358+
# We want to keep the very first commit message, or maybe a combined one?
359+
# The requirement is "squash all the commits it's made into a single migration commit"
360+
# The first commit we made was "chore($SOURCE_REPO_NAME): migrate $SOURCE_REPO_NAME into monorepo"
361+
362+
# We can do this by soft resetting back COMMIT_COUNT-1 commits
363+
# This leaves the first commit as HEAD, but with changes from subsequent commits staged.
364+
# NO. Soft reset back N commits.
365+
# If we made 3 commits: C1, C2, C3. HEAD is C3.
366+
# reset --soft HEAD~2 results in HEAD at C1, with changes from C2 and C3 staged.
367+
# then commit --amend adds those staged changes to C1.
368+
369+
git reset --soft "HEAD~$((COMMIT_COUNT - 1))"
370+
git commit --amend --no-edit --no-gpg-sign
371+
echo "Squashed everything into one commit."
372+
fi
373+
fi
374+
338375
# 8. Cleanup
339376
echo "Cleaning up temporary source clone..."
340377
rm -rf "$SOURCE_DIR"

0 commit comments

Comments
 (0)