Skip to content

Commit 5d7eeee

Browse files
dschoJunio C Hamano
authored and
Junio C Hamano
committed
git-show: grok blobs, trees and tags, too
Since git-show is pure Porcelain, it is the ideal candidate to pretty print other things than commits, too. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 02c9e93 commit 5d7eeee

File tree

3 files changed

+115
-9
lines changed

3 files changed

+115
-9
lines changed

Documentation/git-show.txt

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ git-show(1)
33

44
NAME
55
----
6-
git-show - Show one commit with difference it introduces
6+
git-show - Show various types of objects
77

88

99
SYNOPSIS
1010
--------
11-
'git-show' <option>...
11+
'git-show' [options] <object>...
1212

1313
DESCRIPTION
1414
-----------
15-
Shows commit log and textual diff for a single commit. The
16-
command internally invokes 'git-rev-list' piped to
17-
'git-diff-tree', and takes command line options for both of
18-
these commands. It also presents the merge commit in a special
19-
format as produced by 'git-diff-tree --cc'.
15+
Shows one or more objects (blobs, trees, tags and commits).
16+
17+
For commits it shows the log message and textual diff. It also
18+
presents the merge commit in a special format as produced by
19+
'git-diff-tree --cc'.
20+
21+
For tags, it shows the tag message and the referenced objects.
22+
23+
For trees, it shows the names (equivalent to gitlink:git-ls-tree[1]
24+
with \--name-only).
25+
26+
For plain blobs, it shows the plain contents.
2027

2128
This manual page describes only the most frequently used options.
2229

@@ -28,6 +35,25 @@ OPTIONS
2835

2936
include::pretty-formats.txt[]
3037

38+
39+
EXAMPLES
40+
--------
41+
42+
git show v1.0.0::
43+
Shows the tag `v1.0.0`.
44+
45+
git show v1.0.0^{tree}::
46+
Shows the tree pointed to by the tag `v1.0.0`.
47+
48+
git show next~10:Documentation/README
49+
Shows the contents of the file `Documentation/README` as
50+
they were current in the 10th last commit of the branch
51+
`next`.
52+
53+
git show master:Makefile master:t/Makefile
54+
Concatenates the contents of said Makefiles in the head
55+
of the branch `master`.
56+
3157
Author
3258
------
3359
Written by Linus Torvalds <[email protected]> and

builtin-log.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "revision.h"
1111
#include "log-tree.h"
1212
#include "builtin.h"
13+
#include "tag.h"
1314
#include <time.h>
1415
#include <sys/time.h>
1516

@@ -71,9 +72,43 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
7172
return cmd_log_walk(&rev);
7273
}
7374

75+
static int show_object(const unsigned char *sha1, int suppress_header)
76+
{
77+
unsigned long size;
78+
char type[20];
79+
char *buf = read_sha1_file(sha1, type, &size);
80+
int offset = 0;
81+
82+
if (!buf)
83+
return error("Could not read object %s", sha1_to_hex(sha1));
84+
85+
if (suppress_header)
86+
while (offset < size && buf[offset++] != '\n') {
87+
int new_offset = offset;
88+
while (new_offset < size && buf[new_offset++] != '\n')
89+
; /* do nothing */
90+
offset = new_offset;
91+
}
92+
93+
if (offset < size)
94+
fwrite(buf + offset, size - offset, 1, stdout);
95+
free(buf);
96+
return 0;
97+
}
98+
99+
static int show_tree_object(const unsigned char *sha1,
100+
const char *base, int baselen,
101+
const char *pathname, unsigned mode, int stage)
102+
{
103+
printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
104+
return 0;
105+
}
106+
74107
int cmd_show(int argc, const char **argv, const char *prefix)
75108
{
76109
struct rev_info rev;
110+
struct object_array_entry *objects;
111+
int i, count, ret = 0;
77112

78113
git_config(git_log_config);
79114
init_revisions(&rev, prefix);
@@ -85,7 +120,52 @@ int cmd_show(int argc, const char **argv, const char *prefix)
85120
rev.ignore_merges = 0;
86121
rev.no_walk = 1;
87122
cmd_log_init(argc, argv, prefix, &rev);
88-
return cmd_log_walk(&rev);
123+
124+
count = rev.pending.nr;
125+
objects = rev.pending.objects;
126+
for (i = 0; i < count && !ret; i++) {
127+
struct object *o = objects[i].item;
128+
const char *name = objects[i].name;
129+
switch (o->type) {
130+
case OBJ_BLOB:
131+
ret = show_object(o->sha1, 0);
132+
break;
133+
case OBJ_TAG: {
134+
struct tag *t = (struct tag *)o;
135+
136+
printf("%stag %s%s\n\n",
137+
diff_get_color(rev.diffopt.color_diff,
138+
DIFF_COMMIT),
139+
t->tag,
140+
diff_get_color(rev.diffopt.color_diff,
141+
DIFF_RESET));
142+
ret = show_object(o->sha1, 1);
143+
objects[i].item = (struct object *)t->tagged;
144+
i--;
145+
break;
146+
}
147+
case OBJ_TREE:
148+
printf("%stree %s%s\n\n",
149+
diff_get_color(rev.diffopt.color_diff,
150+
DIFF_COMMIT),
151+
name,
152+
diff_get_color(rev.diffopt.color_diff,
153+
DIFF_RESET));
154+
read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
155+
show_tree_object);
156+
break;
157+
case OBJ_COMMIT:
158+
rev.pending.nr = rev.pending.alloc = 0;
159+
rev.pending.objects = NULL;
160+
add_object_array(o, name, &rev.pending);
161+
ret = cmd_log_walk(&rev);
162+
break;
163+
default:
164+
ret = error("Unknown type: %d", o->type);
165+
}
166+
}
167+
free(objects);
168+
return ret;
89169
}
90170

91171
int cmd_log(int argc, const char **argv, const char *prefix)

generate-cmdlist.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ echo "/* Automatically generated by $0 */
44
struct cmdname_help
55
{
66
char name[16];
7-
char help[64];
7+
char help[80];
88
};
99
1010
struct cmdname_help common_cmds[] = {"

0 commit comments

Comments
 (0)