[v4,11/11] squash! pretty: implement 'reference' format
diff mbox series

Message ID 580aa7c88c0b18d752f9aa951166a2c82dbcd031.1574122784.git.liu.denton@gmail.com
State New
Headers show
Series
  • learn the "reference" pretty format
Related show

Commit Message

Denton Liu Nov. 19, 2019, 12:21 a.m. UTC
Note that we explicitly special-case `--pretty=reference` to use
`DATE_SHORT` by default in get_commit_format(). Although this isn't a
very elegant way of doing this, it has to be done this way because we
implement `--pretty=reference` as a user-format and, after this
function, it is treated as a normal user-format. This is the only part
of the pretty codepath where we can actually tell that we are dealing
with a "special" format so we have to do it here.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---

Notes:
    This patch implements the overridable `--date` for `--pretty=reference`.
    The only reason I'm hesitant about squashing this in is, as described
    above, that this patch isn't very elegant. I'd welcome suggestions on
    how to possibly make it more elegant.
    
    Also, if we squash this in, we could drop the "pretty: provide short
    date format" patch. We'd only need to update the test cases to stop
    using `%as`. Thoughts on that?

 Documentation/pretty-formats.txt | 8 +++++---
 pretty.c                         | 8 +++++++-
 t/t4205-log-pretty-formats.sh    | 9 ++++++++-
 3 files changed, 20 insertions(+), 5 deletions(-)

Comments

Junio C Hamano Nov. 19, 2019, 3:10 a.m. UTC | #1
Denton Liu <liu.denton@gmail.com> writes:

> Note that we explicitly special-case `--pretty=reference` to use
> `DATE_SHORT` by default in get_commit_format(). Although this isn't a
> very elegant way of doing this, it has to be done this way because we
> implement `--pretty=reference` as a user-format and, after this
> function, it is treated as a normal user-format. This is the only part
> of the pretty codepath where we can actually tell that we are dealing
> with a "special" format so we have to do it here.

I was hoping that by extending cmt_fmt_map horizontally, we can
cause the date mode (and other things in the future) to optionally
default to the format specific value, without hardcoding the string
constant in the code.  After all, that (i.e. not having to hardcode
per-format specific constants) is the original point of using
table(s) to describe the customization.

Patch
diff mbox series

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index ccd0921123..1a7212ce5a 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -68,9 +68,11 @@  This is designed to be as compact as possible.
 	  <abbrev hash> (<title line>, <short author date>)
 +
 This format is used to refer to another commit in a commit message and
-is the same as `--pretty='format:%C(auto)%h (%s, %as)'`.  As with any
-`format:` with format placeholders, its output is not affected by other
-options like `--decorate` and `--walk-reflogs`.
+is the same as `--pretty='format:%C(auto)%h (%s, %ad)'`.  By default,
+the date is formatted with `--date=short` unless another `--date` option
+is explicitly specified.  As with any `format:` with format
+placeholders, its output is not affected by other options like
+`--decorate` and `--walk-reflogs`.
 
 * 'email'
 
diff --git a/pretty.c b/pretty.c
index 88a3bc621d..8bb4b1d67f 100644
--- a/pretty.c
+++ b/pretty.c
@@ -99,7 +99,7 @@  static void setup_commit_formats(void)
 		{ "full",	CMIT_FMT_FULL,		0,	8 },
 		{ "oneline",	CMIT_FMT_ONELINE,	1,	0 },
 		{ "reference",	CMIT_FMT_USERFORMAT,	1,	0,
-			0, "%C(auto)%h (%s, %as)" },
+			0, "%C(auto)%h (%s, %ad)" },
 		/*
 		 * Please update $__git_log_pretty_formats in
 		 * git-completion.bash when you add new formats.
@@ -187,6 +187,12 @@  void get_commit_format(const char *arg, struct rev_info *rev)
 		save_user_format(rev, commit_format->user_format,
 				 commit_format->is_tformat);
 	}
+	/*
+	 * --pretty=reference is special; it should use the short date format
+	 * unless otherwise specified
+	 */
+	if (!rev->date_mode_explicit && !strcmp(arg, "reference"))
+		rev->date_mode.type = DATE_SHORT;
 }
 
 /*
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 9a9a18f104..a8ef3784cf 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -830,8 +830,15 @@  test_expect_success 'log --pretty=reference' '
 	test_cmp expect actual
 '
 
-test_expect_success 'log --pretty=reference always uses short date' '
+test_expect_success 'log --pretty=reference with log.date is overridden by short date' '
 	git log --pretty="tformat:%h (%s, %as)" >expect &&
+	test_config log.date rfc &&
+	git log --pretty=reference >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference with explicit date overrides short date' '
+	git log --date=rfc --pretty="tformat:%h (%s, %ad)" >expect &&
 	git log --date=rfc --pretty=reference >actual &&
 	test_cmp expect actual
 '