get-git-version.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import os
  4. import re
  5. # Find the nearest tag to the current HEAD.
  6. # This is equivalent to our old logic of "use a value in package.json" for the
  7. # following reasons:
  8. #
  9. # 1. Whenever we updated the package.json we ALSO pushed a tag with the same
  10. # version.
  11. # 2. Whenever we _reverted_ a bump all we actually did was push a commit that
  12. # deleted the tag and changed the version number back.
  13. #
  14. # The only difference in the "git describe" technique is that technically a
  15. # commit can "change" its version number if a tag is created / removed
  16. # retroactively. i.e. the first time a commit is pushed it will be 1.2.3
  17. # and after the tag is made rebuilding the same commit will result in it being
  18. # 1.2.4.
  19. try:
  20. output = subprocess.check_output(
  21. ['git', 'describe', '--tags', '--abbrev=0'],
  22. cwd=os.path.abspath(os.path.join(os.path.dirname(__file__), '..')),
  23. stderr=subprocess.PIPE,
  24. universal_newlines=True)
  25. # only remove the 'v' prefix from the tag name.
  26. version = re.sub('^v', '', output.strip())
  27. print(version)
  28. except Exception:
  29. # When there is error we print a null version string instead of throwing an
  30. # exception, this is because for linux/bsd packages and some vendor builds
  31. # electron is built from a source code tarball and there is no git information
  32. # there.
  33. print('0.0.0-no-git-tag-found')