Return a numeric identifier of the latest git changeset. The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format. This value isn't guaranteed to be unique, but collisions are very unlikely, so it's sufficient for generating the development version numbers.
()
| 76 | |
| 77 | @functools.lru_cache |
| 78 | def get_git_changeset(): |
| 79 | """Return a numeric identifier of the latest git changeset. |
| 80 | |
| 81 | The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format. |
| 82 | This value isn't guaranteed to be unique, but collisions are very unlikely, |
| 83 | so it's sufficient for generating the development version numbers. |
| 84 | """ |
| 85 | # Repository may not be found if __file__ is undefined, e.g. in a frozen |
| 86 | # module. |
| 87 | if "__file__" not in globals(): |
| 88 | return None |
| 89 | repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 90 | git_log = subprocess.run( |
| 91 | "git log --pretty=format:%ct --quiet -1 HEAD", |
| 92 | capture_output=True, |
| 93 | shell=True, |
| 94 | cwd=repo_dir, |
| 95 | text=True, |
| 96 | ) |
| 97 | timestamp = git_log.stdout |
| 98 | tz = datetime.UTC |
| 99 | try: |
| 100 | timestamp = datetime.datetime.fromtimestamp(int(timestamp), tz=tz) |
| 101 | except ValueError: |
| 102 | return None |
| 103 | return timestamp.strftime("%Y%m%d%H%M%S") |
| 104 | |
| 105 | |
| 106 | version_component_re = _lazy_re_compile(r"(\d+|[a-z]+|\.)") |