Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Work with %'s in git

samintz

Scott Mintz
May
1,587
27
I have a Python script I have to work with that has this code:
Code:
cmd = 'cd {} && git log -1 --format="%aN <%aE>" -- {}'.format(
    dir_name, script_name)
last_author_output = subprocess.check_output(
    cmd, shell=True, universal_newlines=True)

That --format statement extracts the author and email of the last person to modify the script_name.

That code works correctly in CMD, Powershell, and bash. However, because of the %'s, it fails in TCC. If I double the %'s it works in TCC but fails in CMD, Powershell, and bash. It just outputs %aN <%aE>

Aside from using SETDOS /x before running the script, do I have any alternatives?
 
Don't use internal Python support ... ? I reckon it'll then just use the external Python interpreter as other shells do?

1680801176369.png
 
The script gets invoked using the external Python launcher: py -3-32.
For now, I'm just doing cmd /c py -3-32 ...
 
subprocess.checkoutput( ... shell=True ... ) launches the 'git log' command using the current shell.
 
The Python script is creating the command as
'cd {} && git log -1 --format="%aN <%aE>" -- {}'.format(dir_name, script_name)
That command is launched using the current shell from Python.

I tried setting shell=False, but the CD command is not an executable.
 
Use git bash already?
I am already running in TCC. For now, the easiest solution is cmd /c ...

If you run
Code:
git log -1 --format="%aN <%aE>" -- myfile
It will fail in TCC because TCC expands the %aN/%aE as environment vars and the resulting format string becomes " <>"
When run from the CLI, doubling the percents is an easy solution. But I can't do that inside this Python script because the script gets run from CMD, git bash, or Powershell depending on the user running it.
 
If it were a git script - yes. It's a Python testing script that contains git commands that get invoked once testing is complete and it is creating the test results file.
 
Create a bash script, then. Problem solved.
If you have git, you have bash (or at the very least, sh). Unavoidable.
 
I am already running in TCC. For now, the easiest solution is cmd /c ...

If you run
Code:
git log -1 --format="%aN <%aE>" -- myfile
It will fail in TCC because TCC expands the %aN/%aE as environment vars and the resulting format string becomes " <>"
When run from the CLI, doubling the percents is an easy solution. But I can't do that inside this Python script because the script gets run from CMD, git bash, or Powershell depending on the user running it.
Can the script's definition of 'cmd' be made to depend on the COMSPEC environment variable? And would that help?
 
Back
Top