Skip to content

Commit

Permalink
new: use tagger date when tags are annotated instead of commit date. (f…
Browse files Browse the repository at this point in the history
…ixes #60)
  • Loading branch information
vaab committed Mar 17, 2017
1 parent af99904 commit 804c872
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/gitchangelog/gitchangelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,30 @@ def date(self):
float(self.author_date_timestamp))
return d.strftime('%Y-%m-%d')

@property
def has_annotated_tag(self):
try:
self.git.rev_parse(['%s^{tag}' % self.identifier, "--"])
return True
except ShellError as e:
if e.errlvl != 128:
raise
return False

@property
def tagger_date_timestamp(self):
if not self.has_annotated_tag:
raise ValueError("Can't access 'tagger_date_timestamp' on commit without annotated tag.")
tagger_date_utc = self.git.for_each_ref(
'refs/tags/%s' % self.identifier, format='%(taggerdate:raw)')
return tagger_date_utc.split(" ", 1)[0]

@property
def tagger_date(self):
d = datetime.datetime.utcfromtimestamp(
float(self.tagger_date_timestamp))
return d.strftime('%Y-%m-%d')

def __le__(self, value):
if not isinstance(value, GitCommit):
value = self._repos.commit(value)
Expand Down Expand Up @@ -1065,9 +1089,11 @@ def dir_swrap(command, **kwargs):
os.chdir(old_dir)

def method(*args, **kwargs):
if (len(args) == 1 and len(kwargs) == 0 and
not isinstance(args[0], basestring)):
return dir_swrap(['git', label, ] + args[0], shell=False)
if (len(args) == 1 and not isinstance(args[0], basestring)):
return dir_swrap(
['git', label, ] + args[0],
shell=False,
env=kwargs.get("env", None))
cli_args = []
for key, value in kwargs.items():
cli_key = (("-%s" if len(key) == 1 else "--%s")
Expand Down Expand Up @@ -1533,10 +1559,13 @@ def versions_data_iter(repository, revlist=None,
for idx, tag in enumerate(tags):

## New version
current_version = {"date": tag.date}
current_version["tag"] = tag.identifier \
if tag.identifier != "HEAD" else \
None
current_version = {
"date": tag.tagger_date if tag.has_annotated_tag else tag.date,
"commit_date": tag.date,
"tagger_date": tag.tagger_date if tag.has_annotated_tag else None,
"tag": tag.identifier if tag.identifier != "HEAD" else None,
"commit": tag,
}

sections = collections.defaultdict(list)
commits = repository.log(
Expand Down
122 changes: 122 additions & 0 deletions test/test_tagger_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# -*- encoding: utf-8 -*-
"""Testing tagger_date display
https://github.com/vaab/gitchangelog/issues/60
"""

from __future__ import unicode_literals

import textwrap

from .common import BaseGitReposTest


def simple_renderer_using_commit_internal(data, opts):
s = ""
for version in data["versions"]:
s += "%s (%s)\n" % (version["tag"], version["commit"].tagger_date)
for section in version["sections"]:
s += " %s:\n" % section["label"]
for commit in section["commits"]:
s += " * %(subject)s [%(author)s]\n" % commit
s += "\n"
return s


def simple_renderer(data, opts):
s = ""
for version in data["versions"]:
s += "%s (%s)\n" % (version["tag"], version["tagger_date"])
for section in version["sections"]:
s += " %s:\n" % section["label"]
for commit in section["commits"]:
s += " * %(subject)s [%(author)s]\n" % commit
s += "\n"
return s


def simple_renderer_auto_date(data, opts):
s = ""
for version in data["versions"]:
s += "%s (%s)\n" % (version["tag"], version["date"])
for section in version["sections"]:
s += " %s:\n" % section["label"]
for commit in section["commits"]:
s += " * %(subject)s [%(author)s]\n" % commit
s += "\n"
return s


class TaggerDateTest(BaseGitReposTest):

REFERENCE = textwrap.dedent("""\
1.2 (2017-03-17)
None:
* b [The Committer]
""")

def setUp(self):
super(TaggerDateTest, self).setUp()

self.git.commit(message="b",
date="2017-02-20 11:00:00",
allow_empty=True)
self.git.tag(['-a', "1.2", '--message="tag message"'],
env={'GIT_COMMITTER_DATE': "2017-03-17 11:00:00"})

def test_checking_tagger_date_in_commit_object(self):
out = self.changelog(
output_engine=simple_renderer_using_commit_internal)
self.assertNoDiff(self.REFERENCE, out)

def test_checking_tagger_date(self):
out = self.changelog(output_engine=simple_renderer)
self.assertNoDiff(self.REFERENCE, out)


class TaggerDateNonAnnotatedTest(BaseGitReposTest):

def setUp(self):
super(TaggerDateNonAnnotatedTest, self).setUp()

self.git.commit(message="b",
date="2017-02-20 11:00:00",
allow_empty=True)
self.git.tag(["1.2"])

def test_checking_tagger_date_in_non_tag_with_commit_object(self):
with self.assertRaises(ValueError):
self.changelog(output_engine=simple_renderer_using_commit_internal)


class TaggerDateAutoDateTest(BaseGitReposTest):

REFERENCE = textwrap.dedent("""\
1.3 (2017-02-20)
None:
* b [The Committer]
1.2 (2017-03-17)
None:
* b [The Committer]
""")

def setUp(self):
super(TaggerDateAutoDateTest, self).setUp()

self.git.commit(message="b",
date="2017-02-20 11:00:00",
allow_empty=True)
self.git.tag(['-a', "1.2", '--message="tag message"'],
env={'GIT_COMMITTER_DATE': "2017-03-17 11:00:00"})
self.git.commit(message="b",
date="2017-02-20 11:00:00",
allow_empty=True)
self.git.tag(["1.3"])

def test_checking_tagger_date(self):
out = self.changelog(output_engine=simple_renderer_auto_date)
self.assertNoDiff(self.REFERENCE, out)

0 comments on commit 804c872

Please sign in to comment.