Skip to content

Commit b1ca38e

Browse files
committed
Bugfix in regexp to work with git 1.8 output
The screen output of 'git branch -vv' changed between version 1.8 and 2.10. Update the regexp parsing this output to be compatible with both versions. Add new unit test for this output. Testing: make test - pass, 1 skip - python2/3 manual testing - escomp-cesm, clm-demo, cesm-demo, checkout, status - ok
1 parent a93458b commit b1ca38e

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

manic/repository_git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class GitRepository(Repository):
3838

3939
# match XYZ of '* (HEAD detached at {XYZ}):
4040
# e.g. * (HEAD detached at origin/feature-2)
41-
RE_DETACHED = re.compile(r'\* \([\w]+[\s]+detached at ([\w\-./]+)\)')
41+
RE_DETACHED = re.compile(
42+
r'\* \((?:[\w]+[\s]+)?detached (?:at|from) ([\w\-./]+)\)')
4243

4344
# match tracking reference info, return XYZ from [XYZ]
4445
# e.g. [origin/master]

test/test_unit_repository_git.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
already in the python path.
77
88
"""
9+
# pylint: disable=too-many-lines,protected-access
910

1011
from __future__ import absolute_import
1112
from __future__ import unicode_literals
@@ -22,7 +23,12 @@
2223
from manic.externals_description import ExternalsDescriptionDict
2324
from manic.global_constants import EMPTY_STR
2425

25-
# pylint: disable=W0212
26+
# pylint: disable=C0103
27+
GIT_BRANCH_OUTPUT_DETACHED_BRANCH_v1_8 = '''
28+
* (detached from origin/feature2) 36418b4 Work on feature2
29+
master 9b75494 [origin/master] Initialize repository.
30+
'''
31+
# pylint: enable=C0103
2632

2733

2834
GIT_BRANCH_OUTPUT_DETACHED_BRANCH = '''
@@ -126,6 +132,16 @@ def test_ref_detached_branch(self):
126132
git_output)
127133
self.assertEqual(result, expected)
128134

135+
def test_ref_detached_branch_v1_8(self):
136+
"""Test that we can identify ref is detached from a remote branch
137+
138+
"""
139+
git_output = GIT_BRANCH_OUTPUT_DETACHED_BRANCH_v1_8
140+
expected = 'origin/feature2'
141+
result = self._repo._current_ref_from_branch_command(
142+
git_output)
143+
self.assertEqual(result, expected)
144+
129145
def test_ref_tracking_branch(self):
130146
"""Test that we correctly identify we are on a tracking branch
131147
"""
@@ -604,9 +620,12 @@ class TestGitRegExp(unittest.TestCase):
604620
def setUp(self):
605621
"""Common constans
606622
"""
607-
self._detached_tmpl = string.Template(
623+
self._detached_git_v2_tmpl = string.Template(
608624
'* (HEAD detached at $ref) 36418b4 Work on feature-2')
609625

626+
self._detached_git_v1_tmpl = string.Template(
627+
'* (detached from $ref) 36418b4 Work on feature-2')
628+
610629
self._tracking_tmpl = string.Template(
611630
'* feature-2 36418b4 [$ref] Work on feature-2')
612631

@@ -617,7 +636,11 @@ def test_re_detached_alphnum(self):
617636
"""Test re correctly matches alphnumeric (basic debugging)
618637
"""
619638
value = 'feature2'
620-
input_str = self._detached_tmpl.substitute(ref=value)
639+
input_str = self._detached_git_v2_tmpl.substitute(ref=value)
640+
match = GitRepository.RE_DETACHED.search(input_str)
641+
self.assertIsNotNone(match)
642+
self.assertEqual(match.group(1), value)
643+
input_str = self._detached_git_v1_tmpl.substitute(ref=value)
621644
match = GitRepository.RE_DETACHED.search(input_str)
622645
self.assertIsNotNone(match)
623646
self.assertEqual(match.group(1), value)
@@ -626,7 +649,11 @@ def test_re_detached_underscore(self):
626649
"""Test re matches with underscore
627650
"""
628651
value = 'feature_2'
629-
input_str = self._detached_tmpl.substitute(ref=value)
652+
input_str = self._detached_git_v2_tmpl.substitute(ref=value)
653+
match = GitRepository.RE_DETACHED.search(input_str)
654+
self.assertIsNotNone(match)
655+
self.assertEqual(match.group(1), value)
656+
input_str = self._detached_git_v1_tmpl.substitute(ref=value)
630657
match = GitRepository.RE_DETACHED.search(input_str)
631658
self.assertIsNotNone(match)
632659
self.assertEqual(match.group(1), value)
@@ -635,7 +662,11 @@ def test_re_detached_hyphen(self):
635662
"""Test re matches -
636663
"""
637664
value = 'feature-2'
638-
input_str = self._detached_tmpl.substitute(ref=value)
665+
input_str = self._detached_git_v2_tmpl.substitute(ref=value)
666+
match = GitRepository.RE_DETACHED.search(input_str)
667+
self.assertIsNotNone(match)
668+
self.assertEqual(match.group(1), value)
669+
input_str = self._detached_git_v1_tmpl.substitute(ref=value)
639670
match = GitRepository.RE_DETACHED.search(input_str)
640671
self.assertIsNotNone(match)
641672
self.assertEqual(match.group(1), value)
@@ -644,7 +675,11 @@ def test_re_detached_period(self):
644675
"""Test re matches .
645676
"""
646677
value = 'feature.2'
647-
input_str = self._detached_tmpl.substitute(ref=value)
678+
input_str = self._detached_git_v2_tmpl.substitute(ref=value)
679+
match = GitRepository.RE_DETACHED.search(input_str)
680+
self.assertIsNotNone(match)
681+
self.assertEqual(match.group(1), value)
682+
input_str = self._detached_git_v1_tmpl.substitute(ref=value)
648683
match = GitRepository.RE_DETACHED.search(input_str)
649684
self.assertIsNotNone(match)
650685
self.assertEqual(match.group(1), value)
@@ -653,7 +688,11 @@ def test_re_detached_slash(self):
653688
"""Test re matches /
654689
"""
655690
value = 'feature/2'
656-
input_str = self._detached_tmpl.substitute(ref=value)
691+
input_str = self._detached_git_v2_tmpl.substitute(ref=value)
692+
match = GitRepository.RE_DETACHED.search(input_str)
693+
self.assertIsNotNone(match)
694+
self.assertEqual(match.group(1), value)
695+
input_str = self._detached_git_v1_tmpl.substitute(ref=value)
657696
match = GitRepository.RE_DETACHED.search(input_str)
658697
self.assertIsNotNone(match)
659698
self.assertEqual(match.group(1), value)

0 commit comments

Comments
 (0)