Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/git-format-rev.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / git-format-rev.adoc` 216 lines · 6690 bytes Raw Blame History 1 git-format-rev(1) 2 ================= 3 4 NAME 5 ---- 6 git-format-rev - EXPERIMENTAL: Pretty format revisions on demand 7 8 9 SYNOPSIS 10 -------- 11 [synopsis] 12 (EXPERIMENTAL!) git format-rev --stdin-mode= --format= [--[no-]notes=] [-z] [--[no-]null-output] [--[no-]null-input] 13 14 DESCRIPTION 15 ----------- 16 17 Pretty format revisions from standard input. 18 19 THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE. 20 21 OPTIONS 22 ------- 23 24 `--stdin-mode=`:: 25 How to interpret standard input data: 26 + 27 -- 28 `revs`;; Each line or record (see the <> 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 + 33 The 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 + 39 Anything that is parsed as an object name but that is not found to be a 40 commit object name is left alone (echoed). 41 -- 42 43 `--format=`:: 44 Pretty format string. 45 46 `--notes=`:: 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 + 56 This is useful if both the input and output could contain newlines or if 57 the input to this command also uses _NUL_ character termination; see the 58 <> section below. 59 + 60 The mode `--stdin-mode=text` can have use for this option when it needs 61 to process input like for example `git last-modified -z`; see the 62 <> 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 + 69 This 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 + 77 This is useful if the input revision expressions could contain newlines. 78 79 [[io]] 80 INPUT AND OUTPUT FORMAT 81 ----------------------- 82 83 The command uses newlines for both input and output termination by 84 default. See the `-z`, `--null-output`, and `--null-input` options for 85 using _NUL_ character as the terminator. 86 87 The mode `--stdin-mode=revs` outputs one formatted commit followed by 88 the terminator. This could either be called a _line_ or a _record_ in 89 case "line" is too suggestive of newline termination. 90 91 Note that this means that the terminator character (newline or _NUL_) 92 acts as a _terminator_, not a _separator_. In other words, the final 93 line or record is also terminated by the terminator character. 94 95 The mode `--stdin-mode=text` replaces each object name with the 96 formatted commit, i.e. the format `"%s"` would transform some commit 97 object name to `""` without any termination. Like this: 98 99 [synopsis] 100 -- 101 Did we not fix this in ""? 102 -- 103 104 It is safe to interactively read and write from this command since each 105 record is immediately flushed. 106 107 [[examples]] 108 EXAMPLES 109 -------- 110 111 The command linkgit:git-last-modified[1] shows the commit that each file 112 was last modified in. 113 114 ---- 115 $ git last-modified -- README.md Makefile 116 `7798034171030be0909c56377a4e0e10e6d2df93` Makefile 117 `c50fbb2dd225e7e82abba4380423ae105089f4d7` README.md 118 ---- 119 120 We can pipe the result to this command in order to replace the object 121 name with the commit author. 122 123 ---- 124 $ git last-modified -- README.md Makefile | 125 git format-rev --stdin-mode=text --format=%an 126 Junio C Hamano Makefile 127 Todd Zullinger README.md 128 ---- 129 130 Another example is _formatting commits in commit messages_. Given this commit message: 131 132 ---- 133 Fix off-by-one error 134 135 Fix off-by-one error introduced in 136 `e83c5163316f89bfbde7d9ab23ca2e25604af290`. 137 138 We thought we fixed this in `5569bf9bbedd63a00780fc5c110e0cfab3aa97b9` but 139 that only covered 1/3 of the faulty cases. 140 ---- 141 142 We 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 151 msg="$1" 152 rewritten=$(mktemp) 153 git format-rev --stdin-mode=text --format=reference <"$msg" | 154 par >"$rewritten" 155 mv "$rewritten" "$msg" 156 ---- 157 158 Which will produce something like this: 159 160 ---- 161 Fix off-by-one error 162 163 Fix off-by-one error introduced in `e83c5163316` (Implement better memory 164 allocator, 2005-04-07). 165 166 We thought we fixed this in `5569bf9bbed` (Fix memory allocator, 167 2005-06-22) but that only covered 1/3 of the faulty cases. 168 ---- 169 170 DISCUSSION 171 ---------- 172 173 This command lets you format any number of revisions in any order 174 through one command invocation. Consider the 175 linkgit:git-last-modified[1] case from the <> section 176 above: 177 178 1. There might be hundreds of files 179 2. Commits can be repeated, i.e. two or more files were last modified in 180 the same commit 181 182 Two widely-used commands which pretty formats commits are 183 linkgit:git-log[1] and linkgit:git-show[1]. It turns out that they are 184 not 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 204 In short, it is straightforward to use these two commands if you use one 205 process per line. It is much more work if you just want to use one 206 process, but still doable. In contrast, this problem is solved with just 207 another shell pipeline with this command. 208 209 SEE ALSO 210 -------- 211 linkgit:git-name-rev[1], 212 linkgit:git-log[1]. 213 214 GIT 215 --- 216 Part of the linkgit:git[1] suite