Skip to content

Commit 2d0048f

Browse files
authored
🐛 Make sure staged changes are applied to clones (#59)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description Make sure staged changes are applied to clones ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent d735945 commit 2d0048f

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

continuous_delivery_scripts/utils/git_helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ def uncommitted_changes(self) -> List[Path]:
601601

602602
return [Path(self.root).joinpath(line.strip().split(" ")[-1]) for line in status.splitlines()]
603603

604+
@property
605+
def uncommitted_staged_changes(self) -> List[Path]:
606+
"""Gets list of uncommitted staged changes.
607+
608+
Returns:
609+
list of uncommitted staged changes
610+
"""
611+
staged = self.repo.git.diff(staged=True, name_only=True)
612+
if not staged:
613+
return []
614+
615+
return [Path(self.root).joinpath(line.strip()) for line in staged.splitlines()]
616+
604617
@staticmethod
605618
def _apply_modifications(destination: Path, modified_file: Path) -> None:
606619
logger.info(f"Applying change in {modified_file} to {destination}")
@@ -627,6 +640,8 @@ def apply_uncommitted_changes(self, other_repo: "GitWrapper") -> None:
627640
GitWrapper._apply_modifications(destination, f)
628641
else:
629642
GitWrapper._apply_deletions(destination)
643+
for f in self.uncommitted_staged_changes:
644+
other_repo.add(f.relative_to(self.root))
630645

631646
def get_corresponding_path(self, path_in_initial_repo: Path) -> Path:
632647
"""Gets the path in current repository corresponding to path in initial repository.

news/20221221183120.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`[git]` :bug: make sure any staged changes are also applied to repository clones

tests/git_helper/test_git_helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ def test_git_clone_independent(self):
3838
self.assertEqual(git.get_remote_url(), clone.get_remote_url())
3939
self.assertNotEqual(git.root, clone.root)
4040

41+
def test_git_clone_roll_over_changes(self):
42+
"""Ensures staged changes in original repository are applied in clone."""
43+
with ProjectTempClone(desired_branch_name="main") as origin:
44+
test_file = Path(origin.root).joinpath(f"file-test-{uuid4()}.txt")
45+
test_file.touch()
46+
47+
uncommitted_changes = origin.uncommitted_changes
48+
staged_changes = origin.uncommitted_staged_changes
49+
self.assertTrue(origin.is_dirty())
50+
self.assertTrue(test_file in uncommitted_changes)
51+
self.assertFalse(test_file in staged_changes)
52+
origin.add(test_file)
53+
uncommitted_changes = origin.uncommitted_changes
54+
staged_changes = origin.uncommitted_staged_changes
55+
self.assertTrue(origin.is_dirty())
56+
self.assertTrue(test_file in uncommitted_changes)
57+
self.assertTrue(test_file in staged_changes)
58+
59+
with GitTempClone(repository_to_clone=origin, desired_branch_name="main") as clone:
60+
self.assertNotEqual(origin.root, clone.root)
61+
self.assertTrue(clone.is_dirty())
62+
uncommitted_changes = clone.uncommitted_changes
63+
staged_changes = clone.uncommitted_staged_changes
64+
cloned_test_file = clone.root.joinpath(test_file.relative_to(origin.root))
65+
self.assertIsNotNone(uncommitted_changes)
66+
self.assertIsNotNone(staged_changes)
67+
self.assertTrue(cloned_test_file in uncommitted_changes)
68+
self.assertTrue(cloned_test_file in staged_changes)
69+
4170
def test_git_branch_actions(self):
4271
"""Test basic git branch actions on the clone."""
4372
with ProjectTempClone(desired_branch_name="main") as clone:

0 commit comments

Comments
 (0)