Add an option to git-ls-tree to display also the size of blob
Add -l/--long option to git-ls-tree command, which displays object size of a blob entry. Object size is placed after object id (left-justified with minimum width of 7 characters). For non-blob entries `-' is used. Rationale: for non-blob entries size of an object has no much meaning, and is not very interesting. Moreover, in planned pack v4 tree objects would be constructed on demand, so tree size would need to be calculated. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Jakub Narebski committed
May 19, 2007 at 22:08 UTC
a5bbda8b7b7f25f6caaa9f940e455e702f39766e
2 files changed
+41
-6
Documentation/git-ls-tree.txt
+13
-1
@@ -9,7 +9,7 @@ git-ls-tree - List the contents of a tree object
9
SYNOPSIS
10
--------
11
[verse]
12
-'git-ls-tree' [-d] [-r] [-t] [-z]
12
+'git-ls-tree' [-d] [-r] [-t] [-l] [-z]
13
[--name-only] [--name-status] [--full-name] [--abbrev=[<n>]]
14
<tree-ish> [paths...]
15
@@ -36,6 +36,10 @@ OPTIONS
36
Show tree entries even when going to recurse them. Has no effect
37
if '-r' was not passed. '-d' implies '-t'.
38
39
+-l::
40
+--long::
41
+ Show object size of blob (file) entries.
42
+
43
-z::
44
\0 line termination on output.
45
@@ -65,6 +69,14 @@ Output Format
69
When the `-z` option is not used, TAB, LF, and backslash characters
70
in pathnames are represented as `\t`, `\n`, and `\\`, respectively.
71
72
+When the `-l` option is used, format changes to
73
+
74
+ <mode> SP <type> SP <object> SP <object size> TAB <file>
75
+
76
+Object size identified by <object> is given in bytes, and right-justified
77
+with minimum width of 7 characters. Object size is given only for blobs
78
+(file) entries; for other entries `-` character is used in place of size.
79
+
80
81
Author
82
------
builtin-ls-tree.c
+28
-5
@@ -15,6 +15,7 @@ static int line_termination = '\n';
15
#define LS_TREE_ONLY 2
16
#define LS_SHOW_TREES 4
17
#define LS_NAME_ONLY 8
18
+#define LS_SHOW_SIZE 16
19
static int abbrev;
20
static int ls_options;
21
static const char **pathspec;
@@ -22,7 +23,7 @@ static int chomp_prefix;
23
static const char *ls_tree_prefix;
24
25
static const char ls_tree_usage[] =
25
- "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
26
+ "git-ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
27
28
static int show_recursive(const char *base, int baselen, const char *pathname)
29
{
@@ -59,6 +60,7 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
60
{
61
int retval = 0;
62
const char *type = blob_type;
63
+ unsigned long size;
64
65
if (S_ISDIRLNK(mode)) {
66
/*
@@ -92,10 +94,24 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
94
(baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
95
return 0;
96
95
- if (!(ls_options & LS_NAME_ONLY))
96
- printf("%06o %s %s\t", mode, type,
97
- abbrev ? find_unique_abbrev(sha1,abbrev)
98
- : sha1_to_hex(sha1));
97
+ if (!(ls_options & LS_NAME_ONLY)) {
98
+ if (ls_options & LS_SHOW_SIZE) {
99
+ if (!strcmp(type, blob_type)) {
100
+ sha1_object_info(sha1, &size);
101
+ printf("%06o %s %s %7lu\t", mode, type,
102
+ abbrev ? find_unique_abbrev(sha1, abbrev)
103
+ : sha1_to_hex(sha1),
104
+ size);
105
+ } else
106
+ printf("%06o %s %s %7c\t", mode, type,
107
+ abbrev ? find_unique_abbrev(sha1, abbrev)
108
+ : sha1_to_hex(sha1),
109
+ '-');
110
+ } else
111
+ printf("%06o %s %s\t", mode, type,
112
+ abbrev ? find_unique_abbrev(sha1, abbrev)
113
+ : sha1_to_hex(sha1));
114
+ }
115
write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
116
pathname,
117
line_termination, stdout);
@@ -126,12 +142,19 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
142
case 't':
143
ls_options |= LS_SHOW_TREES;
144
break;
145
+ case 'l':
146
+ ls_options |= LS_SHOW_SIZE;
147
+ break;
148
case '-':
149
if (!strcmp(argv[1]+2, "name-only") ||
150
!strcmp(argv[1]+2, "name-status")) {
151
ls_options |= LS_NAME_ONLY;
152
break;
153
}
154
+ if (!strcmp(argv[1]+2, "long")) {
155
+ ls_options |= LS_SHOW_SIZE;
156
+ break;
157
+ }
158
if (!strcmp(argv[1]+2, "full-name")) {
159
chomp_prefix = 0;
160
break;