Skip to contentSkip to conversation

Add 'human' date format

PATCH Superseded Stephen P. Smith 6 participants 56 messages Dec 31, 2018 – Jan 29, 2019 Documentation

lore
Share & tools
lore
#0 lore raw

cover Add 'human' date format

Added documentation and tests for the previously submitted patch. The previous patch was rebased and the conflict in cache.h was resolved.

Range diff for Linus' original code:

1: 74e8221b52 ! 1: dd8ea66414 Add 'human' date format
@@ -26,6 +26,10 @@

+ Rebased lt/date-human to git version 2.20.1 and resolved a conflict.
+
+ Intentionally did not change logic or add documentation/tests to the
+ original commit so that the sign-offs would still be legitimate.

diff --git a/builtin/blame.c b/builtin/blame.c --- a/builtin/blame.c
@@ -46,13 +50,13 @@
--- a/cache.h
+++ b/cache.h
@@
- struct date_mode {
- enum date_mode_type {
- DATE_NORMAL = 0,
-+ DATE_HUMAN,
- DATE_RELATIVE,
- DATE_SHORT,
- DATE_ISO8601,
+
+ enum date_mode_type {
+ DATE_NORMAL = 0,
++ DATE_HUMAN,
+ DATE_RELATIVE,
+ DATE_SHORT,
+ DATE_ISO8601,

diff --git a/date.c b/date.c
--- a/date.c

Linus Torvalds (1):
Add 'human' date format

Stephen P. Smith (2):
Add 'human' date format documentation
t0006-date.sh: add human date format tests.

 Documentation/rev-list-options.txt |   8 ++
 builtin/blame.c                    |   4 +
 cache.h                            |   1 +
 date.c                             | 130 ++++++++++++++++++++++++-----
 t/t0006-date.sh                    |  24 ++++++
 t/t4202-log.sh                     |  24 ++++++
 t/t7007-show.sh                    |  25 ++++++
 7 files changed, 196 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1.2.gb21ebb671b
#1 lore raw

PATCH 1/3 Add 'human' date format

From: Linus Torvalds <[email protected]>

This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was).

For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone.

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view.

---
 builtin/blame.c |   4 ++
 cache.h         |   1 +
 date.c          | 130 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 20 deletions(-)
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 		break;
cache.h +1 -0
index ca36b44ee0..c4396ebaa6 100644
@@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen,
 
 enum date_mode_type {
 	DATE_NORMAL = 0,
+	DATE_HUMAN,
 	DATE_RELATIVE,
 	DATE_SHORT,
 	DATE_ISO8601,
date.c +110 -20

179 lines not on this page · raw message

Signature · 1 line
2.20.1.2.gb21ebb671b
#2 lore raw
Quoted 7 lines · Stephen P. Smith

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

I like the idea of "human", and I like the idea of "auto", but it seems to me that these are really two orthogonal things. E.g., might some people not want to do something like:

git config log.date auto:relative

?

I don't personally care about using this myself, but we already had to deal with retrofitting "local" as a modifier. I'd prefer to avoid making the same mistake again.

(I'd actually argue that "log.date" should basically _always_ have the "auto" behavior, since it tends to get treated as plumbing anyway, and I suspect that anybody who sets log.date now would see subtle breakage from scripts. But maybe it's too late at this point?).

builtin/blame.c int cmd_blame(int argc, const char **argv, const char *prefix) · line 931

Quoted 12 lines
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;

OK, and we expect the year to be less than 5 characters. I briefly wondered what would happen at Y100K (or somebody maliciously using a bogus year), but it is not a buffer overflow. It is simply a mis-aligned blame line (and actually, the same goes for the existing entries, which use a 4-digit year).

-Peff

#3 lore raw
Quoted 5 lines · MST Jeff King

I like the idea of "human", and I like the idea of "auto", but it seems to me that these are really two orthogonal things. E.g., might some people not want to do something like:

git config log.date auto:relative

I didn't see anything in the code which would prohibit setting something like that.

Quoted 4 lines

I don't personally care about using this myself, but we already had to deal with retrofitting "local" as a modifier. I'd prefer to avoid making the same mistake again.

Since I wasn't involved could you summarize the you are referring to?

Quoted 5 lines

(I'd actually argue that "log.date" should basically _always_ have the "auto" behavior, since it tends to get treated as plumbing anyway, and I suspect that anybody who sets log.date now would see subtle breakage from scripts. But maybe it's too late at this point?).

If auto isn't added to the "log.date" file, then the date behaviour is not
changed from is currently in the code base. Therefore, there shouldn't be
any breakage.

Quoted 24 lines
Quoted 6 lines
builtin/blame.c +0 -0
index 6d798f9939..f684e31d82 100644

builtin/blame.c int cmd_blame(int argc, const char **argv, const char · line 934

Quoted 10 lines
builtin/blame.c +4 -0
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char
 *prefix)> 
 		 */
 
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /*
 		add the null */ break;
 
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;

OK, and we expect the year to be less than 5 characters. I briefly wondered what would happen at Y100K (or somebody maliciously using a bogus year), but it is not a buffer overflow. It is simply a mis-aligned blame line (and actually, the same goes for the existing entries, which use a 4-digit year).

-Peff

#4 lore raw
Quoted 8 lines · Stephen P. Smith
Quoted 5 lines · MST Jeff King

I like the idea of "human", and I like the idea of "auto", but it seems to me that these are really two orthogonal things. E.g., might some people not want to do something like:

git config log.date auto:relative

I didn't see anything in the code which would prohibit setting something like that.

Yeah, I don't think supporting that is too hard. I was thinking something like this:

date.c +8 -7
index 4486c028ac..f731803872 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";

That removes "auto" completely. We could still support it as an alias for "auto:human" with something like:

if (!strcmp(format, "auto"))

	format = "auto:human";

but IMHO it is a simpler interface to just have the user be explicit (this is meant to be set once in config, after all).

Quoted 4 lines
Quoted 3 lines

I don't personally care about using this myself, but we already had to deal with retrofitting "local" as a modifier. I'd prefer to avoid making the same mistake again.

Since I wasn't involved could you summarize the you are referring to?

The format "local" was a variant of "default" that would use the local timezone instead of the author's. But there was no way to format, say, iso8601 in the local timezone. So we had to invent a new syntax that was compatible ("iso8601-local"), and keep "local" around forever for backwards compatibility. Not the end of the world, but we can avoid it in this case with a little preparation.

Quoted 7 lines
Quoted 4 lines

(I'd actually argue that "log.date" should basically _always_ have the "auto" behavior, since it tends to get treated as plumbing anyway, and I suspect that anybody who sets log.date now would see subtle breakage from scripts. But maybe it's too late at this point?).

If auto isn't added to the "log.date" file, then the date behaviour is not
changed from is currently in the code base. Therefore, there shouldn't be
any breakage.

Right, this isn't a problem with your patches. I mean that the existing "log.date" is arguably mis-designed, and we ought to have had something like "auto" from day one (or even made it the default for log.date).

-Peff

#5 lore raw
Quoted 7 lines · MST Jeff King
Quoted 3 lines · Stephen P. Smith

I didn't see anything in the code which would prohibit setting something like that.

Yeah, I don't think supporting that is too hard. I was thinking something like this:

I take it that if I update Linus's patch, I still keep Junio's and Linus' sign-off line for the purpose of the chain of custody? Of should I use a second patch?

Just trying to follow the rules.
sps

#6 lore raw
Quoted 6 lines · Stephen P Smith
Quoted 5 lines · MST Jeff King
Quoted 3 lines · Stephen P. Smith

I didn't see anything in the code which would prohibit setting something like that.

Quoted 6 lines
Quoted 2 lines

Yeah, I don't think supporting that is too hard. I was thinking something like this:

I take it that if I update Linus's patch, I still keep Junio's and Linus' sign-off line for the purpose of the chain of custody? Of should I use a second patch?

I think the most interesting question is the actual authorship (i.e., the "From:" field). I think people are generally OK with having their patches polished a bit to fix obvious bugs or short-comings. But at some point if you make too many changes they or may not want to have the result attributed to them. ;)

For the particular change I suggested, it's borderline to me on whether it hits that case, so I'd probably err on the side of caution. And I'd either expect Linus to say "yeah, that sounds like a good direction", or I'd do it as a separate patch. And if a separate patch, I'd probably tease Linus's patch out into two separate ones: one to add "human", and one to implement "auto". And then drop the "auto" one in favor of your new patch (with you as the author).

And I think that makes the signoff questions go away for this instance (keep the signoffs for Linus's, and just signoff the new patch yourself). But here's some general pontificating in that direction:

Normally you can just drop Junio's signoff. The chain of custody is usually "author, then maintainer" and he'll re-add his maintainer signoff when he picks up your patch. In this case of this patch it's "author, then polisher, then maintainer", but Junio is still at the end.

Now one can argue that Junio picked up Linus's patch, which you then picked up from Junio's repository and fed back to Junio. But you could just as well have picked Linus's patch up from the mailing list and then polished it. So I don't know that having Junio twice in the chain is really that interesting.

Generally, yes, I'd keep Linus's signoff in a situation like this. He is asserting that the original work done meets the DCO requirements. You polishing the patch does not change that (of course you could introduce a bunch of new code that doesn't meet the DCO and sign it off anyway, but that's why there's ordering in the chain of custody. Somebody investigating would probably walk backwards up the chain).

-Peff

#7 lore raw

PATCH 2/3 Add 'human' date format documentation

In reply to Stephen P. Smith

Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate.

---
 Documentation/rev-list-options.txt | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/rev-list-options.txt +8 -0
index bab5f50b17..b491c3b999 100644
@@ -835,6 +835,14 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if it matches the current time-zone,
+and doesn't print the whole date if that matches (ie skip printing
+year for dates that are "this year", but also skip the whole date
+itself if it's in the last few days and we can just say what weekday
+it was).
++
+`--date=auto` defaults to human if we're using the pager.
++
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
Signature · 1 line
2.20.1.2.gb21ebb671b
#8 lore raw

PATCH 3/3 t0006-date.sh: add `human` date format tests.

In reply to Stephen P. Smith

The human date format varies based on two inputs: the date in the reference time which is constant and the local computers date which varies. Using hardcoded test expected output dates would require holding the local machines date and time constant which is not desireable.

Alternatively, letting the local date vary, which is the normal situation, implies that the tests would be checking for formating changes based on on a ref date relative to the local computers time.

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Test using a regular expression to verify that fields that are expected to be suppressed are not displayed.

---
 t/t0006-date.sh | 24 ++++++++++++++++++++++++
 t/t4202-log.sh  | 24 ++++++++++++++++++++++++
 t/t7007-show.sh | 25 +++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
t/t0006-date.sh +24 -0
index ffb2975e48..f208a80867 100755
@@ -40,6 +40,16 @@ check_show () {
 	'
 }
 
+check_human_date () {
+	time=$1
+	expect=$2
+	test_expect_success "check date ($format:$time)" '
+		echo "$time -> $expect" >expect &&
+		TZ=${zone:-$TZ} test-tool date show:"$format" "$time" >actual &&
+		grep "$expect" actual 
+	'
+}
+
 # arbitrary but sensible time for examples
 TIME='1466000000 +0200'
 check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
@@ -52,6 +62,20 @@ check_show unix "$TIME" '1466000000'
 check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
 check_show raw-local "$TIME" '1466000000 +0000'
 check_show unix-local "$TIME" '1466000000'
+check_show human "$TIME" 'Jun 15 2016'
+
+# Subtract some known constant time and look for expected field format
+TODAY_REGEX='5 hours ago'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
 
 check_show 'format:%z' "$TIME" '+0200'
 check_show 'format-local:%z' "$TIME" '+0000'
t/t4202-log.sh +24 -0
index 819c24d10e..d7f3b73650 100755
@@ -1707,4 +1707,28 @@ test_expect_success '--exclude-promisor-objects does not BUG-crash' '
 	test_must_fail git log --exclude-promisor-objects source-a
 '
 
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates && 
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git log -1 --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"
+}
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
+
 test_done
t/t7007-show.sh +25 -0
index 42d3db6246..0a0334a8b5 100755
@@ -128,4 +128,29 @@ test_expect_success 'show --graph is forbidden' '
   test_must_fail git show --graph HEAD
 '
 
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates && 
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git show --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"
+}
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
+
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#9 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

t/t0006-date.sh check_show unix "$TIME" '1466000000' · line 72

Quoted 6 lines · Stephen P. Smith
t/t0006-date.sh +6 -0
+# Subtract some known constant time and look for expected field format
+TODAY_REGEX='5 hours ago'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago

'date +%s' is used everywhere in this patch but has never been used in our test suite before. It is not portable.

We perhaps can use "test-tool date timestamp", like so

	check_human_date $(test-tool date timestamp "18000 seconds ago") ...

or moving the part that munges 18000 into the above form inside check_human_date helper function, e.g.

	check_human_date () {
		commit_date=$(test-tool date timestamp "$1 seconds ago")
		commit_date="$commit_date +0200"
                expect=$2
		...
	}

which would let us write

	check_human_date 432000" $THIS_YEAR_REGEX  # 5 days ago

t/t4202-log.sh test_expect_success '--exclude-promisor-objects does not BUG-crash' ' · line 1719

Quoted 10 lines
t/t4202-log.sh +10 -0
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates && 
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git log -1 --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"

As the body of the test_expect_success helper is eval'ed, variables $commit_date and $expect should be visible to it, without turning them into values before executing test_expect_success function, i.e.

	test_expect_success "$commit_date" '
		echo "$expect $commit_date" >dates &&
		...
		git commit -m "Expect String" --date="$commit_date" dates &&
		git show -s --date=human | grep '^Date:" >actual &&
		grep "$expect" actual
	'

which would reduce the need for unreadable backslashes.

Instead of duplicating, perhaps move this to a more common place? Would it make sense to make it "check_date_format ()" helper by passing another argument to parameterize --date=human part

t/t7007-show.sh test_expect_success 'show --graph is forbidden' ' · line 138

Quoted 8 lines
t/t7007-show.sh +8 -0
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates && 
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git show --date=human | grep \"^Date:\" >actual &&

Using "show" here is much better than "log -1" above; using "show -s" would be even better.

Quoted 18 lines
t/t7007-show.sh +17 -0
+		grep \"$expect\" actual
+"
+}
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
+
+
 test_done
