Skip to content

Commit 1f0fc1d

Browse files
Denton-Lgitster
authored andcommitted
pretty: implement 'reference' format
The standard format for referencing other commits within some projects (such as git.git) is the reference format. This is described in Documentation/SubmittingPatches as If you want to reference a previous commit in the history of a stable branch, use the format "abbreviated hash (subject, date)", like this: .... Commit f86a374 (pack-bitmap.c: fix a memleak, 2015-03-30) noticed that ... .... Since this format is so commonly used, standardize it as a pretty format. The tests that are implemented essentially show that the format-string does not change in response to various log options. This is useful because, for future developers, it shows that we've considered the limitations of the "canned format-string" approach and we are fine with them. Based-on-a-patch-by: SZEDER Gábor <[email protected]> Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 618a855 commit 1f0fc1d

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

Documentation/pretty-formats.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ This is designed to be as compact as possible.
6363

6464
<full commit message>
6565

66+
* 'reference'
67+
68+
<abbrev hash> (<title line>, <short author date>)
69+
+
70+
This format is used to refer to another commit in a commit message and
71+
is the same as `--pretty='format:%C(auto)%h (%s, %ad)'`. By default,
72+
the date is formatted with `--date=short` unless another `--date` option
73+
is explicitly specified. As with any `format:` with format
74+
placeholders, its output is not affected by other options like
75+
`--decorate` and `--walk-reflogs`.
76+
6677
* 'email'
6778

6879
From <hash> <date>

Documentation/pretty-options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Pretty-print the contents of the commit logs in a given format,
55
where '<format>' can be one of 'oneline', 'short', 'medium',
6-
'full', 'fuller', 'email', 'raw', 'format:<string>'
6+
'full', 'fuller', 'reference', 'email', 'raw', 'format:<string>'
77
and 'tformat:<string>'. When '<format>' is none of the above,
88
and has '%placeholder' in it, it acts as if
99
'--pretty=tformat:<format>' were given.

Documentation/rev-list-options.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ list.
269269
exclude (that is, '{caret}commit', 'commit1..commit2',
270270
and 'commit1\...commit2' notations cannot be used).
271271
+
272-
With `--pretty` format other than `oneline` (for obvious reasons),
272+
With `--pretty` format other than `oneline` and `reference` (for obvious reasons),
273273
this causes the output to have two extra lines of information
274274
taken from the reflog. The reflog designator in the output may be shown
275275
as `ref@{Nth}` (where `Nth` is the reverse-chronological index in the
@@ -293,6 +293,8 @@ Under `--pretty=oneline`, the commit message is
293293
prefixed with this information on the same line.
294294
This option cannot be combined with `--reverse`.
295295
See also linkgit:git-reflog[1].
296+
+
297+
Under `--pretty=reference`, this information will not be shown at all.
296298

297299
--merge::
298300
After a failed merge, show refs that touch files having a

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ __git_log_shortlog_options="
17371737
--all-match --invert-grep
17381738
"
17391739

1740-
__git_log_pretty_formats="oneline short medium full fuller email raw format: tformat: mboxrd"
1740+
__git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
17411741
__git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default raw unix format:"
17421742

17431743
_git_log ()

pretty.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ static void setup_commit_formats(void)
9898
{ "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
9999
{ "fuller", CMIT_FMT_FULLER, 0, 8 },
100100
{ "full", CMIT_FMT_FULL, 0, 8 },
101-
{ "oneline", CMIT_FMT_ONELINE, 1, 0 }
101+
{ "oneline", CMIT_FMT_ONELINE, 1, 0 },
102+
{ "reference", CMIT_FMT_USERFORMAT, 1, 0,
103+
0, DATE_SHORT, "%C(auto)%h (%s, %ad)" },
102104
/*
103105
* Please update $__git_log_pretty_formats in
104106
* git-completion.bash when you add new formats.

t/t4205-log-pretty-formats.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,4 +824,47 @@ test_expect_success '%S in git log --format works with other placeholders (part
824824
test_cmp expect actual
825825
'
826826

827+
test_expect_success 'log --pretty=reference' '
828+
git log --pretty="tformat:%h (%s, %as)" >expect &&
829+
git log --pretty=reference >actual &&
830+
test_cmp expect actual
831+
'
832+
833+
test_expect_success 'log --pretty=reference with log.date is overridden by short date' '
834+
git log --pretty="tformat:%h (%s, %as)" >expect &&
835+
test_config log.date rfc &&
836+
git log --pretty=reference >actual &&
837+
test_cmp expect actual
838+
'
839+
840+
test_expect_success 'log --pretty=reference with explicit date overrides short date' '
841+
git log --date=rfc --pretty="tformat:%h (%s, %ad)" >expect &&
842+
git log --date=rfc --pretty=reference >actual &&
843+
test_cmp expect actual
844+
'
845+
846+
test_expect_success 'log --pretty=reference is never unabbreviated' '
847+
git log --pretty="tformat:%h (%s, %as)" >expect &&
848+
git log --no-abbrev-commit --pretty=reference >actual &&
849+
test_cmp expect actual
850+
'
851+
852+
test_expect_success 'log --pretty=reference is never decorated' '
853+
git log --pretty="tformat:%h (%s, %as)" >expect &&
854+
git log --decorate=short --pretty=reference >actual &&
855+
test_cmp expect actual
856+
'
857+
858+
test_expect_success 'log --pretty=reference does not output reflog info' '
859+
git log --walk-reflogs --pretty="tformat:%h (%s, %as)" >expect &&
860+
git log --walk-reflogs --pretty=reference >actual &&
861+
test_cmp expect actual
862+
'
863+
864+
test_expect_success 'log --pretty=reference is colored appropriately' '
865+
git log --color=always --pretty="tformat:%C(auto)%h (%s, %as)" >expect &&
866+
git log --color=always --pretty=reference >actual &&
867+
test_cmp expect actual
868+
'
869+
827870
test_done

0 commit comments

Comments
 (0)