1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include"cache.h" 7#include"blob.h" 8#include"tree.h" 9 10static int line_termination ='\n'; 11#define LS_RECURSIVE 1 12#define LS_TREE_ONLY 2 13static int ls_options =0; 14 15static struct tree_entry_list root_entry; 16 17static voidprepare_root(unsigned char*sha1) 18{ 19unsigned char rsha[20]; 20unsigned long size; 21void*buf; 22struct tree *root_tree; 23 24 buf =read_object_with_reference(sha1,"tree", &size, rsha); 25free(buf); 26if(!buf) 27die("Could not read%s",sha1_to_hex(sha1)); 28 29 root_tree =lookup_tree(rsha); 30if(!root_tree) 31die("Could not read%s",sha1_to_hex(sha1)); 32 33/* Prepare a fake entry */ 34 root_entry.directory =1; 35 root_entry.executable = root_entry.symlink =0; 36 root_entry.mode = S_IFDIR; 37 root_entry.name =""; 38 root_entry.item.tree = root_tree; 39 root_entry.parent = NULL; 40} 41 42static intprepare_children(struct tree_entry_list *elem) 43{ 44if(!elem->directory) 45return-1; 46if(!elem->item.tree->object.parsed) { 47struct tree_entry_list *e; 48if(parse_tree(elem->item.tree)) 49return-1; 50/* Set up the parent link */ 51for(e = elem->item.tree->entries; e; e = e->next) 52 e->parent = elem; 53} 54return0; 55} 56 57static struct tree_entry_list *find_entry(const char*path) 58{ 59const char*next, *slash; 60int len; 61struct tree_entry_list *elem = &root_entry; 62 63/* Find tree element, descending from root, that 64 * corresponds to the named path, lazily expanding 65 * the tree if possible. 66 */ 67 68while(path) { 69/* The fact we still have path means that the caller 70 * wants us to make sure that elem at this point is a 71 * directory, and possibly descend into it. Even what 72 * is left is just trailing slashes, we loop back to 73 * here, and this call to prepare_children() will 74 * catch elem not being a tree. Nice. 75 */ 76if(prepare_children(elem)) 77return NULL; 78 79 slash =strchr(path,'/'); 80if(!slash) { 81 len =strlen(path); 82 next = NULL; 83} 84else{ 85 next = slash +1; 86 len = slash - path; 87} 88if(len) { 89/* (len == 0) if the original path was "drivers/char/" 90 * and we have run already two rounds, having elem 91 * pointing at the drivers/char directory. 92 */ 93 elem = elem->item.tree->entries; 94while(elem) { 95if((strlen(elem->name) == len) && 96!strncmp(elem->name, path, len)) { 97/* found */ 98break; 99} 100 elem = elem->next; 101} 102if(!elem) 103return NULL; 104} 105 path = next; 106} 107 108return elem; 109} 110 111static voidshow_entry_name(struct tree_entry_list *e) 112{ 113/* This is yucky. The root level is there for 114 * our convenience but we really want to do a 115 * forest. 116 */ 117if(e->parent && e->parent != &root_entry) { 118show_entry_name(e->parent); 119putchar('/'); 120} 121printf("%s", e->name); 122} 123 124static const char*entry_type(struct tree_entry_list *e) 125{ 126return(e->directory ?"tree":"blob"); 127} 128 129static const char*entry_hex(struct tree_entry_list *e) 130{ 131returnsha1_to_hex(e->directory 132? e->item.tree->object.sha1 133: e->item.blob->object.sha1); 134} 135 136/* forward declaration for mutually recursive routines */ 137static intshow_entry(struct tree_entry_list *,int); 138 139static intshow_children(struct tree_entry_list *e,int level) 140{ 141if(prepare_children(e)) 142die("internal error: ls-tree show_children called with non tree"); 143 e = e->item.tree->entries; 144while(e) { 145show_entry(e, level); 146 e = e->next; 147} 148return0; 149} 150 151static intshow_entry(struct tree_entry_list *e,int level) 152{ 153int err =0; 154 155if(e != &root_entry) { 156printf("%06o%s %s", e->mode,entry_type(e), 157entry_hex(e)); 158show_entry_name(e); 159putchar(line_termination); 160} 161 162if(e->directory) { 163/* If this is a directory, we have the following cases: 164 * (1) This is the top-level request (explicit path from the 165 * command line, or "root" if there is no command line). 166 * a. Without any flag. We show direct children. We do not 167 * recurse into them. 168 * b. With -r. We do recurse into children. 169 * c. With -d. We do not recurse into children. 170 * (2) We came here because our caller is either (1-a) or 171 * (1-b). 172 * a. Without any flag. We do not show our children (which 173 * are grandchildren for the original request). 174 * b. With -r. We continue to recurse into our children. 175 * c. With -d. We should not have come here to begin with. 176 */ 177if(level ==0&& !(ls_options & LS_TREE_ONLY)) 178/* case (1)-a and (1)-b */ 179 err = err |show_children(e, level+1); 180else if(level && ls_options & LS_RECURSIVE) 181/* case (2)-b */ 182 err = err |show_children(e, level+1); 183} 184return err; 185} 186 187static intlist_one(const char*path) 188{ 189int err =0; 190struct tree_entry_list *e =find_entry(path); 191if(!e) { 192/* traditionally ls-tree does not complain about 193 * missing path. We may change this later to match 194 * what "/bin/ls -a" does, which is to complain. 195 */ 196return err; 197} 198 err = err |show_entry(e,0); 199return err; 200} 201 202static intlist(char**path) 203{ 204int i; 205int err =0; 206for(i =0; path[i]; i++) 207 err = err |list_one(path[i]); 208return err; 209} 210 211static const char ls_tree_usage[] = 212"git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]"; 213 214intmain(int argc,char**argv) 215{ 216static char*path0[] = {"", NULL }; 217char**path; 218unsigned char sha1[20]; 219 220while(1< argc && argv[1][0] =='-') { 221switch(argv[1][1]) { 222case'z': 223 line_termination =0; 224break; 225case'r': 226 ls_options |= LS_RECURSIVE; 227break; 228case'd': 229 ls_options |= LS_TREE_ONLY; 230break; 231default: 232usage(ls_tree_usage); 233} 234 argc--; argv++; 235} 236 237if(argc <2) 238usage(ls_tree_usage); 239if(get_sha1(argv[1], sha1) <0) 240usage(ls_tree_usage); 241 242 path = (argc ==2) ? path0 : (argv +2); 243prepare_root(sha1); 244if(list(path) <0) 245die("list failed"); 246return0; 247}