Skip to content

Commit d37cfe3

Browse files
committed
Merge branch 'dl/pretty-reference'
"git log" family learned "--pretty=reference" that gives the name of a commit in the format that is often used to refer to it in log messages. * dl/pretty-reference: SubmittingPatches: use `--pretty=reference` pretty: implement 'reference' format pretty: add struct cmt_fmt_map::default_date_mode_type pretty: provide short date format t4205: cover `git log --reflog -z` blindspot pretty.c: inline initalize format_context revision: make get_revision_mark() return const pointer completion: complete `tformat:` pretty format SubmittingPatches: remove dq from commit reference pretty-formats.txt: use generic terms for hash SubmittingPatches: use generic terms for hash
2 parents 99c4ff1 + 3798149 commit d37cfe3

File tree

9 files changed

+134
-26
lines changed

9 files changed

+134
-26
lines changed

Documentation/SubmittingPatches

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,25 @@ archive, summarize the relevant points of the discussion.
142142

143143
[[commit-reference]]
144144
If you want to reference a previous commit in the history of a stable
145-
branch, use the format "abbreviated sha1 (subject, date)",
146-
with the subject enclosed in a pair of double-quotes, like this:
145+
branch, use the format "abbreviated hash (subject, date)", like this:
147146

148147
....
149-
Commit f86a374 ("pack-bitmap.c: fix a memleak", 2015-03-30)
148+
Commit f86a374 (pack-bitmap.c: fix a memleak, 2015-03-30)
150149
noticed that ...
151150
....
152151

153152
The "Copy commit summary" command of gitk can be used to obtain this
154-
format, or this invocation of `git show`:
153+
format (with the subject enclosed in a pair of double-quotes), or this
154+
invocation of `git show`:
155155

156156
....
157-
git show -s --date=short --pretty='format:%h ("%s", %ad)' <commit>
157+
git show -s --pretty=reference <commit>
158+
....
159+
160+
or, on an older version of Git without support for --pretty=reference:
161+
162+
....
163+
git show -s --date=short --pretty='format:%h (%s, %ad)' <commit>
158164
....
159165

160166
[[git-tools]]

Documentation/pretty-formats.txt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PRETTY FORMATS
44
If the commit is a merge, and if the pretty-format
55
is not 'oneline', 'email' or 'raw', an additional line is
66
inserted before the 'Author:' line. This line begins with
7-
"Merge: " and the sha1s of ancestral commits are printed,
7+
"Merge: " and the hashes of ancestral commits are printed,
88
separated by spaces. Note that the listed commits may not
99
necessarily be the list of the *direct* parent commits if you
1010
have limited your view of history: for example, if you are
@@ -20,20 +20,20 @@ built-in formats:
2020

2121
* 'oneline'
2222

23-
<sha1> <title line>
23+
<hash> <title line>
2424
+
2525
This is designed to be as compact as possible.
2626

2727
* 'short'
2828

29-
commit <sha1>
29+
commit <hash>
3030
Author: <author>
3131

3232
<title line>
3333

3434
* 'medium'
3535

36-
commit <sha1>
36+
commit <hash>
3737
Author: <author>
3838
Date: <author date>
3939

@@ -43,7 +43,7 @@ This is designed to be as compact as possible.
4343

4444
* 'full'
4545

46-
commit <sha1>
46+
commit <hash>
4747
Author: <author>
4848
Commit: <committer>
4949

@@ -53,7 +53,7 @@ This is designed to be as compact as possible.
5353

5454
* 'fuller'
5555

56-
commit <sha1>
56+
commit <hash>
5757
Author: <author>
5858
AuthorDate: <author date>
5959
Commit: <committer>
@@ -63,9 +63,20 @@ 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

68-
From <sha1> <date>
79+
From <hash> <date>
6980
From: <author>
7081
Date: <author date>
7182
Subject: [PATCH] <title line>
@@ -75,7 +86,7 @@ This is designed to be as compact as possible.
7586
* 'raw'
7687
+
7788
The 'raw' format shows the entire commit exactly as
78-
stored in the commit object. Notably, the SHA-1s are
89+
stored in the commit object. Notably, the hashes are
7990
displayed in full, regardless of whether --abbrev or
8091
--no-abbrev are used, and 'parents' information show the
8192
true parent commits, without taking grafts or history
@@ -172,6 +183,7 @@ The placeholders are:
172183
'%at':: author date, UNIX timestamp
173184
'%ai':: author date, ISO 8601-like format
174185
'%aI':: author date, strict ISO 8601 format
186+
'%as':: author date, short format (`YYYY-MM-DD`)
175187
'%cn':: committer name
176188
'%cN':: committer name (respecting .mailmap, see
177189
linkgit:git-shortlog[1] or linkgit:git-blame[1])
@@ -187,6 +199,7 @@ The placeholders are:
187199
'%ct':: committer date, UNIX timestamp
188200
'%ci':: committer date, ISO 8601-like format
189201
'%cI':: committer date, strict ISO 8601 format
202+
'%cs':: committer date, short format (`YYYY-MM-DD`)
190203
'%d':: ref names, like the --decorate option of linkgit:git-log[1]
191204
'%D':: ref names without the " (", ")" wrapping.
192205
'%S':: ref name given on the command line by which the commit was reached

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
@@ -1749,7 +1749,7 @@ __git_log_shortlog_options="
17491749
--all-match --invert-grep
17501750
"
17511751

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

17551755
_git_log ()

pretty.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static struct cmt_fmt_map {
2020
int is_tformat;
2121
int expand_tabs_in_log;
2222
int is_alias;
23+
enum date_mode_type default_date_mode_type;
2324
const char *user_format;
2425
} *commit_formats;
2526
static size_t builtin_formats_len;
@@ -97,7 +98,9 @@ static void setup_commit_formats(void)
9798
{ "mboxrd", CMIT_FMT_MBOXRD, 0, 0 },
9899
{ "fuller", CMIT_FMT_FULLER, 0, 8 },
99100
{ "full", CMIT_FMT_FULL, 0, 8 },
100-
{ "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)" },
101104
/*
102105
* Please update $__git_log_pretty_formats in
103106
* git-completion.bash when you add new formats.
@@ -181,6 +184,8 @@ void get_commit_format(const char *arg, struct rev_info *rev)
181184
rev->commit_format = commit_format->format;
182185
rev->use_terminator = commit_format->is_tformat;
183186
rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
187+
if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
188+
rev->date_mode.type = commit_format->default_date_mode_type;
184189
if (commit_format->format == CMIT_FMT_USERFORMAT) {
185190
save_user_format(rev, commit_format->user_format,
186191
commit_format->is_tformat);
@@ -738,6 +743,9 @@ static size_t format_person_part(struct strbuf *sb, char part,
738743
case 'I': /* date, ISO 8601 strict */
739744
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(ISO8601_STRICT)));
740745
return placeholder_len;
746+
case 's':
747+
strbuf_addstr(sb, show_ident_date(&s, DATE_MODE(SHORT)));
748+
return placeholder_len;
741749
}
742750

743751
skip:
@@ -1617,14 +1625,14 @@ void repo_format_commit_message(struct repository *r,
16171625
const char *format, struct strbuf *sb,
16181626
const struct pretty_print_context *pretty_ctx)
16191627
{
1620-
struct format_commit_context context;
1628+
struct format_commit_context context = {
1629+
.commit = commit,
1630+
.pretty_ctx = pretty_ctx,
1631+
.wrap_start = sb->len
1632+
};
16211633
const char *output_enc = pretty_ctx->output_encoding;
16221634
const char *utf8 = "UTF-8";
16231635

1624-
memset(&context, 0, sizeof(context));
1625-
context.commit = commit;
1626-
context.pretty_ctx = pretty_ctx;
1627-
context.wrap_start = sb->len;
16281636
/*
16291637
* convert a commit message to UTF-8 first
16301638
* as far as 'format_commit_item' assumes it in UTF-8

revision.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,7 @@ struct commit *get_revision(struct rev_info *revs)
39603960
return c;
39613961
}
39623962

3963-
char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3963+
const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
39643964
{
39653965
if (commit->object.flags & BOUNDARY)
39663966
return "-";
@@ -3982,7 +3982,7 @@ char *get_revision_mark(const struct rev_info *revs, const struct commit *commit
39823982

39833983
void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
39843984
{
3985-
char *mark = get_revision_mark(revs, commit);
3985+
const char *mark = get_revision_mark(revs, commit);
39863986
if (!strlen(mark))
39873987
return;
39883988
fputs(mark, stdout);

revision.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ int handle_revision_arg(const char *arg, struct rev_info *revs,
322322
void reset_revision_walk(void);
323323
int prepare_revision_walk(struct rev_info *revs);
324324
struct commit *get_revision(struct rev_info *revs);
325-
char *get_revision_mark(const struct rev_info *revs,
326-
const struct commit *commit);
325+
const char *get_revision_mark(const struct rev_info *revs,
326+
const struct commit *commit);
327327
void put_revision_mark(const struct rev_info *revs,
328328
const struct commit *commit);
329329

t/t4205-log-pretty-formats.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ test_expect_failure C_LOCALE_OUTPUT 'NUL termination with --stat' '
134134
test_cmp expected actual
135135
'
136136

137+
for p in short medium full fuller email raw
138+
do
139+
test_expect_success "NUL termination with --reflog --pretty=$p" '
140+
revs="$(git rev-list --reflog)" &&
141+
for r in $revs
142+
do
143+
git show -s "$r" --pretty="$p" &&
144+
printf "\0" || return 1
145+
done >expect &&
146+
{
147+
git log -z --reflog --pretty="$p" &&
148+
printf "\0"
149+
} >actual &&
150+
test_cmp expect actual
151+
'
152+
done
153+
154+
test_expect_success 'NUL termination with --reflog --pretty=oneline' '
155+
revs="$(git rev-list --reflog)" &&
156+
for r in $revs
157+
do
158+
git show -s --pretty=oneline "$r" >raw &&
159+
cat raw | lf_to_nul || exit 1
160+
done >expect &&
161+
# the trailing NUL is already produced so we do not need to
162+
# output another one
163+
git log -z --pretty=oneline --reflog >actual &&
164+
test_cmp expect actual
165+
'
166+
137167
test_expect_success 'setup more commits' '
138168
test_commit "message one" one one message-one &&
139169
test_commit "message two" two two message-two &&
@@ -503,6 +533,12 @@ test_expect_success 'ISO and ISO-strict date formats display the same values' '
503533
test_cmp expected actual
504534
'
505535

536+
test_expect_success 'short date' '
537+
git log --format=%ad%n%cd --date=short >expected &&
538+
git log --format=%as%n%cs >actual &&
539+
test_cmp expected actual
540+
'
541+
506542
# get new digests (with no abbreviations)
507543
test_expect_success 'set up log decoration tests' '
508544
head1=$(git rev-parse --verify HEAD~0) &&
@@ -788,4 +824,47 @@ test_expect_success '%S in git log --format works with other placeholders (part
788824
test_cmp expect actual
789825
'
790826

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+
791870
test_done

0 commit comments

Comments
 (0)