git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/1] ls-tree.c: support `--oid-only` option for "git-ls-tree"

From
PBPeter Baumann <peter.baumann@gmail.com>
Date
Nov 22, 2021, 18:11 UTC
Message-ID
<CAJm9OHd_MDjFqXok663O8bQmvdPCjrQeEHumPLDpxqVC4i4=1Q@mail.gmail.com>
In-Reply-To
<6c15b4c176b7c03072fa59a4efd9f6fea7d62eae.1637567328.git.dyroneteng@gmail.com>

[ Sorry if you receive this mail twice, it seems like it didn't get through the first time. ]

On Mon, Nov 22, 2021 at 9:50 AM Teng Long <dyroneteng@gmail.com> wrote:
Show 30 quoted lines
>
> Sometimes, we only want to get the objects from output of `ls-tree`
> and commands like `sed` or `cut` is usually used to intercept the
> origin output to achieve this purpose in practical.
>
> This commit supply an option names `--oid-only` to let `git ls-tree`
> only print out the OID of the object. `--oid-only` and `--name-only`
> are mutually exclusive in use.
>
> Signed-off-by: Teng Long <dyroneteng@gmail.com>
> ---
>  Documentation/git-ls-tree.txt |  8 +++++--
>  builtin/ls-tree.c             | 27 ++++++++++++++++-------
>  t/t3104-ls-tree-oid.sh        | 40 +++++++++++++++++++++++++++++++++++
>  3 files changed, 65 insertions(+), 10 deletions(-)
>  create mode 100755 t/t3104-ls-tree-oid.sh
>
> diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
> index db02d6d79a..bc711dc00a 100644
> --- a/Documentation/git-ls-tree.txt
> +++ b/Documentation/git-ls-tree.txt
> @@ -10,7 +10,8 @@ SYNOPSIS
>  --------
>  [verse]
>  'git ls-tree' [-d] [-r] [-t] [-l] [-z]
> -           [--name-only] [--name-status] [--full-name] [--full-tree] [--abbrev[=<n>]]
> +           [--name-only] [--name-status] [--oid-only]
> +           [--full-name] [--full-tree] [--abbrev[=<n>]]
>             <tree-ish> [<path>...]
>
Shouldn't the synopsis also indicate that the options are exclusive, e.g.
  [--name-only | --oid-only]  ?
Besides adding the new --oid-only mode, you also add one letter acronyms for
  [-n | --name-only]
  [-s | --name-status ]
and one letter abbreviation
  [-o | --oid-only ]
which are all undocumented in the help page. If we want the short one
letter version,
they should be documented. For me, it is at least questionable why we
introduce them
and more so in a commit adding --oid-only.
Show 93 quoted lines
>  DESCRIPTION
> @@ -59,7 +60,10 @@ OPTIONS
>  --name-only::
>  --name-status::
>         List only filenames (instead of the "long" output), one per line.
> -
> +       Cannot be used with `--oid-only` together.
> +--oid-only::
> +       List only OIDs of the objects, one per line. Cannot be used with
> +       `--name-only` or `--name-status` together.
>  --abbrev[=<n>]::
>         Instead of showing the full 40-byte hexadecimal object
>         lines, show the shortest prefix that is at least '<n>'
> diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
> index 3a442631c7..1e4a82e669 100644
> --- a/builtin/ls-tree.c
> +++ b/builtin/ls-tree.c
> @@ -18,19 +18,26 @@ static int line_termination = '\n';
>  #define LS_RECURSIVE 1
>  #define LS_TREE_ONLY 2
>  #define LS_SHOW_TREES 4
> -#define LS_NAME_ONLY 8
> -#define LS_SHOW_SIZE 16
> +#define LS_SHOW_SIZE 8
>  static int abbrev;
>  static int ls_options;
>  static struct pathspec pathspec;
>  static int chomp_prefix;
>  static const char *ls_tree_prefix;
>
> -static const  char * const ls_tree_usage[] = {
> +static const char * const ls_tree_usage[] = {
>         N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
>         NULL
>  };
>
> +enum {
> +       MODE_UNSPECIFIED = 0,
> +       MODE_NAME_ONLY,
> +       MODE_OID_ONLY
> +};
> +
> +static int cmdmode = MODE_UNSPECIFIED;
> +
>  static int show_recursive(const char *base, int baselen, const char *pathname)
>  {
>         int i;
> @@ -90,7 +97,12 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
>         else if (ls_options & LS_TREE_ONLY)
>                 return 0;
>
> -       if (!(ls_options & LS_NAME_ONLY)) {
> +       if (cmdmode == 2) {
> +               printf("%s\n", find_unique_abbrev(oid, abbrev));
> +               return 0;
> +       }
> +
> +       if (cmdmode == 0) {
>                 if (ls_options & LS_SHOW_SIZE) {
>                         char size_text[24];
>                         if (!strcmp(type, blob_type)) {
> @@ -135,10 +147,9 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
>                             N_("terminate entries with NUL byte"), 0),
>                 OPT_BIT('l', "long", &ls_options, N_("include object size"),
>                         LS_SHOW_SIZE),
> -               OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
> -                       LS_NAME_ONLY),
> -               OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
> -                       LS_NAME_ONLY),
> +               OPT_CMDMODE('n', "name-only", &cmdmode, N_("list only filenames"), MODE_NAME_ONLY),
> +               OPT_CMDMODE('s', "name-status", &cmdmode, N_("list only filenames"), MODE_NAME_ONLY),
> +               OPT_CMDMODE('o', "oid-only", &cmdmode, N_("list only oids"), MODE_OID_ONLY),
>                 OPT_SET_INT(0, "full-name", &chomp_prefix,
>                             N_("use full path names"), 0),
>                 OPT_BOOL(0, "full-tree", &full_tree,
> diff --git a/t/t3104-ls-tree-oid.sh b/t/t3104-ls-tree-oid.sh
> new file mode 100755
> index 0000000000..4c02cdd3c3
> --- /dev/null
> +++ b/t/t3104-ls-tree-oid.sh
> @@ -0,0 +1,40 @@
> +#!/bin/sh
> +
> +test_description='git ls-tree oids handling.'
> +
> +. ./test-lib.sh
> +
> +test_expect_success 'setup' '
> +       echo 111 >1.txt &&
> +       echo 222 >2.txt &&
> +       mkdir -p path0/a/b/c &&
> +       echo 333 >path0/a/b/c/3.txt &&
> +       find *.txt path* \( -type f -o -type l \) -print |

I don't see the test using any symbolic links. Why are we searching for symbolic links with "-type l" here?

-Peter
> +       xargs git update-index --add &&
> +       tree=$(git write-tree) &&
> +       echo $tree
> +'
Show 27 quoted lines
> +
> +test_expect_success 'usage: --oid-only' '
> +       git ls-tree --oid-only $tree >current &&
> +       git ls-tree $tree | awk "{print \$3}" >expected &&
> +       test_cmp current expected
> +'
> +
> +test_expect_success 'usage: --oid-only with -r' '
> +       git ls-tree --oid-only -r $tree >current &&
> +       git ls-tree -r $tree | awk "{print \$3}" >expected &&
> +       test_cmp current expected
> +'
> +
> +test_expect_success 'usage: --oid-only with --abbrev' '
> +       git ls-tree --oid-only --abbrev=6 $tree >current &&
> +       git ls-tree --abbrev=6 $tree | awk "{print \$3}" > expected &&
> +       test_cmp current expected
> +'
> +
> +test_expect_failure 'usage: incompatible options: --name-only with --oid-only' '
> +       test_incompatible_usage git ls-tree --oid-only --name-only
> +'
> +
> +test_done
> --
> 2.33.1.10.g438dd9044d.dirty
>
Previous: Teng LongNext: Junio C Hamano
Message 26 of 55 in “support `--oid-only` in `ls-tree`”
  1. 0/3 support `--oid-only` in `ls-tree`Teng Long, Nov 15, 2021
  2. 1/3 ls-tree.c: support `--oid-only` option for "git-ls-tree"Teng Long, Nov 15, 2021
  3. Ævar Arnfjörð BjarmasonNov 15, 2021
  4. Teng LongNov 18, 2021
  5. Ævar Arnfjörð BjarmasonNov 18, 2021
  6. Jeff KingNov 15, 2021
  7. Jeff KingNov 15, 2021
  8. Teng LongNov 18, 2021
  9. 2/3 t3104: add related tests for `--oid-only` optionTeng Long, Nov 15, 2021
  10. Đoàn Trần Công DanhNov 15, 2021
  11. Teng LongNov 18, 2021
  12. 3/3 git-ls-tree.txt: description of the 'oid-only' optionTeng Long, Nov 15, 2021
  13. Ævar Arnfjörð BjarmasonNov 15, 2021
  14. Jeff KingNov 15, 2021
  15. Ævar Arnfjörð BjarmasonNov 15, 2021
  16. Teng LongNov 19, 2021
  17. Jeff KingNov 15, 2021
  18. 0/1 support `--oid-only` in `ls-tree`Teng Long, Nov 19, 2021
  19. 1/1 ls-tree.c: support `--oid-only` option for "git-ls-tree"Teng Long, Nov 19, 2021
  20. Ævar Arnfjörð BjarmasonNov 19, 2021
  21. Junio C HamanoNov 19, 2021
  22. Teng LongNov 22, 2021
  23. Ævar Arnfjörð BjarmasonNov 22, 2021
  24. 0/1 ls-tree.c: support `--oid-only` optionTeng Long, Nov 22, 2021
  25. 1/1 ls-tree.c: support `--oid-only` option for "git-ls-tree"Teng Long, Nov 22, 2021
  26. Peter BaumannNov 22, 2021
  27. Junio C HamanoNov 22, 2021
  28. Ævar Arnfjörð BjarmasonNov 23, 2021
  29. Junio C HamanoNov 23, 2021
  30. Ævar Arnfjörð BjarmasonNov 23, 2021
  31. Junio C HamanoNov 23, 2021
  32. Junio C HamanoNov 23, 2021
  33. Ævar Arnfjörð BjarmasonNov 23, 2021
  34. Đoàn Trần Công DanhNov 23, 2021
  35. 0/1 ls-tree.c: support `--oid-only` optionTeng Long, Nov 23, 2021
  36. 1/1 ls-tree.c: support `--oid-only` option for "git-ls-tree"Teng Long, Nov 23, 2021
  37. Junio C HamanoNov 23, 2021
  38. Teng LongDec 6, 2021
  39. 0/1 support `--object-only` option for "git-ls-tree"Teng Long, Dec 8, 2021
  40. 1/1 ls-tree.c: support `--object-only` option for "git-ls-tree"Teng Long, Dec 8, 2021
  41. Junio C HamanoDec 15, 2021
  42. Teng LongDec 16, 2021
  43. Junio C HamanoDec 16, 2021
  44. Ævar Arnfjörð BjarmasonDec 16, 2021
  45. 0/1 support `--object-only` option for "git-ls-tree"Teng Long, Dec 17, 2021
  46. 1/1 ls-tree.c: support `--object-only` option for "git-ls-tree"Teng Long, Dec 17, 2021
  47. Ævar Arnfjörð BjarmasonDec 17, 2021
  48. 0/7 ls-tree --formatÆvar Arnfjörð Bjarmason, Dec 17, 2021
  49. 1/7 ls-tree: remove commented-out codeÆvar Arnfjörð Bjarmason, Dec 17, 2021
  50. 2/7 ls-tree: add missing braces to "else" armsÆvar Arnfjörð Bjarmason, Dec 17, 2021
  51. 4/7 ls-tree: use "size_t", not "int" for "struct strbuf"'s "len"Ævar Arnfjörð Bjarmason, Dec 17, 2021
  52. 3/7 ls-tree: use "enum object_type", not {blob,tree,commit}_typeÆvar Arnfjörð Bjarmason, Dec 17, 2021
  53. 6/7 ls-tree: add a --format=<fmt> optionÆvar Arnfjörð Bjarmason, Dec 17, 2021
  54. 5/7 ls-tree: split up the "init" part of show_tree()Ævar Arnfjörð Bjarmason, Dec 17, 2021
  55. 7/7 ls-tree.c: support `--object-only` option for "git-ls-tree"Ævar Arnfjörð Bjarmason, Dec 17, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.