summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Baudis <pasky@ucw.cz>2005-04-13 09:02:34 (GMT)
committerPetr Baudis <xpasky@machine>2005-04-13 09:02:34 (GMT)
commit7912c07037cf704394e9bcb7cb24c05ee03aa921 (patch)
tree69bfdfa541c368a47a86482dfe66e488b152fe3d
parentc57a3a91f1cc0d037e7b7992cbfa6f4213dcf37e (diff)
downloadgit-7912c07037cf704394e9bcb7cb24c05ee03aa921.zip
git-7912c07037cf704394e9bcb7cb24c05ee03aa921.tar.gz
git-7912c07037cf704394e9bcb7cb24c05ee03aa921.tar.bz2
[PATCH] ls-tree for listing trees
ls-tree tool provides just a way to export the binary tree objects to a usable text format. This is bound to be useful in variety of scripts, although none of those I have currently uses it. But e.g. the simple script I've sent to HPA for purging the object database uses it. Signed-off-by: Petr Baudis <pasky@ucw.cz>
-rw-r--r--Makefile7
-rw-r--r--ls-tree.c53
2 files changed, 58 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index dc6e5ab..7c2c1b5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,9 @@
CFLAGS=-g -O3 -Wall
CC=gcc
-PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
- check-files
+ check-files ls-tree
all: $(PROG)
@@ -50,6 +50,9 @@ show-files: show-files.o read-cache.o
check-files: check-files.o read-cache.o
$(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
read-cache.o: cache.h
show-diff.o: cache.h
diff --git a/ls-tree.c b/ls-tree.c
new file mode 100644
index 0000000..05c743a
--- /dev/null
+++ b/ls-tree.c
@@ -0,0 +1,53 @@
+/*
+ * GIT - The information manager from hell
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+
+static int list(unsigned char *sha1)
+{
+ void *buffer;
+ unsigned long size;
+ char type[20];
+
+ buffer = read_sha1_file(sha1, type, &size);
+ if (!buffer)
+ usage("unable to read sha1 file");
+ if (strcmp(type, "tree"))
+ usage("expected a 'tree' node");
+ while (size) {
+ int len = strlen(buffer)+1;
+ unsigned char *sha1 = buffer + len;
+ char *path = strchr(buffer, ' ')+1;
+ unsigned int mode;
+ unsigned char *type;
+
+ if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
+ usage("corrupt 'tree' file");
+ buffer = sha1 + 20;
+ size -= len + 20;
+ /* XXX: We do some ugly mode heuristics here.
+ * It seems not worth it to read each file just to get this
+ * and the file size. -- pasky@ucw.cz */
+ type = S_ISDIR(mode) ? "tree" : "blob";
+ printf("%03o\t%s\t%s\t%s\n", mode, type, sha1_to_hex(sha1), path);
+ }
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ unsigned char sha1[20];
+
+ if (argc != 2)
+ usage("ls-tree <key>");
+ if (get_sha1_hex(argv[1], sha1) < 0)
+ usage("ls-tree <key>");
+ sha1_file_directory = getenv(DB_ENVIRONMENT);
+ if (!sha1_file_directory)
+ sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
+ if (list(sha1) < 0)
+ usage("list failed");
+ return 0;
+}