Skip to content

Commit 7912c07

Browse files
paskyPetr Baudis
authored andcommitted
[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 <[email protected]>
1 parent c57a3a9 commit 7912c07

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
CFLAGS=-g -O3 -Wall
22
CC=gcc
33

4-
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
4+
PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
55
cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
6-
check-files
6+
check-files ls-tree
77

88
all: $(PROG)
99

@@ -50,6 +50,9 @@ show-files: show-files.o read-cache.o
5050
check-files: check-files.o read-cache.o
5151
$(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
5252

53+
ls-tree: ls-tree.o read-cache.o
54+
$(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
55+
5356
read-cache.o: cache.h
5457
show-diff.o: cache.h
5558

ls-tree.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* GIT - The information manager from hell
3+
*
4+
* Copyright (C) Linus Torvalds, 2005
5+
*/
6+
#include "cache.h"
7+
8+
static int list(unsigned char *sha1)
9+
{
10+
void *buffer;
11+
unsigned long size;
12+
char type[20];
13+
14+
buffer = read_sha1_file(sha1, type, &size);
15+
if (!buffer)
16+
usage("unable to read sha1 file");
17+
if (strcmp(type, "tree"))
18+
usage("expected a 'tree' node");
19+
while (size) {
20+
int len = strlen(buffer)+1;
21+
unsigned char *sha1 = buffer + len;
22+
char *path = strchr(buffer, ' ')+1;
23+
unsigned int mode;
24+
unsigned char *type;
25+
26+
if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
27+
usage("corrupt 'tree' file");
28+
buffer = sha1 + 20;
29+
size -= len + 20;
30+
/* XXX: We do some ugly mode heuristics here.
31+
* It seems not worth it to read each file just to get this
32+
* and the file size. -- [email protected] */
33+
type = S_ISDIR(mode) ? "tree" : "blob";
34+
printf("%03o\t%s\t%s\t%s\n", mode, type, sha1_to_hex(sha1), path);
35+
}
36+
return 0;
37+
}
38+
39+
int main(int argc, char **argv)
40+
{
41+
unsigned char sha1[20];
42+
43+
if (argc != 2)
44+
usage("ls-tree <key>");
45+
if (get_sha1_hex(argv[1], sha1) < 0)
46+
usage("ls-tree <key>");
47+
sha1_file_directory = getenv(DB_ENVIRONMENT);
48+
if (!sha1_file_directory)
49+
sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
50+
if (list(sha1) < 0)
51+
usage("list failed");
52+
return 0;
53+
}

0 commit comments

Comments
 (0)