X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=ls-tree.c;h=cf4223f4bfd63f047d8f9d3bed3ee5fbeaeef3c4;hb=53228a5fb8e80f87803e4a3ba8ed25b70fb4871d;hp=e198a20cb3dd88ae46da1aaf005846fe42843ea7;hpb=51017101c7a308745ba3c04944457f1dc6a55780;p=git.git diff --git a/ls-tree.c b/ls-tree.c index e198a20c..cf4223f4 100644 --- a/ls-tree.c +++ b/ls-tree.c @@ -6,6 +6,7 @@ #include "cache.h" #include "blob.h" #include "tree.h" +#include "quote.h" static int line_termination = '\n'; #define LS_RECURSIVE 1 @@ -54,11 +55,13 @@ static int prepare_children(struct tree_entry_list *elem) return 0; } -static struct tree_entry_list *find_entry(const char *path) +static struct tree_entry_list *find_entry(const char *path, char *pathbuf) { const char *next, *slash; int len; - struct tree_entry_list *elem = &root_entry; + struct tree_entry_list *elem = &root_entry, *oldelem = NULL; + + *(pathbuf) = '\0'; /* Find tree element, descending from root, that * corresponds to the named path, lazily expanding @@ -86,6 +89,10 @@ static struct tree_entry_list *find_entry(const char *path) len = slash - path; } if (len) { + if (oldelem) { + pathbuf += sprintf(pathbuf, "%s/", oldelem->name); + } + /* (len == 0) if the original path was "drivers/char/" * and we have run already two rounds, having elem * pointing at the drivers/char directory. @@ -101,6 +108,8 @@ static struct tree_entry_list *find_entry(const char *path) } if (!elem) return NULL; + + oldelem = elem; } path = next; } @@ -108,19 +117,6 @@ static struct tree_entry_list *find_entry(const char *path) return elem; } -static void show_entry_name(struct tree_entry_list *e) -{ - /* This is yucky. The root level is there for - * our convenience but we really want to do a - * forest. - */ - if (e->parent && e->parent != &root_entry) { - show_entry_name(e->parent); - putchar('/'); - } - printf("%s", e->name); -} - static const char *entry_type(struct tree_entry_list *e) { return (e->directory ? "tree" : "blob"); @@ -134,28 +130,38 @@ static const char *entry_hex(struct tree_entry_list *e) } /* forward declaration for mutually recursive routines */ -static int show_entry(struct tree_entry_list *, int); +static int show_entry(struct tree_entry_list *, int, char *pathbuf); -static int show_children(struct tree_entry_list *e, int level) +static int show_children(struct tree_entry_list *e, int level, char *pathbuf) { + int oldlen = strlen(pathbuf); + + if (e != &root_entry) + sprintf(pathbuf + oldlen, "%s/", e->name); + if (prepare_children(e)) die("internal error: ls-tree show_children called with non tree"); e = e->item.tree->entries; while (e) { - show_entry(e, level); + show_entry(e, level, pathbuf); e = e->next; } + + pathbuf[oldlen] = '\0'; + return 0; } -static int show_entry(struct tree_entry_list *e, int level) +static int show_entry(struct tree_entry_list *e, int level, char *pathbuf) { int err = 0; if (e != &root_entry) { - printf("%06o %s %s ", e->mode, entry_type(e), - entry_hex(e)); - show_entry_name(e); + int pathlen = strlen(pathbuf); + printf("%06o %s %s ", + e->mode, entry_type(e), entry_hex(e)); + write_name_quoted(pathbuf, pathlen, e->name, + line_termination, stdout); putchar(line_termination); } @@ -176,10 +182,10 @@ static int show_entry(struct tree_entry_list *e, int level) */ if (level == 0 && !(ls_options & LS_TREE_ONLY)) /* case (1)-a and (1)-b */ - err = err | show_children(e, level+1); + err = err | show_children(e, level+1, pathbuf); else if (level && ls_options & LS_RECURSIVE) /* case (2)-b */ - err = err | show_children(e, level+1); + err = err | show_children(e, level+1, pathbuf); } return err; } @@ -187,7 +193,8 @@ static int show_entry(struct tree_entry_list *e, int level) static int list_one(const char *path) { int err = 0; - struct tree_entry_list *e = find_entry(path); + char pathbuf[MAXPATHLEN + 1]; + struct tree_entry_list *e = find_entry(path, pathbuf); if (!e) { /* traditionally ls-tree does not complain about * missing path. We may change this later to match @@ -195,11 +202,11 @@ static int list_one(const char *path) */ return err; } - err = err | show_entry(e, 0); + err = err | show_entry(e, 0, pathbuf); return err; } -static int list(char **path) +static int list(const char **path) { int i; int err = 0; @@ -211,11 +218,16 @@ static int list(char **path) static const char ls_tree_usage[] = "git-ls-tree [-d] [-r] [-z] [path...]"; -int main(int argc, char **argv) +int main(int argc, const char **argv) { - static char *path0[] = { "", NULL }; - char **path; + static const char *path0[] = { "", NULL }; + const char **path; unsigned char sha1[20]; + int nongit = 0; + const char *prefix = setup_git_directory_gently(&nongit); + + if (prefix) + path0[0] = prefix; while (1 < argc && argv[1][0] == '-') { switch (argv[1][1]) { @@ -239,7 +251,11 @@ int main(int argc, char **argv) if (get_sha1(argv[1], sha1) < 0) usage(ls_tree_usage); - path = (argc == 2) ? path0 : (argv + 2); + if (argc == 2) + path = path0; + else + path = get_pathspec(prefix, argv + 2); + prepare_root(sha1); if (list(path) < 0) die("list failed");