#10 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 2 lines · MST Junio C Hamano

'date +%s' is used everywhere in this patch but has never been used in our test suite before. It is not portable.

So I don't make this mistake again, Is there a reference somewhere for that is and is not portable?

Quoted 15 lines

We perhaps can use "test-tool date timestamp", like so

	check_human_date $(test-tool date timestamp "18000 seconds ago") ...

or moving the part that munges 18000 into the above form inside check_human_date helper function, e.g.

	check_human_date () {
		commit_date=$(test-tool date timestamp "$1 seconds ago")
		commit_date="$commit_date +0200"
                expect=$2
		...
	}
Quoted 3 lines

which would let us write

	check_human_date 432000" $THIS_YEAR_REGEX  # 5 days ago

Thanks

Quoted 25 lines

t/t4202-log.sh test_expect_success '--exclude-promisor-objects does not BUG-crash' ' · line 1719

Quoted 11 lines
t/t4202-log.sh +10 -0
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates &&
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git log -1 --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"

As the body of the test_expect_success helper is eval'ed, variables $commit_date and $expect should be visible to it, without turning them into values before executing test_expect_success function, i.e.

	test_expect_success "$commit_date" '
		echo "$expect $commit_date" >dates &&
		...
		git commit -m "Expect String" --date="$commit_date" dates &&
		git show -s --date=human | grep '^Date:" >actual &&
		grep "$expect" actual
	'
Quoted 1 line

which would reduce the need for unreadable backslashes.

I was worried about embedded spaces that might not be parsed correctly by the called function. I will update

Quoted 4 lines

Instead of duplicating, perhaps move this to a more common place? Would it make sense to make it "check_date_format ()" helper by passing another argument to parameterize --date=human part

I had considered that, but then noted that for the other formats specific strings were being used. The use of specific strings was possible since the other formats were always guarenteed to have the same string literal due to a singe unvarying input.

I don't mind parameterize the format and it would make the solution more general.

Quoted 11 lines
Quoted 11 lines

"Stephen P. Smith" <[email protected]> writes:

t/t0006-date.sh +3 -0
+# Subtract some known constant time and look for expected field format
+TODAY_REGEX='5 hours ago'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]*

[012][0-9]:[0-6][0-9]' +MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]' +check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago +check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX # 5 days ago

t/t4202-log.sh +3 -0
+check_human_date() {
+	commit_date=$1
+	expect=$2
Quoted 8 lines

t/t7007-show.sh test_expect_success 'show --graph is forbidden' ' · line 138

Quoted 5 lines
t/t7007-show.sh +5 -0
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates &&
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git show --date=human | grep \"^Date:\" >actual &&

Using "show" here is much better than "log -1" above; using "show -s" would be even better.

I was attempting to test both git log and git show. For get log the -1 was to only get the latest commit.

Are you suggesting that t4202-log.sh not be updated and that only and t7007- show.sh and t0006-date.sh updated?

Side note: I found when updating that all three scripts that log and show returned the same formats, but date returned a different string if the delta date was less than 24hours

I just noted that the patch 3/3 should be re-titled since the tests are currently for three commands.

Hope you are better.
sps

#11 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 5 lines · Stephen & Linda Smith
Quoted 2 lines · MST Junio C Hamano

'date +%s' is used everywhere in this patch but has never been used in our test suite before. It is not portable.

So I don't make this mistake again, Is there a reference somewhere for that is and is not portable?

I usually go to http://pubs.opengroup.org/onlinepubs/9699919799/

Even though we do not say "We'll use anything that is in POSIX.1; it is your problem if your platform does not support it", we tend to say "It's not even in POSIX, so let's see if we can avoid it".

Quoted 8 lines
Quoted 2 lines

Using "show" here is much better than "log -1" above; using "show -s" would be even better.

I was attempting to test both git log and git show. For get log the -1 was to only get the latest commit.

Are you suggesting that t4202-log.sh not be updated and that only and t7007- show.sh and t0006-date.sh updated?

I am saying that using "log -1" and "show" in different tests _only_ for the value of "Date:" field does not buy us much. And by unifying, I was hoping that the single helper can be placed in a common file that is dot-sourced by these three scripts more easily.

Thanks.

#12 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 7 lines · MST Junio C Hamano
Quoted 2 lines

Are you suggesting that t4202-log.sh not be updated and that only and t7007- show.sh and t0006-date.sh updated?

I am saying that using "log -1" and "show" in different tests _only_ for the value of "Date:" field does not buy us much. And by unifying, I was hoping that the single helper can be placed in a common file that is dot-sourced by these three scripts more easily.

Thanks for the clarification.

#13 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

In reply to Junio C Hamano
Quoted 14 lines · Junio C Hamano

We perhaps can use "test-tool date timestamp", like so

	check_human_date $(test-tool date timestamp "18000 seconds ago") ...

or moving the part that munges 18000 into the above form inside check_human_date helper function, e.g.

	check_human_date () {
		commit_date=$(test-tool date timestamp "$1 seconds ago")
		commit_date="$commit_date +0200"
                 expect=$2
		...
	}
Quoted 3 lines

which would let us write

	check_human_date 432000" $THIS_YEAR_REGEX  # 5 days ago

Just a quick bikeshed: if used, would this have a year end 5 day roll-over error potential, or will it always use the single date?

(I appreciate it is just suggestion code, not tested)

Signature · 1 line
Philip
#14 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 15 lines · Philip Oakley
Quoted 14 lines · Junio C Hamano

We perhaps can use "test-tool date timestamp", like so

	check_human_date $(test-tool date timestamp "18000 seconds ago") ...

or moving the part that munges 18000 into the above form inside check_human_date helper function, e.g.

	check_human_date () {
		commit_date=$(test-tool date timestamp "$1 seconds ago")
		commit_date="$commit_date +0200"
                 expect=$2
		...
	}
Quoted 7 lines
Quoted 3 lines

which would let us write

	check_human_date 432000 "$THIS_YEAR_REGEX" # 5 days ago

Just a quick bikeshed: if used, would this have a year end 5 day roll-over error potential, or will it always use the single date?

Hmph, interesting point. Indeed, date.c::show_date_normal() decides to hide the year portion if the timestamp and the current time share the same year, so on Thu Jan 3rd, an attempt to show a commit made on Mon Dec 31st of the same week would end up showing the year, so yes, I agree with you that the above would break.

+TODAY_REGEX='5 hours ago'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]' +MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'

