Skip to content

Commit c5495f5

Browse files
committed
Ensure apply_diff also includes new files when committing changes
1 parent b235bdb commit c5495f5

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

git_wrapper/branch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def apply_diff(self, branch_name, diff_path, message, signoff=False):
196196
:param bool signoff: Whether to add signed-off-by to commit message
197197
"""
198198
# Ensure we don't commit more than we mean to
199-
if self.git_repo.repo.is_dirty():
199+
if self.git_repo.repo.is_dirty(untracked_files=True):
200200
msg = ("Repository {repo} contains uncommitted changes. Please clean workspace "
201201
"before proceeding.".format(repo=self.git_repo.repo.working_dir))
202202
raise exceptions.DirtyRepositoryException(msg)
@@ -218,6 +218,9 @@ def apply_diff(self, branch_name, diff_path, message, signoff=False):
218218
msg = "Could not apply diff {path} on branch {name}. Error: {error}".format(path=full_path, name=branch_name, error=ex)
219219
raise_from(exceptions.ChangeNotAppliedException(msg), ex)
220220

221+
# The diff may have added new files, ensure they are staged
222+
self.git_repo.git.add(".")
223+
221224
# Commit
222225
self.git_repo.commit.commit(message, signoff)
223226

integration_tests/test_branch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def test_apply_diff(repo_root, datadir):
2525
assert "Test commit message" in message
2626
assert "Signed-off-by" in message
2727

28+
# Check the working directory is clean and the new files also committed
29+
assert repo.repo.is_dirty(untracked_files=True) is False
30+
2831

2932
def test_apply_patch(repo_root, patch_cleanup, datadir):
3033
repo = GitRepo(repo_root)

integration_tests/test_branch/test.diff

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@
99

1010
.. image:: https://img.shields.io/pypi/v/git_wrapper.svg
1111
:target: https://pypi.python.org/pypi/git_wrapper
12+
13+
--- /dev/null
14+
+++ a/newdir/newfile
15+
@@ -0,0 +1,1 @@
16+
+This is a test
17+
18+
--- /dev/null
19+
+++ a/.newhiddenfile
20+
@@ -0,0 +1,1 @@
21+
+This is a test adding a hidden file

tests/test_branch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def test_apply_diff(mock_repo):
352352

353353
with patch('git.repo.fun.name_to_object'):
354354
repo.branch.apply_diff('test_branch', './requirements.txt', 'message', True)
355+
assert repo.git.add.called is True
355356
assert repo.git.apply.called is True
356357
assert repo.git.commit.called is True
357358

0 commit comments

Comments
 (0)