8
8
__version__ = '0+unknown'
9
9
10
10
11
+ COMMIT_HASH = '$Format:%h$'
12
+
13
+
11
14
def _cmp (a , b ):
12
15
"""Implementation of ``cmp`` for Python 3"""
13
16
return (a > b ) - (a < b )
@@ -64,15 +67,20 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
64
67
return _cmp (Version (version_str ), Version (pkg_version_str ))
65
68
66
69
67
- def pkg_commit_hash (pkg_path = None ):
70
+ def pkg_commit_hash (pkg_path : str = None ):
68
71
"""Get short form of commit hash
69
72
70
- Versioneer placed a ``_version.py`` file in the package directory . This file
71
- gets updated on installation or ``git archive``.
72
- We inspect the contents of ``_version`` to detect whether we are in a
73
- repository, an archive of the repository, or an installed package.
73
+ In this file is a variable called COMMIT_HASH . This contains a substitution
74
+ pattern that may have been filled by the execution of ``git archive``.
75
+
76
+ We get the commit hash from (in order of preference):
74
77
75
- If detection fails, we return a not-found placeholder tuple
78
+ * A substituted value in ``archive_subst_hash``
79
+ * A truncated commit hash value that is part of the local portion of the
80
+ version
81
+ * git's output, if we are in a git repository
82
+
83
+ If all these fail, we return a not-found placeholder tuple
76
84
77
85
Parameters
78
86
----------
@@ -86,17 +94,22 @@ def pkg_commit_hash(pkg_path=None):
86
94
hash_str : str
87
95
short form of hash
88
96
"""
89
- versions = _version .get_versions ()
90
- hash_str = versions ['full-revisionid' ][:7 ]
91
- if hasattr (_version , 'version_json' ):
92
- hash_from = 'installation'
93
- elif not _version .get_keywords ()['full' ].startswith ('$Format:' ):
94
- hash_from = 'archive substitution'
95
- elif versions ['version' ] == '0+unknown' :
96
- hash_from , hash_str = '(none found)' , '<not found>'
97
- else :
98
- hash_from = 'repository'
99
- return hash_from , hash_str
97
+ from subprocess import run
98
+
99
+ if not COMMIT_HASH .startswith ('$Format' ): # it has been substituted
100
+ return 'archive substitution' , COMMIT_HASH
101
+ ver = Version (__version__ )
102
+ if ver .local is not None and ver .local .startswith ('g' ):
103
+ return ver .local [1 :8 ], 'installation'
104
+ # maybe we are in a repository
105
+ proc = run (
106
+ ('git' , 'rev-parse' , '--short' , 'HEAD' ),
107
+ capture_output = True ,
108
+ cwd = pkg_path ,
109
+ )
110
+ if proc .stdout :
111
+ return 'repository' , proc .stdout .strip ()
112
+ return '(none found)' , '<not found>'
100
113
101
114
102
115
def get_pkg_info (pkg_path ):
@@ -112,7 +125,7 @@ def get_pkg_info(pkg_path):
112
125
context : dict
113
126
with named parameters of interest
114
127
"""
115
- src , hsh = pkg_commit_hash ()
128
+ src , hsh = pkg_commit_hash (pkg_path )
116
129
import numpy
117
130
118
131
return dict (
0 commit comments