Quoted 2 lines

(I appreciate it is just suggestion code, not tested)

#15 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 4 lines · MST Junio C Hamano
Quoted 3 lines · Philip Oakley
Quoted 2 lines
	check_human_date 432000 "$THIS_YEAR_REGEX" # 5 days ago
Quoted 9 lines
Quoted 2 lines

Just a quick bikeshed: if used, would this have a year end 5 day roll-over error potential, or will it always use the single date?

Hmph, interesting point. Indeed, date.c::show_date_normal() decides to hide the year portion if the timestamp and the current time share the same year, so on Thu Jan 3rd, an attempt to show a commit made on Mon Dec 31st of the same week would end up showing the year, so yes, I agree with you that the above would break.

Thanks Philip.

I wrote the test just before the new year so didn't see the rollover. I
haven't run the test this year.
sps

#16 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

In reply to Stephen P. Smith
Quoted 9 lines · Stephen P. Smith

The human date format varies based on two inputs: the date in the reference time which is constant and the local computers date which varies. Using hardcoded test expected output dates would require holding the local machines date and time constant which is not desireable.

Alternatively, letting the local date vary, which is the normal situation, implies that the tests would be checking for formating changes based on on a ref date relative to the local computers time.

We already have $TEST_DATE_NOW, which "test-tool date" will respect for various commands to pretend that it's currently a particular time. I think you'd need to add a sub-command similar to "relative" (which directly calls show_date_relative()) which calls into the "human" code.

Note that there _isn't_ a way to have actual non-test git programs read the current time from an environment variable (as opposed to actually calling gettimeofday()).

-Peff

#17 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 4 lines · MST Jeff King

We already have $TEST_DATE_NOW, which "test-tool date" will respect for various commands to pretend that it's currently a particular time. I think you'd need to add a sub-command similar to "relative" (which directly calls show_date_relative()) which calls into the "human" code.

I'll investigate. Looks like this comment is related other comments.

Quoted 3 lines

Note that there _isn't_ a way to have actual non-test git programs read the current time from an environment variable (as opposed to actually calling gettimeofday()).

Agreed

Quoted 2 lines

-Peff

#18 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

In reply to Stephen P. Smith

Am 31.12.18 um 01:31 schrieb Stephen P. Smith:

Quoted 88 lines
t/t0006-date.sh +24 -0
+check_human_date () {
+	time=$1
+	expect=$2
+	test_expect_success "check date ($format:$time)" '
+		echo "$time -> $expect" >expect &&
+		TZ=${zone:-$TZ} test-tool date show:"$format" "$time" >actual &&
+		grep "$expect" actual
+	'
+}
+
  # arbitrary but sensible time for examples
  TIME='1466000000 +0200'
  check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
@@ -52,6 +62,20 @@ check_show unix "$TIME" '1466000000'
  check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
  check_show raw-local "$TIME" '1466000000 +0000'
  check_show unix-local "$TIME" '1466000000'
+check_show human "$TIME" 'Jun 15 2016'
+
+# Subtract some known constant time and look for expected field format
+TODAY_REGEX='5 hours ago'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
 
  check_show 'format:%z' "$TIME" '+0200'
  check_show 'format-local:%z' "$TIME" '+0000'
t/t4202-log.sh +24 -0
index 819c24d10e..d7f3b73650 100755
@@ -1707,4 +1707,28 @@ test_expect_success '--exclude-promisor-objects does not BUG-crash' '
  	test_must_fail git log --exclude-promisor-objects source-a
  '
 
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates &&
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git log -1 --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"
+}
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago
+
  test_done
t/t7007-show.sh +13 -0
index 42d3db6246..0a0334a8b5 100755
@@ -128,4 +128,29 @@ test_expect_success 'show --graph is forbidden' '
    test_must_fail git show --graph HEAD
  '
 
+check_human_date() {
+	commit_date=$1
+	expect=$2
+	test_expect_success "$commit_date" "
+		echo $expect $commit_date >dates &&
+		git add dates &&
+		git commit -m 'Expect String' --date=\"$commit_date\" dates &&
+		git show --date=human | grep \"^Date:\" >actual &&
+		grep \"$expect\" actual
+"
+}
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'

t/t7007-show.sh test_expect_success 'show --graph is forbidden' ' · line 153

Quoted 10 lines
t/t7007-show.sh +10 -0
+THIS_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [012][0-9]:[0-6][0-9]'
+MORE_THAN_A_YEAR_REGEX='[A-Z][a-z][a-z] [A-Z][a-z][a-z] [0-9]* [0-9][0-9][0-9][0-9]'
+check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX # 5 hours ago
+check_human_date "$(($(date +%s)-432000)) +0200" $THIS_YEAR_REGEX  # 5 days ago
+check_human_date "$(($(date +%s)-1728000)) +0200" $THIS_YEAR_REGEX # 3 weeks ago
+check_human_date "$(($(date +%s)-13000000)) +0200" $THIS_YEAR_REGEX # 5 months ago
+check_human_date "$(($(date +%s)-31449600)) +0200" $THIS_YEAR_REGEX # 12 months ago
+check_human_date "$(($(date +%s)-37500000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 2 months ago
+check_human_date "$(($(date +%s)-55188000)) +0200" $MORE_THAN_A_YEAR_REGEX # 1 year, 9 months ago
+check_human_date "$(($(date +%s)-630000000)) +0200" $MORE_THAN_A_YEAR_REGEX # 20 years ago

The $...REGEX expansions must be put in double-quotes to protect them from field splitting. But then the tests do not pass anymore (I tested only t4202). Please revisit this change.

-- Hannes

#19 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 3 lines · MST Johannes Sixt

Am 31.12.18 um 01:31 schrieb Stephen P. Smith:

Quoted 2 lines
(quoted diff) +2 -0
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'

<snip>

Quoted 5 lines

The $...REGEX expansions must be put in double-quotes to protect them from field splitting. But then the tests do not pass anymore (I tested only t4202). Please revisit this change.

-- Hannes

I will later figure out why you are seeing the fields splitting but I am not. In the mean time I will change the quoting.

I started working on test updates based on prior comments this past weekend.

sps

#20 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Am 09.01.19 um 01:44 schrieb Stephen P. Smith:

Quoted 11 lines · MST Johannes Sixt
Quoted 3 lines

Am 31.12.18 um 01:31 schrieb Stephen P. Smith:

Quoted 2 lines
(quoted diff) +2 -0
+
+TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'

<snip>

Quoted 3 lines

The $...REGEX expansions must be put in double-quotes to protect them from field splitting. But then the tests do not pass anymore (I tested only t4202). Please revisit this change.

I will later figure out why you are seeing the fields splitting but I am not. In the mean time I will change the quoting.

In this line

TODAY_REGEX='[A-Z][a-z][a-z] [012][0-9]:[0-6][0-9] .0200'

no field splitting occurs. The quoting is fine here. But notice that the value of $TODAY_REGEX contains blanks.

In this line

check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX

the value of $TODAY_REGEX is substituted and then the value is split into fields at the blanks because the expansion is not quoted.

As a consequence, function check_human_date considers only the first part of $TODAY_REGEX, i.e. 'A-Z][a-z][a-z]' (which is parameter $2), but ignores everything else (because it does not use $3 or $4).

-- Hannes

#21 lore raw

Re: [PATCH 3/3] t0006-date.sh: add `human` date format tests.

Quoted 6 lines · MST Johannes Sixt

But notice that the value of $TODAY_REGEX contains blanks.

In this line

check_human_date "$(($(date +%s)-18000)) +0200" $TODAY_REGEX

Quoted 8 lines

the value of $TODAY_REGEX is substituted and then the value is split into fields at the blanks because the expansion is not quoted.

As a consequence, function check_human_date considers only the first part of $TODAY_REGEX, i.e. 'A-Z][a-z][a-z]' (which is parameter $2), but ignores everything else (because it does not use $3 or $4).

-- Hannes

I hadn't understood your original comment, but now i understand. Will fix.

#22 lore raw

v2 cover Re-roll of 'human' date format patch set

In reply to Stephen P. Smith

Reworked documentation and tests for the previously submitted patch set.

Linus Torvalds (1):
Add 'human' date format

Stephen P. Smith (4):
Remove the proposed use of auto as secondary way to specify human Add 'human' date format documentation
Add human format to test-tool
Add human date format tests.

 Documentation/git-log.txt          |   4 +
 Documentation/rev-list-options.txt |   6 ++
 builtin/blame.c                    |   4 +
 cache.h                            |   3 +
 date.c                             | 153 +++++++++++++++++++++++++----
 t/helper/test-date.c               |  15 +++
 t/t0006-date.sh                    |  20 ++++
 7 files changed, 185 insertions(+), 20 deletions(-)
Signature · 1 line
2.20.1.2.gb21ebb671b
#23 lore raw

PATCH v2 1/5 Add 'human' date format

From: Linus Torvalds <[email protected]>

This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was).

For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone.

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view.

