Skip to content

Commit

Permalink
ls-tree: default <tree-ish> to HEAD
Browse files Browse the repository at this point in the history
When no positional arguments are passed to `git ls-tree`, it currently
prints "usage" info to stderr and exits with code 129. A more intuitive
default would be to operate on the `HEAD` commit's tree (similarly to
`git show`, `git log`, and possibly others).

This patch updates `git ls-tree [options...]` to operate identically to
`git ls-tree [options...] HEAD`, updates the docs to reflect that
`<tree-ish>` is optional (and `[path...]` args can only be provided if a
`<tree-ish>` is explicitly provided first), and duplicates some existing
test cases to omit the `HEAD` argument to `ls-tree` (verifying that
`ls-tree` behaves identically whether `HEAD` is provided or not).

Signed-off-by: Ryan Williams <ryan@runsascoded.com>
  • Loading branch information
ryan-williams committed Aug 6, 2023
1 parent ac83bc5 commit f3e24fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Documentation/git-ls-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SYNOPSIS
[verse]
'git ls-tree' [-d] [-r] [-t] [-l] [-z]
[--name-only] [--name-status] [--object-only] [--full-name] [--full-tree] [--abbrev[=<n>]] [--format=<format>]
<tree-ish> [<path>...]
[<tree-ish> [<path>...]]

DESCRIPTION
-----------
Expand All @@ -36,7 +36,7 @@ in the current working directory. Note that:
OPTIONS
-------
<tree-ish>::
Id of a tree-ish.
Id of a tree-ish. If omitted, defaults to "HEAD".

-d::
Show only the named tree entry itself, not its children.
Expand Down Expand Up @@ -139,7 +139,7 @@ which is able to interpolate different fields using a `%(fieldname)` notation.
For example, if you only care about the "objectname" and "path" fields, you
can execute with a specific "--format" like

git ls-tree --format='%(objectname) %(path)' <tree-ish>
git ls-tree --format='%(objectname) %(path)' [<tree-ish>]

FIELD NAMES
-----------
Expand Down
12 changes: 9 additions & 3 deletions builtin/ls-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "pathspec.h"

static const char * const ls_tree_usage[] = {
N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
N_("git ls-tree [<options>] [<tree-ish> [<path>...]]"),
NULL
};

Expand Down Expand Up @@ -405,8 +405,14 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
usage_msg_opt(
_("--format can't be combined with other format-altering options"),
ls_tree_usage, ls_tree_options);
if (argc < 1)
usage_with_options(ls_tree_usage, ls_tree_options);

/* If no positional args were passed, default <tree-ish> to HEAD. */
const char* fallback_argv[] = { "HEAD", NULL };

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / win build

builtin/ls-tree.c:410:9: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/ls-tree.c:410:2: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

builtin/ls-tree.c:410:9: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

builtin/ls-tree.c:410:9: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

builtin/ls-tree.c:410:14: mixing declarations and code is incompatible with standards before C99 [-Werror,-Wdeclaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

builtin/ls-tree.c:410:2: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

builtin/ls-tree.c:410:9: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

builtin/ls-tree.c:410:9: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

builtin/ls-tree.c:410:14: mixing declarations and code is incompatible with standards before C99 [-Werror,-Wdeclaration-after-statement]

Check failure on line 410 in builtin/ls-tree.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

builtin/ls-tree.c:410:2: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
if (argc < 1) {
argv = fallback_argv;
argc = 1;
}

if (repo_get_oid(the_repository, argv[0], &oid))
die("Not a valid object name %s", argv[0]);

Expand Down
7 changes: 6 additions & 1 deletion t/t3105-ls-tree-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ test_ls_tree_format_mode_output () {
local mode="$1" &&
shift &&

test_expect_success "'ls-tree $opts${mode:+ $mode}' output" '
test_expect_success "'ls-tree ${mode:+$mode }$opts' output" '
git ls-tree ${mode:+$mode }$opts HEAD >actual &&
test_cmp expect actual
'

test_expect_success "'ls-tree ${mode:+$mode }$opts' (default HEAD) output" '
git ls-tree ${mode:+$mode }$opts >actual &&
test_cmp expect actual
'

case "$opts" in
--full-tree)
test_expect_success "'ls-tree $opts${mode:+ $mode}' output (via subdir, fails)" '
Expand Down

0 comments on commit f3e24fe

Please sign in to comment.