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 <[email protected]>
  • Loading branch information
ryan-williams committed Aug 6, 2023
1 parent ac83bc5 commit a60036d
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 @@ -377,6 +377,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
};
struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;
int ret;
const char* fallback_argv[] = { "HEAD", NULL };

git_config(git_default_config, NULL);

Expand Down Expand Up @@ -405,8 +406,13 @@ 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. */
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 a60036d

Please sign in to comment.