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 printf("%06o\t%s\t%s\t", mode
,
52 S_ISDIR(mode
) ? "tree" : "blob",
54 print_path_prefix(prefix
);
56 putchar(line_termination
);
58 if (! recursive
|| ! S_ISDIR(mode
))
61 if (! (eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
)) ) {
62 error("cannot read %s", sha1_to_hex(sha1
));
65 this_prefix
.name
= path
;
66 list_recursive(eltbuf
, elttype
, eltsize
, &this_prefix
);
71 static int list(unsigned char *sha1
)
77 buffer
= read_sha1_file(sha1
, type
, &size
);
79 die("unable to read sha1 file");
80 list_recursive(buffer
, type
, size
, NULL
);
84 static const char *ls_tree_usage
= "ls-tree [-r] [-z] <key>";
86 int main(int argc
, char **argv
)
88 unsigned char sha1
[20];
90 while (1 < argc
&& argv
[1][0] == '-') {
105 usage(ls_tree_usage
);
106 if (get_sha1_hex(argv
[1], sha1
) < 0)
107 usage(ls_tree_usage
);
108 sha1_file_directory
= getenv(DB_ENVIRONMENT
);
109 if (!sha1_file_directory
)
110 sha1_file_directory
= DEFAULT_DB_ENVIRONMENT
;