From 6ceddf089fe8aba7241a41aac1401a82d5e1fe44 Mon Sep 17 00:00:00 2001 From: David James Date: Thu, 28 Oct 2010 13:59:57 -0700 Subject: [PATCH] Update cros_changelog to support sort-by-date option. Other changes: 1. Clean up revision handling to be more forgiving if people want to use things that don't start with "cros/". Instead, we only enforce that people use tags if they want to use the one-argument version of cros_changelog. 2. Tags are quoted in the command-line so that people can, for instance, diff 'cros/master{2 days ago}' vs 'cros/master{1 day ago}'. Change-Id: I28c7d77ae83256d4c28411ef589f3e726cc81d80 BUG=chromium-os:8205 TEST=Ran ( ./cros_changelog 0.9.104.0 cros/master > /tmp/test1.html && ./cros_changelog --sort-by-date 0.9.104.0 cros/master > /tmp/test2.html ) Both commands print changes between 0.9.104.0 and master, but the second command prints results sorted by date. Review URL: http://codereview.chromium.org/4130009 --- chromite/bin/cros_changelog | 40 +++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/chromite/bin/cros_changelog b/chromite/bin/cros_changelog index 4ce093c15d..c79c2e8c56 100755 --- a/chromite/bin/cros_changelog +++ b/chromite/bin/cros_changelog @@ -8,6 +8,8 @@ import cgi from datetime import datetime +import operator +import optparse import os import re import sys @@ -136,7 +138,7 @@ def _GrabChanges(path, tag1, tag2): cmd = 'cd %s && git config --get remote.cros.projectname' % path projectname = _GrabOutput(cmd).strip() log_fmt = '%x00%H\t%ce\t%cd\t%s\t%b' - cmd_fmt = 'cd %s && git log --format="%s" --date=local %s..%s' + cmd_fmt = 'cd %s && git log --format="%s" --date=local "%s..%s"' cmd = cmd_fmt % (path, log_fmt, tag1, tag2) output = _GrabOutput(cmd) commits = [] @@ -147,29 +149,33 @@ def _GrabChanges(path, tag1, tag2): commits.append(change) return commits +def _ParseArgs(): + parser = optparse.OptionParser() + parser.add_option("--sort-by-date", dest="sort_by_date", default=False, + action='store_true', help="Sort commits by date.") + return parser.parse_args() + def main(): tags = _GrabTags() tag1 = None - if len(sys.argv) == 3: - tag1 = sys.argv[1] - tag2 = sys.argv[2] - elif len(sys.argv) == 2: - tag2 = sys.argv[1] + options, args = _ParseArgs() + if len(args) == 2: + tag1, tag2 = args + elif len(args) == 1: + tag2, = args + if tag2 in tags: + tag1 = tags[tags.index(tag2) + 1] + else: + print >>sys.stderr, 'Unrecognized tag: %s' % tag2 + sys.exit(1) else: print >>sys.stderr, 'Usage: %s [tag1] tag2' % sys.argv[0] print >>sys.stderr, 'If only one tag is specified, we view the differences' print >>sys.stderr, 'between that tag and the previous tag. You can also' print >>sys.stderr, 'specify cros/master to show differences with' print >>sys.stderr, 'tip-of-tree.' - sys.exit(1) - if not tag2.startswith('cros/') and tag2 not in tags: - print >>sys.stderr, 'Unrecognized tag: %s' % tag2 - sys.exit(1) - if not tag1: - tag1 = tags[tags.index(tag2) + 1] - if not tag1.startswith('cros/') and tag1 not in tags: - print >>sys.stderr, 'Unrecognized tag: %s' % tag1 + print >>sys.stderr, 'E.g. %s %s cros/master' % (sys.argv[0], tags[0]) sys.exit(1) print >>sys.stderr, 'Finding differences between %s and %s' % (tag1, tag2) @@ -185,7 +191,11 @@ def main(): cols = ['Project', 'Date', 'Commit', 'Committer', 'Bugs', 'Subject'] print '' print '' % ('
%s'.join(cols)) - for change in sorted(changes): + if options.sort_by_date: + changes.sort(key=operator.attrgetter("commit_date")) + else: + changes.sort() + for change in changes: print change.AsHTMLTableRow() print '
' print ''