1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-10-29 21:37:53 +01:00

Merge branch 'sb/ls-tree-parseopt'

* sb/ls-tree-parseopt:
  ls-tree: migrate to parse-options
  t3101: test more ls-tree options
This commit is contained in:
Junio C Hamano 2009-11-20 23:46:03 -08:00
commit 3fa95ce52b
2 changed files with 127 additions and 62 deletions

View file

@ -9,6 +9,7 @@
#include "commit.h"
#include "quote.h"
#include "builtin.h"
#include "parse-options.h"
static int line_termination = '\n';
#define LS_RECURSIVE 1
@ -22,8 +23,10 @@ static const char **pathspec;
static int chomp_prefix;
static const char *ls_tree_prefix;
static const char ls_tree_usage[] =
"git ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]] <tree-ish> [path...]";
static const char * const ls_tree_usage[] = {
"git ls-tree [<options>] <tree-ish> [path...]",
NULL
};
static int show_recursive(const char *base, int baselen, const char *pathname)
{
@ -117,76 +120,53 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
{
unsigned char sha1[20];
struct tree *tree;
int full_tree = 0;
const struct option ls_tree_options[] = {
OPT_BIT('d', NULL, &ls_options, "only show trees",
LS_TREE_ONLY),
OPT_BIT('r', NULL, &ls_options, "recurse into subtrees",
LS_RECURSIVE),
OPT_BIT('t', NULL, &ls_options, "show trees when recursing",
LS_SHOW_TREES),
OPT_SET_INT('z', NULL, &line_termination,
"terminate entries with NUL byte", 0),
OPT_BIT('l', "long", &ls_options, "include object size",
LS_SHOW_SIZE),
OPT_BIT(0, "name-only", &ls_options, "list only filenames",
LS_NAME_ONLY),
OPT_BIT(0, "name-status", &ls_options, "list only filenames",
LS_NAME_ONLY),
OPT_SET_INT(0, "full-name", &chomp_prefix,
"use full path names", 0),
OPT_BOOLEAN(0, "full-tree", &full_tree,
"list entire tree; not just current directory "
"(implies --full-name)"),
OPT__ABBREV(&abbrev),
OPT_END()
};
git_config(git_default_config, NULL);
ls_tree_prefix = prefix;
if (prefix && *prefix)
chomp_prefix = strlen(prefix);
while (1 < argc && argv[1][0] == '-') {
switch (argv[1][1]) {
case 'z':
line_termination = 0;
break;
case 'r':
ls_options |= LS_RECURSIVE;
break;
case 'd':
ls_options |= LS_TREE_ONLY;
break;
case 't':
ls_options |= LS_SHOW_TREES;
break;
case 'l':
ls_options |= LS_SHOW_SIZE;
break;
case '-':
if (!strcmp(argv[1]+2, "name-only") ||
!strcmp(argv[1]+2, "name-status")) {
ls_options |= LS_NAME_ONLY;
break;
}
if (!strcmp(argv[1]+2, "long")) {
ls_options |= LS_SHOW_SIZE;
break;
}
if (!strcmp(argv[1]+2, "full-name")) {
chomp_prefix = 0;
break;
}
if (!strcmp(argv[1]+2, "full-tree")) {
ls_tree_prefix = prefix = NULL;
chomp_prefix = 0;
break;
}
if (!prefixcmp(argv[1]+2, "abbrev=")) {
abbrev = strtoul(argv[1]+9, NULL, 10);
if (abbrev && abbrev < MINIMUM_ABBREV)
abbrev = MINIMUM_ABBREV;
else if (abbrev > 40)
abbrev = 40;
break;
}
if (!strcmp(argv[1]+2, "abbrev")) {
abbrev = DEFAULT_ABBREV;
break;
}
/* otherwise fallthru */
default:
usage(ls_tree_usage);
}
argc--; argv++;
argc = parse_options(argc, argv, prefix, ls_tree_options,
ls_tree_usage, 0);
if (full_tree) {
ls_tree_prefix = prefix = NULL;
chomp_prefix = 0;
}
/* -d -r should imply -t, but -d by itself should not have to. */
if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
ls_options |= LS_SHOW_TREES;
if (argc < 2)
usage(ls_tree_usage);
if (get_sha1(argv[1], sha1))
die("Not a valid object name %s", argv[1]);
if (argc < 1)
usage_with_options(ls_tree_usage, ls_tree_options);
if (get_sha1(argv[0], sha1))
die("Not a valid object name %s", argv[0]);
pathspec = get_pathspec(prefix, argv + 2);
pathspec = get_pathspec(prefix, argv + 1);
tree = parse_tree_indirect(sha1);
if (!tree)
die("not a tree object");

View file

@ -39,8 +39,8 @@ test_expect_success \
tree=`git write-tree` &&
echo $tree'
_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
test_output () {
sed -e "s/ $_x40 / X /" <current >check
test_cmp expected check
@ -141,4 +141,89 @@ test_expect_success 'ls-tree filter is leading path match' '
test_output
'
test_expect_success 'ls-tree --full-name' '
(
cd path0 &&
git ls-tree --full-name $tree a
) >current &&
cat >expected <<\EOF &&
040000 tree X path0/a
EOF
test_output
'
test_expect_success 'ls-tree --full-tree' '
(
cd path1/b/c &&
git ls-tree --full-tree $tree
) >current &&
cat >expected <<\EOF &&
100644 blob X 1.txt
100644 blob X 2.txt
040000 tree X path0
040000 tree X path1
040000 tree X path2
040000 tree X path3
EOF
test_output
'
test_expect_success 'ls-tree --full-tree -r' '
(
cd path3/ &&
git ls-tree --full-tree -r $tree
) >current &&
cat >expected <<\EOF &&
100644 blob X 1.txt
100644 blob X 2.txt
100644 blob X path0/a/b/c/1.txt
100644 blob X path1/b/c/1.txt
100644 blob X path2/1.txt
100644 blob X path3/1.txt
100644 blob X path3/2.txt
EOF
test_output
'
test_expect_success 'ls-tree --abbrev=5' '
git ls-tree --abbrev=5 $tree >current &&
sed -e "s/ $_x05[0-9a-f]* / X /" <current >check &&
cat >expected <<\EOF &&
100644 blob X 1.txt
100644 blob X 2.txt
040000 tree X path0
040000 tree X path1
040000 tree X path2
040000 tree X path3
EOF
test_cmp expected check
'
test_expect_success 'ls-tree --name-only' '
git ls-tree --name-only $tree >current
cat >expected <<\EOF &&
1.txt
2.txt
path0
path1
path2
path3
EOF
test_output
'
test_expect_success 'ls-tree --name-only -r' '
git ls-tree --name-only -r $tree >current
cat >expected <<\EOF &&
1.txt
2.txt
path0/a/b/c/1.txt
path1/b/c/1.txt
path2/1.txt
path3/1.txt
path3/2.txt
EOF
test_output
'
test_done