git.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. """
  12. This module contains various helper functions for git DVCS.
  13. """
  14. import os
  15. from PyInstaller.compat import exec_command, exec_command_rc
  16. try:
  17. WindowsError
  18. except NameError:
  19. # Not running on Windows
  20. WindowsError = FileNotFoundError
  21. def get_repo_revision():
  22. path = os.path # shortcut
  23. gitdir = path.normpath(path.join(path.dirname(os.path.abspath(__file__)), '..', '..', '.git'))
  24. cwd = os.path.dirname(gitdir)
  25. if not path.exists(gitdir):
  26. try:
  27. from PyInstaller.utils._gitrevision import rev
  28. if not rev.startswith('$'):
  29. # the format specifier has been substituted
  30. return '+' + rev
  31. except ImportError:
  32. pass
  33. return ''
  34. try:
  35. # need to update index first to get reliable state
  36. exec_command_rc('git', 'update-index', '-q', '--refresh', cwd=cwd)
  37. recent = exec_command('git', 'describe', '--long', '--dirty', '--tag', cwd=cwd).strip()
  38. if recent.endswith('-dirty'):
  39. tag, changes, rev, dirty = recent.rsplit('-', 3)
  40. rev = rev + '.mod'
  41. else:
  42. tag, changes, rev = recent.rsplit('-', 2)
  43. if changes == '0':
  44. return ''
  45. # According to PEP440, local version identifier starts with '+'.
  46. return '+' + rev
  47. except (FileNotFoundError, WindowsError):
  48. # Be silent when git command is not found.
  49. pass
  50. return ''
  51. if __name__ == '__main__':
  52. print(get_repo_revision())