diff.c: don't add extra '/' to pathname
[git/git.git] / ls-tree.c
CommitLineData
7912c070
PB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
aa1c48df
JH
8int line_termination = '\n';
9int recursive = 0;
10
11struct path_prefix {
12 struct path_prefix *prev;
13 const char *name;
14};
15
16static void print_path_prefix(struct path_prefix *prefix)
7912c070 17{
aa1c48df
JH
18 if (prefix) {
19 if (prefix->prev)
20 print_path_prefix(prefix->prev);
21 fputs(prefix->name, stdout);
22 putchar('/');
23 }
24}
25
26static void list_recursive(void *buffer,
23b127ed
JH
27 const unsigned char *type,
28 unsigned long size,
29 struct path_prefix *prefix)
aa1c48df
JH
30{
31 struct path_prefix this_prefix;
32 this_prefix.prev = prefix;
7912c070 33
7912c070 34 if (strcmp(type, "tree"))
2de381f9 35 die("expected a 'tree' node");
aa1c48df 36
7912c070 37 while (size) {
aa1c48df
JH
38 int namelen = strlen(buffer)+1;
39 void *eltbuf;
40 char elttype[20];
41 unsigned long eltsize;
42 unsigned char *sha1 = buffer + namelen;
43 char *path = strchr(buffer, ' ') + 1;
7912c070 44 unsigned int mode;
7912c070 45
aa1c48df 46 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
2de381f9 47 die("corrupt 'tree' file");
7912c070 48 buffer = sha1 + 20;
aa1c48df
JH
49 size -= namelen + 20;
50
0f2303f7
JH
51 printf("%06o\t%s\t%s\t", mode,
52 S_ISDIR(mode) ? "tree" : "blob",
53 sha1_to_hex(sha1));
aa1c48df
JH
54 print_path_prefix(prefix);
55 fputs(path, stdout);
56 putchar(line_termination);
57
0f2303f7
JH
58 if (! recursive || ! S_ISDIR(mode))
59 continue;
60
61 if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
62 error("cannot read %s", sha1_to_hex(sha1));
63 continue;
aa1c48df 64 }
0f2303f7
JH
65 this_prefix.name = path;
66 list_recursive(eltbuf, elttype, eltsize, &this_prefix);
aa1c48df 67 free(eltbuf);
7912c070 68 }
aa1c48df
JH
69}
70
71static int list(unsigned char *sha1)
72{
73 void *buffer;
74 unsigned long size;
aa1c48df 75
23b127ed 76 buffer = read_tree_with_tree_or_commit_sha1(sha1, &size, 0);
aa1c48df
JH
77 if (!buffer)
78 die("unable to read sha1 file");
23b127ed 79 list_recursive(buffer, "tree", size, NULL);
7912c070
PB
80 return 0;
81}
82
0f2303f7 83static const char *ls_tree_usage = "ls-tree [-r] [-z] <key>";
aa1c48df 84
7912c070
PB
85int main(int argc, char **argv)
86{
87 unsigned char sha1[20];
88
aa1c48df
JH
89 while (1 < argc && argv[1][0] == '-') {
90 switch (argv[1][1]) {
91 case 'z':
92 line_termination = 0;
93 break;
94 case 'r':
95 recursive = 1;
96 break;
97 default:
0f2303f7 98 usage(ls_tree_usage);
aa1c48df
JH
99 }
100 argc--; argv++;
101 }
102
7912c070 103 if (argc != 2)
0f2303f7 104 usage(ls_tree_usage);
7912c070 105 if (get_sha1_hex(argv[1], sha1) < 0)
0f2303f7 106 usage(ls_tree_usage);
7912c070
PB
107 sha1_file_directory = getenv(DB_ENVIRONMENT);
108 if (!sha1_file_directory)
109 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
110 if (list(sha1) < 0)
2de381f9 111 die("list failed");
7912c070
PB
112 return 0;
113}