Message ID | pull.1566.git.1691412557518.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | ls-tree: default <tree-ish> to HEAD | expand |
"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.
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
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.
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)" '