Skip to content

Instantly share code, notes, and snippets.

@andreykin
Created July 1, 2024 19:44
Show Gist options
  • Save andreykin/16aec2d5eb04ece67173a3e6aed064b3 to your computer and use it in GitHub Desktop.
Save andreykin/16aec2d5eb04ece67173a3e6aed064b3 to your computer and use it in GitHub Desktop.
Batch script to automate creation of Git release branch, merge features, and copy push command to clipboard.
@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion
:: Fetching latest changes from remote repository
git fetch --all
:: Finding the latest release branch number
set latestReleaseNumber=0
for /f "tokens=2 delims=-" %%j in ('git branch -r --list "origin/release-*"') do (
set releaseNumber=%%j
set releaseNumber=!releaseNumber:~0,1!
if !releaseNumber! gtr !latestReleaseNumber! (
set latestReleaseNumber=!releaseNumber!
)
)
set /a newReleaseNumber=latestReleaseNumber+1
set newReleaseBranch=release-%newReleaseNumber%
:: Checking existence of release branch
git rev-parse --verify --quiet origin/%newReleaseBranch%
if not errorlevel 1 (
echo Release branch %newReleaseBranch% already exists in the remote repository.
exit /b 1
)
:: Switching to master branch and pulling latest changes
git switch master
git pull origin master
:: Creating new release branch
git switch -c %newReleaseBranch%
:: Interactive part for merging feature branches into release branch
:askBranch
set "featureBranch="
set /p featureBranch="Enter the name of the feature branch to merge (leave empty to finish): "
if "%featureBranch%"=="" goto endMerge
:: Checking if the entered branch name is purely numeric
set "isNumeric=true"
for /f "delims=0123456789" %%i in ("%featureBranch%") do set "isNumeric=false"
if "%isNumeric%"=="true" (
set featureBranch=task-%featureBranch%
)
:: Checking if the branch exists in the remote repository
git rev-parse --verify --quiet origin/%featureBranch%
if errorlevel 1 (
echo Branch %featureBranch% does not exist in the remote repository. Please try again.
goto askBranch
)
:: Switching to feature branch, pulling latest changes and merging into release branch
git switch %featureBranch%
git pull origin %featureBranch%
git switch %newReleaseBranch%
git merge --no-ff %featureBranch%
:: Returning to prompt for input of next branches
goto askBranch
:endMerge
echo All specified branches have been merged into %newReleaseBranch%.
:: git push origin %newReleaseBranch%
:: Copying push command to clipboard
echo git push origin %newReleaseBranch% | clip
echo Release branch %newReleaseBranch% has been created, but not yet pushed. The push command has been copied to the clipboard.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment