Skip to content

Commit b4c3c4a

Browse files
committed
Bug 1542625 - worktree should be clean if untracked files are present when applying a patch; r=zalun!
Exclude untracked files when determing current state, and use `--index` to add new files while applying the patch. Differential Revision: https://phabricator.services.mozilla.com/D31193
1 parent 1feb537 commit b4c3c4a

4 files changed

Lines changed: 14 additions & 17 deletions

File tree

moz-phab

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,9 @@ class Git(Repository):
13321332
self.branch = None
13331333

13341334
def is_worktree_clean(self):
1335-
return self.git_out(["status", "--short"], split=False) == ""
1335+
return all(
1336+
[l.startswith("?? ") for l in self.git_out(["status", "--porcelain"])]
1337+
)
13361338

13371339
def before_submit(self):
13381340
# Store current branch (fails if HEAD in detached state)
@@ -1698,10 +1700,6 @@ class Git(Repository):
16981700
def checkout(self, node):
16991701
self.git(["checkout", "--quiet", node])
17001702

1701-
def add(self):
1702-
"""Add new files in the working directory."""
1703-
self.git(["add", "."])
1704-
17051703
def commit(self, body, author=None, author_date=None):
17061704
"""Commit the changes in the working directory."""
17071705
commands = ["commit", "-a"]
@@ -1759,8 +1757,7 @@ class Git(Repository):
17591757

17601758
def apply_patch(self, diff, body, author, author_date):
17611759
with temporary_file(diff) as patch_file:
1762-
self.git(["apply", patch_file])
1763-
self.add()
1760+
self.git(["apply", "--index", patch_file])
17641761
self.commit(body, author, author_date)
17651762

17661763
def _get_current_head(self):

tests/test_git.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,14 @@ def test_worktree_clean(m_git_out, git):
257257
m_git_out.return_value = ""
258258
assert git.is_worktree_clean()
259259

260-
m_git_out.return_value = "xxx"
260+
m_git_out.return_value = ["xxx"]
261261
assert not git.is_worktree_clean()
262262

263+
m_git_out.return_value = ["?? one", "?? two"]
264+
assert git.is_worktree_clean()
263265

264-
@mock.patch("mozphab.Git.git")
265-
def test_add(m_git, git):
266-
git.add()
267-
assert m_git.called_once_with(["add", "."])
266+
m_git_out.return_value = ["?? one", "?? two", " M xxx"]
267+
assert not git.is_worktree_clean()
268268

269269

270270
@mock.patch("mozphab.Git.git")
@@ -375,13 +375,11 @@ def __init__(
375375

376376
@mock.patch("mozphab.temporary_file")
377377
@mock.patch("mozphab.Git.git")
378-
@mock.patch("mozphab.Git.add")
379378
@mock.patch("mozphab.Git.commit")
380-
def test_apply_patch(m_commit, m_add, m_git, m_temp_fn, git):
379+
def test_apply_patch(m_commit, m_git, m_temp_fn, git):
381380
m_temp_fn.return_value = create_temp_fn("filename")
382381
git.apply_patch("diff", "commit message", "user", 1)
383-
m_git.assert_called_once_with(["apply", "filename"])
384-
m_add.assert_called_once()
382+
m_git.assert_called_once_with(["apply", "--index", "filename"])
385383
m_commit.assert_called_with("commit message", "user", 1)
386384
m_temp_fn.assert_called_once_with("diff")
387385

tests/test_integration-git.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def test_submit_create(in_process, git_repo_path, init_sha):
3636
testfile.write_text(u"a")
3737
git_out("add", ".")
3838
git_out("commit", "--message", "A r?alice")
39+
testfile = git_repo_path / "untracked"
40+
testfile.write_text(u"a")
3941

4042
mozphab.main(["submit", "--yes", "--bug", "1", init_sha])
4143

tests/test_integration-patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_git_patch_with_commit(
138138
result = git_out("branch")
139139
assert "* D1" in result
140140

141-
time.sleep(1)
141+
time.sleep(1) # to ensure the patch is applied with a different timestamp
142142
mozphab.main(["patch", "D1"])
143143
assert [".arcconfig", ".git", "X"] == sorted(os.listdir(str(git_repo_path)))
144144
test_file = git_repo_path / "X"

0 commit comments

Comments
 (0)