Return a detailed version string, sourced either from VERSION or obtained dynamically using git.
()
| 11 | |
| 12 | |
| 13 | def get_dev_version() -> str: |
| 14 | """ |
| 15 | Return a detailed version string, sourced either from VERSION or obtained dynamically using git. |
| 16 | """ |
| 17 | |
| 18 | mitmproxy_version = VERSION |
| 19 | |
| 20 | here = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
| 21 | try: # pragma: no cover |
| 22 | # Check that we're in the mitmproxy repository: https://github.com/mitmproxy/mitmproxy/issues/3987 |
| 23 | # cb0e3287090786fad566feb67ac07b8ef361b2c3 is the first mitmproxy commit. |
| 24 | subprocess.run( |
| 25 | ["git", "cat-file", "-e", "cb0e3287090786fad566feb67ac07b8ef361b2c3"], |
| 26 | stdout=subprocess.DEVNULL, |
| 27 | stderr=subprocess.DEVNULL, |
| 28 | cwd=here, |
| 29 | check=True, |
| 30 | ) |
| 31 | git_describe = subprocess.check_output( |
| 32 | ["git", "describe", "--tags", "--long"], |
| 33 | stderr=subprocess.STDOUT, |
| 34 | cwd=here, |
| 35 | ) |
| 36 | last_tag, tag_dist_str, commit = git_describe.decode().strip().rsplit("-", 2) |
| 37 | commit = commit.lstrip("g")[:7] |
| 38 | tag_dist = int(tag_dist_str) |
| 39 | except Exception: |
| 40 | pass |
| 41 | else: |
| 42 | # Add commit info for non-tagged releases |
| 43 | if tag_dist > 0: |
| 44 | mitmproxy_version += f" (+{tag_dist}, commit {commit})" |
| 45 | |
| 46 | # PyInstaller build indicator, if using precompiled binary |
| 47 | if getattr(sys, "frozen", False): |
| 48 | mitmproxy_version += " binary" |
| 49 | |
| 50 | return mitmproxy_version |
| 51 | |
| 52 | |
| 53 | if __name__ == "__main__": # pragma: no cover |