Skip to content

Commit c9660a8

Browse files
authored
CP: Use git rev-parse to get last commit sha (#6366) (#6375)
Do not use `git describe`, it is using tags which might be out of date and unrelated to the current build and version. (cherry picked from commit aaab2a7)
1 parent 99c7810 commit c9660a8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

utils/version/gen_version.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,28 @@ def get_output_of(cmd):
3535
output = subprocess.check_output(cmd, cwd=enlistment_root)
3636
return output.decode('ASCII').strip()
3737

38+
def is_dirty():
39+
diff = get_output_of(["git", "diff", "HEAD", "--shortstat"])
40+
return len(diff.strip()) != 0
41+
3842
def get_last_commit_sha():
3943
try:
40-
return get_output_of([ "git", "describe", "--always", "--dirty" ])
44+
sha = get_output_of(["git", "rev-parse", "--short", "HEAD"])
45+
if is_dirty():
46+
sha += "-dirty"
47+
return sha
4148
except subprocess.CalledProcessError:
4249
return "00000000"
4350

4451
def get_current_branch():
4552
try:
46-
return get_output_of([ "git", "rev-parse", "--abbrev-ref", "HEAD" ])
53+
return get_output_of(["git", "rev-parse", "--abbrev-ref", "HEAD"])
4754
except subprocess.CalledProcessError:
4855
return "private"
4956

5057
def get_commit_count(sha):
5158
try:
52-
return get_output_of([ "git", "rev-list", "--count", sha ])
59+
return get_output_of(["git", "rev-list", "--count", sha])
5360
except subprocess.CalledProcessError:
5461
return 0
5562

0 commit comments

Comments
 (0)