0

I'm trying to add a pre-commit hook in TurtoiseGit to automatically add the JIRA number in my commit message. The JIRA number is always the first part of my branch name.

For example: JIRA-456-fix-cleanup-crash, I would like to have JIRA-456

The git command I'm using is git rev-parse --abbrev-ref HEAD > %2. But unfortunately this give me the whole branch name. I only need JIRA-456.

I tried using the shell command findstr to substring the branch name without success.

The mechanism I use is the one listed here: https://gitlab.com/tortoisegit/tortoisegit/issues/2229#note_1781864

4
  • So what is your question and where are you stuck?
    – gronostaj
    Jan 10, 2019 at 13:22
  • @gronostaj I added clarification. I hope this explains my problem a little better. :) Jan 10, 2019 at 14:04
  • 1
    Reword your question - this isn't about git at all, but more like "How do I get JIRA-456 from JIRA-456-fix-cleanup-crash in a batch file?"
    – Attie
    Jan 10, 2019 at 14:15
  • @Attie I specified git since maybe there is a git command that could also help me :) Jan 10, 2019 at 15:19

2 Answers 2

1

Instead of redirecting the output of

git rev-parse --abbrev-ref HEAD

directly into a file > %2, parse it with a for /f

@Echo off
for /f "tokens=1-2 delims=-" %%A in ('
    git rev-parse --abbrev-ref HEAD
') Do Set "Var=%%A-%%B"
(Echo:%Var%)>%2
1

This will only work if the structure of the string is like you said AAAA-BBBB-CCCC-....

for /F "tokens=1,2 delims=-" %%a in ("JIRA-456-fix-cleanup-crash") do (set "expectedString=%%a-%%b")
echo %expectedString%

Yields JIRA-456, it works by splitting the string using - as a delimiter, then it takes the tokens 1(%%a) and 2(%%b) and concatenate them using set, so you can make use of the string further in your batch file.

So, for you command you will need to pass the command to the FOR function in it's own context using single quotes '

:: I Haven't used git so I'm not really sure about why you redirected the output stream
:: but this should give you a good idea how to use it

for /F "tokens=1,2 delims=-" %%a in ('git rev-parse --abbrev-ref HEAD') do (set "ExString=%%a-%%b")

The %%a syntax is for batch files if you want to test it as a command use %a

Cheers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .