interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-format-rev.adoc

216 lines · 6690 bytes Raw Blame History
1git-format-rev(1)
2=================
3
4NAME
5----
6git-format-rev - EXPERIMENTAL: Pretty format revisions on demand
7
8
9SYNOPSIS
10--------
11[synopsis]
12(EXPERIMENTAL!) git format-rev --stdin-mode=<mode> --format=<pretty> [--[no-]notes=<ref>] [-z] [--[no-]null-output] [--[no-]null-input]
13
14DESCRIPTION
15-----------
16
17Pretty format revisions from standard input.
18
19THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
20
21OPTIONS
22-------
23
24`--stdin-mode=<mode>`::
25 How to interpret standard input data:
26+
27--
28`revs`;; Each line or record (see the <<io,INPUT AND OUTPUT FORMATS>>
29 section) is interpreted as a commit. Any kind of revision
30 expression can be used (see linkgit:gitrevisions[7]). Annotated
31 tags are peeled (see linkgit:gitglossary[7]).
32+
33The argument `rev` is also accepted.
34
35`text`;; Formats all commit object names found in freeform text. These
36 must be full object names, i.e. abbreviated hexadecimal object
37 names will not be interpreted.
38+
39Anything that is parsed as an object name but that is not found to be a
40commit object name is left alone (echoed).
41--
42
43`--format=<pretty>`::
44 Pretty format string.
45
46`--notes=<ref>`::
47`--no-notes`::
48 Custom notes ref. Notes are displayed when using the `%N`
49 atom. See linkgit:git-notes[1].
50
51`-z`::
52`--null`::
53 Use _NUL_ character to terminate both input and output instead
54 of newline. This option cannot be negated.
55+
56This is useful if both the input and output could contain newlines or if
57the input to this command also uses _NUL_ character termination; see the
58<<io,INPUT AND OUTPUT FORMATS>> section below.
59+
60The mode `--stdin-mode=text` can have use for this option when it needs
61to process input like for example `git last-modified -z`; see the
62<<examples,EXAMPLES>> section below.
63
64`--null-output`::
65`--no-null-output`::
66 Use _NUL_ character to terminate output instead of newline. The
67 default is `--no-null-output`.
68+
69This is useful if the output could contain newlines, for example if the
70`%n` (newline) atom is used.
71
72`--null-input`::
73`--no-null-input`::
74 Use _NUL_ character to terminate input instead of newline. The
75 default is `--no-null-input`.
76+
77This is useful if the input revision expressions could contain newlines.
78
79[[io]]
80INPUT AND OUTPUT FORMAT
81-----------------------
82
83The command uses newlines for both input and output termination by
84default. See the `-z`, `--null-output`, and `--null-input` options for
85using _NUL_ character as the terminator.
86
87The mode `--stdin-mode=revs` outputs one formatted commit followed by
88the terminator. This could either be called a _line_ or a _record_ in
89case "line" is too suggestive of newline termination.
90
91Note that this means that the terminator character (newline or _NUL_)
92acts as a _terminator_, not a _separator_. In other words, the final
93line or record is also terminated by the terminator character.
94
95The mode `--stdin-mode=text` replaces each object name with the
96formatted commit, i.e. the format `"%s"` would transform some commit
97object name to `"<subject>"` without any termination. Like this:
98
99[synopsis]
100--
101Did we not fix this in "<subject>"?
102--
103
104It is safe to interactively read and write from this command since each
105record is immediately flushed.
106
107[[examples]]
108EXAMPLES
109--------
110
111The command linkgit:git-last-modified[1] shows the commit that each file
112was last modified in.
113
114----
115$ git last-modified -- README.md Makefile
1167798034171030be0909c56377a4e0e10e6d2df93 Makefile
117c50fbb2dd225e7e82abba4380423ae105089f4d7 README.md
118----
119
120We can pipe the result to this command in order to replace the object
121name with the commit author.
122
123----
124$ git last-modified -- README.md Makefile |
125 git format-rev --stdin-mode=text --format=%an
126Junio C Hamano Makefile
127Todd Zullinger README.md
128----
129
130Another example is _formatting commits in commit messages_. Given this commit message:
131
132----
133Fix off-by-one error
134
135Fix off-by-one error introduced in
136e83c5163316f89bfbde7d9ab23ca2e25604af290.
137
138We thought we fixed this in 5569bf9bbedd63a00780fc5c110e0cfab3aa97b9 but
139that only covered 1/3 of the faulty cases.
140----
141
142We can format the commits and use par(1) to reflow the text, say in a
143`commit-msg` hook:
144
145----
146$ git config set hook.reference-commits.event commit-msg
147$ git config set hook.reference-commits.command reference-commits
148$ cat $(which reference-commits)
149#/bin/sh
150
151msg="$1"
152rewritten=$(mktemp)
153git format-rev --stdin-mode=text --format=reference <"$msg" |
154 par >"$rewritten"
155mv "$rewritten" "$msg"
156----
157
158Which will produce something like this:
159
160----
161Fix off-by-one error
162
163Fix off-by-one error introduced in e83c5163316 (Implement better memory
164allocator, 2005-04-07).
165
166We thought we fixed this in 5569bf9bbed (Fix memory allocator,
1672005-06-22) but that only covered 1/3 of the faulty cases.
168----
169
170DISCUSSION
171----------
172
173This command lets you format any number of revisions in any order
174through one command invocation. Consider the
175linkgit:git-last-modified[1] case from the <<examples,EXAMPLES>> section
176above:
177
1781. There might be hundreds of files
1792. Commits can be repeated, i.e. two or more files were last modified in
180 the same commit
181
182Two widely-used commands which pretty formats commits are
183linkgit:git-log[1] and linkgit:git-show[1]. It turns out that they are
184not a good fit for the above use case.
185
186- The output of linkgit:git-last-modified[1] would have to be processed
187 in stages since you need to transform the first column separately and
188 then link the author to the filename. But this is surmountable.
189- You can feed each commit to `git show` or `git log --no-walk -1`. But
190 that means that you need to create a process for each line.
191- Let’s say that you want to use one process, not one per line. So you
192 want to feed all the commits to the command. Now you face the problem
193 that you have to feed all the commits to the commands before you get
194 any output (this is also the case for the `--stdin` modes). In other
195 words, you cannot loop through each line, get the author for the
196 commit, and output the author and the filename. You need to feed all
197 the commits, get back all the output, and match the output with the
198 filename.
199- But the next problem is that commands will deduplicate the input and
200 only output one commit one single time only. Thus you cannot make the
201 output order match the input order, since a commit could have been
202 repeated in the original input.
203
204In short, it is straightforward to use these two commands if you use one
205process per line. It is much more work if you just want to use one
206process, but still doable. In contrast, this problem is solved with just
207another shell pipeline with this command.
208
209SEE ALSO
210--------
211linkgit:git-name-rev[1],
212linkgit:git-log[1].
213
214GIT
215---
216Part of the linkgit:git[1] suite