* [PATCH] ls-tree: default <tree-ish> to HEAD
@ 2023-08-07 12:49 Ryan Williams via GitGitGadget
2023-08-07 16:25 ` Junio C Hamano
0 siblings, 1 reply; 4+ messages in thread
From: Ryan Williams via GitGitGadget @ 2023-08-07 12:49 UTC (permalink / raw)
To: git; +Cc: Ryan Williams, Ryan Williams
From: Ryan Williams <ryan@runsascoded.com>
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>
---
ls-tree: default to HEAD
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1566%2Frunsascoded%2Fls-tree-head-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1566/runsascoded/ls-tree-head-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1566
Documentation/git-ls-tree.txt | 6 +++---
builtin/ls-tree.c | 11 ++++++++---
t/t3105-ls-tree-output.sh | 7 ++++++-
3 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
index 6572095d8d6..6211d630974 100644
--- a/Documentation/git-ls-tree.txt
+++ b/Documentation/git-ls-tree.txt
@@ -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
-----------
@@ -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.
@@ -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
-----------
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index f558db5f3b8..b1e337ccde9 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -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
};
@@ -377,6 +377,8 @@ 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;
+ /* If no positional args were passed, default <tree-ish> to HEAD. */
+ const char *fallback_args[] = { "HEAD", NULL };
git_config(git_default_config, NULL);
@@ -405,8 +407,11 @@ 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 (argc < 1) {
+ /* `git ls-tree [flags...]` -> `git ls-tree [flags...] HEAD`. */
+ argv = fallback_args;
+ argc = 1;
+ }
if (repo_get_oid(the_repository, argv[0], &oid))
die("Not a valid object name %s", argv[0]);
diff --git a/t/t3105-ls-tree-output.sh b/t/t3105-ls-tree-output.sh
index ce2391e28be..cb05529c0ad 100755
--- a/t/t3105-ls-tree-output.sh
+++ b/t/t3105-ls-tree-output.sh
@@ -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)" '
base-commit: ac83bc5054c2ac489166072334b4147ce6d0fccb
--
gitgitgadget
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] ls-tree: default <tree-ish> to HEAD
2023-08-07 12:49 [PATCH] ls-tree: default <tree-ish> to HEAD Ryan Williams via GitGitGadget
@ 2023-08-07 16:25 ` Junio C Hamano
2023-08-11 17:35 ` Taylor Blau
2023-08-11 18:22 ` Linus Arver
0 siblings, 2 replies; 4+ messages in thread
From: Junio C Hamano @ 2023-08-07 16:25 UTC (permalink / raw)
To: Ryan Williams via GitGitGadget; +Cc: git, Ryan Williams
"Ryan Williams via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Ryan Williams <ryan@runsascoded.com>
>
> 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).
As 'ls-tree' is a plumbing command meant for script writers, it was
designed to require the users to be more explicit. So, similarity
to "show" and other Porcelain commands do not weigh much here. Same
for "rev-list" that does not fall back to HEAD.
This was a very deliberate design decision to help use of the
plumbing commands. A buggy script may say
TREE=$(some command to find a tree object)
git ls-tree $TREE
without making sure something sensible is in $TREE (and not even
quoting it like "$TREE"), and if ls-tree defaulted to something, the
script will silently produce a wrong result, instead of failing,
robbing the script writer a chance to notice a bug in their code to
come up with the TREE object name.
So, I dunno.
Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] ls-tree: default <tree-ish> to HEAD
2023-08-07 16:25 ` Junio C Hamano
@ 2023-08-11 17:35 ` Taylor Blau
2023-08-11 18:22 ` Linus Arver
1 sibling, 0 replies; 4+ messages in thread
From: Taylor Blau @ 2023-08-11 17:35 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Ryan Williams via GitGitGadget, git, Ryan Williams
On Mon, Aug 07, 2023 at 09:25:57AM -0700, Junio C Hamano wrote:
> "Ryan Williams via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: Ryan Williams <ryan@runsascoded.com>
> >
> > 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).
>
> As 'ls-tree' is a plumbing command meant for script writers, it was
> designed to require the users to be more explicit. So, similarity
> to "show" and other Porcelain commands do not weigh much here. Same
> for "rev-list" that does not fall back to HEAD.
>
> This was a very deliberate design decision to help use of the
> plumbing commands. A buggy script may say
>
> TREE=$(some command to find a tree object)
> git ls-tree $TREE
>
> without making sure something sensible is in $TREE (and not even
> quoting it like "$TREE"), and if ls-tree defaulted to something, the
> script will silently produce a wrong result, instead of failing,
> robbing the script writer a chance to notice a bug in their code to
> come up with the TREE object name.
Yeah, I am similarly torn. On one hand, I think that it is true that
people use ls-tree for various tasks more than we'd expect from a
typical plumbing command (but less than a porcelain one, to be sure).
So in that sense I am inclined to agree with Ryan that their suggestion
is a good one. ...But, I think even more important than that is avoiding
buggy invocations like the kind you describe above that would produce
subtly wrong results.
I think you could make an argument that you could implement that
behavior and also emit a warning() when tree-ish is blank and the output
isn't going to a TTY. But that seems kind of gross to me, so I'd just as
soon not change the existing behavior here.
Thanks,
Taylor
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] ls-tree: default <tree-ish> to HEAD
2023-08-07 16:25 ` Junio C Hamano
2023-08-11 17:35 ` Taylor Blau
@ 2023-08-11 18:22 ` Linus Arver
1 sibling, 0 replies; 4+ messages in thread
From: Linus Arver @ 2023-08-11 18:22 UTC (permalink / raw)
To: Junio C Hamano, Ryan Williams via GitGitGadget; +Cc: git, Ryan Williams
Junio C Hamano <gitster@pobox.com> writes:
> "Ryan Williams via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> From: Ryan Williams <ryan@runsascoded.com>
>>
>> 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).
>
> As 'ls-tree' is a plumbing command meant for script writers, it was
> designed to require the users to be more explicit. So, similarity
> to "show" and other Porcelain commands do not weigh much here. Same
> for "rev-list" that does not fall back to HEAD.
Indeed, we already partition these commands into these porcelain vs
plumbing categories in "man git". There, we say for plumbing
The interface (input, output, set of options and the semantics) to
these low-level commands are meant to be a lot more stable than
Porcelain level commands, because these commands are primarily for
scripted use. The interface to Porcelain commands on the other hand
are subject to change in order to improve the end user experience.
> This was a very deliberate design decision to help use of the
> plumbing commands. A buggy script may say
>
> TREE=$(some command to find a tree object)
> git ls-tree $TREE
>
> without making sure something sensible is in $TREE (and not even
> quoting it like "$TREE"), and if ls-tree defaulted to something, the
> script will silently produce a wrong result, instead of failing,
> robbing the script writer a chance to notice a bug in their code to
> come up with the TREE object name.
I think this illustrative example (at least the general language of it,
if not the entire example) could be added to the referenced section of
our docs (I'd be happy to add this).
On a related note, does it make sense to explicitly call out the
plumbing vs porcelain distinction in the individual manpages for each
command? For example, I grepped for "plumb" in the docs for
"git-ls-tree" but came up empty. We could add just a simple "Part of
Git plumbing" to a slightly longer
Part of Git plumbing. Plumbing commands are more stable and are
designed to work reliably in scripting environments ...
and similarly for all the porecelain commands.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-08-11 18:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-07 12:49 [PATCH] ls-tree: default <tree-ish> to HEAD Ryan Williams via GitGitGadget
2023-08-07 16:25 ` Junio C Hamano
2023-08-11 17:35 ` Taylor Blau
2023-08-11 18:22 ` Linus Arver
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).