xref: /aosp_15_r20/external/google-cloud-java/generation/merge_repository.sh (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1#!/bin/bash
2
3# Merging repository to google-cloud-java using git-filter-repo.
4# https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories
5#
6# Usage:
7#   cd google-cloud-java
8#   # edit the repository to merge.
9#   vim generation/repo.txt
10#   # use your account
11#   export USERNAME=XXX
12#   # Run the script
13#   ./generation/merge_repository.sh
14#   # Create a pull request. Ensure you MERGE the pull request. Not 'squash'.
15#   cd generation/monorepo/google-cloud-java-merged
16#   git checkout -b merge_repositories
17#   gh pr create --title 'chore: merge new repository into google-cloud-java' --body ''
18
19set -xe
20
21[ -z "`git config user.email`" ] && git config --global user.email "${USERNAME:-script}@google.com"
22[ -z "`git config user.name`" ] && git config --global user.name "${USERNAME:-script}"
23
24cd "$(dirname "$0")"
25
26rm -rf monorepo
27mkdir monorepo
28
29cp merge_repository.sh monorepo
30cp repos.txt monorepo
31
32cd monorepo
33
34git clone https://github.com/newren/git-filter-repo.git
35export PATH=$PATH:`pwd`/git-filter-repo
36
37merged_repository=google-cloud-java-merged
38git clone https://github.com/googleapis/google-cloud-java "${merged_repository}"
39
40cd "${merged_repository}"
41
42cat ../repos.txt | while read service
43do
44  cd ..
45  git clone https://github.com/googleapis/${service}.git
46  cd  ${service}
47  git filter-repo --to-subdirectory-filter ${service}
48
49  # setup owlbot files correctly to match monorepo configuration
50  if [ -r "${service}/.github/.OwlBot.yaml" ]; then
51    cp ${service}/.github/.OwlBot.yaml ${service}/.OwlBot.yaml
52    rm ${service}/.github/.OwlBot.lock.yaml
53    rm ${service}/.github/.OwlBot.yaml
54    sed -i.bak '/docker/d' ${service}/.OwlBot.yaml && rm ${service}/.OwlBot.yaml.bak
55    sed -i.bak '/image/d' ${service}/.OwlBot.yaml && rm ${service}/.OwlBot.yaml.bak
56
57    # In monorepo, the staging directory structure tells the destination module to
58    # which the OwlBot Java postprocessor copies the files.
59    sed -i.bak "s|owl-bot-staging|owl-bot-staging/${service}|" ${service}/.OwlBot.yaml && rm ${service}/.OwlBot.yaml.bak
60
61    text=$(grep '^.*api_shortname.*' ${service}/.repo-metadata.json)
62    text=$(echo "$text" | sed 's/\"//g; s/\,//g; s/^[[:space:]]*//' )
63    text=${text/api_shortname/api-name}
64    echo -e "\n"$text>> ${service}/.OwlBot.yaml
65    git add .
66    git config --add secrets.allowed "dest.*src"
67    git commit -am "chore: setup owlbot configuration"
68  fi
69
70  cd "../${merged_repository}"
71  git remote add ${service} ../${service}
72  git config --add secrets.allowed "dest.*src"
73  git fetch ${service} #--tags
74  EDITOR=true git merge --quiet --allow-unrelated-histories ${service}/main
75  git remote remove ${service}
76  rm -rf ../${service}
77done
78
79# cwd: monorepo/google-cloud-java-merged
80echo "Working directory: $(pwd)"
81
82cp -R ../../../google-cloud-jar-parent google-cloud-jar-parent
83cp -R ../../../google-cloud-pom-parent google-cloud-pom-parent
84
85git add --all
86git commit -m 'chore: add template files'
87
88../../generate_root_pom.sh
89
90git add pom.xml
91git commit -am 'chore: create aggregator pom' --allow-empty
92
93# Point modules poms and BOMs to the aggregator pom as parent
94bash ../../set_parent_pom.sh
95
96git add --all
97git commit -am 'chore: point modules to the aggregator pom as parent' \
98    --allow-empty
99
100../../consolidate_config.sh
101
102git add --all
103git commit -am 'chore: consolidate config to parent' \
104    --allow-empty
105
106../../generate_gapic_bom.sh
107
108# add the gapic bom module to root pom.xml by regenerating aggregator pom
109../../generate_root_pom.sh
110
111git add gapic-libraries-bom/pom.xml
112git commit -am 'chore: create gapic-libraries-bom' \
113    --allow-empty
114
115cp ../../gapic_bom_versions.txt gapic-libraries-bom/versions.txt
116
117../../delete_non_generated_samples.sh
118
119git add --all
120git commit -am 'chore: delete non-auto-generated samples' \
121    --allow-empty
122
123../../generate_root_versions_txt.sh
124../../update_versions.sh -s
125../../apply_current_versions.sh
126
127git add --all
128git commit -am 'chore: update versions to latest in maven' \
129    --allow-empty
130
131../../update_owlbot_postprocessor_config.sh
132
133git add --all
134git commit -am 'chore: remove and disable owlbot postprocessor templates' \
135    --allow-empty
136
137../../set_owlbot_config.sh
138
139git commit -am 'chore: set owlbot copy config' --allow-empty
140
141# create a monorepo/diff repo
142cd ..
143cp -R "${merged_repository}" split
144rm -rf split/.git
145git clone -b main --single-branch https://github.com/googleapis/google-cloud-java.git shadow
146cp -R shadow/.git split/.git
147rm -rf shadow
148mv split diff
149cd diff
150
151git add "*/src/main/*" || true
152git commit -m 'split repo - diff src/main' --allow-empty
153
154git add "*/src/test/*" || true
155git commit -m 'split repo - diff src/test' --allow-empty
156
157git add "*/samples/*" || true
158git commit -m 'split repo - diff samples' --allow-empty
159
160git add "**/pom.xml" || true
161git commit -m 'split repo - diff pom.xml' --allow-empty
162
163git add "**/CHANGELOG.md" || true
164git commit -m 'split repo - diff CHANGELOG.md' --allow-empty
165
166git add "**/README.md" || true
167git commit -m 'split repo - diff README.md' --allow-empty
168
169git add "**/.OwlBot.yaml" || true
170git commit -m 'split repo - diff .OwlBot.yaml' --allow-empty
171
172git add "**/versions.txt" || true
173git commit -m 'split repo - diff versions.txt' --allow-empty
174
175git add --all || true
176git commit -am 'split repo - diff everything else' --allow-empty
177cd ../..
178