Skip to content

Commit 533cc75

Browse files
committed
refactor(git): Extract add and commit into individual methods
1 parent f0256f5 commit 533cc75

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/dda/tools/git.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from dda.utils.git.remote import Remote
1313

1414
if TYPE_CHECKING:
15+
from collections.abc import Iterable
16+
1517
from dda.utils.fs import Path
1618
from dda.utils.git.commit import Commit, CommitDetails
1719

@@ -124,13 +126,29 @@ def get_commit_details(self, sha1: str) -> CommitDetails:
124126
parent_shas=list(parents_str.split()),
125127
)
126128

129+
def add(self, paths: Iterable[Path]) -> None:
130+
"""
131+
Add the given paths to the index.
132+
Will fail if any path is not under cwd.
133+
"""
134+
self.run(["add", *[str(path) for path in paths]])
135+
136+
def commit(self, message: str, *, allow_empty: bool = False) -> None:
137+
"""
138+
Commit the changes in the index.
139+
"""
140+
args = ["commit", "-m", message]
141+
if allow_empty:
142+
args.append("--allow-empty")
143+
self.run(args)
144+
127145
def commit_file(self, path: Path, *, content: str, commit_message: str) -> None:
128146
"""
129147
Create and commit a single file with the given content.
130148
"""
131149
path.write_text(content)
132-
self.run(["add", str(path)]) # Will fail if path is not under cwd
133-
self.run(["commit", "-m", commit_message])
150+
self.add((path,))
151+
self.commit(commit_message)
134152

135153
def _capture_diff_lines(self, *args: str, **kwargs: Any) -> list[str]:
136154
diff_args = [

0 commit comments

Comments
 (0)