Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/ls-tree.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / ls-tree.c` 452 lines ยท 12452 bytes Raw Blame History 1 /* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6 #define USE_THE_REPOSITORY_VARIABLE 7 #include "builtin.h" 8 9 #include "config.h" 10 #include "environment.h" 11 #include "gettext.h" 12 #include "hex.h" 13 #include "object-name.h" 14 #include "odb.h" 15 #include "tree.h" 16 #include "path.h" 17 #include "quote.h" 18 #include "parse-options.h" 19 #include "pathspec.h" 20 21 static const char * const ls_tree_usage[] = { 22 N_("git ls-tree [] [...]"), 23 NULL 24 }; 25 26 static void expand_objectsize(struct strbuf *line, const struct object_id *oid, 27 const enum object_type type, unsigned int padded) 28 { 29 static const char padding[] = " "; 30 size_t min_len = padded ? strlen(padding) : 0; 31 size_t orig_len = line->len; 32 size_t len; 33 34 if (type == OBJ_BLOB) { 35 size_t size; 36 if (odb_read_object_info(the_repository->objects, oid, &size) < 0) 37 die(_("could not get object info about '%s'"), 38 oid_to_hex(oid)); 39 strbuf_add_uint(line, size); 40 } else { 41 strbuf_addstr(line, "-"); 42 } 43 len = line->len - orig_len; 44 if (len < min_len) 45 strbuf_insert(line, orig_len, padding, min_len - len); 46 } 47 48 struct ls_tree_options { 49 unsigned null_termination:1; 50 int abbrev; 51 enum ls_tree_path_options { 52 LS_RECURSIVE = 1 << 0, 53 LS_TREE_ONLY = 1 << 1, 54 LS_SHOW_TREES = 1 << 2, 55 } ls_options; 56 struct pathspec pathspec; 57 const char *prefix; 58 const char *format; 59 }; 60 61 static int show_recursive(struct ls_tree_options *options, const char *base, 62 size_t baselen, const char *pathname) 63 { 64 int i; 65 66 if (options->ls_options & LS_RECURSIVE) 67 return 1; 68 69 if (!options->pathspec.nr) 70 return 0; 71 72 for (i = 0; i < options->pathspec.nr; i++) { 73 const char *spec = options->pathspec.items[i].match; 74 size_t len, speclen; 75 76 if (strncmp(base, spec, baselen)) 77 continue; 78 len = strlen(pathname); 79 spec += baselen; 80 speclen = strlen(spec); 81 if (speclen <= len) 82 continue; 83 if (spec[len] && spec[len] != '/') 84 continue; 85 if (memcmp(pathname, spec, len)) 86 continue; 87 return 1; 88 } 89 return 0; 90 } 91 92 static int show_tree_fmt(const struct object_id *oid, struct strbuf *base, 93 const char *pathname, unsigned mode, void *context) 94 { 95 struct ls_tree_options *options = context; 96 int recurse = 0; 97 struct strbuf sb = STRBUF_INIT; 98 enum object_type type = object_type(mode); 99 const char *format = options->format; 100 101 if (type == OBJ_TREE && show_recursive(options, base->buf, base->len, pathname)) 102 recurse = READ_TREE_RECURSIVE; 103 if (type == OBJ_TREE && recurse && !(options->ls_options & LS_SHOW_TREES)) 104 return recurse; 105 if (type == OBJ_BLOB && (options->ls_options & LS_TREE_ONLY)) 106 return 0; 107 108 while (strbuf_expand_step(&sb, &format)) { 109 size_t len; 110 111 if (skip_prefix(format, "%", &format)) 112 strbuf_addch(&sb, '%'); 113 else if ((len = strbuf_expand_literal(&sb, format))) 114 format += len; 115 else if (skip_prefix(format, "(objectmode)", &format)) 116 strbuf_addf(&sb, "%06o", mode); 117 else if (skip_prefix(format, "(objecttype)", &format)) 118 strbuf_addstr(&sb, type_name(type)); 119 else if (skip_prefix(format, "(objectsize:padded)", &format)) 120 expand_objectsize(&sb, oid, type, 1); 121 else if (skip_prefix(format, "(objectsize)", &format)) 122 expand_objectsize(&sb, oid, type, 0); 123 else if (skip_prefix(format, "(objectname)", &format)) 124 strbuf_add_unique_abbrev(&sb, oid, options->abbrev); 125 else if (skip_prefix(format, "(path)", &format)) { 126 const char *name; 127 const char *prefix = options->prefix; 128 struct strbuf sbuf = STRBUF_INIT; 129 size_t baselen = base->len; 130 131 strbuf_addstr(base, pathname); 132 name = relative_path(base->buf, prefix, &sbuf); 133 quote_c_style(name, &sb, NULL, 0); 134 strbuf_setlen(base, baselen); 135 strbuf_release(&sbuf); 136 } else 137 strbuf_expand_bad_format(format, "ls-tree"); 138 } 139 strbuf_addch(&sb, options->null_termination ? '\0' : '\n'); 140 fwrite(sb.buf, sb.len, 1, stdout); 141 strbuf_release(&sb); 142 return recurse; 143 } 144 145 static int show_tree_common(struct ls_tree_options *options, int *recurse, 146 struct strbuf *base, const char *pathname, 147 enum object_type type) 148 { 149 int ret = -1; 150 *recurse = 0; 151 152 if (type == OBJ_BLOB) { 153 if (options->ls_options & LS_TREE_ONLY) 154 ret = 0; 155 } else if (type == OBJ_TREE && 156 show_recursive(options, base->buf, base->len, pathname)) { 157 *recurse = READ_TREE_RECURSIVE; 158 if (!(options->ls_options & LS_SHOW_TREES)) 159 ret = *recurse; 160 } 161 162 return ret; 163 } 164 165 static void show_tree_common_default_long(struct ls_tree_options *options, 166 struct strbuf *base, 167 const char *pathname, 168 const size_t baselen) 169 { 170 const char *prefix = options->prefix; 171 172 strbuf_addstr(base, pathname); 173 174 if (options->null_termination) { 175 struct strbuf sb = STRBUF_INIT; 176 const char *name = relative_path(base->buf, prefix, &sb); 177 178 fputs(name, stdout); 179 fputc('\0', stdout); 180 181 strbuf_release(&sb); 182 } else { 183 write_name_quoted_relative(base->buf, prefix, stdout, '\n'); 184 } 185 186 strbuf_setlen(base, baselen); 187 } 188 189 static int show_tree_default(const struct object_id *oid, struct strbuf *base, 190 const char *pathname, unsigned mode, 191 void *context) 192 { 193 struct ls_tree_options *options = context; 194 int early; 195 int recurse; 196 enum object_type type = object_type(mode); 197 198 early = show_tree_common(options, &recurse, base, pathname, type); 199 if (early >= 0) 200 return early; 201 202 printf("%06o %s %s\t", mode, type_name(object_type(mode)), 203 repo_find_unique_abbrev(the_repository, oid, options->abbrev)); 204 show_tree_common_default_long(options, base, pathname, base->len); 205 return recurse; 206 } 207 208 static int show_tree_long(const struct object_id *oid, struct strbuf *base, 209 const char *pathname, unsigned mode, 210 void *context) 211 { 212 struct ls_tree_options *options = context; 213 int early; 214 int recurse; 215 char size_text[24]; 216 enum object_type type = object_type(mode); 217 218 early = show_tree_common(options, &recurse, base, pathname, type); 219 if (early >= 0) 220 return early; 221 222 if (type == OBJ_BLOB) { 223 size_t size; 224 if (odb_read_object_info(the_repository->objects, oid, &size) == OBJ_BAD) 225 xsnprintf(size_text, sizeof(size_text), "BAD"); 226 else 227 xsnprintf(size_text, sizeof(size_text), 228 "%" PRIuMAX, (uintmax_t)size); 229 } else { 230 xsnprintf(size_text, sizeof(size_text), "-"); 231 } 232 233 printf("%06o %s %s %7s\t", mode, type_name(type), 234 repo_find_unique_abbrev(the_repository, oid, options->abbrev), 235 size_text); 236 show_tree_common_default_long(options, base, pathname, base->len); 237 return recurse; 238 } 239 240 static int show_tree_name_only(const struct object_id *oid UNUSED, 241 struct strbuf *base, 242 const char *pathname, unsigned mode, 243 void *context) 244 { 245 struct ls_tree_options *options = context; 246 int early; 247 int recurse; 248 const size_t baselen = base->len; 249 enum object_type type = object_type(mode); 250 const char *prefix; 251 252 early = show_tree_common(options, &recurse, base, pathname, type); 253 if (early >= 0) 254 return early; 255 256 prefix = options->prefix; 257 strbuf_addstr(base, pathname); 258 if (options->null_termination) { 259 struct strbuf sb = STRBUF_INIT; 260 const char *name = relative_path(base->buf, prefix, &sb); 261 262 fputs(name, stdout); 263 fputc('\0', stdout); 264 265 strbuf_release(&sb); 266 } else { 267 write_name_quoted_relative(base->buf, prefix, stdout, '\n'); 268 } 269 strbuf_setlen(base, baselen); 270 return recurse; 271 } 272 273 static int show_tree_object(const struct object_id *oid, struct strbuf *base, 274 const char *pathname, unsigned mode, 275 void *context) 276 { 277 struct ls_tree_options *options = context; 278 int early; 279 int recurse; 280 enum object_type type = object_type(mode); 281 const char *str; 282 283 early = show_tree_common(options, &recurse, base, pathname, type); 284 if (early >= 0) 285 return early; 286 287 str = repo_find_unique_abbrev(the_repository, oid, options->abbrev); 288 if (options->null_termination) { 289 fputs(str, stdout); 290 fputc('\0', stdout); 291 } else { 292 puts(str); 293 } 294 return recurse; 295 } 296 297 enum ls_tree_cmdmode { 298 MODE_DEFAULT = 0, 299 MODE_LONG, 300 MODE_NAME_ONLY, 301 MODE_NAME_STATUS, 302 MODE_OBJECT_ONLY, 303 }; 304 305 struct ls_tree_cmdmode_to_fmt { 306 enum ls_tree_cmdmode mode; 307 const char *const fmt; 308 read_tree_fn_t fn; 309 }; 310 311 static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = { 312 { 313 .mode = MODE_DEFAULT, 314 .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)", 315 .fn = show_tree_default, 316 }, 317 { 318 .mode = MODE_LONG, 319 .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)", 320 .fn = show_tree_long, 321 }, 322 { 323 .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */ 324 .fmt = "%(path)", 325 .fn = show_tree_name_only, 326 }, 327 { 328 .mode = MODE_OBJECT_ONLY, 329 .fmt = "%(objectname)", 330 .fn = show_tree_object 331 }, 332 { 333 /* fallback */ 334 .fn = show_tree_default, 335 }, 336 }; 337 338 int cmd_ls_tree(int argc, 339 const char **argv, 340 const char *prefix, 341 struct repository *repo UNUSED) 342 { 343 struct object_id oid; 344 struct tree *tree; 345 int i, full_tree = 0; 346 int full_name = !prefix || !*prefix; 347 read_tree_fn_t fn = NULL; 348 enum ls_tree_cmdmode cmdmode = MODE_DEFAULT; 349 int null_termination = 0; 350 struct ls_tree_options options = { 0 }; 351 const struct option ls_tree_options[] = { 352 OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"), 353 LS_TREE_ONLY), 354 OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"), 355 LS_RECURSIVE), 356 OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"), 357 LS_SHOW_TREES), 358 OPT_BOOL('z', NULL, &null_termination, 359 N_("terminate entries with NUL byte")), 360 OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"), 361 MODE_LONG), 362 OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"), 363 MODE_NAME_ONLY), 364 OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"), 365 MODE_NAME_STATUS), 366 OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"), 367 MODE_OBJECT_ONLY), 368 OPT_BOOL(0, "full-name", &full_name, N_("use full path names")), 369 OPT_BOOL(0, "full-tree", &full_tree, 370 N_("list entire tree; not just current directory " 371 "(implies --full-name)")), 372 OPT_STRING_F(0, "format", &options.format, N_("format"), 373 N_("format to use for the output"), 374 PARSE_OPT_NONEG), 375 OPT__ABBREV(&options.abbrev), 376 OPT_END() 377 }; 378 struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format; 379 int ret; 380 381 repo_config(the_repository, git_default_config, NULL); 382 383 argc = parse_options(argc, argv, prefix, ls_tree_options, 384 ls_tree_usage, 0); 385 options.null_termination = null_termination; 386 387 if (full_tree) 388 prefix = NULL; 389 options.prefix = full_name ? NULL : prefix; 390 391 /* 392 * We wanted to detect conflicts between --name-only and 393 * --name-status, but once we're done with that subsequent 394 * code should only need to check the primary name. 395 */ 396 if (cmdmode == MODE_NAME_STATUS) 397 cmdmode = MODE_NAME_ONLY; 398 399 /* -d -r should imply -t, but -d by itself should not have to. */ 400 if ( (LS_TREE_ONLY|LS_RECURSIVE) == 401 ((LS_TREE_ONLY|LS_RECURSIVE) & options.ls_options)) 402 options.ls_options |= LS_SHOW_TREES; 403 404 if (options.format && cmdmode) 405 usage_msg_opt( 406 _("--format can't be combined with other format-altering options"), 407 ls_tree_usage, ls_tree_options); 408 if (argc < 1) 409 usage_with_options(ls_tree_usage, ls_tree_options); 410 if (repo_get_oid_with_flags(the_repository, argv[0], &oid, 411 GET_OID_HASH_ANY)) 412 die("Not a valid object name %s", argv[0]); 413 414 /* 415 * show_recursive() rolls its own matching code and is 416 * generally ignorant of 'struct pathspec'. The magic mask 417 * cannot be lifted until it is converted to use 418 * match_pathspec() or tree_entry_interesting() 419 */ 420 parse_pathspec(&options.pathspec, PATHSPEC_ALL_MAGIC & 421 ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL), 422 PATHSPEC_PREFER_CWD, 423 prefix, argv + 1); 424 for (i = 0; i < options.pathspec.nr; i++) 425 options.pathspec.items[i].nowildcard_len = options.pathspec.items[i].len; 426 options.pathspec.has_wildcard = 0; 427 tree = repo_parse_tree_indirect(the_repository, &oid); 428 if (!tree) 429 die("not a tree object"); 430 /* 431 * The generic show_tree_fmt() is slower than show_tree(), so 432 * take the fast path if possible. 433 */ 434 while (m2f) { 435 if (!m2f->fmt) { 436 fn = options.format ? show_tree_fmt : show_tree_default; 437 } else if (options.format && !strcmp(options.format, m2f->fmt)) { 438 cmdmode = m2f->mode; 439 fn = m2f->fn; 440 } else if (!options.format && cmdmode == m2f->mode) { 441 fn = m2f->fn; 442 } else { 443 m2f++; 444 continue; 445 } 446 break; 447 } 448 449 ret = !!read_tree(the_repository, tree, &options.pathspec, fn, &options); 450 clear_pathspec(&options.pathspec); 451 return ret; 452 }