Skip to content

Commit 32fe7ff

Browse files
committed
Merge branch 'zh/ls-files-format-atoms'
Some atoms that can be used in "--format=<format>" for "git ls-tree" were not supported by "git ls-files", even though they were relevant in the context of the latter. * zh/ls-files-format-atoms: ls-files: align format atoms with ls-tree
2 parents ca9c063 + 4d28c4f commit 32fe7ff

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Documentation/git-ls-files.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,14 @@ interpolated. The following "fieldname" are understood:
270270

271271
objectmode::
272272
The mode of the file which is recorded in the index.
273+
objecttype::
274+
The object type of the file which is recorded in the index.
273275
objectname::
274276
The name of the file which is recorded in the index.
277+
objectsize[:padded]::
278+
The object size of the file which is recorded in the index
279+
("-" if the object is a `commit` or `tree`).
280+
It also supports a padded format of size with "%(objectsize:padded)".
275281
stage::
276282
The stage of the file which is recorded in the index.
277283
eolinfo:index::

builtin/ls-files.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "setup.h"
2626
#include "submodule.h"
2727
#include "submodule-config.h"
28+
#include "object-store.h"
29+
#include "hex.h"
30+
2831

2932
static int abbrev;
3033
static int show_deleted;
@@ -241,6 +244,24 @@ static void show_submodule(struct repository *superproject,
241244
repo_clear(&subrepo);
242245
}
243246

247+
static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
248+
const enum object_type type, unsigned int padded)
249+
{
250+
if (type == OBJ_BLOB) {
251+
unsigned long size;
252+
if (oid_object_info(the_repository, oid, &size) < 0)
253+
die(_("could not get object info about '%s'"),
254+
oid_to_hex(oid));
255+
if (padded)
256+
strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size);
257+
else
258+
strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size);
259+
} else if (padded) {
260+
strbuf_addf(line, "%7s", "-");
261+
} else {
262+
strbuf_addstr(line, "-");
263+
}
264+
}
244265
struct show_index_data {
245266
const char *pathname;
246267
struct index_state *istate;
@@ -272,6 +293,12 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,
272293
strbuf_addf(sb, "%06o", data->ce->ce_mode);
273294
else if (skip_prefix(start, "(objectname)", &p))
274295
strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);
296+
else if (skip_prefix(start, "(objecttype)", &p))
297+
strbuf_addstr(sb, type_name(object_type(data->ce->ce_mode)));
298+
else if (skip_prefix(start, "(objectsize:padded)", &p))
299+
expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1);
300+
else if (skip_prefix(start, "(objectsize)", &p))
301+
expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0);
275302
else if (skip_prefix(start, "(stage)", &p))
276303
strbuf_addf(sb, "%d", ce_stage(data->ce));
277304
else if (skip_prefix(start, "(eolinfo:index)", &p))

t/t3013-ls-files-format.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ test_expect_success 'git ls-files --format objectname v.s. -s' '
3838
test_cmp expect actual
3939
'
4040

41+
test_expect_success 'git ls-files --format objecttype' '
42+
git ls-files --format="%(objectname)" o1.txt o4.txt o6.txt >objectname &&
43+
git cat-file --batch-check="%(objecttype)" >expect <objectname &&
44+
git ls-files --format="%(objecttype)" o1.txt o4.txt o6.txt >actual &&
45+
test_cmp expect actual
46+
'
47+
48+
test_expect_success 'git ls-files --format objectsize' '
49+
cat>expect <<-\EOF &&
50+
26
51+
29
52+
27
53+
26
54+
-
55+
26
56+
EOF
57+
git ls-files --format="%(objectsize)" >actual &&
58+
59+
test_cmp expect actual
60+
'
61+
62+
test_expect_success 'git ls-files --format objectsize:padded' '
63+
cat>expect <<-\EOF &&
64+
26
65+
29
66+
27
67+
26
68+
-
69+
26
70+
EOF
71+
git ls-files --format="%(objectsize:padded)" >actual &&
72+
73+
test_cmp expect actual
74+
'
75+
4176
test_expect_success 'git ls-files --format v.s. --eol' '
4277
git ls-files --eol >tmp &&
4378
sed -e "s/ / /g" -e "s/ */ /g" tmp >expect 2>err &&

0 commit comments

Comments
 (0)