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 <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Dec 14, 2006 at 11:31 UTC
5d7eeee2ac64e277e47ce2cdabd1af0d2501a96f
3 files changed
+115
-9
Documentation/git-show.txt
+33
-7
@@ -3,20 +3,27 @@ git-show(1)
3
4
NAME
5
----
6
-git-show - Show one commit with difference it introduces
6
+git-show - Show various types of objects
7
8
9
SYNOPSIS
10
--------
11
-'git-show' <option>...
11
+'git-show' [options] <object>...
12
13
DESCRIPTION
14
-----------
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.
27
28
This manual page describes only the most frequently used options.
29
@@ -28,6 +35,25 @@ OPTIONS
35
36
include::pretty-formats.txt[]
37
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
+
57
Author
58
------
59
Written by Linus Torvalds <torvalds@osdl.org> and
builtin-log.c
+81
-1
@@ -10,6 +10,7 @@
10
#include "revision.h"
11
#include "log-tree.h"
12
#include "builtin.h"
13
+#include "tag.h"
14
#include <time.h>
15
#include <sys/time.h>
16
@@ -71,9 +72,43 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
72
return cmd_log_walk(&rev);
73
}
74
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
+
107
int cmd_show(int argc, const char **argv, const char *prefix)
108
{
109
struct rev_info rev;
110
+ struct object_array_entry *objects;
111
+ int i, count, ret = 0;
112
113
git_config(git_log_config);
114
init_revisions(&rev, prefix);
@@ -85,7 +120,52 @@ int cmd_show(int argc, const char **argv, const char *prefix)
120
rev.ignore_merges = 0;
121
rev.no_walk = 1;
122
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;
169
}
170
171
int cmd_log(int argc, const char **argv, const char *prefix)
generate-cmdlist.sh
+1
-1
@@ -4,7 +4,7 @@ echo "/* Automatically generated by $0 */
4
struct cmdname_help
5
{
6
char name[16];
7
- char help[64];
7
+ char help[80];
8
};
9
10
struct cmdname_help common_cmds[] = {"