get-git-version.py 1.3 KB

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