]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-show: grok blobs, trees and tags, too
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>
Thu, 14 Dec 2006 10:31:05 +0000 (11:31 +0100)
committerJunio C Hamano <junkio@cox.net>
Thu, 14 Dec 2006 10:56:24 +0000 (02:56 -0800)
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>
Documentation/git-show.txt
builtin-log.c
generate-cmdlist.sh

index 4c880a871792a64355ac94537af934aca14879ec..98dea6125da78fa74800925b57dad2f002b1d6f6 100644 (file)
@@ -3,20 +3,27 @@ git-show(1)
 
 NAME
 ----
-git-show - Show one commit with difference it introduces
+git-show - Show various types of objects
 
 
 SYNOPSIS
 --------
-'git-show' <option>...
+'git-show' [options] <object>...
 
 DESCRIPTION
 -----------
-Shows commit log and textual diff for a single commit.  The
-command internally invokes 'git-rev-list' piped to
-'git-diff-tree', and takes command line options for both of
-these commands. It also presents the merge commit in a special
-format as produced by 'git-diff-tree --cc'.
+Shows one or more objects (blobs, trees, tags and commits).
+
+For commits it shows the log message and textual diff. It also
+presents the merge commit in a special format as produced by
+'git-diff-tree --cc'.
+
+For tags, it shows the tag message and the referenced objects.
+
+For trees, it shows the names (equivalent to gitlink:git-ls-tree[1]
+with \--name-only).
+
+For plain blobs, it shows the plain contents.
 
 This manual page describes only the most frequently used options.
 
@@ -28,6 +35,25 @@ OPTIONS
 
 include::pretty-formats.txt[]
 
+
+EXAMPLES
+--------
+
+git show v1.0.0::
+       Shows the tag `v1.0.0`.
+
+git show v1.0.0^{tree}::
+       Shows the tree pointed to by the tag `v1.0.0`.
+
+git show next~10:Documentation/README
+       Shows the contents of the file `Documentation/README` as
+       they were current in the 10th last commit of the branch
+       `next`.
+
+git show master:Makefile master:t/Makefile
+       Concatenates the contents of said Makefiles in the head
+       of the branch `master`.
+
 Author
 ------
 Written by Linus Torvalds <torvalds@osdl.org> and
index 6821a08442603ba28ee2190a9f602f3d8d0f43f3..17014f70a2896de46ff4714e77093b8adaa9fc8f 100644 (file)
@@ -10,6 +10,7 @@
 #include "revision.h"
 #include "log-tree.h"
 #include "builtin.h"
+#include "tag.h"
 #include <time.h>
 #include <sys/time.h>
 
@@ -71,9 +72,43 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
        return cmd_log_walk(&rev);
 }
 
+static int show_object(const unsigned char *sha1, int suppress_header)
+{
+       unsigned long size;
+       char type[20];
+       char *buf = read_sha1_file(sha1, type, &size);
+       int offset = 0;
+
+       if (!buf)
+               return error("Could not read object %s", sha1_to_hex(sha1));
+
+       if (suppress_header)
+               while (offset < size && buf[offset++] != '\n') {
+                       int new_offset = offset;
+                       while (new_offset < size && buf[new_offset++] != '\n')
+                               ; /* do nothing */
+                       offset = new_offset;
+               }
+
+       if (offset < size)
+               fwrite(buf + offset, size - offset, 1, stdout);
+       free(buf);
+       return 0;
+}
+
+static int show_tree_object(const unsigned char *sha1,
+               const char *base, int baselen,
+               const char *pathname, unsigned mode, int stage)
+{
+       printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
+       return 0;
+}
+
 int cmd_show(int argc, const char **argv, const char *prefix)
 {
        struct rev_info rev;
+       struct object_array_entry *objects;
+       int i, count, ret = 0;
 
        git_config(git_log_config);
        init_revisions(&rev, prefix);
@@ -85,7 +120,52 @@ int cmd_show(int argc, const char **argv, const char *prefix)
        rev.ignore_merges = 0;
        rev.no_walk = 1;
        cmd_log_init(argc, argv, prefix, &rev);
-       return cmd_log_walk(&rev);
+
+       count = rev.pending.nr;
+       objects = rev.pending.objects;
+       for (i = 0; i < count && !ret; i++) {
+               struct object *o = objects[i].item;
+               const char *name = objects[i].name;
+               switch (o->type) {
+               case OBJ_BLOB:
+                       ret = show_object(o->sha1, 0);
+                       break;
+               case OBJ_TAG: {
+                       struct tag *t = (struct tag *)o;
+
+                       printf("%stag %s%s\n\n",
+                                       diff_get_color(rev.diffopt.color_diff,
+                                               DIFF_COMMIT),
+                                       t->tag,
+                                       diff_get_color(rev.diffopt.color_diff,
+                                               DIFF_RESET));
+                       ret = show_object(o->sha1, 1);
+                       objects[i].item = (struct object *)t->tagged;
+                       i--;
+                       break;
+               }
+               case OBJ_TREE:
+                       printf("%stree %s%s\n\n",
+                                       diff_get_color(rev.diffopt.color_diff,
+                                               DIFF_COMMIT),
+                                       name,
+                                       diff_get_color(rev.diffopt.color_diff,
+                                               DIFF_RESET));
+                       read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
+                                       show_tree_object);
+                       break;
+               case OBJ_COMMIT:
+                       rev.pending.nr = rev.pending.alloc = 0;
+                       rev.pending.objects = NULL;
+                       add_object_array(o, name, &rev.pending);
+                       ret = cmd_log_walk(&rev);
+                       break;
+               default:
+                       ret = error("Unknown type: %d", o->type);
+               }
+       }
+       free(objects);
+       return ret;
 }
 
 int cmd_log(int argc, const char **argv, const char *prefix)
index 5450918be339bf101a6562d518c0f4cc7b37c0f2..06c42b042d999856aacb9e156cffc39eb8dbce3f 100755 (executable)
@@ -4,7 +4,7 @@ echo "/* Automatically generated by $0 */
 struct cmdname_help
 {
     char name[16];
-    char help[64];
+    char help[80];
 };
 
 struct cmdname_help common_cmds[] = {"