diff --git a/fastlane/lanes/release.rb b/fastlane/lanes/release.rb index 9d234d8032ce..0864e0778bfb 100644 --- a/fastlane/lanes/release.rb +++ b/fastlane/lanes/release.rb @@ -212,7 +212,20 @@ # Parse the provided version into an AppVersion object parsed_version = VERSION_FORMATTER.parse(new_version) + # Validate that this is a hotfix version (must have a patch component > 0) + UI.user_error!("Invalid hotfix version '#{new_version}'. Must include a patch number.") unless parsed_version.patch.to_i.positive? previous_version = VERSION_FORMATTER.release_version(VERSION_CALCULATOR.previous_patch_version(version: parsed_version)) + previous_release_branch = "release/#{previous_version}" + + # Determine the base for the hotfix branch: either a tag or a release branch + base_ref_for_hotfix = if git_tag_exists(tag: previous_version, remote: true) + previous_version + elsif Fastlane::Helper::GitHelper.branch_exists_on_remote?(branch_name: previous_release_branch) + UI.message("ℹ️ Tag '#{previous_version}' not found on the remote. Using release branch '#{previous_release_branch}' as the base for hotfix instead.") + previous_release_branch + else + UI.user_error!("Neither tag '#{previous_version}' nor branch '#{previous_release_branch}' exists on the remote! A hotfix branch cannot be created.") + end # Check versions message = <<-MESSAGE @@ -223,7 +236,7 @@ Current build code: #{current_build_code} New build code: #{new_build_code} - Branching from tag: #{previous_version} + Branching from #{base_ref_for_hotfix} MESSAGE @@ -232,22 +245,27 @@ UI.user_error!('Aborted by user request') unless skip_confirm || UI.confirm('Do you want to continue?') # Check tags - UI.user_error!("The version `#{new_version}` tag already exists!") if git_tag_exists(tag: new_version) - UI.user_error!("Version #{previous_version} is not tagged! A hotfix branch cannot be created.") unless git_tag_exists(tag: previous_version) + UI.user_error!("Version '#{new_version}' already exists on the remote! Abort!") if git_tag_exists(tag: new_version, remote: true) + + # Fetch the base ref to ensure it's available locally + sh('git', 'fetch', 'origin', base_ref_for_hotfix) + + hotfix_branch = "release/#{new_version}" + ensure_branch_does_not_exist!(hotfix_branch) # Create the hotfix branch - UI.message 'Creating hotfix branch...' - Fastlane::Helper::GitHelper.create_branch("release/#{new_version}", from: previous_version) - UI.success "Done! New hotfix branch is: #{git_branch}" + UI.message("Creating hotfix branch from '#{base_ref_for_hotfix}'...") + Fastlane::Helper::GitHelper.create_branch(hotfix_branch, from: base_ref_for_hotfix) + UI.success("Done! New hotfix branch is: '#{git_branch}'") # Bump the hotfix version and build code and write it to the `version.properties` file - UI.message 'Bumping hotfix version and build code...' + UI.message('Bumping hotfix version and build code...') VERSION_FILE.write_version( version_name: new_version, version_code: new_build_code ) commit_version_bump - UI.success "Done! New Release Version: #{current_release_version}. New Build Code: #{current_build_code}" + UI.success("Done! New Release Version: '#{current_release_version}'. New Build Code: '#{current_build_code}'") push_to_git_remote(tags: false) end