Read git version from file as back up in make.py (#4247)

* Read git version from file rather than using Git client

* Use git files reading only as fallback
This commit is contained in:
jonpas 2016-08-20 11:52:18 +02:00 committed by Glowbal
parent 1f8f463f32
commit 4d81a21e3f

View File

@ -678,20 +678,47 @@ def get_commit_ID():
# Get latest commit ID # Get latest commit ID
global make_root global make_root
curDir = os.getcwd() curDir = os.getcwd()
commit_id = ""
try: try:
# Verify if Git repository
gitpath = os.path.join(os.path.dirname(make_root), ".git") gitpath = os.path.join(os.path.dirname(make_root), ".git")
assert os.path.exists(gitpath) assert os.path.exists(gitpath)
os.chdir(make_root)
# Try to get commit ID through Git client
os.chdir(make_root)
commit_id = subprocess.check_output(["git", "rev-parse", "HEAD"]) commit_id = subprocess.check_output(["git", "rev-parse", "HEAD"])
commit_id = str(commit_id, "utf-8")[:8] commit_id = str(commit_id, "utf-8")[:8]
except FileNotFoundError:
# Try to get commit ID from git files (subprocess failed - eg. no Git client)
head_path = os.path.join(gitpath, "HEAD")
if os.path.exists(head_path):
with open(head_path, "r") as head_file:
branch_path = head_file.readline().split(": ")
# Commit ID is written in HEAD file directly when in detached state
if len(branch_path) == 1:
commit_id = branch_path[0]
else:
branch_path = branch_path[-1].strip()
ref_path = os.path.join(gitpath, branch_path)
if os.path.exists(ref_path):
with open(ref_path, "r") as ref_file:
commit_id = ref_file.readline()
if commit_id != "":
commit_id = commit_id.strip()[:8]
else:
raise
except: except:
print_error("FAILED TO DETERMINE COMMIT ID.") # All other exceptions (eg. AssertionException)
print_yellow("Verify that \GIT\BIN or \GIT\CMD is in your system path or user path.") if commit_id == "":
commit_id = "NOGIT" raise
raise
finally: finally:
pass pass
if commit_id == "":
print_error("Failed to determine commit ID - folder is not a Git repository.")
commit_id = "NOGIT"
os.chdir(curDir) os.chdir(curDir)
print_yellow("COMMIT ID set to {}".format(commit_id)) print_yellow("COMMIT ID set to {}".format(commit_id))