Get the git version from the repository. This function runs `git describe ...` in the path given as `git_base_path`. This will return a string of the form: - - For example, 'v0.10.0-1585-gbb717a6' means v0.10.0 was the last tag when
(git_base_path, git_tag_override)
| 140 | |
| 141 | |
| 142 | def get_git_version(git_base_path, git_tag_override): |
| 143 | """Get the git version from the repository. |
| 144 | |
| 145 | This function runs `git describe ...` in the path given as `git_base_path`. |
| 146 | This will return a string of the form: |
| 147 | <base-tag>-<number of commits since tag>-<shortened sha hash> |
| 148 | |
| 149 | For example, 'v0.10.0-1585-gbb717a6' means v0.10.0 was the last tag when |
| 150 | compiled. 1585 commits are after that commit tag, and we can get back to this |
| 151 | version by running `git checkout gbb717a6`. |
| 152 | |
| 153 | Args: |
| 154 | git_base_path: where the .git directory is located |
| 155 | git_tag_override: Override the value for the git tag. This is useful for |
| 156 | releases where we want to build the release before the git tag is |
| 157 | created. |
| 158 | Returns: |
| 159 | A bytestring representing the git version |
| 160 | """ |
| 161 | unknown_label = b"unknown" |
| 162 | try: |
| 163 | # Force to bytes so this works on python 2 and python 3 |
| 164 | val = bytes(subprocess.check_output([ |
| 165 | "git", str("--git-dir=%s/.git" % git_base_path), |
| 166 | str("--work-tree=" + git_base_path), "describe", "--long", "--tags" |
| 167 | ]).strip()) |
| 168 | version_separator = b"-" |
| 169 | if git_tag_override and val: |
| 170 | split_val = val.split(version_separator) |
| 171 | if len(split_val) < 3: |
| 172 | raise Exception( |
| 173 | ("Expected git version in format 'TAG-COMMITS AFTER TAG-HASH' " |
| 174 | "but got '%s'") % val) |
| 175 | # There might be "-" in the tag name. But we can be sure that the final |
| 176 | # two "-" are those inserted by the git describe command. |
| 177 | abbrev_commit = split_val[-1] |
| 178 | val = version_separator.join( |
| 179 | [bytes(git_tag_override, "utf-8"), b"0", abbrev_commit]) |
| 180 | return val if val else unknown_label |
| 181 | except (subprocess.CalledProcessError, OSError): |
| 182 | return unknown_label |
| 183 | |
| 184 | |
| 185 | def write_version_info(filename, git_version): |
no test coverage detected