Skip to content

Commit

Permalink
Merge pull request #4 from Wonshtrum/name_with_spaces
Browse files Browse the repository at this point in the history
Added git support for files with names containing spaces
  • Loading branch information
elmotec committed Jul 17, 2021
2 parents ae82a74 + 7b91ed0 commit 669e0bc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
35 changes: 19 additions & 16 deletions codemetrics/git.py
Expand Up @@ -42,8 +42,14 @@ def __init__(self, git_client=default_client, cwd: pl.Path = None, _pdb=False):
super().__init__(cwd=cwd)
self._pdb = _pdb
self.git_client = git_client
self.log_re = re.compile(
r"([-\d]+)\s+([-\d]+)\s+(.*)"
)
self.log_moved_re = re.compile(
r"([-\d]+)\s+([-\d]+)\s+(\S*)\{(\S*) => (\S*)\}(\S*)"
r"([-\d]+)\s+([-\d]+)\s+(.*) => (.*)"
)
self.log_curly_re = re.compile(
r"([-\d]+)\s+([-\d]+)\s+(.*)\{(.*) => (.*)\}(.*)"
)

def parse_path_elem(self, path_elem: str):
Expand All @@ -60,22 +66,19 @@ def parse_path_elem(self, path_elem: str):
"""
copy_from_path: typing.Optional[str] = None
if "{" not in path_elem:
if "=>" in path_elem:
added, removed, copy_from_path, _, rel_path = path_elem.split()
else:
added, removed, rel_path = path_elem.split()
match_log = self.log_re.match(path_elem)
match_log_moved = self.log_moved_re.match(path_elem)
match_log_curly = self.log_curly_re.match(path_elem)
if match_log_curly:
added, removed, base, from_mid, rel_mid, end = match_log_curly.groups()
rel_path = (base + rel_mid + end).replace("//", "/")
copy_from_path = (base + from_mid + end).replace("//", "/")
elif match_log_moved:
added, removed, copy_from_path, rel_path = match_log_moved.groups()
elif match_log:
added, removed, rel_path = match_log.groups()
else:
match = self.log_moved_re.match(path_elem)
if not match:
raise ValueError(f"{path_elem} not understood")
added = match.group(1)
removed = match.group(2)
rel_path = match.group(3) + match.group(5) + match.group(6)
rel_path = rel_path.replace("//", "/")
copy_from_path = (match.group(3) + match.group(4) + match.group(6)).replace(
"//", "/"
)
raise ValueError(f"{path_elem} not understood")
added_as_int = int(added) if added != "-" else np.nan
removed_as_int = int(removed) if removed != "-" else np.nan
return added_as_int, removed_as_int, rel_path, copy_from_path
Expand Down
54 changes: 54 additions & 0 deletions tests/test_git.py
Expand Up @@ -33,6 +33,33 @@ def test_parse_path_elem(self):
self.assertEqual("dir/test.py", relpath)
self.assertIsNone(copyfrompath)

def test_parse_path_elem_with_spaces(self):
"""Parsing of path element."""
pe = "21 2 dir 1/test 1.py"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertEqual(21, added)
self.assertEqual(2, removed)
self.assertEqual("dir 1/test 1.py", relpath)
self.assertIsNone(copyfrompath)

def test_parse_renamed_path_no_curly(self):
"""Parsing of path element."""
pe = "1 1 a.py => b.py"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertEqual(1, added)
self.assertEqual(1, removed)
self.assertEqual("b.py", relpath)
self.assertEqual("a.py", copyfrompath)

def test_parse_renamed_path_no_curly_with_spaces(self):
"""Parsing of path element."""
pe = "1 1 a 1.py => b 1.py"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertEqual(1, added)
self.assertEqual(1, removed)
self.assertEqual("b 1.py", relpath)
self.assertEqual("a 1.py", copyfrompath)

def test_parse_renamed_path(self):
"""Parsing of path element."""
pe = "1 1 dir/{b/a.py => a/b.py}"
Expand All @@ -42,6 +69,15 @@ def test_parse_renamed_path(self):
self.assertEqual("dir/a/b.py", relpath)
self.assertEqual("dir/b/a.py", copyfrompath)

def test_parse_renamed_path_with_spaces(self):
"""Parsing of path element."""
pe = "1 1 dir 1/{b 1/a a.py => a 1/b b.py}"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertEqual(1, added)
self.assertEqual(1, removed)
self.assertEqual("dir 1/a 1/b b.py", relpath)
self.assertEqual("dir 1/b 1/a a.py", copyfrompath)

def test_parse_renamed_path_empty_right(self):
"""Parsing of path element."""
pe = "21 2 dir/{category => }/test.py"
Expand All @@ -51,6 +87,15 @@ def test_parse_renamed_path_empty_right(self):
self.assertEqual("dir/test.py", relpath)
self.assertEqual("dir/category/test.py", copyfrompath)

def test_parse_renamed_path_empty_right_with_spaces(self):
"""Parsing of path element."""
pe = "21 2 dir 1/{category 1 => }/test 1.py"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertEqual(21, added)
self.assertEqual(2, removed)
self.assertEqual("dir 1/test 1.py", relpath)
self.assertEqual("dir 1/category 1/test 1.py", copyfrompath)

def test_parse_renamed_path_empty_left(self):
"""Parsing of path element."""
pe = "- - dir/{ => subdir}/file.py"
Expand All @@ -60,6 +105,15 @@ def test_parse_renamed_path_empty_left(self):
self.assertEqual("dir/subdir/file.py", relpath)
self.assertEqual("dir/file.py", copyfrompath)

def test_parse_renamed_path_empty_left_with_spaces(self):
"""Parsing of path element."""
pe = "- - dir 1/{ => subdir 1}/file 1.py"
added, removed, relpath, copyfrompath = self.git.parse_path_elem(pe)
self.assertTrue(np.isnan(added))
self.assertTrue(np.isnan(removed))
self.assertEqual("dir 1/subdir 1/file 1.py", relpath)
self.assertEqual("dir 1/file 1.py", copyfrompath)


def get_log():
retval = textwrap.dedent(
Expand Down

0 comments on commit 669e0bc

Please sign in to comment.