Skip to content

Commit

Permalink
[git] Fix file stats for merged files on subdirs
Browse files Browse the repository at this point in the history
Stats about changes on a file were not reported correctly
for files that where moved to a subdirectory. They
were reported as an invalid entry and without
action associated.

For example, the file `dir/filename` was moved to
`dir/subdir/filename, but it was reported as `dir//filename`.
Therefore, the entry of the file `dir/subdire/filename`
didn't have the stats about added and deleted lines.
This error has been fixed and stats are reported correctly.

Signed-off-by: Santiago Dueñas <[email protected]>
  • Loading branch information
sduenas committed Jun 10, 2024
1 parent dcf3777 commit fc7c9d7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
9 changes: 8 additions & 1 deletion perceval/backends/core/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,14 @@ def __get_old_filepath(self, f):
prefix = f[0:i]
inner = f[i + 1:f.find(' => ', i)]
suffix = f[j + 1:]
return prefix + inner + suffix
old_filepath = prefix + inner + suffix

# Remove double '/' for corner cases like
# 'dir/{ => subdir}/filename'.
# The resulting old path on these entries is 'dir//filename'.
old_filepath = old_filepath.replace('//', '/')

return old_filepath
elif ' => ' in f:
return f.split(' => ')[0]
else:
Expand Down
16 changes: 16 additions & 0 deletions releases/unreleased/wrong-filepath-for-movedcopied-files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Empty stats for moved/copied files in git
category: fixed
author: Santiago Dueñas <[email protected]>
issue: null
notes: >
Stats about changes on a file were not reported correctly
for files that where moved to a subdirectory. They
were reported as an invalid entry and without
action associated.
For example, the file `dir/filename` was moved to `dir/subdir/filename,
but it was reported as `dir//filename`. Therefore,
the entry of the file `dir/subdire/filename` didn't
have the stats about added and deleted lines.
This error has been fixed and stats are reported
correctly.
12 changes: 12 additions & 0 deletions tests/data/git/git_log_moved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
commit 1e6250992e6cddebfaf4e6fdf241171091874523 ff93e98029e7b8356bf87456df0254332f4a6549
Author: John Smith <[email protected]>
AuthorDate: Tue Nov 22 14:00:06 2016 +0100
Commit: John Smith <[email protected]>
CommitDate: Tue Nov 22 14:00:06 2016 +0100

[backends] Move backends to core sub-package

:000000 100644 0000000 e69de29 A perceval/backends/core/__init__.py
:100644 100644 232acb3 7923641 R098 perceval/backends/bugzilla.py perceval/backends/core/bugzilla.py
0 0 perceval/backends/core/__init__.py
4 4 perceval/backends/{ => core}/bugzilla.py
41 changes: 41 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,47 @@ def test_parser(self):

self.assertDictEqual(commits[9], expected)

def test_parser_renamed_files(self):
"""Check it moved or copied files are correctly renamed"""

with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/git/git_log_moved.txt"), 'r') as f:
parser = GitParser(f)
commits = [commit for commit in parser.parse()]

self.assertEqual(len(commits), 1)

expected = {
'commit': '1e6250992e6cddebfaf4e6fdf241171091874523',
'parents': ['ff93e98029e7b8356bf87456df0254332f4a6549'],
'refs': [],
'Author': 'John Smith <[email protected]>',
'AuthorDate': 'Tue Nov 22 14:00:06 2016 +0100',
'Commit': 'John Smith <[email protected]>',
'CommitDate': 'Tue Nov 22 14:00:06 2016 +0100',
'message': '[backends] Move backends to core sub-package',
'files': [
{
'file': 'perceval/backends/bugzilla.py',
'newfile': 'perceval/backends/core/bugzilla.py',
'added': '4',
'removed': '4',
'modes': ['100644', '100644'],
'indexes': ['232acb3', '7923641'],
'action': 'R098'
},
{
'file': 'perceval/backends/core/__init__.py',
'added': '0',
'removed': '0',
'modes': ['000000', '100644'],
'indexes': ['0000000', 'e69de29'],
'action': 'A'
}
]
}

self.assertDictEqual(commits[0], expected)

def test_parser_merge_commit(self):
"""Test if it parses all the available data on a merge commit"""

Expand Down

0 comments on commit fc7c9d7

Please sign in to comment.