Search code examples
batch-filejira

Extract Jira issue id from Git Branch name using Windows Batch Script


I have a branch name in the form of:

origin/release/JIRA-1000-some-issue-name

I want to write a Batch script to extract only the Jira issue ID as such:

JIRA-1000

So that I could use the ID in a commit message further on.

Could someone please suggest a solution!


Solution

  • With the help of Suggestions in the Comments, I was able to arrive at a solution.

    The Batch Script has the following steps:

    1. Retrieve Branch Name and store into temporary file
    2. Set variable branch_name
    3. Use Forward Slashes as delimiter and set parameter lastPart
    4. Use lastPart to extract Jira ID with delimiters /-
    git rev-parse --abbrev-ref HEAD > %temp%\git.txt
    for /f "Tokens=*" %%a in (%temp%\git.txt) do @set branch_name=%%a
    for %%a in ("%branch_name%\.") do set "lastPart=%%~nxa"
    for /f "tokens=1,2 delims=/-" %%G in ("%lastPart%\.") do @set jiraid=%%G-%%H
    
    echo %branch_name%
    echo %lastPart%
    echo %jiraid%
    

    The output should be:

    origin/release/JIRA-1000-some-issue-name
    JIRA-1000-some-issue-name
    JIRA-1000
    

    Then the variable jiraid can be used further.