@@ -108,6 +108,45 @@ publish_to_maven() {
108108 echo " Maven publishing completed"
109109}
110110
111+ # Function to preserve existing commit history before Maven publishing
112+ preserve_commit_history () {
113+ local artifact_id=" $1 "
114+ local snapshot_repo_url=" ${2:- $SNAPSHOT_REPO_URL } "
115+
116+ if [ " $DISABLE_COMMIT_MAPPING " = " true" ]; then
117+ return 0
118+ fi
119+
120+ echo " Preserving existing commit history for ${artifact_id} "
121+
122+ TEMP_DIR=$( mktemp -d)
123+ OLD_METADATA_FILE=" ${TEMP_DIR} /old-maven-metadata.xml"
124+ COMMIT_HISTORY_FILE=" ${TEMP_DIR} /commit-history.xml"
125+
126+ # Download existing root metadata to preserve commit history
127+ ROOT_META_URL=" ${snapshot_repo_url} org/opensearch/${artifact_id} /maven-metadata.xml"
128+ HTTP_CODE=$( curl -s -o " ${OLD_METADATA_FILE} " -w " %{http_code}" -u " ${SONATYPE_USERNAME} :${SONATYPE_PASSWORD} " " ${ROOT_META_URL} " || echo " 000" )
129+
130+ if [ " $HTTP_CODE " = " 200" ] && [ -s " ${OLD_METADATA_FILE} " ]; then
131+ # Extract existing commit history if it exists
132+ if grep -q " <commitHistory>" " ${OLD_METADATA_FILE} " ; then
133+ echo " Extracting existing commit history"
134+ sed -n ' /<commitHistory>/,/<\/commitHistory>/p' " ${OLD_METADATA_FILE} " > " ${COMMIT_HISTORY_FILE} "
135+ echo " Commit history preserved at: ${COMMIT_HISTORY_FILE} "
136+ else
137+ echo " No existing commit history found"
138+ echo " " > " ${COMMIT_HISTORY_FILE} "
139+ fi
140+ else
141+ echo " No existing metadata found, starting fresh"
142+ echo " " > " ${COMMIT_HISTORY_FILE} "
143+ fi
144+
145+ # Export the file path for use by restore function
146+ export PRESERVED_COMMIT_HISTORY_FILE=" ${COMMIT_HISTORY_FILE} "
147+ export PRESERVED_TEMP_DIR=" ${TEMP_DIR} "
148+ }
149+
111150# Function to update root maven-metadata.xml with commit history
112151update_root_metadata_with_commit_history () {
113152 local artifact_id=" $1 "
@@ -126,9 +165,9 @@ update_root_metadata_with_commit_history() {
126165 TEMP_DIR=$( mktemp -d)
127166 METADATA_FILE=" ${TEMP_DIR} /maven-metadata.xml"
128167
129- # Download existing root metadata (artifact level, not version level)
168+ # Download the fresh metadata that Maven just created
130169 ROOT_META_URL=" ${snapshot_repo_url} org/opensearch/${artifact_id} /maven-metadata.xml"
131- echo " Downloading root metadata from ${ROOT_META_URL} "
170+ echo " Downloading fresh metadata from ${ROOT_META_URL} "
132171
133172 HTTP_CODE=$( curl -s -o " ${METADATA_FILE} " -w " %{http_code}" -u " ${SONATYPE_USERNAME} :${SONATYPE_PASSWORD} " " ${ROOT_META_URL} " || echo " 000" )
134173
@@ -146,13 +185,26 @@ update_root_metadata_with_commit_history() {
146185 </versions>
147186 <lastUpdated>$( date +%Y%m%d%H%M%S) </lastUpdated>
148187 </versioning>
188+ <version>${version} </version>
149189</metadata>
150190EOF
151191 fi
152192
153- # Check if commitHistory section exists, if not add it
154- if ! grep -q " <commitHistory>" " ${METADATA_FILE} " ; then
155- echo " Adding commitHistory section to metadata"
193+ # Restore preserved commit history and add new entry
194+ if [ -n " $PRESERVED_COMMIT_HISTORY_FILE " ] && [ -s " $PRESERVED_COMMIT_HISTORY_FILE " ]; then
195+ echo " Restoring preserved commit history"
196+ # Remove existing commitHistory if it exists (shouldn't, but just in case)
197+ sed -i.bak ' /<commitHistory>/,/<\/commitHistory>/d' " ${METADATA_FILE} "
198+ # Add preserved commit history before closing </metadata> tag
199+ sed -i.bak2 ' /<\/metadata>/i\
200+ <commitHistory>\
201+ </commitHistory>' " ${METADATA_FILE} "
202+ # Replace empty commitHistory with preserved content
203+ PRESERVED_CONTENT=$( sed ' 1d;$d' " $PRESERVED_COMMIT_HISTORY_FILE " ) # Remove first and last line (<commitHistory> tags)
204+ sed -i.bak3 " /<commitHistory>/a\\
205+ $PRESERVED_CONTENT " " ${METADATA_FILE} "
206+ else
207+ echo " No preserved history, adding new commitHistory section"
156208 # Add commitHistory section before closing </metadata> tag
157209 sed -i.bak ' /<\/metadata>/i\
158210 <commitHistory>\
@@ -218,6 +270,13 @@ ${NEW_MAPPING}" "${METADATA_FILE}"
218270
219271 # Clean up
220272 rm -rf " ${TEMP_DIR} "
273+
274+ # Clean up preserved history files
275+ if [ -n " $PRESERVED_TEMP_DIR " ]; then
276+ rm -rf " $PRESERVED_TEMP_DIR "
277+ unset PRESERVED_COMMIT_HISTORY_FILE
278+ unset PRESERVED_TEMP_DIR
279+ fi
221280
222281 echo " Root metadata with commit history updated successfully"
223282 return 0
@@ -478,8 +537,11 @@ publish_grammar_files() {
478537
479538 # Generate checksums
480539 generate_checksums
540+
541+ # IMPORTANT: Preserve existing commit history BEFORE Maven publishing overwrites metadata
542+ preserve_commit_history " $ARTIFACT_ID "
481543
482- # Publish to Maven
544+ # Publish to Maven (this will overwrite maven-metadata.xml)
483545 publish_to_maven
484546
485547 # Extract the actual artifact version from metadata for commit history
@@ -534,8 +596,11 @@ publish_async_query_core() {
534596
535597 # Generate checksums
536598 generate_checksums
599+
600+ # IMPORTANT: Preserve existing commit history BEFORE Maven publishing overwrites metadata
601+ preserve_commit_history " $ARTIFACT_ID "
537602
538- # Publish to Maven
603+ # Publish to Maven (this will overwrite maven-metadata.xml)
539604 publish_to_maven
540605
541606 # Extract the actual artifact version from metadata for commit history
0 commit comments