Fix a bug in getting changed files
The original implementation takes into account the changed fils from all commits of a PR. It causes a bug when files get changed and reverted in those commits. e.g. A file is added in first commit then removed in second commit, this file should should not be considered as a changed file in the PR. Change-Id: I7db8b9d3f3267073c5e1a71f52e75939ffa91773
This commit is contained in:
parent
26ceb3fee8
commit
e63d7b0cdb
|
@ -5175,8 +5175,9 @@ class ZuulTestCase(BaseTestCase):
|
||||||
repo.head.reset(working_tree=True)
|
repo.head.reset(working_tree=True)
|
||||||
repo.delete_head(repo.heads[branch], force=True)
|
repo.delete_head(repo.heads[branch], force=True)
|
||||||
|
|
||||||
def create_commit(self, project, files=None, head='master',
|
def create_commit(self, project, files=None, delete_files=None,
|
||||||
message='Creating a fake commit', **kwargs):
|
head='master', message='Creating a fake commit',
|
||||||
|
**kwargs):
|
||||||
path = os.path.join(self.upstream_root, project)
|
path = os.path.join(self.upstream_root, project)
|
||||||
repo = git.Repo(path)
|
repo = git.Repo(path)
|
||||||
repo.head.reference = repo.heads[head]
|
repo.head.reference = repo.heads[head]
|
||||||
|
@ -5188,6 +5189,12 @@ class ZuulTestCase(BaseTestCase):
|
||||||
with open(file_name, 'a') as f:
|
with open(file_name, 'a') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
repo.index.add([file_name])
|
repo.index.add([file_name])
|
||||||
|
|
||||||
|
delete_files = delete_files or []
|
||||||
|
for name in delete_files:
|
||||||
|
file_name = os.path.join(path, name)
|
||||||
|
repo.index.remove([file_name])
|
||||||
|
|
||||||
commit = repo.index.commit(message, **kwargs)
|
commit = repo.index.commit(message, **kwargs)
|
||||||
return commit.hexsha
|
return commit.hexsha
|
||||||
|
|
||||||
|
|
|
@ -353,6 +353,33 @@ class TestMergerRepo(ZuulTestCase):
|
||||||
|
|
||||||
self.assertEqual(['README'], changed_files)
|
self.assertEqual(['README'], changed_files)
|
||||||
|
|
||||||
|
def test_files_changes_add_and_remove_files(self):
|
||||||
|
"""
|
||||||
|
If the changed files in previous commits are reverted in later commits,
|
||||||
|
they should not be considered as changed in the PR.
|
||||||
|
"""
|
||||||
|
parent_path = os.path.join(self.upstream_root, 'org/project1')
|
||||||
|
self.create_branch('org/project1', 'feature1')
|
||||||
|
|
||||||
|
work_repo = Repo(parent_path, self.workspace_root,
|
||||||
|
'none@example.org', 'User Name', '0', '0')
|
||||||
|
|
||||||
|
# Add a file in first commit
|
||||||
|
files = {'to-be-deleted.txt': 'test'}
|
||||||
|
self.create_commit('org/project1', files=files, head='feature1',
|
||||||
|
message='Add file')
|
||||||
|
changed_files = work_repo.getFilesChanges('feature1', 'master')
|
||||||
|
self.assertEqual(sorted(['README', 'to-be-deleted.txt']),
|
||||||
|
sorted(changed_files))
|
||||||
|
|
||||||
|
# Delete the file in second commit
|
||||||
|
delete_files = ['to-be-deleted.txt']
|
||||||
|
self.create_commit('org/project1', files={},
|
||||||
|
delete_files=delete_files, head='feature1',
|
||||||
|
message='Delete file')
|
||||||
|
changed_files = work_repo.getFilesChanges('feature1', 'master')
|
||||||
|
self.assertEqual(['README'], changed_files)
|
||||||
|
|
||||||
def test_files_changes_master_fork_merges(self):
|
def test_files_changes_master_fork_merges(self):
|
||||||
"""Regression test for getFilesChanges()
|
"""Regression test for getFilesChanges()
|
||||||
|
|
||||||
|
|
|
@ -673,9 +673,9 @@ class Repo(object):
|
||||||
files = set()
|
files = set()
|
||||||
|
|
||||||
if tosha:
|
if tosha:
|
||||||
commit_diff = "{}..{}".format(tosha, head.hexsha)
|
head_commit = repo.commit(head.hexsha)
|
||||||
for cmt in repo.iter_commits(commit_diff, no_merges=True):
|
diff_index = head_commit.diff(tosha)
|
||||||
files.update(cmt.stats.files.keys())
|
files.update((item.a_path for item in diff_index))
|
||||||
else:
|
else:
|
||||||
files.update(head.stats.files.keys())
|
files.update(head.stats.files.keys())
|
||||||
return list(files)
|
return list(files)
|
||||||
|
|
Loading…
Reference in New Issue