[英]Check latest commit with Python in Linux/Windows/Mac
I'm trying to generate a simple Python code that: 我正在尝试生成一个简单的Python代码:
git
folder git
文件夹中运行 I have this code that works correctly under Linux: 我有在Linux下可以正常工作的以下代码:
from subprocess import call, STDOUT
import os
if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w')) != 0:
# Not a git folder
commit = ''
else:
# Inside a git folder.: fetch latest commit
commit = subprocess.check_output(['git', 'rev-parse', '{}'.format('HEAD')])
print(commit)
but I have no way of checking if itwill work under Windows and Mac. 但是我无法检查它是否可以在Windows和Mac下使用。
Does it work? 它行得通吗? Is there any way of checking/knowing this sort of things when one has no access to the other operating system?
当一个人无法访问另一操作系统时,是否有任何方法可以检查/了解这种情况?
You don't want to run git branch
to detect whether you're in a Git repository, because you may or may not have any branches. 您不想运行
git branch
来检测您是否在Git存储库中,因为您可能有也可能没有任何分支。 To detect whether you're able to use Git commands, you'll want to run something like git rev-parse --git-dir
, which will exit non-zero if you're not within a Git repository. 要检测您是否能够使用Git命令,您需要运行
git rev-parse --git-dir
类的命令,如果您不在Git存储库中,则会退出非零值。
However, there are a couple of other issues with your code. 但是,您的代码还有其他几个问题。 First of all, in a new repository (one created fresh with
git init
), there will be a .git
directory and the above command will succeed, but HEAD
will not point anywhere. 首先,在一个新的存储库(使用
git init
新鲜创建的存储库)中,将存在一个.git
目录,并且以上命令将成功执行,但是HEAD
不会指向任何地方。 Therefore, your git rev-parse HEAD
command will fail and print HEAD
and an error. 因此,您的
git rev-parse HEAD
命令将失败并显示HEAD
和错误。
Finally, if you want parse a revision, you should usually use --verify
so that you don't print the dummy HEAD
value on failure. 最后,如果要分析修订,通常应使用
--verify
这样就不会在失败时打印伪HEAD
值。 So your invocation should look like git rev-parse --verify HEAD
. 因此,您的调用应类似于
git rev-parse --verify HEAD
。
Ultimately, it's up to you to figure out what you want to do in a newly initialized repository, whether that's fail or fall back to an empty string. 最终,由您决定要在新初始化的存储库中执行什么操作,无论是失败还是退回到空字符串。
The behaviors I've described here are consistent across platforms; 我在这里描述的行为在各个平台上都是一致的。 they're built into Git and well defined.
它们内置在Git中并且定义明确。
There's a method check_output in subprocess library 子流程库中有一个方法check_output
from subprocess import check_output
try:
# use python to parse this log for info. This is your entire last commit
logs = check_output(['git', 'log', '-1', '--stat']).decode("UTF-8")
except Exception as e:
# Do whatever you wanna do otherwise if not git repository
print(e)
Git has a command called "git log". Git有一个名为“ git log”的命令。
"-1" indicates the last commit and --stat will give you the files that were changed, commit ID, TIME ETC “ -1”表示最后一次提交,-stat将为您提供已更改的文件,提交ID,TIME ETC
then you can use python to parse this log and retrive any information you want Check this out for more info on git log 那么您可以使用python解析此日志并检索您想要的任何信息签出有关git log的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.