意思是
1 2 3 4 | import subprocess cmd = "git rev-parse --short HEAD" hash = subprocess.check_output(cmd.split()).strip().decode('utf-8') print(hash) |
- 有了子流程,我没有迷路...
附录
-
请注意
hash 是python的保留字...
附录
获取分支名称
1 2 3 4 | import subprocess _cmd = "git rev-parse --abbrev-ref HEAD" branch = subprocess.check_output(_cmd.split()).strip().decode('utf-8') branch = "-".join(branch.split("/")) |
-
请注意,如果您没有适当地替换斜杠,则在从python执行
os.mkdir() 时会收到错误消息。
参考
- https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script