---
 builtin/blame.c |   4 ++
 cache.h         |   1 +
 date.c          | 130 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 20 deletions(-)
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 		break;
cache.h +1 -0
index 49713cc5a5..34c33e6a28 100644
@@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen,
 
 enum date_mode_type {
 	DATE_NORMAL = 0,
+	DATE_HUMAN,
 	DATE_RELATIVE,
 	DATE_SHORT,
 	DATE_ISO8601,
date.c +110 -20

179 lines not on this page · raw message

Signature · 1 line
2.20.1.2.gb21ebb671b
#24 lore raw

PATCH v2 2/5 Remove the proposed use of auto as secondary way to specify human

In reply to Stephen P. Smith

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Instead add 'auto:human' date mode which defaults to human if we're using the pager. So you can do

	git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.

---
 date.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
date.c +8 -7
index a8d50eb206..43c3a84e25 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";
Signature · 1 line
2.20.1.2.gb21ebb671b
#25 lore raw

Re: [PATCH v2 2/5] Remove the proposed use of auto as secondary way to specify human

Quoted 5 lines · Stephen P. Smith

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Quoted 7 lines

Instead add 'auto:human' date mode which defaults to human if we're using the pager. So you can do

	git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.

I think doing two things in this step (i.e. reverting Linus's "auto" support from 1/5, and adding "auto" that is similar to color's auto) is OK, but then the title should list both. It sounded like it was this step is doing only the former.

Quoted 46 lines
---
 date.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
date.c +8 -7
index a8d50eb206..43c3a84e25 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";
#26 lore raw

Re: [PATCH v2 2/5] Remove the proposed use of auto as secondary way to specify human

Quoted 5 lines · MST Junio C Hamano

"Stephen P. Smith" <[email protected]> writes:
I think doing two things in this step (i.e. reverting Linus's "auto" support from 1/5, and adding "auto" that is similar to color's auto) is OK, but then the title should list both. It sounded like it was this step is doing only the former.

Will change as part of a re-roll.

#27 lore raw

PATCH v2 3/5 Add 'human' date format documentation

In reply to Stephen P. Smith

Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate.

---
 Documentation/git-log.txt          | 4 ++++
 Documentation/rev-list-options.txt | 6 ++++++
 2 files changed, 10 insertions(+)
Documentation/git-log.txt +4 -0
index 90761f1694..1d2d932c76 100644
@@ -193,6 +193,10 @@ log.date::
 	`--date` option.)  Defaults to "default", which means to write
 	dates like `Sat May 8 19:35:34 2010 -0500`.
 
+	If the format is set to "auto:foo", then if the pager is in
+	use format "foo" will be the used for the date format, otherwise
+	"default" will be used.
+
 log.follow::
 	If `true`, `git log` will act as if the `--follow` option was used when
 	a single <path> is given.  This has the same limitations as `--follow`,
Documentation/rev-list-options.txt +6 -0
index bab5f50b17..5d58f35d19 100644
@@ -835,6 +835,12 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if it matches the current time-zone,
+and doesn't print the whole date if that matches (ie skip printing
+year for dates that are "this year", but also skip the whole date
+itself if it's in the last few days and we can just say what weekday
+it was).
++
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
Signature · 1 line
2.20.1.2.gb21ebb671b
#28 lore raw

Re: [PATCH v2 3/5] Add 'human' date format documentation

Documentation/git-log.txt log.date:: · line 199

Quoted 12 lines · Stephen P. Smith
Documentation/git-log.txt +4 -0
index 90761f1694..1d2d932c76 100644
@@ -193,6 +193,10 @@ log.date::
 	`--date` option.)  Defaults to "default", which means to write
 	dates like `Sat May 8 19:35:34 2010 -0500`.
 
+	If the format is set to "auto:foo", then if the pager is in
+	use format "foo" will be the used for the date format, otherwise
+	"default" will be used.
+

This text is good, but this would break ASCIIdoc formatting, wouldn't it? Observe how "notes.displayRef::" section does three-paragraph description and mimick it to make this two-paragraph description, perhaps.

Documentation/rev-list-options.txt Note that the `-local` option does not affect the seconds-since-epoch · line 838

Quoted 12 lines
Documentation/git-log.txt +0 -0
 log.follow::
 	If `true`, `git log` will act as if the `--follow` option was used when
 	a single <path> is given.  This has the same limitations as `--follow`,
Documentation/rev-list-options.txt +1 -0
index bab5f50b17..5d58f35d19 100644
@@ -835,6 +835,12 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if it matches the current time-zone,

Is it clear in the context that "it" refers to "the timestamp being shown"?

I think the behaviour is that timezone is shown only the timestamp being shown is from a different timezone (i.e. if it *does* *not* match), though.

Documentation/rev-list-options.txt Note that the `-local` option does not affect the seconds-since-epoch · line 843

Quoted 5 lines
Documentation/rev-list-options.txt +5 -0
+and doesn't print the whole date if that matches (ie skip printing
+year for dates that are "this year", but also skip the whole date
+itself if it's in the last few days and we can just say what weekday
+it was).
++

... and also omit hour/minute part for a timestamp that is old enough.

Quoted 3 lines
Documentation/rev-list-options.txt +0 -0
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
#29 lore raw

PATCH v2 4/5 Add `human` format to test-tool

In reply to Stephen P. Smith

Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time.

A static variable is used for passing the tool specified value to get_date. The get_date helper function eliminates the need to refactor up the show_date and show_date normal functions to pass the time value.

---
 cache.h              |  2 ++
 date.c               | 26 ++++++++++++++++++++++++--
 t/helper/test-date.c | 15 +++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)
cache.h +2 -0
index 34c33e6a28..fe00ddf910 100644
@@ -1467,6 +1467,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
 const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
 void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
 			struct strbuf *timebuf);
+void show_date_human(timestamp_t time, int tz, const struct timeval *now,
+			struct strbuf *timebuf);
 int parse_date(const char *date, struct strbuf *out);
 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
 int parse_expiry_date(const char *date, timestamp_t *timestamp);
date.c +24 -2
index 43c3a84e25..24435b1e1d 100644
@@ -115,6 +115,28 @@ static int local_tzoffset(timestamp_t time)
 	return local_time_tzoffset((time_t)time, &tm);
 }
 
+const struct timeval *test_time = 0;
+void show_date_human(timestamp_t time, int tz,
+			       const struct timeval *now,
+			       struct strbuf *timebuf)
+{
+	test_time = (const struct timeval *) now;
+	strbuf_addstr( timebuf, show_date(time, tz, DATE_MODE(HUMAN)));
+	test_time = (const struct timeval *) 0;
+}
+
+static void get_time(struct timeval *now)
+{
+	if(test_time != 0)
+		/*
+		 * If show_date was called via the test
+		 *  interface use the test_tool time
+		 */
+		*now = *test_time;
+	else
+		gettimeofday(now, NULL);
+}
+
 void show_date_relative(timestamp_t time, int tz,
 			       const struct timeval *now,
 			       struct strbuf *timebuf)
@@ -228,7 +250,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
 	/* Show "today" times as just relative times */
 	if (hide.wday) {
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, buf);
 		return;
 	}
@@ -284,7 +306,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 	if (mode->type == DATE_HUMAN) {
 		struct timeval now;
 
-		gettimeofday(&now, NULL);
+		get_time(&now);
 
 		/* Fill in the data for "current time" in human_tz and human_tm */
 		human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
t/helper/test-date.c +15 -0
index a0837371ab..22d42a2174 100644
@@ -3,6 +3,7 @@
 
 static const char *usage_msg = "\n"
 "  test-tool date relative [time_t]...\n"
+"  test-tool date human [time_t]...\n"
 "  test-tool date show:<format> [time_t]...\n"
 "  test-tool date parse [date]...\n"
 "  test-tool date approxidate [date]...\n"
@@ -22,6 +23,18 @@ static void show_relative_dates(const char **argv, struct timeval *now)
 	strbuf_release(&buf);
 }
 
+static void show_human_dates(const char **argv, struct timeval *now)
+{
+	struct strbuf buf = STRBUF_INIT;
+
+	for (; *argv; argv++) {
+		time_t t = atoi(*argv);
+		show_date_human(t, 0, now, &buf);
+		printf("%s -> %s\n", *argv, buf.buf);
+	}
+	strbuf_release(&buf);
+}
+
 static void show_dates(const char **argv, const char *format)
 {
 	struct date_mode mode;
@@ -100,6 +113,8 @@ int cmd__date(int argc, const char **argv)
 		usage(usage_msg);
 	if (!strcmp(*argv, "relative"))
 		show_relative_dates(argv+1, &now);
+	else if (!strcmp(*argv, "human"))
+		show_human_dates(argv+1, &now);
 	else if (skip_prefix(*argv, "show:", &x))
 		show_dates(argv+1, x);
 	else if (!strcmp(*argv, "parse"))
Signature · 1 line
2.20.1.2.gb21ebb671b
#30 lore raw

Re: [PATCH v2 4/5] Add `human` format to test-tool

Quoted 7 lines · Stephen P. Smith

Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time.

A static variable is used for passing the tool specified value to get_date. The get_date helper function eliminates the need to refactor up the show_date and show_date normal functions to pass the time value.

Hmph. An interesting approach, but the implementation is a bit too messy.

date.c static int local_tzoffset(timestamp_t time) · line 118

Quoted 9 lines
date.c +1 -0
index 43c3a84e25..24435b1e1d 100644
@@ -115,6 +115,28 @@ static int local_tzoffset(timestamp_t time)
 	return local_time_tzoffset((time_t)time, &tm);
 }
 
+const struct timeval *test_time = 0;

Shouldn't this be file-scope static?

Let BSS take care of initializing a variable to 0/NULL; drop " = 0" at the end.

date.c static int local_tzoffset(timestamp_t time) · line 124

Quoted 6 lines
date.c +6 -0
+void show_date_human(timestamp_t time, int tz,
+			       const struct timeval *now,
+			       struct strbuf *timebuf)
+{
+	test_time = (const struct timeval *) now;
+	strbuf_addstr( timebuf, show_date(time, tz, DATE_MODE(HUMAN)));

Style:

	strbuf_addstr(timebuf, show_date(time, tz, DATE_MODE(HUMAN)));

date.c static int local_tzoffset(timestamp_t time) · line 126

Quoted 2 lines
date.c +2 -0
+	test_time = (const struct timeval *) 0;
+}

It is a shame that you introduced a nicely reusable get_time() mechanism to let external callers of show_date() specify what time to format, instead of the returned timestamp of gettimeofday(), but limited its usefulness to only testing "human" format output. If somebody wants to extend "test-tool date" for other formats, they also have to add a similar "show_date_XXX" hack for their format.

How about doing it slightly differently? E.g.

- Get rid of show_date_human().

