Make for-each-ref's grab_date() support per-atom formatting

grab_date() gets an extra parameter - atomname; this extra parameter is
checked to see if it has a ":<format>" extra component in it, and if so
that "<format>" string is passed to parse_date_format() to produce an
enum date_mode value which is then further passed to show_date().

In short it allows the user of git-for-each-ref to do things like this:

 $ git-for-each-ref --format='%(taggerdate:default)' refs/tags/v1.5.2
 Sun May 20 00:30:42 2007 -0700
 $ git-for-each-ref --format='%(taggerdate:relative)' refs/tags/v1.5.2
 4 months ago
 $ git-for-each-ref --format='%(taggerdate:short)' refs/tags/v1.5.2
 2007-05-20
 $ git-for-each-ref --format='%(taggerdate:local)' refs/tags/v1.5.2
 Sun May 20 08:30:42 2007
 $ git-for-each-ref --format='%(taggerdate:iso8601)' refs/tags/v1.5.2
 2007-05-20 00:30:42 -0700
 $ git-for-each-ref --format='%(taggerdate:rfc2822)' refs/tags/v1.5.2
 Sun, 20 May 2007 00:30:42 -0700

The default, when no ":<format>" is specified is ":default", leaving the
existing behaviour unchanged.

Signed-off-by: Andy Parkins <andyparkins@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Andy Parkins 2007-09-28 15:17:45 +01:00 committed by Junio C Hamano
parent b64265ca8d
commit d392e712a8
2 changed files with 26 additions and 7 deletions

View File

@ -100,6 +100,11 @@ In any case, a field name that refers to a field inapplicable to
the object referred by the ref does not cause an error. It the object referred by the ref does not cause an error. It
returns an empty string instead. returns an empty string instead.
As a special case for the date-type fields, you may specify a format for
the date by adding one of `:default`, `:relative`, `:short`, `:local`,
`:iso8601` or `:rfc2822` to the end of the fieldname; e.g.
`%(taggerdate:relative)`.
EXAMPLES EXAMPLES
-------- --------

View File

@ -356,12 +356,26 @@ static const char *copy_email(const char *buf)
return line; return line;
} }
static void grab_date(const char *buf, struct atom_value *v) static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
{ {
const char *eoemail = strstr(buf, "> "); const char *eoemail = strstr(buf, "> ");
char *zone; char *zone;
unsigned long timestamp; unsigned long timestamp;
long tz; long tz;
enum date_mode date_mode = DATE_NORMAL;
const char *formatp;
/*
* We got here because atomname ends in "date" or "date<something>";
* it's not possible that <something> is not ":<format>" because
* parse_atom() wouldn't have allowed it, so we can assume that no
* ":" means no format is specified, and use the default.
*/
formatp = strchr(atomname, ':');
if (formatp != NULL) {
formatp++;
date_mode = parse_date_format(formatp);
}
if (!eoemail) if (!eoemail)
goto bad; goto bad;
@ -371,7 +385,7 @@ static void grab_date(const char *buf, struct atom_value *v)
tz = strtol(zone, NULL, 10); tz = strtol(zone, NULL, 10);
if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
goto bad; goto bad;
v->s = xstrdup(show_date(timestamp, tz, 0)); v->s = xstrdup(show_date(timestamp, tz, date_mode));
v->ul = timestamp; v->ul = timestamp;
return; return;
bad: bad:
@ -398,7 +412,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
if (name[wholen] != 0 && if (name[wholen] != 0 &&
strcmp(name + wholen, "name") && strcmp(name + wholen, "name") &&
strcmp(name + wholen, "email") && strcmp(name + wholen, "email") &&
strcmp(name + wholen, "date")) prefixcmp(name + wholen, "date"))
continue; continue;
if (!wholine) if (!wholine)
wholine = find_wholine(who, wholen, buf, sz); wholine = find_wholine(who, wholen, buf, sz);
@ -410,8 +424,8 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
v->s = copy_name(wholine); v->s = copy_name(wholine);
else if (!strcmp(name + wholen, "email")) else if (!strcmp(name + wholen, "email"))
v->s = copy_email(wholine); v->s = copy_email(wholine);
else if (!strcmp(name + wholen, "date")) else if (!prefixcmp(name + wholen, "date"))
grab_date(wholine, v); grab_date(wholine, v, name);
} }
/* For a tag or a commit object, if "creator" or "creatordate" is /* For a tag or a commit object, if "creator" or "creatordate" is
@ -431,8 +445,8 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
if (deref) if (deref)
name++; name++;
if (!strcmp(name, "creatordate")) if (!prefixcmp(name, "creatordate"))
grab_date(wholine, v); grab_date(wholine, v, name);
else if (!strcmp(name, "creator")) else if (!strcmp(name, "creator"))
v->s = copy_line(wholine); v->s = copy_line(wholine);
} }