Skip to content

Commit 1041d58

Browse files
committed
Merge branch 'tl/ls-tree-oid-only'
"git ls-tree" learns "--oid-only" option, similar to "--name-only", and more generalized "--format" option. * tl/ls-tree-oid-only: ls-tree: split up "fast path" callbacks ls-tree: detect and error on --name-only --name-status ls-tree: support --object-only option for "git-ls-tree" ls-tree: introduce "--format" option cocci: allow padding with `strbuf_addf()` ls-tree: introduce struct "show_tree_data" ls-tree: slightly refactor `show_tree()` ls-tree: fix "--name-only" and "--long" combined use bug ls-tree: simplify nesting if/else logic in "show_tree()" ls-tree: rename "retval" to "recurse" in "show_tree()" ls-tree: use "size_t", not "int" for "struct strbuf"'s "len" ls-tree: use "enum object_type", not {blob,tree,commit}_type ls-tree: add missing braces to "else" arms ls-tree: remove commented-out code ls-tree tests: add tests for --name-status
2 parents 3ff8cbf + 9c4d58f commit 1041d58

File tree

6 files changed

+492
-94
lines changed

6 files changed

+492
-94
lines changed

Documentation/git-ls-tree.txt

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git ls-tree' [-d] [-r] [-t] [-l] [-z]
13-
[--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]]
13+
[--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=<n>]] [--format=<format>]
1414
<tree-ish> [<path>...]
1515

1616
DESCRIPTION
@@ -59,6 +59,15 @@ OPTIONS
5959
--name-only::
6060
--name-status::
6161
List only filenames (instead of the "long" output), one per line.
62+
Cannot be combined with `--object-only`.
63+
64+
--object-only::
65+
List only names of the objects, one per line. Cannot be combined
66+
with `--name-only` or `--name-status`.
67+
This is equivalent to specifying `--format='%(objectname)'`, but
68+
for both this option and that exact format the command takes a
69+
hand-optimized codepath instead of going through the generic
70+
formatting mechanism.
6271

6372
--abbrev[=<n>]::
6473
Instead of showing the full 40-byte hexadecimal object
@@ -74,6 +83,16 @@ OPTIONS
7483
Do not limit the listing to the current working directory.
7584
Implies --full-name.
7685

86+
--format=<format>::
87+
A string that interpolates `%(fieldname)` from the result
88+
being shown. It also interpolates `%%` to `%`, and
89+
`%xx` where `xx` are hex digits interpolates to character
90+
with hex code `xx`; for example `%00` interpolates to
91+
`\0` (NUL), `%09` to `\t` (TAB) and `%0a` to `\n` (LF).
92+
When specified, `--format` cannot be combined with other
93+
format-altering options, including `--long`, `--name-only`
94+
and `--object-only`.
95+
7796
[<path>...]::
7897
When paths are given, show them (note that this isn't really raw
7998
pathnames, but rather a list of patterns to match). Otherwise
@@ -82,16 +101,29 @@ OPTIONS
82101

83102
Output Format
84103
-------------
85-
<mode> SP <type> SP <object> TAB <file>
104+
105+
The output format of `ls-tree` is determined by either the `--format`
106+
option, or other format-altering options such as `--name-only` etc.
107+
(see `--format` above).
108+
109+
The use of certain `--format` directives is equivalent to using those
110+
options, but invoking the full formatting machinery can be slower than
111+
using an appropriate formatting option.
112+
113+
In cases where the `--format` would exactly map to an existing option
114+
`ls-tree` will use the appropriate faster path. Thus the default format
115+
is equivalent to:
116+
117+
%(objectmode) %(objecttype) %(objectname)%x09%(path)
86118

87119
This output format is compatible with what `--index-info --stdin` of
88120
'git update-index' expects.
89121

90122
When the `-l` option is used, format changes to
91123

92-
<mode> SP <type> SP <object> SP <object size> TAB <file>
124+
%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)
93125

94-
Object size identified by <object> is given in bytes, and right-justified
126+
Object size identified by <objectname> is given in bytes, and right-justified
95127
with minimum width of 7 characters. Object size is given only for blobs
96128
(file) entries; for other entries `-` character is used in place of size.
97129

@@ -100,6 +132,34 @@ quoted as explained for the configuration variable `core.quotePath`
100132
(see linkgit:git-config[1]). Using `-z` the filename is output
101133
verbatim and the line is terminated by a NUL byte.
102134

135+
Customized format:
136+
137+
It is possible to print in a custom format by using the `--format` option,
138+
which is able to interpolate different fields using a `%(fieldname)` notation.
139+
For example, if you only care about the "objectname" and "path" fields, you
140+
can execute with a specific "--format" like
141+
142+
git ls-tree --format='%(objectname) %(path)' <tree-ish>
143+
144+
FIELD NAMES
145+
-----------
146+
147+
Various values from structured fields can be used to interpolate
148+
into the resulting output. For each outputing line, the following
149+
names can be used:
150+
151+
objectmode::
152+
The mode of the object.
153+
objecttype::
154+
The type of the object (`blob` or `tree`).
155+
objectname::
156+
The name of the object.
157+
objectsize[:padded]::
158+
The size of the object ("-" if it's a tree).
159+
It also supports a padded format of size with "%(size:padded)".
160+
path::
161+
The pathname of the object.
162+
103163
GIT
104164
---
105165
Part of the linkgit:git[1] suite

0 commit comments

Comments
 (0)