With the following function I am extracting the latest git commit id in a short form and write it into a text file.
from os.path import exists
from subprocess import Popen, PIPE
def get_git_commit_id(txt_file: str) -> str:
"""
Gets the latest git commit id and places in the txt file if not exists
Parameters:
==========
:param txt_file: name of the txt file
Returns:
==========
:return: the latest git commit id
"""
if not exists(txt_file):
print(f"'{txt_file}' did not exist before") # for logging
try:
process = Popen("git rev-parse --short HEAD", stdout=PIPE)
output = process.communicate()[0]
except Exception as error:
output = bytes("latest", "utf-8")
print("It could not read the .git", error) # for logging
with open(txt_file, "w", encoding="utf8") as file:
file.write(output.decode("utf-8"))
file = open(txt_file, "r", encoding="utf8")
git_commit_id = file.readline().strip()
return git_commit_id
get_git_commit_id("git_commit_id.txt")
However this code only works when I have my .git
dir inside my project.
How can extract the latest git commit id in a short form from the URL where my project placed on the internal git?
References: