@matheusbsbs / matheus-bagliano / commits / d392e712a8
Fork 0

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>

Andy Parkins committed Sep 28, 2007 at 15:17 UTC d392e712a844e6ce029aa901902e08a3da82f89d
2 files changed +26 -7
Documentation/git-for-each-ref.txt
+5
@@ -100,6 +100,11 @@ In any case, a field name that refers to a field inapplicable to
100 the object referred by the ref does not cause an error. It
101 returns an empty string instead.
102
103 +As a special case for the date-type fields, you may specify a format for
104 +the date by adding one of `:default`, `:relative`, `:short`, `:local`,
105 +`:iso8601` or `:rfc2822` to the end of the fieldname; e.g.
106 +`%(taggerdate:relative)`.
107 +
108
109 EXAMPLES
110 --------
builtin-for-each-ref.c
+21 -7
@@ -356,12 +356,26 @@ static const char *copy_email(const char *buf)
356 return line;
357 }
358
359 -static void grab_date(const char *buf, struct atom_value *v)
359 +static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
360 {
361 const char *eoemail = strstr(buf, "> ");
362 char *zone;
363 unsigned long timestamp;
364 long tz;
365 + enum date_mode date_mode = DATE_NORMAL;
366 + const char *formatp;
367 +
368 + /*
369 + * We got here because atomname ends in "date" or "date<something>";
370 + * it's not possible that <something> is not ":<format>" because
371 + * parse_atom() wouldn't have allowed it, so we can assume that no
372 + * ":" means no format is specified, and use the default.
373 + */
374 + formatp = strchr(atomname, ':');
375 + if (formatp != NULL) {
376 + formatp++;
377 + date_mode = parse_date_format(formatp);
378 + }
379
380 if (!eoemail)
381 goto bad;
@@ -371,7 +385,7 @@ static void grab_date(const char *buf, struct atom_value *v)
385 tz = strtol(zone, NULL, 10);
386 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
387 goto bad;
374 - v->s = xstrdup(show_date(timestamp, tz, 0));
388 + v->s = xstrdup(show_date(timestamp, tz, date_mode));
389 v->ul = timestamp;
390 return;
391 bad:
@@ -398,7 +412,7 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
412 if (name[wholen] != 0 &&
413 strcmp(name + wholen, "name") &&
414 strcmp(name + wholen, "email") &&
401 - strcmp(name + wholen, "date"))
415 + prefixcmp(name + wholen, "date"))
416 continue;
417 if (!wholine)
418 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
424 v->s = copy_name(wholine);
425 else if (!strcmp(name + wholen, "email"))
426 v->s = copy_email(wholine);
413 - else if (!strcmp(name + wholen, "date"))
414 - grab_date(wholine, v);
427 + else if (!prefixcmp(name + wholen, "date"))
428 + grab_date(wholine, v, name);
429 }
430
431 /* 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
445 if (deref)
446 name++;
447
434 - if (!strcmp(name, "creatordate"))
435 - grab_date(wholine, v);
448 + if (!prefixcmp(name, "creatordate"))
449 + grab_date(wholine, v, name);
450 else if (!strcmp(name, "creator"))
451 v->s = copy_line(wholine);
452 }