Git命令在Python的子进程中不起作用 [英] Git command doesn't work in subprocess in Python

查看:337
本文介绍了Git命令在Python的子进程中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在使用python在团队中自动化一些git工作,并且一切正常。不幸的是,我遇到了一些我想使用的东西,但是在放入python子进程时不起作用。这里是命令:

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

当我在git bash(我们使用Windows)中使用它时,它工作正常,但是当我把它放在一个python子进程中,它抱怨 git rev-list --tags --max-count = 1 不是一个有效的命令。我想知道是否有人可以启发我为什么,最好是使用它的方式。我从这个问题中得到了一条线:



我是试图获得最接近当前HEAD的分支上的最新标签。我现在已经有了一个很好的解决方法,它列出了所有的标签,然后以数字的方式对它们进行排序,但这只是起作用,因为我们没有放出任何非数字标签,这些标签不一定总是如此。



任何人都可以帮我吗?

解决方案 < > Popen 构造函数在默认情况下不使用shell来解析您提供的命令。这意味着像反引号之类的shell元字符将不起作用。您可以传递 shell = True 或首先运行 git rev-list --tags --max-count = 1 然后再创建整个命令。

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.

这篇关于Git命令在Python的子进程中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