1

I have a python script which executes bash commands on the linux server. Name of the method is exec_cmd and it worked perfectly, until now. I need to execute git log command on the linux server which returns one specific git commit. git log --grep="\[AccuRev transaction: 14126178\]"

Output:

`commit a176c1e534735b7b8b6506730288a62c3f2754cb    
Author: ....    
Date:   ...    
    [AccuRev transaction: 14126178]`

You will notice that git log command must have backslash when executing it on the server.

In python I tried with my exec_cmd function which works for any other git command but with this command that includes " and \ it doesn't work. Output is EMPTY.

exec_cmd(['git', 'log', r'--grep="\[AccuRev transaction: 14126178\]"'], fail=True)

As you see I tried with stringprefix string literal r but without luck.

In log I can see it executed like this this: ['git', 'log', '--grep="\\[AccuRev transaction: 14126178\\]"']

I had an issue with similar command for git checkout that I resolved in python and the difference is that it didn't included quotes in it.

git checkout -b mynewbranch1 ':/\[AccuRev transaction: 21114530\]'

And in python code, the solution that worked was this:

exec_cmd(['git', 'checkout', '-b', "branch1", r':/\[AccuRev transaction: ' +str(transaction_id)+'\]'], fail=True)

What should be corrected in the git log python code?

Thanks a lot!

vel
  • 1,000
  • 1
  • 13
  • 35
  • 3
    The double quotes are wrong, you need them for the shell but here, there is no shell. – tripleee Jun 19 '22 at 19:17
  • 2
    Yah, the way you've got it coded here Git's looking for literal quotes. Lose them. When the shell is parsing command lines it does one layer of preprocessing, `$` introduces parameter expansion but the marker is discarded, double quotes shut off wordsplitting and globbing but the quotes are discarded, single quotes shut off *everything* and then the quotes are discarded, and so on. – jthill Jun 19 '22 at 19:47

0 Answers0