LoginSignup
8
2

More than 3 years have passed since last update.

Pythonからgitのブランチ名とhashを取得する!

Last updated at Posted at 2019-06-15

とどのつまり

import subprocess
cmd = "git rev-parse --short HEAD"
hash = subprocess.check_output(cmd.split()).strip().decode('utf-8')
print(hash)

python_git_hash.png

  • subprocess使えば何も迷うことはなかった…

追記

  • hashはpythonの予約語だったので気をつけてください…

追記

ブランチ名の取得

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() とかする時にエラーになるので注意。

参考

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2