Skip to content

Commit 99d0bef

Browse files
committed
Do the SHA normalisation in a central place
1 parent ba2b6f7 commit 99d0bef

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/base.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def parse_line(cls, line):
6161
"""
6262
# re's module-level functions like match(...) internally cache the compiled form of pattern strings.
6363
m = re.match(pattern, line)
64-
return m.groupdict() if m else {}
64+
return cls.postprocess_parse_result(m)
6565

6666
# @todo Add a test for the `parse_line_with_relative_date` function in test_parsing.py
6767
@classmethod
@@ -83,7 +83,19 @@ def parse_line_with_relative_date(cls, line):
8383
"""
8484
# re's module-level functions like match(...) internally cache the compiled form of pattern strings.
8585
m = re.match(pattern, line)
86-
return m.groupdict() if m else {}
86+
return cls.postprocess_parse_result(m)
87+
88+
@classmethod
89+
def postprocess_parse_result(cls, match):
90+
if match:
91+
d = match.groupdict()
92+
# The SHA output by `git blame` may have a leading caret to indicate that it
93+
# is a "boundary commit". That needs to be stripped before passing the SHA
94+
# back to git CLI commands for other purposes.
95+
d["sha_normalised"] = d["sha"].strip("^")
96+
return d
97+
else:
98+
return {}
8799

88100
def handle_phantom_button(self, href):
89101
url = urlparse(href)

src/blame.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,11 @@ def run(self, edit, prevving=False, fixed_row_num=None, sha_skip_list=[]):
6868
)
6969
return
7070
sha = blame["sha"]
71+
sha_normalised = blame["sha_normalised"]
7172
author = blame["author"]
7273
date = blame["date"]
7374
time = blame["time"]
7475

75-
# The SHA output by `git blame` may have a leading caret to indicate that it
76-
# is a "boundary commit". That needs to be stripped before passing the SHA
77-
# back to git CLI commands for other purposes.
78-
sha_normalised = sha.strip("^")
79-
8076
if sha_skip_list:
8177
recently_skipped_sha = sha_skip_list[-1]
8278
if sha_normalised == recently_skipped_sha:

tests/test_parsing.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
# This strange form of import is required because our ST package name has a space in it.
77
base = importlib.import_module("Git blame.src.base")
88

9-
# NOTE: The sample git-blame CLI outputs were taken from the https://github.com/sublimelsp/LSP
10-
# repo because they exhibit different numbers of components in author names.
11-
129

1310
class TestParsing(unittest.TestCase):
1411
def test_git_blame_cli_output_parsing(self):
1512
samples = [
13+
# NOTE: This section of sample git-blame CLI outputs were taken from the
14+
# https://github.com/sublimelsp/LSP repo because they exhibit different
15+
# numbers of components in author names:
1616
(
1717
r"""4a3eb02f plugin/diagnostics.py (Tom van Ommeren 2019-11-27 21:42:13 +0100 1) import html""",
1818
{
@@ -21,6 +21,7 @@ def test_git_blame_cli_output_parsing(self):
2121
"file": "plugin/diagnostics.py",
2222
"line_number": "1",
2323
"sha": "4a3eb02f",
24+
"sha_normalised": "4a3eb02f",
2425
"time": "21:42:13",
2526
"timezone": "+0100",
2627
},
@@ -33,6 +34,7 @@ def test_git_blame_cli_output_parsing(self):
3334
"file": "plugin/diagnostics.py",
3435
"line_number": "114",
3536
"sha": "c937eff9",
37+
"sha_normalised": "c937eff9",
3638
"time": "14:29:47",
3739
"timezone": "+0100",
3840
},
@@ -45,10 +47,27 @@ def test_git_blame_cli_output_parsing(self):
4547
"file": "plugin/diagnostics.py",
4648
"line_number": "272",
4749
"sha": "16a30de1",
50+
"sha_normalised": "16a30de1",
4851
"time": "08:34:46",
4952
"timezone": "+0200",
5053
},
5154
),
55+
#
56+
# A "boundary commit" SHA:
57+
#
58+
(
59+
r"""^ad61094 main.go (Duncan Holm 2016-01-18 21:22:16 +0000 1) package main""",
60+
{
61+
"author": "Duncan Holm",
62+
"date": "2016-01-18",
63+
"file": "main.go",
64+
"line_number": "1",
65+
"sha": "^ad61094",
66+
"sha_normalised": "ad61094",
67+
"time": "21:22:16",
68+
"timezone": "+0000",
69+
},
70+
),
5271
]
5372
for cli_output_line, expected_result in samples:
5473
self.assertEqual(

0 commit comments

Comments
 (0)