|
212 | 212 |
|
213 | 213 | # Parse the provided version into an AppVersion object |
214 | 214 | parsed_version = VERSION_FORMATTER.parse(new_version) |
| 215 | + # Validate that this is a hotfix version (must have a patch component > 0) |
| 216 | + UI.user_error!("Invalid hotfix version '#{new_version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive? |
215 | 217 | previous_version = VERSION_FORMATTER.release_version(VERSION_CALCULATOR.previous_patch_version(version: parsed_version)) |
| 218 | + previous_release_branch = "release/#{previous_version}" |
| 219 | + |
| 220 | + # Determine the base for the hotfix branch: either a tag or a release branch |
| 221 | + base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true) |
| 222 | + previous_version |
| 223 | + elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch) |
| 224 | + UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.") |
| 225 | + previous_release_branch |
| 226 | + else |
| 227 | + UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.") |
| 228 | + end |
216 | 229 |
|
217 | 230 | # Check versions |
218 | 231 | message = <<-MESSAGE |
|
223 | 236 | Current build code: #{current_build_code} |
224 | 237 | New build code: #{new_build_code} |
225 | 238 |
|
226 | | - Branching from tag: #{previous_version} |
| 239 | + Branching from #{base_ref_for_hotfix} |
227 | 240 |
|
228 | 241 | MESSAGE |
229 | 242 |
|
|
232 | 245 | UI.user_error!('Aborted by user request') unless skip_confirm || UI.confirm('Do you want to continue?') |
233 | 246 |
|
234 | 247 | # Check tags |
235 | | - UI.user_error!("The version `#{new_version}` tag already exists!") if git_tag_exists(tag: new_version) |
236 | | - UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version) |
| 248 | + UI.user_error!("Version '#{new_version}' already exists on the remote! Abort!") if git_tag_exists(tag: new_version, remote: true) |
| 249 | + |
| 250 | + # Fetch the base ref to ensure it's available locally |
| 251 | + sh('git', 'fetch', 'origin', base_ref_for_hotfix) |
| 252 | + |
| 253 | + hotfix_branch = "release/#{new_version}" |
| 254 | + ensure_branch_does_not_exist!(hotfix_branch) |
237 | 255 |
|
238 | 256 | # Create the hotfix branch |
239 | | - UI.message 'Creating hotfix branch...' |
240 | | - Fastlane::Helper::GitHelper.create_branch("release/#{new_version}", from: previous_version) |
241 | | - UI.success "Done! New hotfix branch is: #{git_branch}" |
| 257 | + UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...") |
| 258 | + Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix) |
| 259 | + UI.success("Done! New hotfix branch is: '#{git_branch}'") |
242 | 260 |
|
243 | 261 | # Bump the hotfix version and build code and write it to the `version.properties` file |
244 | | - UI.message 'Bumping hotfix version and build code...' |
| 262 | + UI.message('Bumping hotfix version and build code...') |
245 | 263 | VERSION_FILE.write_version( |
246 | 264 | version_name: new_version, |
247 | 265 | version_code: new_build_code |
248 | 266 | ) |
249 | 267 | commit_version_bump |
250 | | - UI.success "Done! New Release Version: #{current_release_version}. New Build Code: #{current_build_code}" |
| 268 | + UI.success("Done! New Release Version: '#{current_release_version}'. New Build Code: '#{current_build_code}'") |
251 | 269 |
|
252 | 270 | push_to_git_remote(tags: false) |
253 | 271 | end |
|
0 commit comments