@@ -57,6 +57,9 @@ UPDATE_GENERATION_CONFIG_SCRIPT="$TRANSFORM_SCRIPT_DIR/update_generation_config.
5757UPDATE_OWLBOT_HERMETIC_SCRIPT=" $TRANSFORM_SCRIPT_DIR /update_owlbot_hermetic.py"
5858TRANSFORM_OWLBOT_SCRIPT=" $TRANSFORM_SCRIPT_DIR /update_owlbot.py"
5959
60+ # Track number of commits made by this script
61+ COMMIT_COUNT=0
62+
6063echo " 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
186189echo " Committing migration..."
187190git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): migrate $SOURCE_REPO_NAME into monorepo"
191+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
188192
189193# 7.1 Update CODEOWNERS
190194if [ -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=$(( COMMIT_COUNT + 1 ))
198203fi
199204
200205# 7.2 Update root pom.xml modules
@@ -204,7 +209,7 @@ python3 "$UPDATE_ROOT_POM_SCRIPT" "pom.xml" "$SOURCE_REPO_NAME"
204209echo " Committing root pom.xml modules update..."
205210git add pom.xml
206211git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): add module to root pom.xml"
207-
212+ COMMIT_COUNT= $(( COMMIT_COUNT + 1 ))
208213
209214# 7.5 Migrate GitHub Actions workflows
210215echo " Checking for GitHub Actions workflows..."
@@ -240,6 +245,7 @@ if [ -d "$SOURCE_REPO_NAME/.github/workflows" ]; then
240245 echo " Committing workflow migration..."
241246 git add .github/workflows
242247 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): migrate and adapt GitHub Actions workflows"
248+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
243249fi
244250
245251# 7.6 Update generation_config.yaml
@@ -254,6 +260,7 @@ if [ -f "$SOURCE_CONFIG" ]; then
254260 echo " Committing generation_config.yaml update..."
255261 git add generation_config.yaml " $SOURCE_CONFIG "
256262 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): add library to generation_config.yaml"
263+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
257264fi
258265
259266# 7.7 Consolidate versions.txt
@@ -269,6 +276,7 @@ if [ -f "$SOURCE_VERSIONS" ]; then
269276 echo " Committing versions.txt update..."
270277 git add versions.txt " $SOURCE_VERSIONS "
271278 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): consolidate versions.txt into root"
279+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
272280fi
273281
274282# 7.8 Migrate .OwlBot-hermetic.yaml
@@ -286,6 +294,7 @@ if [ -n "$SOURCE_OWLBOT" ]; then
286294 echo " Committing .OwlBot-hermetic.yaml migration..."
287295 git add " $TARGET_OWLBOT "
288296 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): migrate .OwlBot-hermetic.yaml"
297+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
289298fi
290299
291300
@@ -299,6 +308,7 @@ if [ -f "$SOURCE_DIR/owlbot.py" ]; then
299308 echo " Committing owlbot.py migration..."
300309 git add " $TARGET_OWLBOT "
301310 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): migrate owlbot.py"
311+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
302312fi
303313
304314# 7.9 Fix copyright headers in Java files
@@ -308,6 +318,7 @@ python3 "$FIX_COPYRIGHT_SCRIPT" "$SOURCE_REPO_NAME"
308318echo " Committing copyright header fixes..."
309319git add " $SOURCE_REPO_NAME "
310320git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): update copyright headers to 2026 Google LLC"
321+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
311322
312323# 7.11 Modernize root pom.xml
313324echo " Modernizing root pom.xml..."
@@ -317,24 +328,49 @@ python3 "$MODERNIZE_POM_SCRIPT" "$SOURCE_REPO_NAME/pom.xml" "$PARENT_VERSION" "$
317328echo " Committing root pom.xml modernization..."
318329git add " $SOURCE_REPO_NAME /pom.xml"
319330git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): modernize root pom.xml"
331+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
320332
321333# 7.12 Modernize BOM pom.xml
322334echo " Modernizing BOM pom.xml..."
323335# 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
336+ # Use process substitution or just a loop over the output of find to avoid subshell issues with counters
337+ while read -r bom_pom; do
325338 echo " Modernizing BOM: $bom_pom "
326339 # BOMs should inherit from google-cloud-pom-parent
327340 python3 " $MODERNIZE_POM_SCRIPT " " $bom_pom " " $PARENT_VERSION " " $SOURCE_REPO_NAME " " google-cloud-pom-parent" " ../../google-cloud-pom-parent/pom.xml"
328341
329342 echo " Committing BOM pom.xml modernization for $bom_pom ..."
330343 git add " $bom_pom "
331344 git commit -n --no-gpg-sign -m " chore($SOURCE_REPO_NAME ): modernize BOM pom.xml"
332- done
345+ COMMIT_COUNT=$(( COMMIT_COUNT + 1 ))
346+ done < <( find " $SOURCE_REPO_NAME " -name " pom.xml" | grep " \-bom/pom.xml" | grep -v " samples" )
333347
334348# 7.11 Verify compilation
335349echo " Verifying compilation..."
336350(cd " $SOURCE_REPO_NAME " && mvn compile -DskipTests -T 1C)
337351
352+ # 7.13 Squash commits
353+ if [ " ${SQUASH_COMMITS:- false} " = " true" ]; then
354+ echo " Squashing $COMMIT_COUNT commits..."
355+ if [ " $COMMIT_COUNT " -gt 1 ]; then
356+ # Reset soft to the first commit of the migration
357+ # We want to keep the very first commit message, or maybe a combined one?
358+ # The requirement is "squash all the commits it's made into a single migration commit"
359+ # The first commit we made was "chore($SOURCE_REPO_NAME): migrate $SOURCE_REPO_NAME into monorepo"
360+
361+ # We can do this by soft resetting back COMMIT_COUNT-1 commits
362+ # This leaves the first commit as HEAD, but with changes from subsequent commits staged.
363+ # NO. Soft reset back N commits.
364+ # If we made 3 commits: C1, C2, C3. HEAD is C3.
365+ # reset --soft HEAD~2 results in HEAD at C1, with changes from C2 and C3 staged.
366+ # then commit --amend adds those staged changes to C1.
367+
368+ git reset --soft " HEAD~$(( COMMIT_COUNT - 1 )) "
369+ git commit --amend --no-edit --no-gpg-sign
370+ echo " Squashed everything into one commit."
371+ fi
372+ fi
373+
338374# 8. Cleanup
339375echo " Cleaning up temporary source clone..."
340376rm -rf " $SOURCE_DIR "
0 commit comments