- Keep get_time(), but have it pay attention to GIT_TEST_TIMESTAMP environment variable, and when it is set, use that as if it is the returned value from gettimeofday().

- If there are gettimeofday() calls in date.c this patch did not touch (because they were not part of the "human-format" codepath), adjust them to use get_time() instead.

- Have "test-tool date" excersize show_date() directly.

Quoted 73 lines
date.c +14 -2
+static void get_time(struct timeval *now)
+{
+	if(test_time != 0)
+		/*
+		 * If show_date was called via the test
+		 *  interface use the test_tool time
+		 */
+		*now = *test_time;
+	else
+		gettimeofday(now, NULL);
+}
+
 void show_date_relative(timestamp_t time, int tz,
 			       const struct timeval *now,
 			       struct strbuf *timebuf)
@@ -228,7 +250,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
 	/* Show "today" times as just relative times */
 	if (hide.wday) {
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, buf);
 		return;
 	}
@@ -284,7 +306,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 	if (mode->type == DATE_HUMAN) {
 		struct timeval now;
 
-		gettimeofday(&now, NULL);
+		get_time(&now);
 
 		/* Fill in the data for "current time" in human_tz and human_tm */
 		human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
t/helper/test-date.c +15 -0
index a0837371ab..22d42a2174 100644
@@ -3,6 +3,7 @@
 
 static const char *usage_msg = "\n"
 "  test-tool date relative [time_t]...\n"
+"  test-tool date human [time_t]...\n"
 "  test-tool date show:<format> [time_t]...\n"
 "  test-tool date parse [date]...\n"
 "  test-tool date approxidate [date]...\n"
@@ -22,6 +23,18 @@ static void show_relative_dates(const char **argv, struct timeval *now)
 	strbuf_release(&buf);
 }
 
+static void show_human_dates(const char **argv, struct timeval *now)
+{
+	struct strbuf buf = STRBUF_INIT;
+
+	for (; *argv; argv++) {
+		time_t t = atoi(*argv);
+		show_date_human(t, 0, now, &buf);
+		printf("%s -> %s\n", *argv, buf.buf);
+	}
+	strbuf_release(&buf);
+}
+
 static void show_dates(const char **argv, const char *format)
 {
 	struct date_mode mode;
@@ -100,6 +113,8 @@ int cmd__date(int argc, const char **argv)
 		usage(usage_msg);
 	if (!strcmp(*argv, "relative"))
 		show_relative_dates(argv+1, &now);
+	else if (!strcmp(*argv, "human"))
+		show_human_dates(argv+1, &now);
 	else if (skip_prefix(*argv, "show:", &x))
 		show_dates(argv+1, x);
 	else if (!strcmp(*argv, "parse"))
#31 lore raw

Re: [PATCH v2 4/5] Add `human` format to test-tool

Quoted 15 lines · MST Junio C Hamano

It is a shame that you introduced a nicely reusable get_time() mechanism to let external callers of show_date() specify what time to format, instead of the returned timestamp of gettimeofday(), but limited its usefulness to only testing "human" format output. If somebody wants to extend "test-tool date" for other formats, they also have to add a similar "show_date_XXX" hack for their format.

How about doing it slightly differently? E.g.

- Get rid of show_date_human().

- Keep get_time(), but have it pay attention to GIT_TEST_TIMESTAMP environment variable, and when it is set, use that as if it is the returned value from gettimeofday().

Quoted 6 lines

- If there are gettimeofday() calls in date.c this patch did not touch (because they were not part of the "human-format" codepath), adjust them to use get_time() instead.

- Have "test-tool date" excersize show_date() directly.

I did follow the pattern set for relative (which is why I created
show_date_human() to mimic show_date_relative() ) as had been suggested. I
like this pattern better.

Why don't I create a second patch set after I git this one to next for relative to match your suggestion. I don't like the idea of conflating two topics.

sps

#32 lore raw

Re: [PATCH v2 4/5] Add `human` format to test-tool

Quoted 3 lines · Stephen P. Smith

Why don't I create a second patch set after I git this one to next for relative to match your suggestion. I don't like the idea of conflating two topics.

I'd prefer two separate patches, too, and I'd prefer to see clean-up first and then a new feature.

Thanks.

#33 lore raw

PATCH v2 5/5 Add `human` date format tests.

In reply to Stephen P. Smith

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant.

---
 t/t0006-date.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
t/t0006-date.sh +20 -0
index ffb2975e48..c7c0786b24 100755
@@ -128,4 +128,24 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
 check_approxidate '2008-12-01' '2008-12-01 19:20:00'
 check_approxidate '2009-12-01' '2009-12-01 19:20:00'
 
+check_date_format() {
+	format=$1
+	t=$(($TEST_DATE_NOW - $2))
+	expect=$3
+	test_expect_success "human date $t" "
+	echo $TEST_DATE_NOW >now &&
+	test-tool date human $t >actual &&
+	grep '$expect' actual
+"
+}
+
+check_date_format human 18000 "5 hours ago" # 5 hours ago
+check_date_format human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#34 lore raw

Re: [PATCH v2 5/5] Add `human` date format tests.

t/t0006-date.sh check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00' · line 134

Quoted 4 lines · Stephen P. Smith
t/t0006-date.sh +4 -0
+check_date_format() {
+	format=$1
+	t=$(($TEST_DATE_NOW - $2))
+	expect=$3

Notice that neither $format nor $1 is used in this test, which means that "check_date_format" is not a generic "I can take a format parameter to check the specified one". So perhaps

	check_date_format_human () {

and then lose the first parameter?

t/t0006-date.sh check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00' · line 139

Quoted 5 lines
t/t0006-date.sh +5 -0
+	test_expect_success "human date $t" "
+	echo $TEST_DATE_NOW >now &&
+	test-tool date human $t >actual &&
+	grep '$expect' actual
+"

Hopefully $3 does not have a single quote in it ;-)

But the test block can see the shell variables just fine, so writing it like the following is more in line with how the test framework is designed to be used.

	test_expect_success "human date $t" '
		echo "$TEST_DATE_NOW" >now &&
		test-tool date human "$t" >actual &&
		grep "$expect" actual
	'

How is the file 'now' get used? Nobody seems to read it around here. Is the last one supposed to be "grep"? Or should we do

	echo "$expect" >expect &&
	test_cmp expect actual

instead?

Quoted 12 lines
t/t0006-date.sh +11 -0
+}
+
+check_date_format human 18000 "5 hours ago" # 5 hours ago
+check_date_format human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
#35 lore raw

v3 cover Re-roll of 'human' date format patch set

In reply to Stephen P. Smith

Reworked documentation and tests for the previously submitted patch set.

Linus Torvalds (1):
Add 'human' date format

Stephen P. Smith (4):
Replace the proposed 'auto' mode with 'auto:'
Add 'human' date format documentation
Add human format to test-tool
Add human date format tests.

 Documentation/git-log.txt          |   4 +
 Documentation/rev-list-options.txt |   7 ++
 builtin/blame.c                    |   4 +
 cache.h                            |   3 +
 date.c                             | 148 ++++++++++++++++++++++++-----
 t/helper/test-date.c               |  11 +++
 t/t0006-date.sh                    |  18 ++++
 7 files changed, 173 insertions(+), 22 deletions(-)
Signature · 1 line
2.20.1.2.gb21ebb671b
#36 lore raw

PATCH v3 1/5 Add 'human' date format

From: Linus Torvalds <[email protected]>

This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was).

For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone.

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view.

---
 builtin/blame.c |   4 ++
 cache.h         |   1 +
 date.c          | 130 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 20 deletions(-)
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 		break;
cache.h +1 -0
index 49713cc5a5..34c33e6a28 100644
@@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen,
 
 enum date_mode_type {
 	DATE_NORMAL = 0,
+	DATE_HUMAN,
 	DATE_RELATIVE,
 	DATE_SHORT,
 	DATE_ISO8601,
date.c +110 -20

179 lines not on this page · raw message

Signature · 1 line
2.20.1.2.gb21ebb671b
#37 lore raw

PATCH v3 2/5 Replace the proposed 'auto' mode with 'auto:'

In reply to Stephen P. Smith

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax. Therefore, 'auto:human' date mode defaults to human if we're using the pager. So you can do

	git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.

---
 date.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
date.c +8 -7
index a8d50eb206..43c3a84e25 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";
Signature · 1 line
2.20.1.2.gb21ebb671b
#38 lore raw

PATCH v3 3/5 Add 'human' date format documentation

In reply to Stephen P. Smith

Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate.

---
 Documentation/git-log.txt          | 4 ++++
 Documentation/rev-list-options.txt | 7 +++++++
 2 files changed, 11 insertions(+)
Documentation/git-log.txt +4 -0
index 90761f1694..b02e922dc3 100644
@@ -192,6 +192,10 @@ log.date::
 	Default format for human-readable dates.  (Compare the
 	`--date` option.)  Defaults to "default", which means to write
 	dates like `Sat May 8 19:35:34 2010 -0500`.
++
+If the format is set to "auto:foo" and the pager is in use, format
+"foo" will be the used for the date format. Otherwise "default" will
+be used.
 
 log.follow::
 	If `true`, `git log` will act as if the `--follow` option was used when
Documentation/rev-list-options.txt +7 -0
index 98b538bc77..867a063a1c 100644
@@ -831,6 +831,13 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if the timezone does not match the
+current time-zone, and doesn't print the whole date if that matches
+(ie skip printing year for dates that are "this year", but also skip
+the whole date itself if it's in the last few days and we can just say
+what weekday it was).  For older dates the hour and minute is also
+omitted.
++
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
Signature · 1 line
2.20.1.2.gb21ebb671b
#39 lore raw

PATCH v3 4/5 Add `human` format to test-tool

In reply to Stephen P. Smith

Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time.

The get_time() helper function was created and and checks the TEST_DATE_NOW environment variable. If TEST_DATE_NOW is set, then that date is used instead of the date returned by by gettimeofday().

All calls to gettimeofday() were replaced by calls to get_time().

---
 cache.h              |  2 ++
 date.c               | 21 +++++++++++++++++----
 t/helper/test-date.c | 11 +++++++++++
 3 files changed, 30 insertions(+), 4 deletions(-)
cache.h +2 -0
index 34c33e6a28..fe00ddf910 100644
@@ -1467,6 +1467,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
 const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
 void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
 			struct strbuf *timebuf);
