[PATCH] Do not expose internal scaling to diff-helper.
[git/git-svn.git] / ls-tree.c
blob26cea3ff02212293edae0602f4398cbad1e894f2
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static int line_termination = '\n';
9 static int recursive = 0;
11 struct path_prefix {
12 struct path_prefix *prev;
13 const char *name;
16 #define DEBUG(fmt, ...)
18 static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
20 int len = 0;
21 if (prefix) {
22 if (prefix->prev) {
23 len = string_path_prefix(buff,blen,prefix->prev);
24 buff += len;
25 blen -= len;
26 if (blen > 0) {
27 *buff = '/';
28 len++;
29 buff++;
30 blen--;
33 strncpy(buff,prefix->name,blen);
34 return len + strlen(prefix->name);
37 return 0;
40 static void print_path_prefix(struct path_prefix *prefix)
42 if (prefix) {
43 if (prefix->prev) {
44 print_path_prefix(prefix->prev);
45 putchar('/');
47 fputs(prefix->name, stdout);
52 * return:
53 * -1 if prefix is *not* a subset of path
54 * 0 if prefix == path
55 * 1 if prefix is a subset of path
57 static int pathcmp(const char *path, struct path_prefix *prefix)
59 char buff[PATH_MAX];
60 int len,slen;
62 if (prefix == NULL)
63 return 1;
65 len = string_path_prefix(buff, sizeof buff, prefix);
66 slen = strlen(path);
68 if (slen < len)
69 return -1;
71 if (strncmp(path,buff,len) == 0) {
72 if (slen == len)
73 return 0;
74 else
75 return 1;
78 return -1;
82 * match may be NULL, or a *sorted* list of paths
84 static void list_recursive(void *buffer,
85 const char *type,
86 unsigned long size,
87 struct path_prefix *prefix,
88 char **match, int matches)
90 struct path_prefix this_prefix;
91 this_prefix.prev = prefix;
93 if (strcmp(type, "tree"))
94 die("expected a 'tree' node");
96 if (matches)
97 recursive = 1;
99 while (size) {
100 int namelen = strlen(buffer)+1;
101 void *eltbuf = NULL;
102 char elttype[20];
103 unsigned long eltsize;
104 unsigned char *sha1 = buffer + namelen;
105 char *path = strchr(buffer, ' ') + 1;
106 unsigned int mode;
107 const char *matched = NULL;
108 int mtype = -1;
109 int mindex;
111 if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
112 die("corrupt 'tree' file");
113 buffer = sha1 + 20;
114 size -= namelen + 20;
116 this_prefix.name = path;
117 for ( mindex = 0; mindex < matches; mindex++) {
118 mtype = pathcmp(match[mindex],&this_prefix);
119 if (mtype >= 0) {
120 matched = match[mindex];
121 break;
126 * If we're not matching, or if this is an exact match,
127 * print out the info
129 if (!matches || (matched != NULL && mtype == 0)) {
130 printf("%06o %s %s\t", mode,
131 S_ISDIR(mode) ? "tree" : "blob",
132 sha1_to_hex(sha1));
133 print_path_prefix(&this_prefix);
134 putchar(line_termination);
137 if (! recursive || ! S_ISDIR(mode))
138 continue;
140 if (matches && ! matched)
141 continue;
143 if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
144 error("cannot read %s", sha1_to_hex(sha1));
145 continue;
148 /* If this is an exact directory match, we may have
149 * directory files following this path. Match on them.
150 * Otherwise, we're at a pach subcomponent, and we need
151 * to try to match again.
153 if (mtype == 0)
154 mindex++;
156 list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
157 free(eltbuf);
161 static int qcmp(const void *a, const void *b)
163 return strcmp(*(char **)a, *(char **)b);
166 static int list(unsigned char *sha1,char **path)
168 void *buffer;
169 unsigned long size;
170 int npaths;
172 for (npaths = 0; path[npaths] != NULL; npaths++)
175 qsort(path,npaths,sizeof(char *),qcmp);
177 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
178 if (!buffer)
179 die("unable to read sha1 file");
180 list_recursive(buffer, "tree", size, NULL, path, npaths);
181 free(buffer);
182 return 0;
185 static const char *ls_tree_usage = "git-ls-tree [-r] [-z] <key> [paths...]";
187 int main(int argc, char **argv)
189 unsigned char sha1[20];
191 while (1 < argc && argv[1][0] == '-') {
192 switch (argv[1][1]) {
193 case 'z':
194 line_termination = 0;
195 break;
196 case 'r':
197 recursive = 1;
198 break;
199 default:
200 usage(ls_tree_usage);
202 argc--; argv++;
205 if (argc < 2)
206 usage(ls_tree_usage);
207 if (get_sha1(argv[1], sha1) < 0)
208 usage(ls_tree_usage);
209 if (list(sha1, &argv[2]) < 0)
210 die("list failed");
211 return 0;