-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgit.py
288 lines (229 loc) · 9.91 KB
/
git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Git source control management implementation."""
import os
import re
import shlex
import subprocess
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, ClassVar, List, MutableMapping, Optional
from bumpversion.exceptions import DirtyWorkingDirectoryError
from bumpversion.scm.models import LatestTagInfo, SCMConfig
from bumpversion.ui import get_indented_logger
from bumpversion.utils import Pathlike, format_and_raise_error, is_subpath, run_command
logger = get_indented_logger(__name__)
class Git:
"""Git implementation."""
_TEST_AVAILABLE_COMMAND: ClassVar[List[str]] = ["git", "rev-parse", "--git-dir"]
_COMMIT_COMMAND: ClassVar[List[str]] = ["git", "commit", "-F"]
_ALL_TAGS_COMMAND: ClassVar[List[str]] = ["git", "tag", "--list"]
def __init__(self, config: SCMConfig):
self.config = config
self._latest_tag_info: Optional[LatestTagInfo] = None
def __repr__(self) -> str:
"""Return a string representation of the SCMTool."""
return self.__str__()
def __str__(self) -> str:
"""A string representation of the object."""
return "Git"
def is_available(self) -> bool:
"""Is the VCS implementation usable?"""
try:
result = run_command(self._TEST_AVAILABLE_COMMAND)
return result.returncode == 0
except (FileNotFoundError, PermissionError, NotADirectoryError, subprocess.CalledProcessError):
return False
def latest_tag_info(self) -> LatestTagInfo:
"""Return information about the latest tag."""
if self._latest_tag_info is not None:
return self._latest_tag_info
if not self.is_available():
return LatestTagInfo()
info: dict[str, Any] = {}
update_index()
info |= commit_info(self.config)
info |= revision_info()
self._latest_tag_info = LatestTagInfo(**info)
return self._latest_tag_info
def add_path(self, path: Pathlike) -> None:
"""Add a path to the VCS."""
repository_root = self.latest_tag_info().repository_root
if not (repository_root and is_subpath(repository_root, path)):
return
cwd = Path.cwd()
temp_path = os.path.relpath(path, cwd)
try:
run_command(["git", "add", "--update", str(temp_path)])
except subprocess.CalledProcessError as e:
format_and_raise_error(e)
def get_all_tags(self) -> List[str]:
"""Return all tags in git."""
try:
result = run_command(self._ALL_TAGS_COMMAND)
return result.stdout.splitlines()
except ( # pragma: no-coverage
FileNotFoundError,
PermissionError,
NotADirectoryError,
subprocess.CalledProcessError,
):
return []
def commit_and_tag(self, files: List[Pathlike], context: MutableMapping, dry_run: bool = False) -> None:
"""Commit and tag files to the repository using the configuration."""
if dry_run:
return
if self.config.commit:
for path in files:
self.add_path(path)
self.commit(context)
if self.config.tag:
tag_name = self.config.tag_name.format(**context)
tag_message = self.config.tag_message.format(**context)
tag(tag_name, sign=self.config.sign_tags, message=tag_message)
for m_tag_name in self.config.moveable_tags:
moveable_tag(m_tag_name)
def assert_nondirty(self) -> None:
"""
Asserts that the repository is not dirty.
Raises:
DirtyWorkingDirectoryError: If the repository is not clean.
"""
assert_nondirty()
def commit(self, context: MutableMapping) -> None:
"""Commit the changes."""
extra_args = shlex.split(self.config.commit_args) if self.config.commit_args else []
current_version = context.get("current_version", "")
new_version = context.get("new_version", "")
commit_message = self.config.message.format(**context)
if not current_version: # pragma: no-coverage
logger.warning("No current version given, using an empty string.")
if not new_version: # pragma: no-coverage
logger.warning("No new version given, using an empty string.")
with NamedTemporaryFile("wb", delete=False) as f:
f.write(commit_message.encode("utf-8"))
env = os.environ.copy()
env["BUMPVERSION_CURRENT_VERSION"] = current_version
env["BUMPVERSION_NEW_VERSION"] = new_version
try:
cmd = [*self._COMMIT_COMMAND, f.name, *extra_args]
run_command(cmd, env=env)
except (subprocess.CalledProcessError, TypeError) as exc: # pragma: no-coverage
format_and_raise_error(exc)
finally:
os.unlink(f.name)
def update_index() -> None:
"""Update the git index."""
try:
run_command(["git", "update-index", "--refresh", "-q"])
except subprocess.CalledProcessError as e:
logger.debug("Error when running git update-index: %s", e.stderr)
def commit_info(config: SCMConfig) -> dict:
"""
Get the commit info for the repo.
Args:
config: The source control configuration.
Returns:
A dictionary containing information about the latest commit.
"""
tag_pattern = config.tag_name.replace("{new_version}", "*")
info = dict.fromkeys(["dirty", "commit_sha", "distance_to_latest_tag", "current_version", "current_tag"])
info["distance_to_latest_tag"] = 0
try:
git_cmd = ["git", "describe", "--dirty", "--tags", "--long", "--abbrev=40", f"--match={tag_pattern}"]
result = run_command(git_cmd)
except subprocess.CalledProcessError as e:
if e.stderr and "fatal: no names found, cannot describe anything." in e.stderr:
logger.debug("No tags found, returning default values.")
else:
logger.debug("Error when running git describe: %s", e.stderr)
return info
describe_out = result.stdout.strip().split("-")
if describe_out[-1].strip() == "dirty":
info["dirty"] = True
describe_out.pop()
else:
info["dirty"] = False
info["commit_sha"] = describe_out.pop().lstrip("g")
info["distance_to_latest_tag"] = int(describe_out.pop())
info["current_tag"] = "-".join(describe_out)
version = config.get_version_from_tag("-".join(describe_out))
info["current_version"] = version or "-".join(describe_out).lstrip("v")
return info
def revision_info() -> dict:
"""
Returns a dictionary containing revision information.
If an error occurs while running the git command, the dictionary values will be set to None.
Returns:
A dictionary with the following keys:
- branch_name: The name of the current branch.
- short_branch_name: A 20 lowercase characters of the branch name with special characters removed.
- repository_root: The root directory of the Git repository.
"""
info = dict.fromkeys(["branch_name", "short_branch_name", "repository_root"])
repo_root_command = ["git", "rev-parse", "--show-toplevel"]
current_branch_command = ["git", "branch", "--show-current"]
try:
repository_root_result = run_command(repo_root_command)
except subprocess.CalledProcessError as e:
logger.debug("Error when determining the repository root: %s", e.stderr)
return info
try:
branch_name_result = run_command(current_branch_command)
except subprocess.CalledProcessError as e:
logger.debug("Error when determining the current branch: %s", e.stderr)
return info
repository_root = Path(repository_root_result.stdout.strip())
branch_name = branch_name_result.stdout.strip()
short_branch_name = re.sub(r"([^a-zA-Z0-9]*)", "", branch_name).lower()[:20]
info["branch_name"] = branch_name
info["short_branch_name"] = short_branch_name
info["repository_root"] = repository_root
return info
def tag(name: str, sign: bool = False, message: Optional[str] = None) -> None:
"""
Create a tag of the new_version in git.
If only name is given, bumpversion uses a lightweight tag.
Otherwise, it uses an annotated tag.
Args:
name: The name of the tag
sign: True to sign the tag
message: An optional message to annotate the tag.
"""
command = ["git", "tag", name]
if sign:
command += ["--sign"]
if message:
command += ["--message", message]
try:
run_command(command)
except subprocess.CalledProcessError as e:
format_and_raise_error(e)
def moveable_tag(name: str) -> None:
"""
Create a new lightweight tag that should overwrite any previous tags with the same name.
Args:
name: The name of the moveable tag.
"""
try:
run_command(["git", "tag", "-f", name])
push_remote("origin", name, force=True)
except subprocess.CalledProcessError as e:
format_and_raise_error(e)
def assert_nondirty() -> None:
"""Assert that the working directory is not dirty."""
lines = [
line.strip()
for line in run_command(["git", "status", "--porcelain"]).stdout.splitlines()
if not line.strip().startswith("??")
]
if joined_lines := "\n".join(lines):
raise DirtyWorkingDirectoryError(f"Git working directory is not clean:\n\n{joined_lines}")
def push_remote(remote_name: str, ref_name: str, force: bool = False) -> None:
"""Push the `ref_name` to the `remote_name` repository, optionally forcing the push."""
try:
result = run_command(["git", "remote"])
if remote_name not in result.stdout:
logger.warning("Remote '%s' not found, skipping push.", remote_name)
return
run_command(["git", "push", remote_name, ref_name, "--force" if force else ""])
except subprocess.CalledProcessError as e:
format_and_raise_error(e)