+void show_date_human(timestamp_t time, int tz, const struct timeval *now,
+			struct strbuf *timebuf);
 int parse_date(const char *date, struct strbuf *out);
 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
 int parse_expiry_date(const char *date, timestamp_t *timestamp);
date.c +17 -4
index 43c3a84e25..5f9ea7d5d7 100644
@@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time)
 	return local_time_tzoffset((time_t)time, &tm);
 }
 
+static void get_time(struct timeval *now)
+{
+	const char *x;
+
+	x = getenv("TEST_DATE_NOW");
+	if (x) {
+		now->tv_sec = atoi(x);
+		now->tv_usec = 0;
+	}
+	else
+		gettimeofday(now, NULL);
+}
+
 void show_date_relative(timestamp_t time, int tz,
 			       const struct timeval *now,
 			       struct strbuf *timebuf)
@@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
 	/* Show "today" times as just relative times */
 	if (hide.wday) {
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, buf);
 		return;
 	}
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 	if (mode->type == DATE_HUMAN) {
 		struct timeval now;
 
-		gettimeofday(&now, NULL);
+		get_time(&now);
 
 		/* Fill in the data for "current time" in human_tz and human_tm */
 		human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
@@ -303,7 +316,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 		struct timeval now;
 
 		strbuf_reset(&timebuf);
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, &timebuf);
 		return timebuf.buf;
 	}
@@ -1296,7 +1309,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
 		return timestamp;
 	}
 
-	gettimeofday(&tv, NULL);
+	get_time(&tv);
 	return approxidate_str(date, &tv, error_ret);
 }
 
t/helper/test-date.c +11 -0
index a0837371ab..811b116c7e 100644
@@ -3,6 +3,7 @@
 
 static const char *usage_msg = "\n"
 "  test-tool date relative [time_t]...\n"
+"  test-tool date human [time_t]...\n"
 "  test-tool date show:<format> [time_t]...\n"
 "  test-tool date parse [date]...\n"
 "  test-tool date approxidate [date]...\n"
@@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now)
 	strbuf_release(&buf);
 }
 
+static void show_human_dates(const char **argv)
+{
+	for (; *argv; argv++) {
+		time_t t = atoi(*argv);
+		printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
+	}
+}
+
 static void show_dates(const char **argv, const char *format)
 {
 	struct date_mode mode;
@@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv)
 		usage(usage_msg);
 	if (!strcmp(*argv, "relative"))
 		show_relative_dates(argv+1, &now);
+	else if (!strcmp(*argv, "human"))
+		show_human_dates(argv+1);
 	else if (skip_prefix(*argv, "show:", &x))
 		show_dates(argv+1, x);
 	else if (!strcmp(*argv, "parse"))
Signature · 1 line
2.20.1.2.gb21ebb671b
#40 lore raw

Re: [PATCH v3 4/5] Add `human` format to test-tool

Quoted 2 lines · Stephen P. Smith

Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time.

This sounds sensible, but let's do s/TEST_DATE_NOW/GIT_&/ to make sure that we will not pick up somebody else's random environment variable in production Git binary.

The name of the original environment variable was OK as it was very much limited to t/helper/test-date.c, but a name that we'll pay attention to in production binary is a different matter.

Quoted 1 line

All calls to gettimeofday() were replaced by calls to get_time().

Yup, good.

The next to go would be the getenv(TEST_DATE_NOW) at the beginning of cmd__date(), which probably would lead to simplification to functions it calls by losing &now parameter. But that is a separate clean-up.

#41 lore raw

PATCH v3 5/5 Add `human` date format tests.

In reply to Stephen P. Smith

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant.

---
 t/t0006-date.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
t/t0006-date.sh +18 -0
index ffb2975e48..35f7b52dd7 100755
@@ -128,4 +128,22 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
 check_approxidate '2008-12-01' '2008-12-01 19:20:00'
 check_approxidate '2009-12-01' '2009-12-01 19:20:00'
 
+check_date_format_human() {
+	t=$(($TEST_DATE_NOW - $1))
+	echo "$t -> $2" >expect
+	test_expect_success "human date $t" '
+	test-tool date human $t >actual &&
+	test_cmp expect actual
+'
+}
+
+check_date_format_human 18000 "5 hours ago" # 5 hours ago
+check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#42 lore raw

v4 cover Re-roll of 'human' date format patch set

In reply to Stephen P. Smith

Changed the binary and test to rename TEST_DATE_NOW environment variable. Changed the 'human' test to test_i18ncmp.

Updates for the relative date format will be done in a follow-on patch.

Range-diff relative to v3:

1:  ee75d9002d = 1:  ee75d9002d Add 'human' date format
2:  722c3319cd = 2:  722c3319cd Replace the proposed 'auto' mode with 'auto:'
3:  67cb73edb4 = 3:  67cb73edb4 Add 'human' date format documentation
4:  1562e7c713 ! 4:  a3f6f820b2 Add `human` format to test-tool
    @@ -2,15 +2,19 @@
     
         Add `human` format to test-tool
     
    -    Add the human format support to the test tool so that TEST_DATE_NOW
    -    can be used to specify the current time.
    +    Add the human format support to the test tool so that
    +    GIT_TEST_DATE_NOW can be used to specify the current time.
     
         The get_time() helper function was created and and checks the
    -    TEST_DATE_NOW environment variable.  If TEST_DATE_NOW is set, then
    -    that date is used instead of the date returned by by gettimeofday().
    +    GIT_TEST_DATE_NOW environment variable.  If GIT_TEST_DATE_NOW is set,
    +    then that date is used instead of the date returned by by
    +    gettimeofday().
     
         All calls to gettimeofday() were replaced by calls to get_time().
     
    +    Renamed occurances of TEST_DATE_NOW to GIT_TEST_DATE_NOW since the
    +    variable is now used in the get binary and not just in the test-tool.
    +
      diff --git a/cache.h b/cache.h
      --- a/cache.h
      +++ b/cache.h
    @@ -35,7 +39,7 @@
     +{
     +	const char *x;
     +
    -+	x = getenv("TEST_DATE_NOW");
    ++	x = getenv("GIT_TEST_DATE_NOW");
     +	if (x) {
     +		now->tv_sec = atoi(x);
     +		now->tv_usec = 0;
    @@ -110,6 +114,15 @@
      static void show_dates(const char **argv, const char *format)
      {
      	struct date_mode mode;
    +@@
    + 	struct timeval now;
    + 	const char *x;
    + 
    +-	x = getenv("TEST_DATE_NOW");
    ++	x = getenv("GIT_TEST_DATE_NOW");
    + 	if (x) {
    + 		now.tv_sec = atoi(x);
    + 		now.tv_usec = 0;
     @@
      		usage(usage_msg);
      	if (!strcmp(*argv, "relative"))
    @@ -119,3 +132,20 @@
      	else if (skip_prefix(*argv, "show:", &x))
      		show_dates(argv+1, x);
      	else if (!strcmp(*argv, "parse"))
    +
    + diff --git a/t/t0006-date.sh b/t/t0006-date.sh
    + --- a/t/t0006-date.sh
    + +++ b/t/t0006-date.sh
    +@@
    + . ./test-lib.sh
    + 
    + # arbitrary reference time: 2009-08-30 19:20:00
    +-TEST_DATE_NOW=1251660000; export TEST_DATE_NOW
    ++GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
    + 
    + check_relative() {
    +-	t=$(($TEST_DATE_NOW - $1))
    ++	t=$(($GIT_TEST_DATE_NOW - $1))
    + 	echo "$t -> $2" >expect
    + 	test_expect_${3:-success} "relative date ($2)" "
    + 	test-tool date relative $t >actual &&
5:  5e6b5705fd ! 5:  87d3a81e8a Add `human` date format tests.
    @@ -19,11 +19,11 @@
      check_approxidate '2009-12-01' '2009-12-01 19:20:00'
      
     +check_date_format_human() {
    -+	t=$(($TEST_DATE_NOW - $1))
    ++	t=$(($GIT_TEST_DATE_NOW - $1))
     +	echo "$t -> $2" >expect
     +	test_expect_success "human date $t" '
    -+	test-tool date human $t >actual &&
    -+	test_cmp expect actual
    ++		test-tool date human $t >actual &&
    ++		test_i18ncmp expect actual
     +'
     +}
     +

Linus Torvalds (1):
Add 'human' date format

Stephen P. Smith (4):
Replace the proposed 'auto' mode with 'auto:'
Add 'human' date format documentation
Add human format to test-tool
Add human date format tests.

 Documentation/git-log.txt          |   4 +
 Documentation/rev-list-options.txt |   7 ++
 builtin/blame.c                    |   4 +
 cache.h                            |   3 +
 date.c                             | 148 ++++++++++++++++++++++++-----
 t/helper/test-date.c               |  13 ++-
 t/t0006-date.sh                    |  22 ++++-
 7 files changed, 176 insertions(+), 25 deletions(-)
Signature · 1 line
2.20.1.2.gb21ebb671b
#43 lore raw

PATCH v4 1/5 Add 'human' date format

From: Linus Torvalds <[email protected]>

This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was).

For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone.

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view.

---
 builtin/blame.c |   4 ++
 cache.h         |   1 +
 date.c          | 130 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 20 deletions(-)
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 		break;
cache.h +1 -0
index 49713cc5a5..34c33e6a28 100644
@@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen,
 
 enum date_mode_type {
 	DATE_NORMAL = 0,
+	DATE_HUMAN,
 	DATE_RELATIVE,
 	DATE_SHORT,
 	DATE_ISO8601,
date.c +110 -20

179 lines not on this page · raw message

Signature · 1 line
2.20.1.2.gb21ebb671b
#44 lore raw

PATCH v4 2/5 Replace the proposed 'auto' mode with 'auto:'

In reply to Stephen P. Smith

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax. Therefore, 'auto:human' date mode defaults to human if we're using the pager. So you can do

	git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.

---
 date.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
date.c +8 -7
index a8d50eb206..43c3a84e25 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";
Signature · 1 line
2.20.1.2.gb21ebb671b
#45 lore raw

PATCH v4 3/5 Add 'human' date format documentation

In reply to Stephen P. Smith

Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate.

---
 Documentation/git-log.txt          | 4 ++++
 Documentation/rev-list-options.txt | 7 +++++++
 2 files changed, 11 insertions(+)
Documentation/git-log.txt +4 -0
index 90761f1694..b02e922dc3 100644
@@ -192,6 +192,10 @@ log.date::
 	Default format for human-readable dates.  (Compare the
 	`--date` option.)  Defaults to "default", which means to write
 	dates like `Sat May 8 19:35:34 2010 -0500`.
++
+If the format is set to "auto:foo" and the pager is in use, format
+"foo" will be the used for the date format. Otherwise "default" will
+be used.
 
 log.follow::
 	If `true`, `git log` will act as if the `--follow` option was used when
Documentation/rev-list-options.txt +7 -0
index 98b538bc77..867a063a1c 100644
@@ -831,6 +831,13 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if the timezone does not match the
+current time-zone, and doesn't print the whole date if that matches
+(ie skip printing year for dates that are "this year", but also skip
+the whole date itself if it's in the last few days and we can just say
+what weekday it was).  For older dates the hour and minute is also
+omitted.
++
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
Signature · 1 line
2.20.1.2.gb21ebb671b
#46 lore raw

PATCH v4 4/5 Add `human` format to test-tool

In reply to Stephen P. Smith

Add the human format support to the test tool so that GIT_TEST_DATE_NOW can be used to specify the current time.

The get_time() helper function was created and and checks the GIT_TEST_DATE_NOW environment variable. If GIT_TEST_DATE_NOW is set, then that date is used instead of the date returned by by gettimeofday().

All calls to gettimeofday() were replaced by calls to get_time().

Renamed occurances of TEST_DATE_NOW to GIT_TEST_DATE_NOW since the variable is now used in the get binary and not just in the test-tool.

---
 cache.h              |  2 ++
 date.c               | 21 +++++++++++++++++----
 t/helper/test-date.c | 13 ++++++++++++-
 t/t0006-date.sh      |  4 ++--
 4 files changed, 33 insertions(+), 7 deletions(-)
cache.h +2 -0
index 34c33e6a28..fe00ddf910 100644
@@ -1467,6 +1467,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
 const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
 void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
 			struct strbuf *timebuf);
+void show_date_human(timestamp_t time, int tz, const struct timeval *now,
+			struct strbuf *timebuf);
 int parse_date(const char *date, struct strbuf *out);
 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
 int parse_expiry_date(const char *date, timestamp_t *timestamp);
date.c +17 -4
index 43c3a84e25..e997664c1a 100644
@@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time)
 	return local_time_tzoffset((time_t)time, &tm);
 }
 
+static void get_time(struct timeval *now)
+{
+	const char *x;
+
+	x = getenv("GIT_TEST_DATE_NOW");
+	if (x) {
+		now->tv_sec = atoi(x);
+		now->tv_usec = 0;
+	}
+	else
+		gettimeofday(now, NULL);
+}
+
 void show_date_relative(timestamp_t time, int tz,
 			       const struct timeval *now,
 			       struct strbuf *timebuf)
@@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
 	/* Show "today" times as just relative times */
 	if (hide.wday) {
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, buf);
 		return;
 	}
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 	if (mode->type == DATE_HUMAN) {
 		struct timeval now;
 
-		gettimeofday(&now, NULL);
+		get_time(&now);
 
 		/* Fill in the data for "current time" in human_tz and human_tm */
 		human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
@@ -303,7 +316,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 		struct timeval now;
 
 		strbuf_reset(&timebuf);
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, &timebuf);
 		return timebuf.buf;
 	}
