Skip to content

Commit

Permalink
Added a "describe_version" routine for git repos.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 13, 2024
1 parent 94a51f7 commit 6effc4c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jaraco/vcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@ def commit_tree(self, spec, msg="committed"):
Apply the tree in spec and commit.
"""
raise NotImplementedError()

def describe_version(self):
"""
Return a string representing a version of the current state.
"""
raise NotImplementedError()
61 changes: 61 additions & 0 deletions jaraco/vcs/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import re
import subprocess
import types

import dateutil.parser
import jaraco.path
Expand Down Expand Up @@ -240,3 +241,63 @@ def commit_tree(self, spec, message: str = 'committed'):
jaraco.path.build(spec)
self._invoke('add', '.')
self._invoke('commit', '-m', message)

def head_date(self):
out = self._invoke(
'-c',
'log.showSignature=false',
'log',
'-n',
'1',
'HEAD',
'--format=%cI',
)
return dateutil.parser.parse(out)

def describe_version(self):
"""
>>> repo = getfixture('git_repo')
>>> _ = repo._invoke('tag', 'v1.0.0')
>>> desc = repo.describe_version()
>>> list(vars(desc))
['date', 'tag', 'distance', 'node', 'dirty']
>>> desc.tag
'v1.0.0'
>>> desc.node
'g...'
>>> desc.distance
0
>>> desc.dirty
False
>>> repo.commit_tree({'bar': {'baz': 'new content'}})
>>> desc = repo.describe_version()
>>> desc.distance
1
>>> desc.dirty
False
>>> jaraco.path.build({'bar': {'baz': 'pending'}})
>>> desc = repo.describe_version()
>>> desc.distance
1
>>> desc.dirty
True
"""
output = self._invoke(
'describe',
'--dirty',
'--tags',
'--long',
'--match',
'*[0-9]*',
)
match = re.match(
r'(?P<tag>.*?)-'
r'(?P<distance>\d+)-'
r'(?P<node>g[0-9A-Fa-f]+)'
r'(-(?P<dirty>dirty))?',
output,
)
desc = types.SimpleNamespace(date=self.head_date(), **match.groupdict())
desc.distance = int(desc.distance)
desc.dirty = bool(desc.dirty)
return desc

0 comments on commit 6effc4c

Please sign in to comment.