Skip to content

Use gitpython for tags #4052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 29, 2018
24 changes: 0 additions & 24 deletions readthedocs/rtd_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,6 @@ def test_git_checkout(self):
repo.checkout()
self.assertTrue(exists(repo.working_dir))

def test_parse_git_tags(self):
data = """\
3b32886c8d3cb815df3793b3937b2e91d0fb00f1 refs/tags/2.0.0
bd533a768ff661991a689d3758fcfe72f455435d refs/tags/2.0.1
c0288a17899b2c6818f74e3a90b77e2a1779f96a refs/tags/2.0.2
a63a2de628a3ce89034b7d1a5ca5e8159534eef0 refs/tags/2.1.0.beta2
c7fc3d16ed9dc0b19f0d27583ca661a64562d21e refs/tags/2.1.0.rc1
edc0a2d02a0cc8eae8b67a3a275f65cd126c05b1 refs/tags/2.1.0.rc2
274a5a8c988a804e40da098f59ec6c8f0378fe34 refs/tags/release/foobar
"""
expected_tags = [
('3b32886c8d3cb815df3793b3937b2e91d0fb00f1', '2.0.0'),
('bd533a768ff661991a689d3758fcfe72f455435d', '2.0.1'),
('c0288a17899b2c6818f74e3a90b77e2a1779f96a', '2.0.2'),
('a63a2de628a3ce89034b7d1a5ca5e8159534eef0', '2.1.0.beta2'),
('c7fc3d16ed9dc0b19f0d27583ca661a64562d21e', '2.1.0.rc1'),
('edc0a2d02a0cc8eae8b67a3a275f65cd126c05b1', '2.1.0.rc2'),
('274a5a8c988a804e40da098f59ec6c8f0378fe34', 'release/foobar'),
]

given_ids = [(x.identifier, x.verbose_name) for x in
self.project.vcs_repo().parse_tags(data)]
self.assertEqual(expected_tags, given_ids)

def test_check_for_submodules(self):
repo = self.project.vcs_repo()

Expand Down
46 changes: 6 additions & 40 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,12 @@ def clone(self):

@property
def tags(self):
retcode, stdout, _ = self.run(
'git',
'show-ref',
'--tags',
record_as_success=True,
)
# error (or no tags found)
if retcode != 0:
return []
return self.parse_tags(stdout)

def parse_tags(self, data):
"""
Parses output of show-ref --tags, eg:

3b32886c8d3cb815df3793b3937b2e91d0fb00f1 refs/tags/2.0.0
bd533a768ff661991a689d3758fcfe72f455435d refs/tags/2.0.1
c0288a17899b2c6818f74e3a90b77e2a1779f96a refs/tags/2.0.2
a63a2de628a3ce89034b7d1a5ca5e8159534eef0 refs/tags/2.1.0.beta2
c7fc3d16ed9dc0b19f0d27583ca661a64562d21e refs/tags/2.1.0.rc1
edc0a2d02a0cc8eae8b67a3a275f65cd126c05b1 refs/tags/2.1.0.rc2

Into VCSTag objects with the tag name as verbose_name and the commit
hash as identifier.
"""
# parse the lines into a list of tuples (commit-hash, tag ref name)
# StringIO below is expecting Unicode data, so ensure that it gets it.
if not isinstance(data, str):
data = str(data)
delimiter = str(' ').encode('utf-8') if PY2 else str(' ')
raw_tags = csv.reader(StringIO(data), delimiter=delimiter)
vcs_tags = []
for row in raw_tags:
row = [f for f in row if f != '']
if row == []:
continue
commit_hash, name = row
clean_name = name.replace('refs/tags/', '')
vcs_tags.append(VCSVersion(self, commit_hash, clean_name))
return vcs_tags
repo = git.Repo(self.working_dir)
versions = [
VCSVersion(self, str(tag.commit), str(tag))
for tag in repo.tags
]
return versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ so much cleaner and no csv parsing hacks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But no tests.


@property
def branches(self):
Expand Down