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