@@ -41,30 +41,60 @@ update_changelog() {
4141 local clean_version=" ${version# v} "
4242 local current_date
4343 current_date=" $( date +%Y-%m-%d) "
44-
44+
4545 log_info " Updating CHANGELOG.md to release version $version "
46-
46+
4747 # Create backup
4848 cp " $CHANGELOG_FILE " " ${CHANGELOG_FILE} .backup"
4949 log_info " Created backup: ${CHANGELOG_FILE} .backup"
50-
50+
5151 # Create temporary file
5252 local temp_file
5353 temp_file=" $( mktemp) "
54-
54+
5555 # Process the changelog
5656 awk -v version=" $clean_version " -v date=" $current_date " '
57- BEGIN { updated_unreleased = 0 }
58-
59- # Replace the first occurrence of [Unreleased] with the version
57+ BEGIN {
58+ updated_unreleased = 0
59+ in_unreleased = 0
60+ unreleased_content = ""
61+ }
62+
63+ # When we hit [Unreleased], start capturing content
6064 /^## \[Unreleased\]$/ && !updated_unreleased {
65+ in_unreleased = 1
66+ updated_unreleased = 1
67+ next
68+ }
69+
70+ # When we hit the next section header while in unreleased, output everything
71+ /^## / && in_unreleased {
72+ # Output the new structure
6173 print "## [Unreleased]"
6274 print ""
6375 print "## [" version "] - " date
64- updated_unreleased = 1
76+
77+ # Output captured content (now under the versioned section)
78+ if (unreleased_content != "") {
79+ print unreleased_content
80+ }
81+
82+ # Now print the current line (next version header) and stop capturing
83+ print $0
84+ in_unreleased = 0
85+ next
86+ }
87+
88+ # Capture content while in unreleased section
89+ in_unreleased {
90+ if (unreleased_content == "") {
91+ unreleased_content = $0
92+ } else {
93+ unreleased_content = unreleased_content "\n" $0
94+ }
6595 next
6696 }
67-
97+
6898 # Update the link at the bottom for the new version
6999 /^\[Unreleased\]:/ {
70100 # Extract the repository URL pattern
@@ -73,14 +103,14 @@ update_changelog() {
73103 print "[" version "]: https://github.com/yourusername/SwiftDependencyAudit/releases/tag/v" version
74104 next
75105 }
76-
106+
77107 # Print all other lines unchanged
78108 { print }
79109 ' " $CHANGELOG_FILE " > " $temp_file "
80-
110+
81111 # Replace original with updated content
82112 mv " $temp_file " " $CHANGELOG_FILE "
83-
113+
84114 log_success " Successfully updated CHANGELOG.md"
85115 log_info " - Converted [Unreleased] to [$clean_version ] - $current_date "
86116 log_info " - Added new empty [Unreleased] section"
@@ -92,7 +122,7 @@ main() {
92122 # Parse arguments
93123 local version=" "
94124 local update_changelog=false
95-
125+
96126 while [[ $# -gt 0 ]]; do
97127 case $1 in
98128 --update-changelog)
@@ -131,91 +161,91 @@ main() {
131161 ;;
132162 esac
133163 done
134-
164+
135165 # Check required arguments
136166 if [[ -z " $version " ]]; then
137167 log_error " Usage: $SCRIPT_NAME <version> [--update-changelog]"
138168 log_error " Example: $SCRIPT_NAME v1.0.0 --update-changelog"
139169 log_error " Use --help for more information"
140170 exit 1
141171 fi
142-
172+
143173 # Remove 'v' prefix if present for matching
144174 local clean_version=" ${version# v} "
145-
175+
146176 # Check if CHANGELOG.md exists
147177 if [[ ! -f " $CHANGELOG_FILE " ]]; then
148178 log_error " CHANGELOG.md not found in current directory"
149179 exit 1
150180 fi
151-
181+
152182 # Update changelog if requested
153183 if [[ " $update_changelog " == true ]]; then
154184 if [[ " $clean_version " == " unreleased" ]] || [[ " $version " == " unreleased" ]]; then
155185 log_error " Cannot update changelog for 'unreleased' version"
156186 log_error " Please specify a specific version (e.g., v1.0.0)"
157187 exit 1
158188 fi
159-
189+
160190 # Check if unreleased section exists
161191 if ! grep -q " ^## \[Unreleased\]" " $CHANGELOG_FILE " ; then
162192 log_error " No [Unreleased] section found in $CHANGELOG_FILE "
163193 log_error " Cannot update changelog without unreleased content"
164194 exit 1
165195 fi
166-
196+
167197 # Update the changelog
168198 update_changelog " $version "
169199 log_info " "
170200 fi
171-
201+
172202 log_info " Extracting release notes for version $version from $CHANGELOG_FILE "
173-
203+
174204 # Extract the unreleased section if no specific version is provided or version is "unreleased"
175205 if [[ " $clean_version " == " unreleased" ]] || [[ " $version " == " unreleased" ]]; then
176206 log_info " Extracting unreleased changes..."
177-
207+
178208 # Extract content between "## [Unreleased]" and the next "## [" line
179209 awk '
180210 /^## \[Unreleased\]/ { found=1; next }
181211 /^## \[/ && found { exit }
182212 found { print }
183213 ' " $CHANGELOG_FILE "
184-
214+
185215 if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
186216 log_error " Failed to extract unreleased section from CHANGELOG.md"
187217 exit 1
188218 fi
189-
219+
190220 log_success " Successfully extracted unreleased changes"
191221 return 0
192222 fi
193-
223+
194224 # Try to find the version in different formats
195225 local version_patterns=(
196226 " ^## \\ [$clean_version \\ ]"
197227 " ^## \\ [v$clean_version \\ ]"
198228 " ^## $clean_version "
199229 " ^## v$clean_version "
200230 )
201-
231+
202232 local found_version=" "
203233 for pattern in " ${version_patterns[@]} " ; do
204234 if grep -q " $pattern " " $CHANGELOG_FILE " ; then
205235 found_version=" $pattern "
206236 break
207237 fi
208238 done
209-
239+
210240 if [[ -z " $found_version " ]]; then
211241 log_error " Version $version not found in $CHANGELOG_FILE "
212242 log_info " Available versions:"
213243 grep " ^## \[" " $CHANGELOG_FILE " | head -5 | sed ' s/^/ /'
214244 exit 1
215245 fi
216-
246+
217247 log_info " Found version using pattern: $found_version "
218-
248+
219249 # Extract content between the version header and the next version header
220250 awk -v pattern=" $found_version " '
221251 $0 ~ pattern { found=1; next }
@@ -224,14 +254,14 @@ main() {
224254 found && /^$/ { print; next }
225255 found && !/^$/ { print }
226256 ' " $CHANGELOG_FILE " | sed ' /^$/d'
227-
257+
228258 if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
229259 log_error " Failed to extract release notes for version $version "
230260 exit 1
231261 fi
232-
262+
233263 log_success " Successfully extracted release notes for version $version "
234264}
235265
236266# Run main function with all arguments
237- main " $@ "
267+ main " $@ "
0 commit comments