@@ -1296,7 +1309,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
 		return timestamp;
 	}
 
-	gettimeofday(&tv, NULL);
+	get_time(&tv);
 	return approxidate_str(date, &tv, error_ret);
 }
 
t/helper/test-date.c +12 -1
index a0837371ab..9b2ac772ee 100644
@@ -3,6 +3,7 @@
 
 static const char *usage_msg = "\n"
 "  test-tool date relative [time_t]...\n"
+"  test-tool date human [time_t]...\n"
 "  test-tool date show:<format> [time_t]...\n"
 "  test-tool date parse [date]...\n"
 "  test-tool date approxidate [date]...\n"
@@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now)
 	strbuf_release(&buf);
 }
 
+static void show_human_dates(const char **argv)
+{
+	for (; *argv; argv++) {
+		time_t t = atoi(*argv);
+		printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
+	}
+}
+
 static void show_dates(const char **argv, const char *format)
 {
 	struct date_mode mode;
@@ -87,7 +96,7 @@ int cmd__date(int argc, const char **argv)
 	struct timeval now;
 	const char *x;
 
-	x = getenv("TEST_DATE_NOW");
+	x = getenv("GIT_TEST_DATE_NOW");
 	if (x) {
 		now.tv_sec = atoi(x);
 		now.tv_usec = 0;
@@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv)
 		usage(usage_msg);
 	if (!strcmp(*argv, "relative"))
 		show_relative_dates(argv+1, &now);
+	else if (!strcmp(*argv, "human"))
+		show_human_dates(argv+1);
 	else if (skip_prefix(*argv, "show:", &x))
 		show_dates(argv+1, x);
 	else if (!strcmp(*argv, "parse"))
t/t0006-date.sh +2 -2
index ffb2975e48..90930c2aa7 100755
@@ -4,10 +4,10 @@ test_description='test date parsing and printing'
 . ./test-lib.sh
 
 # arbitrary reference time: 2009-08-30 19:20:00
-TEST_DATE_NOW=1251660000; export TEST_DATE_NOW
+GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
 
 check_relative() {
-	t=$(($TEST_DATE_NOW - $1))
+	t=$(($GIT_TEST_DATE_NOW - $1))
 	echo "$t -> $2" >expect
 	test_expect_${3:-success} "relative date ($2)" "
 	test-tool date relative $t >actual &&
Signature · 1 line
2.20.1.2.gb21ebb671b
#47 lore raw

PATCH v4 5/5 Add `human` date format tests.

In reply to Stephen P. Smith

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant.

---
 t/t0006-date.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
t/t0006-date.sh +18 -0
index 90930c2aa7..d9fcc829a9 100755
@@ -128,4 +128,22 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
 check_approxidate '2008-12-01' '2008-12-01 19:20:00'
 check_approxidate '2009-12-01' '2009-12-01 19:20:00'
 
+check_date_format_human() {
+	t=$(($GIT_TEST_DATE_NOW - $1))
+	echo "$t -> $2" >expect
+	test_expect_success "human date $t" '
+		test-tool date human $t >actual &&
+		test_i18ncmp expect actual
+'
+}
+
+check_date_format_human 18000 "5 hours ago" # 5 hours ago
+check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#48 lore raw

v3 cover Re-roll of 'human' date format patch set

In reply to Stephen P. Smith

Reworked documentation and tests for the previously submitted patch set.

Linus Torvalds (1):
Add 'human' date format

Stephen P. Smith (4):
Replace the proposed 'auto' mode with 'auto:'
Add 'human' date format documentation
Add human format to test-tool
Add human date format tests.

 Documentation/git-log.txt          |   4 +
 Documentation/rev-list-options.txt |   7 ++
 builtin/blame.c                    |   4 +
 cache.h                            |   3 +
 date.c                             | 148 ++++++++++++++++++++++++-----
 t/helper/test-date.c               |  11 +++
 t/t0006-date.sh                    |  18 ++++
 7 files changed, 173 insertions(+), 22 deletions(-)
Signature · 1 line
2.20.1.2.gb21ebb671b
#49 lore raw

PATCH v3 1/5 Add 'human' date format

From: Linus Torvalds <[email protected]>

This adds --date=human, which skips the timezone if it matches the current time-zone, and doesn't print the whole date if that matches (ie skip printing year for dates that are "this year", but also skip the whole date itself if it's in the last few days and we can just say what weekday it was).

For really recent dates (same day), use the relative date stamp, while for old dates (year doesn't match), don't bother with time and timezone.

Also add 'auto' date mode, which defaults to human if we're using the pager. So you can do

	git config --add log.date auto

and your "git log" commands will show the human-legible format unless you're scripting things.

Note that this time format still shows the timezone for recent enough events (but not so recent that they show up as relative dates). You can combine it with the "-local" suffix to never show timezones for an even more simplified view.

---
 builtin/blame.c |   4 ++
 cache.h         |   1 +
 date.c          | 130 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 115 insertions(+), 20 deletions(-)
builtin/blame.c +4 -0
index 6d798f9939..f684e31d82 100644
@@ -925,6 +925,10 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		 */
 		blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
 		break;
+	case DATE_HUMAN:
+		/* If the year is shown, no time is shown */
+		blame_date_width = sizeof("Thu Oct 19 16:00");
+		break;
 	case DATE_NORMAL:
 		blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
 		break;
cache.h +1 -0
index 49713cc5a5..34c33e6a28 100644
@@ -1439,6 +1439,7 @@ extern struct object *peel_to_type(const char *name, int namelen,
 
 enum date_mode_type {
 	DATE_NORMAL = 0,
+	DATE_HUMAN,
 	DATE_RELATIVE,
 	DATE_SHORT,
 	DATE_ISO8601,
date.c +110 -20

179 lines not on this page · raw message

Signature · 1 line
2.20.1.2.gb21ebb671b
#50 lore raw

PATCH v3 2/5 Replace the proposed 'auto' mode with 'auto:'

In reply to Stephen P. Smith

In addition to adding the 'human' format, the patch added the auto keyword which could be used in the config file as an alternate way to specify the human format. Removing 'auto' cleans up the 'human' format interface.

Added the ability to specify mode 'foo' if the pager is being used by using auto:foo syntax. Therefore, 'auto:human' date mode defaults to human if we're using the pager. So you can do

	git config --add log.date auto:human

and your "git log" commands will show the human-legible format unless you're scripting things.

---
 date.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
date.c +8 -7
index a8d50eb206..43c3a84e25 100644
@@ -883,11 +883,6 @@ int parse_date(const char *date, struct strbuf *result)
 	return 0;
 }
 
-static int auto_date_style(void)
-{
-	return (isatty(1) || pager_in_use()) ? DATE_HUMAN : DATE_NORMAL;
-}
-
 static enum date_mode_type parse_date_type(const char *format, const char **end)
 {
 	if (skip_prefix(format, "relative", end))
@@ -907,8 +902,6 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
 		return DATE_NORMAL;
 	if (skip_prefix(format, "human", end))
 		return DATE_HUMAN;
-	if (skip_prefix(format, "auto", end))
-		return auto_date_style();
 	if (skip_prefix(format, "raw", end))
 		return DATE_RAW;
 	if (skip_prefix(format, "unix", end))
@@ -923,6 +916,14 @@ void parse_date_format(const char *format, struct date_mode *mode)
 {
 	const char *p;
 
+	/* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
+	if (skip_prefix(format, "auto:", &p)) {
+		if (isatty(1) || pager_in_use())
+			format = p;
+		else
+			format = "default";
+	}
+
 	/* historical alias */
 	if (!strcmp(format, "local"))
 		format = "default-local";
Signature · 1 line
2.20.1.2.gb21ebb671b
#51 lore raw

PATCH v3 3/5 Add 'human' date format documentation

In reply to Stephen P. Smith

Display date and time information in a format similar to how people write dates in other contexts. If the year isn't specified then, the reader infers the date is given is in the current year.

By not displaying the redundant information, the reader concentrates on the information that is different. The patch reports relative dates based on information inferred from the date on the machine running the git command at the time the command is executed.

While the format is more useful to humans by dropping inferred information, there is nothing that makes it actually human. If the 'relative' date format wasn't already implemented then using 'relative' would have been appropriate.

---
 Documentation/git-log.txt          | 4 ++++
 Documentation/rev-list-options.txt | 7 +++++++
 2 files changed, 11 insertions(+)
Documentation/git-log.txt +4 -0
index 90761f1694..b02e922dc3 100644
@@ -192,6 +192,10 @@ log.date::
 	Default format for human-readable dates.  (Compare the
 	`--date` option.)  Defaults to "default", which means to write
 	dates like `Sat May 8 19:35:34 2010 -0500`.
++
+If the format is set to "auto:foo" and the pager is in use, format
+"foo" will be the used for the date format. Otherwise "default" will
+be used.
 
 log.follow::
 	If `true`, `git log` will act as if the `--follow` option was used when
Documentation/rev-list-options.txt +7 -0
index 98b538bc77..867a063a1c 100644
@@ -831,6 +831,13 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=human` shows the timezone if the timezone does not match the
+current time-zone, and doesn't print the whole date if that matches
+(ie skip printing year for dates that are "this year", but also skip
+the whole date itself if it's in the last few days and we can just say
+what weekday it was).  For older dates the hour and minute is also
+omitted.
++
 `--date=unix` shows the date as a Unix epoch timestamp (seconds since
 1970).  As with `--raw`, this is always in UTC and therefore `-local`
 has no effect.
Signature · 1 line
2.20.1.2.gb21ebb671b
#52 lore raw

PATCH v3 4/5 Add `human` format to test-tool

In reply to Stephen P. Smith

Add the human format support to the test tool so that TEST_DATE_NOW can be used to specify the current time.

The get_time() helper function was created and and checks the TEST_DATE_NOW environment variable. If TEST_DATE_NOW is set, then that date is used instead of the date returned by by gettimeofday().

All calls to gettimeofday() were replaced by calls to get_time().

---
 cache.h              |  2 ++
 date.c               | 21 +++++++++++++++++----
 t/helper/test-date.c | 11 +++++++++++
 3 files changed, 30 insertions(+), 4 deletions(-)
cache.h +2 -0
index 34c33e6a28..fe00ddf910 100644
@@ -1467,6 +1467,8 @@ struct date_mode *date_mode_from_type(enum date_mode_type type);
 const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
 void show_date_relative(timestamp_t time, int tz, const struct timeval *now,
 			struct strbuf *timebuf);
+void show_date_human(timestamp_t time, int tz, const struct timeval *now,
+			struct strbuf *timebuf);
 int parse_date(const char *date, struct strbuf *out);
 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
 int parse_expiry_date(const char *date, timestamp_t *timestamp);
date.c +17 -4
index 43c3a84e25..5f9ea7d5d7 100644
@@ -115,6 +115,19 @@ static int local_tzoffset(timestamp_t time)
 	return local_time_tzoffset((time_t)time, &tm);
 }
 
+static void get_time(struct timeval *now)
+{
+	const char *x;
+
+	x = getenv("TEST_DATE_NOW");
+	if (x) {
+		now->tv_sec = atoi(x);
+		now->tv_usec = 0;
+	}
+	else
+		gettimeofday(now, NULL);
+}
+
 void show_date_relative(timestamp_t time, int tz,
 			       const struct timeval *now,
 			       struct strbuf *timebuf)
@@ -228,7 +241,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
 	/* Show "today" times as just relative times */
 	if (hide.wday) {
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, buf);
 		return;
 	}
@@ -284,7 +297,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 	if (mode->type == DATE_HUMAN) {
 		struct timeval now;
 
-		gettimeofday(&now, NULL);
+		get_time(&now);
 
 		/* Fill in the data for "current time" in human_tz and human_tm */
 		human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
@@ -303,7 +316,7 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
 		struct timeval now;
 
 		strbuf_reset(&timebuf);
-		gettimeofday(&now, NULL);
+		get_time(&now);
 		show_date_relative(time, tz, &now, &timebuf);
 		return timebuf.buf;
 	}
@@ -1296,7 +1309,7 @@ timestamp_t approxidate_careful(const char *date, int *error_ret)
 		return timestamp;
 	}
 
-	gettimeofday(&tv, NULL);
+	get_time(&tv);
 	return approxidate_str(date, &tv, error_ret);
 }
 
t/helper/test-date.c +11 -0
index a0837371ab..811b116c7e 100644
@@ -3,6 +3,7 @@
 
 static const char *usage_msg = "\n"
 "  test-tool date relative [time_t]...\n"
+"  test-tool date human [time_t]...\n"
 "  test-tool date show:<format> [time_t]...\n"
 "  test-tool date parse [date]...\n"
 "  test-tool date approxidate [date]...\n"
@@ -22,6 +23,14 @@ static void show_relative_dates(const char **argv, struct timeval *now)
 	strbuf_release(&buf);
 }
 
+static void show_human_dates(const char **argv)
+{
+	for (; *argv; argv++) {
+		time_t t = atoi(*argv);
+		printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
+	}
+}
+
 static void show_dates(const char **argv, const char *format)
 {
 	struct date_mode mode;
@@ -100,6 +109,8 @@ int cmd__date(int argc, const char **argv)
 		usage(usage_msg);
 	if (!strcmp(*argv, "relative"))
 		show_relative_dates(argv+1, &now);
+	else if (!strcmp(*argv, "human"))
+		show_human_dates(argv+1);
 	else if (skip_prefix(*argv, "show:", &x))
 		show_dates(argv+1, x);
 	else if (!strcmp(*argv, "parse"))
Signature · 1 line
2.20.1.2.gb21ebb671b
#53 lore raw

PATCH v3 5/5 Add `human` date format tests.

In reply to Stephen P. Smith

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant.

---
 t/t0006-date.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
t/t0006-date.sh +18 -0
index ffb2975e48..35f7b52dd7 100755
@@ -128,4 +128,22 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
 check_approxidate '2008-12-01' '2008-12-01 19:20:00'
 check_approxidate '2009-12-01' '2009-12-01 19:20:00'
 
+check_date_format_human() {
+	t=$(($TEST_DATE_NOW - $1))
+	echo "$t -> $2" >expect
+	test_expect_success "human date $t" '
+	test-tool date human $t >actual &&
+	test_cmp expect actual
+'
+}
+
+check_date_format_human 18000 "5 hours ago" # 5 hours ago
+check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#54 lore raw

Re: [PATCH v3 5/5] Add `human` date format tests.

Quoted 19 lines · Stephen P. Smith

When using human several fields are suppressed depending on the time difference between the reference date and the local computer date. In cases where the difference is less than a year, the year field is supppressed. If the time is less than a day; the month and year is suppressed.

Use TEST_DATE_NOW environment variable when using the test-tool to hold the expected output strings constant.

---
 t/t0006-date.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
t/t0006-date.sh +0 -0
index ffb2975e48..35f7b52dd7 100755

t/t0006-date.sh check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00' · line 136

Quoted 9 lines
t/t0006-date.sh +6 -0
@@ -128,4 +128,22 @@ check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
 check_approxidate '2008-12-01' '2008-12-01 19:20:00'
 check_approxidate '2009-12-01' '2009-12-01 19:20:00'
 
+check_date_format_human() {
+	t=$(($TEST_DATE_NOW - $1))
+	echo "$t -> $2" >expect
+	test_expect_success "human date $t" '
+	test-tool date human $t >actual &&
+	test_cmp expect actual

Note that the relative time output is translated, see all the Q_() calls in show_date_relative(). Consequently, these tests fail in GETTEXT_POISON builds. Please use 'test_i18ncmp' instead.

Furthermore, I think it would help readibility of this helper function if the commands in the 'test_expect_success' block were indented.

Quoted 16 lines
t/t0006-date.sh +12 -0
+'
+}
+
+check_date_format_human 18000 "5 hours ago" # 5 hours ago
+check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
+
 test_done
Signature · 1 line
2.20.1.2.gb21ebb671b
#55 lore raw

Re: [PATCH v3 5/5] Add `human` date format tests.

Quoted 6 lines · MST SZEDER Gábor

Note that the relative time output is translated, see all the Q_() calls in show_date_relative(). Consequently, these tests fail in GETTEXT_POISON builds. Please use 'test_i18ncmp' instead.

Furthermore, I think it would help readibility of this helper function if the commands in the 'test_expect_success' block were indented.

No problem. I will wait a day or two for any more comments