Skip to content

Commit

Permalink
Added .get_timestamp method for retrieving the timestamp of a given r…
Browse files Browse the repository at this point in the history
…evision.
  • Loading branch information
jaraco committed Apr 22, 2024
1 parent 0b24e6c commit 6d2c55f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jaraco/vcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os.path
import posixpath

import dateutil.parser
from more_itertools import one

import jaraco.versioning as versioning
Expand Down Expand Up @@ -120,3 +121,6 @@ def subrepos(self):

def sub_paths(self):
raise NotImplementedError()

def get_timestamp(self, rev):
return dateutil.parser.parse(self._get_timestamp_str(rev))
6 changes: 6 additions & 0 deletions jaraco/vcs/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def sub_paths(self):
except Exception:
return ()

def _get_timestamp_str(self, rev):
return self._invoke('log', '-l', '1', '--template', '{date|isodate}', '-r', rev)


class Git(Command):
exe = 'git'
Expand Down Expand Up @@ -205,3 +208,6 @@ def get_ancestral_tags(self, rev=None):
def sub_paths(self):
lines = self._invoke('submodules', 'status').splitlines()
return (line.split()[1] for line in lines)

def _get_timestamp_str(self, rev):
return self._invoke('log', '-1', '--format=%ai', rev)
1 change: 1 addition & 0 deletions newsfragments/+6c48af32.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added .get_timestamp method for retrieving the timestamp of a given revision.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies = [
"jaraco.classes",
"more_itertools",
"jaraco.versioning",
"python-dateutil",
]
dynamic = ["version"]

Expand All @@ -42,6 +43,7 @@ testing = [
# local
"pygments",
"jaraco.path",
"types-python-dateutil",
]
docs = [
# upstream
Expand Down
9 changes: 9 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import os

import pytest
Expand Down Expand Up @@ -41,3 +42,11 @@ def test_simple(self):
def test_trailing_mess(self):
val = cmd.Git._parse_version('git version 1.9.3 (Mac OS X)')
assert val == '1.9.3'


@pytest.mark.usefixtures("git_repo")
class TestRevisionTimestamp:
def test_tagged_rev_timestamp(self):
mgr = vcs.Git('.')
mgr._invoke('tag', '-am', 'tagging 1.0', '1.0')
assert mgr.get_timestamp('1.0').date() == datetime.date.today()
9 changes: 9 additions & 0 deletions tests/test_mercurial.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import operator
import os
import platform
Expand Down Expand Up @@ -155,3 +156,11 @@ def test_ancestral_tags_specified(self):
self._setup_branchy_tags()
(tag,) = self.mgr.get_ancestral_tags(3)
assert tag.tag == '1.0'


@pytest.mark.usefixtures("hg_repo")
class TestRevisionTimestamp:
def test_tagged_rev_timestamp(self):
mgr = vcs.Mercurial('.')
mgr._invoke('tag', '1.0')
assert mgr.get_timestamp('1.0').date() == datetime.date.today()

0 comments on commit 6d2c55f

Please sign in to comment.