2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 int line_termination
= '\n';
12 struct path_prefix
*prev
;
16 static void print_path_prefix(struct path_prefix
*prefix
)
20 print_path_prefix(prefix
->prev
);
21 fputs(prefix
->name
, stdout
);
26 static void list_recursive(void *buffer
,
29 struct path_prefix
*prefix
)
31 struct path_prefix this_prefix
;
32 this_prefix
.prev
= prefix
;
34 if (strcmp(type
, "tree"))
35 die("expected a 'tree' node");
38 int namelen
= strlen(buffer
)+1;
41 unsigned long eltsize
;
42 unsigned char *sha1
= buffer
+ namelen
;
43 char *path
= strchr(buffer
, ' ') + 1;
46 if (size
< namelen
+ 20 || sscanf(buffer
, "%o", &mode
) != 1)
47 die("corrupt 'tree' file");
51 /* XXX: We do some ugly mode heuristics here.
52 * It seems not worth it to read each file just to get this
53 * and the file size. -- pasky@ucw.cz
54 * ... that is, when we are not recursive -- junkio@cox.net
56 eltbuf
= (recursive
? read_sha1_file(sha1
, elttype
, &eltsize
) :
60 error("cannot read %s", sha1_to_hex(sha1
));
61 type
= S_ISDIR(mode
) ? "tree" : "blob";
66 printf("%03o\t%s\t%s\t", mode
, type
, sha1_to_hex(sha1
));
67 print_path_prefix(prefix
);
69 putchar(line_termination
);
71 if (eltbuf
&& !strcmp(type
, "tree")) {
72 this_prefix
.name
= path
;
73 list_recursive(eltbuf
, elttype
, eltsize
, &this_prefix
);
79 static int list(unsigned char *sha1
)
85 buffer
= read_sha1_file(sha1
, type
, &size
);
87 die("unable to read sha1 file");
88 list_recursive(buffer
, type
, size
, NULL
);
92 static void _usage(void)
94 usage("ls-tree [-r] [-z] <key>");
97 int main(int argc
, char **argv
)
99 unsigned char sha1
[20];
101 while (1 < argc
&& argv
[1][0] == '-') {
102 switch (argv
[1][1]) {
104 line_termination
= 0;
117 if (get_sha1_hex(argv
[1], sha1
) < 0)
119 sha1_file_directory
= getenv(DB_ENVIRONMENT
);
120 if (!sha1_file_directory
)
121 sha1_file_directory
= DEFAULT_DB_ENVIRONMENT
;