简体   繁体   中英

Git command doesn't work in subprocess in Python

We've been using python to automate some git work for quite some time in my group, and everything has worked fine. Unfortunately, I've come across something I would like to use, but doesn't work when put into a python subprocess. Here's the command:

git describe --tags `git rev-list --tags --max-count=1`

When I use it in my git bash (we're using Windows) it works fine, but when I put it in a python subprocess, it complains that git rev-list --tags --max-count=1 is not a valid command. I was wondering if anyone could enlighten me as to why, and preferably, a way of using it. I got the line from this question:

How to get the latest tag name in current branch in Git?

I'm trying to get the LATEST tag on a branch, that is closest to the current HEAD. I've got a hacky workaround right now that lists all of the tags and then sorts them numerically, but that's only working because we haven't put out any non-numeric tags, which won't necessarily be the case always.

Can anyone please help me?

The Popen constructor by default doesn't use a shell to parse the command you're giving it. This means that shell metacharacters like the backquote and such things will not work. You can either pass shell = True or first run git rev-list --tags --max-count=1 and then create the whole command after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM