{"thread":{"id":"58016","subject":"[PATCH 0/2] ls-files: introduce \"--format\" and \"--object-only\" options","startedAt":"2022-06-15T13:46:03Z","lastAt":"2022-09-11T11:01:20Z","messageCount":61,"participants":["ZheNing Hu via GitGitGadget","Ævar Arnfjörð Bjarmason","ZheNing Hu","Phillip Wood","Junio C Hamano","Torsten Bögershausen","Eric Sunshine","Jiang Xin"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"457281","messageId":"pull.1262.git.1655300752.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":null,"subject":"[PATCH 0/2] ls-files: introduce \"--format\" and \"--object-only\" options","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-15T13:45:50Z","receivedAt":"2022-06-15T13:46:03Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Sometime we may need to extract some custom informations from git index\nentries. Add a new option \"--format\" to \"git ls-files\" which can do such\nthing, and add a new option \"--object-only\" which alias to\n\"--format=%(objectname)\".\n\nThe origin discussion is here:\nhttps://lore.kernel.org/git/pull.1250.v2.git.1654778272871.gitgitgadget@gmail.com/\n\nZheNing Hu (2):\n  ls-files: introduce \"--format\" option\n  ls-files: introduce \"--object-only\" option\n\n Documentation/git-ls-files.txt |  59 ++++++++++-\n builtin/ls-files.c             | 160 +++++++++++++++++++++++++++++-\n t/t3013-ls-files-format.sh     | 176 +++++++++++++++++++++++++++++++++\n 3 files changed, 390 insertions(+), 5 deletions(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n-- \ngitgitgadget\n"},{"id":"457282","messageId":"432d80b8c78355e5f70081c05e9a042a9ffcd033.1655300752.git.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.git.1655300752.gitgitgadget@gmail.com","subject":"[PATCH 1/2] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-15T13:45:51Z","receivedAt":"2022-06-15T13:46:07Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, --resolve-undo,\n--deduplicate, --debug.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-ls-files.txt |  51 +++++++++++-\n builtin/ls-files.c             | 126 ++++++++++++++++++++++++++++-\n t/t3013-ls-files-format.sh     | 142 +++++++++++++++++++++++++++++++++\n 3 files changed, 315 insertions(+), 4 deletions(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..b22860ec8c0 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates %(fieldname) from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n+\t`--debug`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+tag::\n+\tThe tag of file status.\n+objectmode::\n+\tThe mode of the object.\n+objectname::\n+\tThe name of the object.\n+stage::\n+\tThe stage of the file.\n+eol::\n+\tThe line endings of files.\n+path::\n+\tThe pathname of the object.\n+ctime::\n+\tThe create time of file.\n+mtime::\n+\tThe modify time of file.\n+dev::\n+\tThe ID of device containing file.\n+ino::\n+\tThe inode number of file.\n+uid::\n+\tThe user id of file owner.\n+gid::\n+\tThe group id of file owner.\n+size::\n+\tThe size of the file.\n+flags::\n+\tThe flags of the file.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..9dd6c55eeb9 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -58,8 +60,8 @@ static const char *tag_modified = \"\";\n static const char *tag_skip_worktree = \"\";\n static const char *tag_resolve_undo = \"\";\n \n-static void write_eolinfo(struct index_state *istate,\n-\t\t\t  const struct cache_entry *ce, const char *path)\n+static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t   const struct cache_entry *ce, const char *path)\n {\n \tif (show_eol) {\n \t\tstruct stat st;\n@@ -71,10 +73,25 @@ static void write_eolinfo(struct index_state *istate,\n \t\t\t\t\t\t\t       ce->name);\n \t\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n \t\t\tw_txt = get_wt_convert_stats_ascii(path);\n-\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\tif (sb)\n+\t\t\tstrbuf_addf(sb, \"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\telse\n+\t\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n \t}\n }\n \n+static void write_eolinfo(struct index_state *istate,\n+\t\t\t  const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(NULL, istate, ce, path);\n+}\n+\n+static void write_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(sb, istate, ce, path);\n+}\n+\n static void write_name(const char *name)\n {\n \t/*\n@@ -85,6 +102,16 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tname = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator) {\n+\t\tquote_c_style(name, sb, NULL, 0);\n+\t} else {\n+\t\tstrbuf_add(sb, name, strlen(name));\n+\t}\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +249,86 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *tag;\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tunsigned int errlen;\n+\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s' does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(tag)\", &p)) {\n+\t\tstrbuf_addstr(sb, get_tag(data->ce, data->tag));\n+\t} else if (skip_prefix(start, \"(objectmode)\", &p)) {\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\t} else if (skip_prefix(start, \"(objectname)\", &p)) {\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\t} else if (skip_prefix(start, \"(stage)\", &p)) {\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\t} else if (skip_prefix(start, \"(eol)\", &p)) {\n+\t\twrite_eolinfo_to_buf(sb, data->istate, data->ce, data->pathname);\n+\t} else if (skip_prefix(start, \"(path)\", &p)) {\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\t} else if (skip_prefix(start, \"(ctime)\", &p)) {\n+\t\tstrbuf_addf(sb, \"ctime: %u:%u\", sd->sd_ctime.sec, sd->sd_ctime.nsec);\n+\t} else if (skip_prefix(start, \"(mtime)\", &p)) {\n+\t\tstrbuf_addf(sb, \"mtime: %u:%u\", sd->sd_mtime.sec, sd->sd_mtime.nsec);\n+\t} else if (skip_prefix(start, \"(dev)\", &p)) {\n+\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n+\t} else if (skip_prefix(start, \"(ino)\", &p)) {\n+\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n+\t} else if (skip_prefix(start, \"(uid)\", &p)) {\n+\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n+\t} else if (skip_prefix(start, \"(gid)\", &p)) {\n+\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n+\t} else if (skip_prefix(start, \"(size)\", &p)) {\n+\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n+\t} else if (skip_prefix(start, \"(flags)\", &p)) {\n+\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n+\t} else {\n+\t\terrlen = (unsigned long)len;\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n+\t}\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname, const char *tag) {\n+\n+\tstruct show_index_data data = {\n+\t\t.tag = tag,\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+\treturn;\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +343,11 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname, tag);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +787,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t\t\t N_(\"format to use for the output\"),\n+\t\t\t\t\t PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +814,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n+\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..61a2e68713a\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,142 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format tag' '\n+\tprintf \"H \\nH \\n\" >expect &&\n+\tgit ls-files --format=\"%(tag)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eol' '\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n+\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format ctime' '\n+\tgit ls-files --debug | grep ctime >expect &&\n+\tgit ls-files --format=\"  %(ctime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format mtime' '\n+\tgit ls-files --debug | grep mtime >expect &&\n+\tgit ls-files --format=\"  %(mtime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format dev and ino' '\n+\tgit ls-files --debug | grep dev >expect &&\n+\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format uid and gid' '\n+\tgit ls-files --debug | grep uid >expect &&\n+\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\trm o1 &&\n+\ttest_when_finished \"git restore o1\" &&\n+\tcat >expect <<-EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format size and flags' '\n+\tgit ls-files --debug | grep size >expect &&\n+\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)%x0a  %(ctime)%x0a  %(mtime)%x0a  %(dev)%x09%(ino)%x0a  %(uid)%x09%(gid)%x0a  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -s must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" -s\n+'\n+\n+test_expect_success 'git ls-files --format with -o must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" -o\n+'\n+\n+test_expect_success 'git ls-files --format with -k must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" -k\n+'\n+\n+test_expect_success 'git ls-files --format with --resolve-undo must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" --resolve-undo\n+'\n+\n+test_expect_success 'git ls-files --format with --deduplicate must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" --deduplicate\n+'\n+\n+test_expect_success 'git ls-files --format with --debug must fail' '\n+\ttest_must_fail git ls-files --format=\"%(objectname)\" --debug\n+'\n+\n+test_done\n-- \ngitgitgadget\n\n"},{"id":"457283","messageId":"81ae1280e8eb471c7a11dceb0aa7a8915948b2ce.1655300752.git.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.git.1655300752.gitgitgadget@gmail.com","subject":"[PATCH 2/2] ls-files: introduce \"--object-only\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-15T13:45:52Z","receivedAt":"2022-06-15T13:46:08Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\n--object-only is an alias for --format=%(objectname),\nwhich output objectname of index entries, taking\ninspiration from the option with the same name in\nthe `git ls-tree` command.\n\n--object-only cannot be used with --format, and -s, -o,\n-k, --resolve-undo, --deduplicate, --debug.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-ls-files.txt |  8 +++++++-\n builtin/ls-files.c             | 36 +++++++++++++++++++++++++++++++++-\n t/t3013-ls-files-format.sh     | 34 ++++++++++++++++++++++++++++++++\n 3 files changed, 76 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex b22860ec8c0..c3f46bb821b 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -13,7 +13,7 @@ SYNOPSIS\n \t\t[-c|--cached] [-d|--deleted] [-o|--others] [-i|--|ignored]\n \t\t[-s|--stage] [-u|--unmerged] [-k|--|killed] [-m|--modified]\n \t\t[--directory [--no-empty-directory]] [--eol]\n-\t\t[--deduplicate]\n+\t\t[--deduplicate] [--object-only]\n \t\t[-x <pattern>|--exclude=<pattern>]\n \t\t[-X <file>|--exclude-from=<file>]\n \t\t[--exclude-per-directory=<file>]\n@@ -199,6 +199,12 @@ followed by the  (\"attr/<eolattr>\").\n \tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n \t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n \t`--debug`.\n+\n+--object-only::\n+\tList only names of the objects, one per line. This is equivalent\n+\tto specifying `--format='%(objectname)'`. Cannot be combined with\n+\t`--format=<format>`.\n+\n \\--::\n \tDo not interpret any more arguments as options.\n \ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 9dd6c55eeb9..4ac8f34baac 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -60,6 +60,27 @@ static const char *tag_modified = \"\";\n static const char *tag_skip_worktree = \"\";\n static const char *tag_resolve_undo = \"\";\n \n+static enum ls_files_cmdmode {\n+\tMODE_DEFAULT = 0,\n+\tMODE_OBJECT_ONLY,\n+} ls_files_cmdmode;\n+\n+struct ls_files_cmdmodee_to_fmt {\n+\tenum ls_files_cmdmode mode;\n+\tconst char *const fmt;\n+};\n+\n+static struct ls_files_cmdmodee_to_fmt ls_files_cmdmode_format[] = {\n+\t{\n+\t\t.mode = MODE_DEFAULT,\n+\t\t.fmt = NULL,\n+\t},\n+\t{\n+\t\t.mode = MODE_OBJECT_ONLY,\n+\t\t.fmt = \"%(objectname)\",\n+\t},\n+};\n+\n static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n \t\t\t\t   const struct cache_entry *ce, const char *path)\n {\n@@ -747,6 +768,8 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\tDIR_SHOW_IGNORED),\n \t\tOPT_BOOL('s', \"stage\", &show_stage,\n \t\t\tN_(\"show staged contents' object name in the output\")),\n+\t\tOPT_CMDMODE(0, \"object-only\", &ls_files_cmdmode, N_(\"list only objects\"),\n+\t\t\t    MODE_OBJECT_ONLY),\n \t\tOPT_BOOL('k', \"killed\", &show_killed,\n \t\t\tN_(\"show files on the filesystem that need to be removed\")),\n \t\tOPT_BIT(0, \"directory\", &dir.flags,\n@@ -815,9 +838,20 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n \n+\tif (format && ls_files_cmdmode)\n+\t\tdie(_(\"--format can't be combined with other format-altering options\"));\n+\n+\tfor (i = 0; !format && i < ARRAY_SIZE(ls_files_cmdmode_format); i++) {\n+\t\tif (ls_files_cmdmode == ls_files_cmdmode_format[i].mode) {\n+\t\t\tformat = ls_files_cmdmode_format[i].fmt;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n \tif (format && (show_stage || show_others || show_killed ||\n \t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n-\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n+\t\tdie(_(\"ls-files --format or other format-altering options \"\n+\t\t      \"cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n \n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nindex 61a2e68713a..1c982ea13e0 100755\n--- a/t/t3013-ls-files-format.sh\n+++ b/t/t3013-ls-files-format.sh\n@@ -139,4 +139,38 @@ test_expect_success 'git ls-files --format with --debug must fail' '\n \ttest_must_fail git ls-files --format=\"%(objectname)\" --debug\n '\n \n+test_expect_success 'git ls-files --object-only equal to --format=%(objectname)' '\n+\tgit ls-files --format=\"%(objectname)\" >expect &&\n+\tgit ls-files --object-only >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --object-only with --format must fail' '\n+\ttest_must_fail git ls-files --format=\"%(path)\" --object-only\n+'\n+\n+test_expect_success 'git ls-files --object-only with -s must fail' '\n+\ttest_must_fail git ls-files --object-only -s\n+'\n+\n+test_expect_success 'git ls-files --object-only with -o must fail' '\n+\ttest_must_fail git ls-files --object-only -o\n+'\n+\n+test_expect_success 'git ls-files --object-only with -k must fail' '\n+\ttest_must_fail git ls-files --object-only -k\n+'\n+\n+test_expect_success 'git ls-files --object-only with --resolve-undo must fail' '\n+\ttest_must_fail git ls-files --object-only --resolve-undo\n+'\n+\n+test_expect_success 'git ls-files --object-only with --deduplicate must fail' '\n+\ttest_must_fail git ls-files --object-only --deduplicate\n+'\n+\n+test_expect_success 'git ls-files --object-only with --debug must fail' '\n+\ttest_must_fail git ls-files --object-only --debug\n+'\n+\n test_done\n-- \ngitgitgadget\n"},{"id":"457311","messageId":"220615.86a6ady983.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"432d80b8c78355e5f70081c05e9a042a9ffcd033.1655300752.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/2] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-06-15T20:07:17Z","receivedAt":"2022-06-15T20:15:16Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Wed, Jun 15 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n\nThanks a lot for pursuing this, this looks good & is much smaller than I\nthought, just some nits below:\n\n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n>\n> --format cannot used with -s, -o, -k, --resolve-undo,\n> --deduplicate, --debug.\n>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>  Documentation/git-ls-files.txt |  51 +++++++++++-\n>  builtin/ls-files.c             | 126 ++++++++++++++++++++++++++++-\n>  t/t3013-ls-files-format.sh     | 142 +++++++++++++++++++++++++++++++++\n>  3 files changed, 315 insertions(+), 4 deletions(-)\n>  create mode 100755 t/t3013-ls-files-format.sh\n>\n> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> index 0dabf3f0ddc..b22860ec8c0 100644\n> --- a/Documentation/git-ls-files.txt\n> +++ b/Documentation/git-ls-files.txt\n> @@ -20,7 +20,7 @@ SYNOPSIS\n>  \t\t[--exclude-standard]\n>  \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n>  \t\t[--full-name] [--recurse-submodules]\n> -\t\t[--abbrev[=<n>]] [--] [<file>...]\n> +\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n>  \n>  DESCRIPTION\n>  -----------\n> @@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n>  \tto the contained files. Sparse directories will be shown with a\n>  \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n>  \n> +--format=<format>::\n> +\tA string that interpolates %(fieldname) from the result being shown.\n\nMissing `` for %(fieldname) ?\n> +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n> +\tinterpolates to character with hex code `xx`; for example `%00`\n> +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n> +\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n\nReplace that last \",\" with \" or \".\n\n> +\t`--debug`.\n>  \\--::\n>  \tDo not interpret any more arguments as options.\n>  \n> @@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n>  (see linkgit:git-config[1]).  Using `-z` the filename is output\n>  verbatim and the line is terminated by a NUL byte.\n>  \n> +It is possible to print in a custom format by using the `--format`\n> +option, which is able to interpolate different fields using\n> +a `%(fieldname)` notation. For example, if you only care about the\n> +\"objectname\" and \"path\" fields, you can execute with a specific\n> +\"--format\" like\n> +\n> +\tgit ls-files --format='%(objectname) %(path)'\n> +\n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +tag::\n> +\tThe tag of file status.\n> +objectmode::\n> +\tThe mode of the object.\n> +objectname::\n> +\tThe name of the object.\n> +stage::\n> +\tThe stage of the file.\n> +eol::\n> +\tThe line endings of files.\n> +path::\n> +\tThe pathname of the object.\n> +ctime::\n> +\tThe create time of file.\n> +mtime::\n> +\tThe modify time of file.\n> +dev::\n> +\tThe ID of device containing file.\n> +ino::\n> +\tThe inode number of file.\n> +uid::\n> +\tThe user id of file owner.\n> +gid::\n> +\tThe group id of file owner.\n> +size::\n> +\tThe size of the file.\n> +flags::\n> +\tThe flags of the file.\n>  \n>  EXCLUDE PATTERNS\n>  ----------------\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index e791b65e7e9..9dd6c55eeb9 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -11,6 +11,7 @@\n>  #include \"quote.h\"\n>  #include \"dir.h\"\n>  #include \"builtin.h\"\n> +#include \"strbuf.h\"\n>  #include \"tree.h\"\n>  #include \"cache-tree.h\"\n>  #include \"parse-options.h\"\n> @@ -48,6 +49,7 @@ static char *ps_matched;\n>  static const char *with_tree;\n>  static int exc_given;\n>  static int exclude_args;\n> +static const char *format;\n>  \n>  static const char *tag_cached = \"\";\n>  static const char *tag_unmerged = \"\";\n> @@ -58,8 +60,8 @@ static const char *tag_modified = \"\";\n>  static const char *tag_skip_worktree = \"\";\n>  static const char *tag_resolve_undo = \"\";\n>  \n> -static void write_eolinfo(struct index_state *istate,\n> -\t\t\t  const struct cache_entry *ce, const char *path)\n> +static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n> +\t\t\t\t   const struct cache_entry *ce, const char *path)\n>  {\n>  \tif (show_eol) {\n>  \t\tstruct stat st;\n> @@ -71,10 +73,25 @@ static void write_eolinfo(struct index_state *istate,\n>  \t\t\t\t\t\t\t       ce->name);\n>  \t\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n>  \t\t\tw_txt = get_wt_convert_stats_ascii(path);\n> -\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n> +\t\tif (sb)\n> +\t\t\tstrbuf_addf(sb, \"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n> +\t\telse\n> +\t\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n>  \t}\n>  }\n>  \n> +static void write_eolinfo(struct index_state *istate,\n> +\t\t\t  const struct cache_entry *ce, const char *path)\n> +{\n> +\twrite_eolinfo_internal(NULL, istate, ce, path);\n> +}\n> +\n> +static void write_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n> +\t\t\t\t const struct cache_entry *ce, const char *path)\n> +{\n> +\twrite_eolinfo_internal(sb, istate, ce, path);\n> +}\n> +\n>  static void write_name(const char *name)\n>  {\n>  \t/*\n> @@ -85,6 +102,16 @@ static void write_name(const char *name)\n>  \t\t\t\t   stdout, line_terminator);\n>  }\n>  \n> +static void write_name_to_buf(struct strbuf *sb, const char *name)\n> +{\n> +\tname = relative_path(name, prefix_len ? prefix : NULL, sb);\nFWIW I'd find this a bit less \"huh?\" if we declared another variable\nhere, so just:\n\n\tconst char *rel = relative_path(name, ...).\n\n> +\tif (line_terminator) {\n> +\t\tquote_c_style(name, sb, NULL, 0);\n> +\t} else {\n> +\t\tstrbuf_add(sb, name, strlen(name));\n> +\t}\n\nCan drop the {} braces here for if/else, see CodingGuidelines.\n\n> +}\n> +\n>  static const char *get_tag(const struct cache_entry *ce, const char *tag)\n>  {\n>  \tstatic char alttag[4];\n> @@ -222,6 +249,86 @@ static void show_submodule(struct repository *superproject,\n>  \trepo_clear(&subrepo);\n>  }\n>  \n> +struct show_index_data {\n> +\tconst char *tag;\n> +\tconst char *pathname;\n> +\tstruct index_state *istate;\n> +\tconst struct cache_entry *ce;\n> +};\n> +\n> +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> +\t\t\t       void *context)\n> +{\n> +\tstruct show_index_data *data = context;\n> +\tconst char *end;\n> +\tconst char *p;\n> +\tunsigned int errlen;\n> +\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n> +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> +\tif (len)\n> +\t\treturn len;\n> +\tif (*start != '(')\n> +\t\tdie(_(\"bad ls-files format: element '%s' does not start with '('\"), start);\n> +\n> +\tend = strchr(start + 1, ')');\n> +\tif (!end)\n> +\t\tdie(_(\"bad ls-files format: element '%s' does not end in ')'\"), start);\n> +\n> +\tlen = end - start + 1;\n> +\tif (skip_prefix(start, \"(tag)\", &p)) {\n\nStyle nit, I'd much rather see us drop the {} on the whole if/else if\nchain here, which we can do if...\n\n> +\t\tstrbuf_addstr(sb, get_tag(data->ce, data->tag));\n> +\t} else if (skip_prefix(start, \"(objectmode)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n> +\t} else if (skip_prefix(start, \"(objectname)\", &p)) {\n> +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n> +\t} else if (skip_prefix(start, \"(stage)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n> +\t} else if (skip_prefix(start, \"(eol)\", &p)) {\n> +\t\twrite_eolinfo_to_buf(sb, data->istate, data->ce, data->pathname);\n> +\t} else if (skip_prefix(start, \"(path)\", &p)) {\n> +\t\twrite_name_to_buf(sb, data->pathname);\n> +\t} else if (skip_prefix(start, \"(ctime)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"ctime: %u:%u\", sd->sd_ctime.sec, sd->sd_ctime.nsec);\n> +\t} else if (skip_prefix(start, \"(mtime)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"mtime: %u:%u\", sd->sd_mtime.sec, sd->sd_mtime.nsec);\n\n(too long lines, keep within 79 chars?)\n\n> +\t} else if (skip_prefix(start, \"(dev)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n> +\t} else if (skip_prefix(start, \"(ino)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n> +\t} else if (skip_prefix(start, \"(uid)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n> +\t} else if (skip_prefix(start, \"(gid)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n> +\t} else if (skip_prefix(start, \"(size)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n> +\t} else if (skip_prefix(start, \"(flags)\", &p)) {\n> +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n> +\t} else {\n> +\t\terrlen = (unsigned long)len;\n> +\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n\nWe just line-wrap the \"(unsigned long)len\" here, which seems worth it\nfor less line noise :)\n\n> +\t}\n> +\n> +\treturn len;\n> +}\n> +\n> +static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n> +\t\t\tconst char *format, const char *fullname, const char *tag) {\n> +\n> +\tstruct show_index_data data = {\n> +\t\t.tag = tag,\n> +\t\t.pathname = fullname,\n> +\t\t.istate = repo->index,\n> +\t\t.ce = ce,\n> +\t};\n> +\n> +\tstruct strbuf sb = STRBUF_INIT;\n> +\tstrbuf_expand(&sb, format, expand_show_index, &data);\n> +\tstrbuf_addch(&sb, line_terminator);\n> +\tfwrite(sb.buf, sb.len, 1, stdout);\n> +\tstrbuf_release(&sb);\n> +\treturn;\n> +}\n> +\n>  static void show_ce(struct repository *repo, struct dir_struct *dir,\n>  \t\t    const struct cache_entry *ce, const char *fullname,\n>  \t\t    const char *tag)\n> @@ -236,6 +343,11 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n>  \t\t\t\t  max_prefix_len, ps_matched,\n>  \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n>  \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n> +\t\tif (format) {\n> +\t\t\tshow_ce_fmt(repo, ce, format, fullname, tag);\n> +\t\t\treturn;\n> +\t\t}\n> +\n>  \t\ttag = get_tag(ce, tag);\n>  \n>  \t\tif (!show_stage) {\n> @@ -675,6 +787,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n>  \t\t\t N_(\"suppress duplicate entries\")),\n>  \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n>  \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n> +\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n> +\t\t\t\t\t N_(\"format to use for the output\"),\n> +\t\t\t\t\t PARSE_OPT_NONEG),\n\nOdd indentation?\n\n>  \t\tOPT_END()\n>  \t};\n>  \tint ret = 0;\n> @@ -699,6 +814,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n>  \tfor (i = 0; i < exclude_list.nr; i++) {\n>  \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n>  \t}\n> +\n> +\tif (format && (show_stage || show_others || show_killed ||\n> +\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n> +\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n\nGood to check this.\n\n> +\n>  \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n>  \t\ttag_cached = \"H \";\n>  \t\ttag_unmerged = \"M \";\n> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> new file mode 100755\n> index 00000000000..61a2e68713a\n> --- /dev/null\n> +++ b/t/t3013-ls-files-format.sh\n> @@ -0,0 +1,142 @@\n> +#!/bin/sh\n> +\n> +test_description='git ls-files --format test'\n> +\n> +. ./test-lib.sh\n> +\n> +test_expect_success 'setup' '\n> +\techo o1 >o1 &&\n> +\techo o2 >o2 &&\n> +\tgit add o1 o2 &&\n> +\tgit add --chmod +x o1 &&\n> +\tgit commit -m base\n> +'\n> +\n> +test_expect_success 'git ls-files --format tag' '\n> +\tprintf \"H \\nH \\n\" >expect &&\n> +\tgit ls-files --format=\"%(tag)\" -t >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format objectmode' '\n> +\tcat >expect <<-EOF &&\n> +\t100755\n> +\t100644\n> +\tEOF\n> +\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format objectname' '\n> +\toid1=$(git hash-object o1) &&\n> +\toid2=$(git hash-object o2) &&\n> +\tcat >expect <<-EOF &&\n> +\t$oid1\n> +\t$oid2\n> +\tEOF\n> +\tgit ls-files --format=\"%(objectname)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format eol' '\n> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n> +\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format path' '\n> +\tcat >expect <<-EOF &&\n> +\to1\n> +\to2\n> +\tEOF\n> +\tgit ls-files --format=\"%(path)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format ctime' '\n> +\tgit ls-files --debug | grep ctime >expect &&\n\nFor this and the rest: don't put git on the left-hand-side of a \"|\", it\nhides its exit code (and potential segfaults)>.\n\nInstead e.g.:\n\n    git ... >out &&\n    grep ctime out >expect &&\n    ...\n\n> +\tgit ls-files --format=\"  %(ctime)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format mtime' '\n> +\tgit ls-files --debug | grep mtime >expect &&\n\nditto here & below.\n\n> +\tgit ls-files --format=\"  %(mtime)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format dev and ino' '\n> +\tgit ls-files --debug | grep dev >expect &&\n> +\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format uid and gid' '\n> +\tgit ls-files --debug | grep uid >expect &&\n> +\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -m' '\n> +\techo change >o1 &&\n> +\tcat >expect <<-EOF &&\n\nWhen not using varibales use <<-\\EOF, applies for the rest.\n\n> +\to1\n> +\tEOF\n> +\tgit ls-files --format=\"%(path)\" -m >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -d' '\n> +\trm o1 &&\n\nDon't \"rm o1\" here, rather have the test that creates it do:\n\n    test_when_finished \"rm o1\" &&\n    [the command that creates o1]\n\n> +\ttest_when_finished \"git restore o1\" &&\n> +\tcat >expect <<-EOF &&\n> +\to1\n> +\tEOF\n> +\tgit ls-files --format=\"%(path)\" -d >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format size and flags' '\n> +\tgit ls-files --debug | grep size >expect &&\n> +\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format imitate --stage' '\n> +\tgit ls-files --stage >expect &&\n> +\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format imitate --debug' '\n> +\tgit ls-files --debug >expect &&\n> +\tgit ls-files --format=\"%(path)%x0a  %(ctime)%x0a  %(mtime)%x0a  %(dev)%x09%(ino)%x0a  %(uid)%x09%(gid)%x0a  %(size)%x09%(flags)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n\nThese tests...:\n\n> +test_expect_success 'git ls-files --format with -s must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" -s\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -o must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" -o\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -k must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" -k\n> +'\n> +\n> +test_expect_success 'git ls-files --format with --resolve-undo must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" --resolve-undo\n> +'\n> +\n> +test_expect_success 'git ls-files --format with --deduplicate must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" --deduplicate\n> +'\n> +\n> +test_expect_success 'git ls-files --format with --debug must fail' '\n> +\ttest_must_fail git ls-files --format=\"%(objectname)\" --debug\n> +'\n\n...would be better done with a for-loop, so:\n\n\tfor flag in -s -o -k --resolve-undo [...]\n\tdo\n\t\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n\t\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n\t\t'\n\tdone\n\nNote the '' on the second argument, that's intentional, as we eval it\nyou don't need \"\".\n\n> +test_done\n\n"},{"id":"457313","messageId":"220615.865yl1y8qh.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"81ae1280e8eb471c7a11dceb0aa7a8915948b2ce.1655300752.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 2/2] ls-files: introduce \"--object-only\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-06-15T20:15:45Z","receivedAt":"2022-06-15T20:25:47Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Wed, Jun 15 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> --object-only is an alias for --format=%(objectname),\n> which output objectname of index entries, taking\n> inspiration from the option with the same name in\n> the `git ls-tree` command.\n>\n> --object-only cannot be used with --format, and -s, -o,\n> -k, --resolve-undo, --deduplicate, --debug.\n>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>  Documentation/git-ls-files.txt |  8 +++++++-\n>  builtin/ls-files.c             | 36 +++++++++++++++++++++++++++++++++-\n>  t/t3013-ls-files-format.sh     | 34 ++++++++++++++++++++++++++++++++\n>  3 files changed, 76 insertions(+), 2 deletions(-)\n>\n> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> index b22860ec8c0..c3f46bb821b 100644\n> --- a/Documentation/git-ls-files.txt\n> +++ b/Documentation/git-ls-files.txt\n> @@ -13,7 +13,7 @@ SYNOPSIS\n>  \t\t[-c|--cached] [-d|--deleted] [-o|--others] [-i|--|ignored]\n>  \t\t[-s|--stage] [-u|--unmerged] [-k|--|killed] [-m|--modified]\n>  \t\t[--directory [--no-empty-directory]] [--eol]\n> -\t\t[--deduplicate]\n> +\t\t[--deduplicate] [--object-only]\n>  \t\t[-x <pattern>|--exclude=<pattern>]\n>  \t\t[-X <file>|--exclude-from=<file>]\n>  \t\t[--exclude-per-directory=<file>]\n> @@ -199,6 +199,12 @@ followed by the  (\"attr/<eolattr>\").\n>  \tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n>  \t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n>  \t`--debug`.\n> +\n> +--object-only::\n> +\tList only names of the objects, one per line. This is equivalent\n> +\tto specifying `--format='%(objectname)'`. Cannot be combined with\n> +\t`--format=<format>`.\n> +\n>  \\--::\n>  \tDo not interpret any more arguments as options.\n>  \n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index 9dd6c55eeb9..4ac8f34baac 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -60,6 +60,27 @@ static const char *tag_modified = \"\";\n>  static const char *tag_skip_worktree = \"\";\n>  static const char *tag_resolve_undo = \"\";\n>  \n> +static enum ls_files_cmdmode {\n> +\tMODE_DEFAULT = 0,\n> +\tMODE_OBJECT_ONLY,\n> +} ls_files_cmdmode;\n> +\n> +struct ls_files_cmdmodee_to_fmt {\n> +\tenum ls_files_cmdmode mode;\n> +\tconst char *const fmt;\n> +};\n> +\n> +static struct ls_files_cmdmodee_to_fmt ls_files_cmdmode_format[] = {\n> +\t{\n> +\t\t.mode = MODE_DEFAULT,\n> +\t\t.fmt = NULL,\n> +\t},\n> +\t{\n> +\t\t.mode = MODE_OBJECT_ONLY,\n> +\t\t.fmt = \"%(objectname)\",\n> +\t},\n> +};\n[...snip...]\n\nThis code all looks OK from skimming it, and is substantially copied\nfrom builtin/ls-tree.c (which is good).\n\nBut I wonder as in that case whether having such an alias is worth it at\nall, especially since in the case of ls-files (unlike ls-tree) we don't\nstart out with various --just-the-X-field type options, this is the\nfirst one.\n\nSo I *really* like that you took my suggestion of \"why not a --format\"\nfrom a previous round, but given the above for ls-files in particular is\nit really worth it to have this extra code just to type:\n\n    --object-only\n\nInstead of:\n\n    --format=\"%(objectname)\"\n\nSo, maybe, and I'm not set against it, but I think it's worth\nre-evaluating in this case.\n\nIn particular because the part of ls-tree's code is missing here where\nwe \"format optimize\", i.e. we take a form like:\n\n    --format=\"%(objectname)\"\n\nAnd dispatch it to the more optimized special function, instead of the\ngeneric strbuf_expand(), whereas in this case it's the other way around,\nthe option is just an alias for --format.\n"},{"id":"457493","messageId":"CAOLTT8SfRz62jVrdtjsGN=-iBAQzA3w_oJgT++xtx_kbdCmdqQ@mail.gmail.com","threadId":"58016","inReplyTo":"220615.86a6ady983.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH 1/2] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-18T10:50:36Z","receivedAt":"2022-06-18T10:50:51Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年6月16日周四 04:15写道：\n>\n>\n> > +static void write_name_to_buf(struct strbuf *sb, const char *name)\n> > +{\n> > +     name = relative_path(name, prefix_len ? prefix : NULL, sb);\n> FWIW I'd find this a bit less \"huh?\" if we declared another variable\n> here, so just:\n>\n>         const char *rel = relative_path(name, ...).\n>\n\nYeah, It's just a wrong code copy.\n\n> > +     o1\n> > +     EOF\n> > +     git ls-files --format=\"%(path)\" -m >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with -d' '\n> > +     rm o1 &&\n>\n> Don't \"rm o1\" here, rather have the test that creates it do:\n>\n>     test_when_finished \"rm o1\" &&\n>     [the command that creates o1]\n>\n\nI thought about how to test 'git ls-files -d', so maybe I need\nsomething like:\n\ntest_expect_success 'git ls-files --format with -d' '\n     echo o3 >o3 &&\n     git add o3 &&\n     rm o3 &&\n     cat >expect <<-\\EOF &&\n     o3\n     EOF\n     git ls-files --format=\"%(path)\" -d >actual &&\n     test_cmp expect actual\n'\n\n> > +     test_when_finished \"git restore o1\" &&\n> > +     cat >expect <<-EOF &&\n> > +     o1\n> > +     EOF\n> > +     git ls-files --format=\"%(path)\" -d >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n>\n> These tests...:\n>\n> > +test_expect_success 'git ls-files --format with -s must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" -s\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with -o must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" -o\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with -k must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" -k\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with --resolve-undo must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" --resolve-undo\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with --deduplicate must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" --deduplicate\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with --debug must fail' '\n> > +     test_must_fail git ls-files --format=\"%(objectname)\" --debug\n> > +'\n>\n> ...would be better done with a for-loop, so:\n>\n>         for flag in -s -o -k --resolve-undo [...]\n>         do\n>                 test_expect_success \"git ls-files --format is incompatible with $flag\" '\n>                         test_must_fail git ls-files --format=\"%(objectname)\" $flag\n>                 '\n>         done\n>\n\nYeah, using this loop will be clear.\n\n> Note the '' on the second argument, that's intentional, as we eval it\n> you don't need \"\".\n>\n> > +test_done\n>\n\nThanks for all these code style suggestions!\n\nZheNing Hu\n"},{"id":"457494","messageId":"CAOLTT8R35G2dcquO5uXHxnt2NKwgpFZ48ZqmKyiE2yCi0+SLEA@mail.gmail.com","threadId":"58016","inReplyTo":"220615.865yl1y8qh.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH 2/2] ls-files: introduce \"--object-only\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-18T10:59:15Z","receivedAt":"2022-06-18T10:59:46Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年6月16日周四 04:25写道：\n>\n>\n> On Wed, Jun 15 2022, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > --object-only is an alias for --format=%(objectname),\n> > which output objectname of index entries, taking\n> > inspiration from the option with the same name in\n> > the `git ls-tree` command.\n> >\n> > --object-only cannot be used with --format, and -s, -o,\n> > -k, --resolve-undo, --deduplicate, --debug.\n> >\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> > ---\n> >  Documentation/git-ls-files.txt |  8 +++++++-\n> >  builtin/ls-files.c             | 36 +++++++++++++++++++++++++++++++++-\n> >  t/t3013-ls-files-format.sh     | 34 ++++++++++++++++++++++++++++++++\n> >  3 files changed, 76 insertions(+), 2 deletions(-)\n> >\n> > diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> > index b22860ec8c0..c3f46bb821b 100644\n> > --- a/Documentation/git-ls-files.txt\n> > +++ b/Documentation/git-ls-files.txt\n> > @@ -13,7 +13,7 @@ SYNOPSIS\n> >               [-c|--cached] [-d|--deleted] [-o|--others] [-i|--|ignored]\n> >               [-s|--stage] [-u|--unmerged] [-k|--|killed] [-m|--modified]\n> >               [--directory [--no-empty-directory]] [--eol]\n> > -             [--deduplicate]\n> > +             [--deduplicate] [--object-only]\n> >               [-x <pattern>|--exclude=<pattern>]\n> >               [-X <file>|--exclude-from=<file>]\n> >               [--exclude-per-directory=<file>]\n> > @@ -199,6 +199,12 @@ followed by the  (\"attr/<eolattr>\").\n> >       interpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n> >       --format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n> >       `--debug`.\n> > +\n> > +--object-only::\n> > +     List only names of the objects, one per line. This is equivalent\n> > +     to specifying `--format='%(objectname)'`. Cannot be combined with\n> > +     `--format=<format>`.\n> > +\n> >  \\--::\n> >       Do not interpret any more arguments as options.\n> >\n> > diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> > index 9dd6c55eeb9..4ac8f34baac 100644\n> > --- a/builtin/ls-files.c\n> > +++ b/builtin/ls-files.c\n> > @@ -60,6 +60,27 @@ static const char *tag_modified = \"\";\n> >  static const char *tag_skip_worktree = \"\";\n> >  static const char *tag_resolve_undo = \"\";\n> >\n> > +static enum ls_files_cmdmode {\n> > +     MODE_DEFAULT = 0,\n> > +     MODE_OBJECT_ONLY,\n> > +} ls_files_cmdmode;\n> > +\n> > +struct ls_files_cmdmodee_to_fmt {\n> > +     enum ls_files_cmdmode mode;\n> > +     const char *const fmt;\n> > +};\n> > +\n> > +static struct ls_files_cmdmodee_to_fmt ls_files_cmdmode_format[] = {\n> > +     {\n> > +             .mode = MODE_DEFAULT,\n> > +             .fmt = NULL,\n> > +     },\n> > +     {\n> > +             .mode = MODE_OBJECT_ONLY,\n> > +             .fmt = \"%(objectname)\",\n> > +     },\n> > +};\n> [...snip...]\n>\n> This code all looks OK from skimming it, and is substantially copied\n> from builtin/ls-tree.c (which is good).\n>\n> But I wonder as in that case whether having such an alias is worth it at\n> all, especially since in the case of ls-files (unlike ls-tree) we don't\n> start out with various --just-the-X-field type options, this is the\n> first one.\n>\n> So I *really* like that you took my suggestion of \"why not a --format\"\n> from a previous round, but given the above for ls-files in particular is\n> it really worth it to have this extra code just to type:\n>\n>     --object-only\n>\n> Instead of:\n>\n>     --format=\"%(objectname)\"\n>\n> So, maybe, and I'm not set against it, but I think it's worth\n> re-evaluating in this case.\n>\n> In particular because the part of ls-tree's code is missing here where\n> we \"format optimize\", i.e. we take a form like:\n>\n>     --format=\"%(objectname)\"\n>\n> And dispatch it to the more optimized special function, instead of the\n> generic strbuf_expand(), whereas in this case it's the other way around,\n> the option is just an alias for --format.\n\nThanks for clarifying that --object-only uses the fast path instead of a simple\nalias of --format=%(objectname). Maybe I should do this too, or just\ndrop this patch\nbecause git ls-file --format has included such a function :-)\n\nZheNing Hu\n"},{"id":"457530","messageId":"pull.1262.v2.git.1655629990185.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.git.1655300752.gitgitgadget@gmail.com","subject":"[PATCH v2] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-19T09:13:10Z","receivedAt":"2022-06-19T09:14:38Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, --resolve-undo,\n--deduplicate and --debug.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v1->v2:\n    \n     1. do some code style fix suggected by Ævar.\n     2. remove --object-only option (I have tried to use fast path for it,\n        but cannot see any performance promote compare with\n        --format=%(objectname))\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v1:\n\n 1:  432d80b8c78 ! 1:  67f2c3b8ebe ls-files: introduce \"--format\" option\n     @@ Commit message\n          command.\n      \n          --format cannot used with -s, -o, -k, --resolve-undo,\n     -    --deduplicate, --debug.\n     +    --deduplicate and --debug.\n      \n          Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n      \n     @@ Documentation/git-ls-files.txt: followed by the  (\"attr/<eolattr>\").\n       \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n       \n      +--format=<format>::\n     -+\tA string that interpolates %(fieldname) from the result being shown.\n     ++\tA string that interpolates `%(fieldname)` from the result being shown.\n      +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n      +\tinterpolates to character with hex code `xx`; for example `%00`\n      +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n     -+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`,\n     ++\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n      +\t`--debug`.\n       \\--::\n       \tDo not interpret any more arguments as options.\n     @@ builtin/ls-files.c: static void write_name(const char *name)\n       \n      +static void write_name_to_buf(struct strbuf *sb, const char *name)\n      +{\n     -+\tname = relative_path(name, prefix_len ? prefix : NULL, sb);\n     -+\tif (line_terminator) {\n     -+\t\tquote_c_style(name, sb, NULL, 0);\n     -+\t} else {\n     -+\t\tstrbuf_add(sb, name, strlen(name));\n     -+\t}\n     ++\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n     ++\tif (line_terminator)\n     ++\t\tquote_c_style(rel, sb, NULL, 0);\n     ++\telse\n     ++\t\tstrbuf_add(sb, rel, strlen(rel));\n      +}\n      +\n       static const char *get_tag(const struct cache_entry *ce, const char *tag)\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\tif (len)\n      +\t\treturn len;\n      +\tif (*start != '(')\n     -+\t\tdie(_(\"bad ls-files format: element '%s' does not start with '('\"), start);\n     ++\t\tdie(_(\"bad ls-files format: element '%s' \"\n     ++\t\t      \"does not start with '('\"), start);\n      +\n      +\tend = strchr(start + 1, ')');\n      +\tif (!end)\n     -+\t\tdie(_(\"bad ls-files format: element '%s' does not end in ')'\"), start);\n     ++\t\tdie(_(\"bad ls-files format: element '%s'\"\n     ++\t\t      \"does not end in ')'\"), start);\n      +\n      +\tlen = end - start + 1;\n     -+\tif (skip_prefix(start, \"(tag)\", &p)) {\n     ++\tif (skip_prefix(start, \"(tag)\", &p))\n      +\t\tstrbuf_addstr(sb, get_tag(data->ce, data->tag));\n     -+\t} else if (skip_prefix(start, \"(objectmode)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(objectmode)\", &p))\n      +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n     -+\t} else if (skip_prefix(start, \"(objectname)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(objectname)\", &p))\n      +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n     -+\t} else if (skip_prefix(start, \"(stage)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(stage)\", &p))\n      +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n     -+\t} else if (skip_prefix(start, \"(eol)\", &p)) {\n     -+\t\twrite_eolinfo_to_buf(sb, data->istate, data->ce, data->pathname);\n     -+\t} else if (skip_prefix(start, \"(path)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(eol)\", &p))\n     ++\t\twrite_eolinfo_to_buf(sb, data->istate,\n     ++\t\t\t\t     data->ce, data->pathname);\n     ++\telse if (skip_prefix(start, \"(path)\", &p))\n      +\t\twrite_name_to_buf(sb, data->pathname);\n     -+\t} else if (skip_prefix(start, \"(ctime)\", &p)) {\n     -+\t\tstrbuf_addf(sb, \"ctime: %u:%u\", sd->sd_ctime.sec, sd->sd_ctime.nsec);\n     -+\t} else if (skip_prefix(start, \"(mtime)\", &p)) {\n     -+\t\tstrbuf_addf(sb, \"mtime: %u:%u\", sd->sd_mtime.sec, sd->sd_mtime.nsec);\n     -+\t} else if (skip_prefix(start, \"(dev)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(ctime)\", &p))\n     ++\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n     ++\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n     ++\telse if (skip_prefix(start, \"(mtime)\", &p))\n     ++\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n     ++\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n     ++\telse if (skip_prefix(start, \"(dev)\", &p))\n      +\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n     -+\t} else if (skip_prefix(start, \"(ino)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(ino)\", &p))\n      +\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n     -+\t} else if (skip_prefix(start, \"(uid)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(uid)\", &p))\n      +\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n     -+\t} else if (skip_prefix(start, \"(gid)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(gid)\", &p))\n      +\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n     -+\t} else if (skip_prefix(start, \"(size)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(size)\", &p))\n      +\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n     -+\t} else if (skip_prefix(start, \"(flags)\", &p)) {\n     ++\telse if (skip_prefix(start, \"(flags)\", &p))\n      +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n     -+\t} else {\n     ++\telse {\n      +\t\terrlen = (unsigned long)len;\n      +\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n      +\t}\n     @@ builtin/ls-files.c: int cmd_ls_files(int argc, const char **argv, const char *cm\n       \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n       \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n      +\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n     -+\t\t\t\t\t N_(\"format to use for the output\"),\n     -+\t\t\t\t\t PARSE_OPT_NONEG),\n     ++\t\t\t     N_(\"format to use for the output\"),\n     ++\t\t\t     PARSE_OPT_NONEG),\n       \t\tOPT_END()\n       \t};\n       \tint ret = 0;\n     @@ t/t3013-ls-files-format.sh (new)\n      +'\n      +\n      +test_expect_success 'git ls-files --format objectmode' '\n     -+\tcat >expect <<-EOF &&\n     ++\tcat >expect <<-\\EOF &&\n      +\t100755\n      +\t100644\n      +\tEOF\n     @@ t/t3013-ls-files-format.sh (new)\n      +'\n      +\n      +test_expect_success 'git ls-files --format path' '\n     -+\tcat >expect <<-EOF &&\n     ++\tcat >expect <<-\\EOF &&\n      +\to1\n      +\to2\n      +\tEOF\n     @@ t/t3013-ls-files-format.sh (new)\n      +'\n      +\n      +test_expect_success 'git ls-files --format ctime' '\n     -+\tgit ls-files --debug | grep ctime >expect &&\n     ++\tgit ls-files --debug >out &&\n     ++\tgrep ctime out >expect &&\n      +\tgit ls-files --format=\"  %(ctime)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format mtime' '\n     -+\tgit ls-files --debug | grep mtime >expect &&\n     ++\tgit ls-files --debug >out &&\n     ++\tgrep mtime out >expect &&\n      +\tgit ls-files --format=\"  %(mtime)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format dev and ino' '\n     -+\tgit ls-files --debug | grep dev >expect &&\n     ++\tgit ls-files --debug >out &&\n     ++\tgrep dev out >expect &&\n      +\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format uid and gid' '\n     -+\tgit ls-files --debug | grep uid >expect &&\n     ++\tgit ls-files --debug >out &&\n     ++\tgrep uid out >expect &&\n      +\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format with -m' '\n      +\techo change >o1 &&\n     -+\tcat >expect <<-EOF &&\n     ++\tcat >expect <<-\\EOF &&\n      +\to1\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -m >actual &&\n     @@ t/t3013-ls-files-format.sh (new)\n      +'\n      +\n      +test_expect_success 'git ls-files --format with -d' '\n     -+\trm o1 &&\n     -+\ttest_when_finished \"git restore o1\" &&\n     -+\tcat >expect <<-EOF &&\n     -+\to1\n     ++\techo o3 >o3 &&\n     ++\tgit add o3 &&\n     ++\trm o3 &&\n     ++\tcat >expect <<-\\EOF &&\n     ++\to3\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -d >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format size and flags' '\n     -+\tgit ls-files --debug | grep size >expect &&\n     ++\tgit ls-files --debug >out &&\n     ++\tgrep size out >expect &&\n      +\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n     @@ t/t3013-ls-files-format.sh (new)\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format with -s must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" -s\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format with -o must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" -o\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format with -k must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" -k\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format with --resolve-undo must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" --resolve-undo\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format with --deduplicate must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" --deduplicate\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format with --debug must fail' '\n     -+\ttest_must_fail git ls-files --format=\"%(objectname)\" --debug\n     -+'\n     -+\n     ++for flag in -s -o -k --resolve-undo --deduplicate --debug\n     ++do\n     ++\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n     ++\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n     ++\t'\n     ++done\n      +test_done\n 2:  81ae1280e8e < -:  ----------- ls-files: introduce \"--object-only\" option\n\n\n Documentation/git-ls-files.txt |  51 ++++++++++++-\n builtin/ls-files.c             | 130 ++++++++++++++++++++++++++++++++-\n t/t3013-ls-files-format.sh     | 130 +++++++++++++++++++++++++++++++++\n 3 files changed, 307 insertions(+), 4 deletions(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..9a88c92f1ad 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n+\t`--debug`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+tag::\n+\tThe tag of file status.\n+objectmode::\n+\tThe mode of the object.\n+objectname::\n+\tThe name of the object.\n+stage::\n+\tThe stage of the file.\n+eol::\n+\tThe line endings of files.\n+path::\n+\tThe pathname of the object.\n+ctime::\n+\tThe create time of file.\n+mtime::\n+\tThe modify time of file.\n+dev::\n+\tThe ID of device containing file.\n+ino::\n+\tThe inode number of file.\n+uid::\n+\tThe user id of file owner.\n+gid::\n+\tThe group id of file owner.\n+size::\n+\tThe size of the file.\n+flags::\n+\tThe flags of the file.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..f037ccb58b4 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -58,8 +60,8 @@ static const char *tag_modified = \"\";\n static const char *tag_skip_worktree = \"\";\n static const char *tag_resolve_undo = \"\";\n \n-static void write_eolinfo(struct index_state *istate,\n-\t\t\t  const struct cache_entry *ce, const char *path)\n+static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t   const struct cache_entry *ce, const char *path)\n {\n \tif (show_eol) {\n \t\tstruct stat st;\n@@ -71,10 +73,25 @@ static void write_eolinfo(struct index_state *istate,\n \t\t\t\t\t\t\t       ce->name);\n \t\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n \t\t\tw_txt = get_wt_convert_stats_ascii(path);\n-\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\tif (sb)\n+\t\t\tstrbuf_addf(sb, \"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\telse\n+\t\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n \t}\n }\n \n+static void write_eolinfo(struct index_state *istate,\n+\t\t\t  const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(NULL, istate, ce, path);\n+}\n+\n+static void write_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(sb, istate, ce, path);\n+}\n+\n static void write_name(const char *name)\n {\n \t/*\n@@ -85,6 +102,15 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_add(sb, rel, strlen(rel));\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +248,91 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *tag;\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tunsigned int errlen;\n+\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(tag)\", &p))\n+\t\tstrbuf_addstr(sb, get_tag(data->ce, data->tag));\n+\telse if (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eol)\", &p))\n+\t\twrite_eolinfo_to_buf(sb, data->istate,\n+\t\t\t\t     data->ce, data->pathname);\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse if (skip_prefix(start, \"(ctime)\", &p))\n+\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n+\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n+\telse if (skip_prefix(start, \"(mtime)\", &p))\n+\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n+\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n+\telse if (skip_prefix(start, \"(dev)\", &p))\n+\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n+\telse if (skip_prefix(start, \"(ino)\", &p))\n+\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n+\telse if (skip_prefix(start, \"(uid)\", &p))\n+\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n+\telse if (skip_prefix(start, \"(gid)\", &p))\n+\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n+\telse if (skip_prefix(start, \"(size)\", &p))\n+\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n+\telse if (skip_prefix(start, \"(flags)\", &p))\n+\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n+\telse {\n+\t\terrlen = (unsigned long)len;\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n+\t}\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname, const char *tag) {\n+\n+\tstruct show_index_data data = {\n+\t\t.tag = tag,\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+\treturn;\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +347,11 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname, tag);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +791,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +818,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n+\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..1a1b09e7b3c\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,130 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format tag' '\n+\tprintf \"H \\nH \\n\" >expect &&\n+\tgit ls-files --format=\"%(tag)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-\\EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eol' '\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n+\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format ctime' '\n+\tgit ls-files --debug >out &&\n+\tgrep ctime out >expect &&\n+\tgit ls-files --format=\"  %(ctime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format mtime' '\n+\tgit ls-files --debug >out &&\n+\tgrep mtime out >expect &&\n+\tgit ls-files --format=\"  %(mtime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format dev and ino' '\n+\tgit ls-files --debug >out &&\n+\tgrep dev out >expect &&\n+\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format uid and gid' '\n+\tgit ls-files --debug >out &&\n+\tgrep uid out >expect &&\n+\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o3 >o3 &&\n+\tgit add o3 &&\n+\trm o3 &&\n+\tcat >expect <<-\\EOF &&\n+\to3\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format size and flags' '\n+\tgit ls-files --debug >out &&\n+\tgrep size out >expect &&\n+\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)%x0a  %(ctime)%x0a  %(mtime)%x0a  %(dev)%x09%(ino)%x0a  %(uid)%x09%(gid)%x0a  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+for flag in -s -o -k --resolve-undo --deduplicate --debug\n+do\n+\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n+\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"457532","messageId":"44f572b8-6cef-fad4-04c0-3cbb79db5bf9@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v2.git.1655629990185.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] ls-files: introduce \"--format\" option","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-06-19T13:50:54Z","receivedAt":"2022-06-19T13:51:01Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi ZheNing\n\nOn 19/06/2022 10:13, ZheNing Hu via GitGitGadget wrote:\n> From: ZheNing Hu <adlternative@gmail.com>\n> \n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n> \n> --format cannot used with -s, -o, -k, --resolve-undo,\n> --deduplicate and --debug.\n\nI think this is an interesting feature that provides functionality that \nis not available by feeding index entries into cat-file.\n\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n>   Documentation/git-ls-files.txt |  51 ++++++++++++-\n>   builtin/ls-files.c             | 130 ++++++++++++++++++++++++++++++++-\n>   t/t3013-ls-files-format.sh     | 130 +++++++++++++++++++++++++++++++++\n>   3 files changed, 307 insertions(+), 4 deletions(-)\n>   create mode 100755 t/t3013-ls-files-format.sh\n> \n> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> index 0dabf3f0ddc..9a88c92f1ad 100644\n> --- a/Documentation/git-ls-files.txt\n> +++ b/Documentation/git-ls-files.txt\n> @@ -20,7 +20,7 @@ SYNOPSIS\n>   \t\t[--exclude-standard]\n>   \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n>   \t\t[--full-name] [--recurse-submodules]\n> -\t\t[--abbrev[=<n>]] [--] [<file>...]\n> +\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n>   \n>   DESCRIPTION\n>   -----------\n> @@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n>   \tto the contained files. Sparse directories will be shown with a\n>   \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n>   \n> +--format=<format>::\n> +\tA string that interpolates `%(fieldname)` from the result being shown.\n> +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n> +\tinterpolates to character with hex code `xx`; for example `%00`\n> +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n> +\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n> +\t`--debug`.\n>   \\--::\n>   \tDo not interpret any more arguments as options.\n>   \n> @@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n>   (see linkgit:git-config[1]).  Using `-z` the filename is output\n>   verbatim and the line is terminated by a NUL byte.\n>   \n> +It is possible to print in a custom format by using the `--format`\n> +option, which is able to interpolate different fields using\n> +a `%(fieldname)` notation. For example, if you only care about the\n> +\"objectname\" and \"path\" fields, you can execute with a specific\n> +\"--format\" like\n> +\n> +\tgit ls-files --format='%(objectname) %(path)'\n> +\n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +tag::\n> +\tThe tag of file status.\n\nThe documentation for -t strong discourages its use, so I wonder if we \nreally want to expose it here.\n\n> +objectmode::\n> +\tThe mode of the object.\n> +objectname::\n> +\tThe name of the object.\n> +stage::\n> +\tThe stage of the file.\n> +eol::\n> +\tThe line endings of files.\n\nEvery other option refers to either a \"file\" or \"object\" but here we \nhave \"files\". Looking at the implementation below this will print the \nline ending from both the index and the worktree, it would be useful to \nclarify that here.\n\n> +path::\n> +\tThe pathname of the object.\n> +ctime::\n> +\tThe create time of file.\n\nIt is not clear from this whether this (and all the file attributes \nbelow) are coming from the worktree or the index or both like eol?\n\n> +mtime::\n> +\tThe modify time of file.\n> +dev::\n> +\tThe ID of device containing file.\n> +ino::\n> +\tThe inode number of file.\n> +uid::\n> +\tThe user id of file owner.\n> +gid::\n> +\tThe group id of file owner.\n> +size::\n> +\tThe size of the file.\n> +flags::\n> +\tThe flags of the file.\n\nWhat are the flags?\n\n> [...]  \n> +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> +\t\t\t       void *context)\n> +{\n> +\tstruct show_index_data *data = context;\n> +\tconst char *end;\n> +\tconst char *p;\n> +\tunsigned int errlen;\n > [...]\n> +\telse if (skip_prefix(start, \"(flags)\", &p))\n> +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n> +\telse {\n> +\t\terrlen = (unsigned long)len;\n> +\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n\nerrlen is declared as an unsigned int, but you cast len which is a \nsize_t to unsigned long when assigning to errlen. Then errlen is used \nwhere a signed int is required by die. There is also a style violation \nas if any branch of an if needs braces then they should all be braced. I \nthink that the best solution would be to drop errlen and just write\n\n\telse\n\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n\nIt would be interesting to check the performance of this implementation \non a large repository as it is doing a lot of branching inside a loop. I \ndon't think we should change it unless it turns out to be a problem. \nThen we could try switching on the first character of the format \nspecifier or some other optimization.\n\nBest Wishes\n\nPhillip\n"},{"id":"457558","messageId":"CAOLTT8R_ZAWBWxzNqFOQc7igBSpODYUi1On9nfVPGkYQd47Rgg@mail.gmail.com","threadId":"58016","inReplyTo":"44f572b8-6cef-fad4-04c0-3cbb79db5bf9@gmail.com","subject":"Re: [PATCH v2] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-20T13:32:42Z","receivedAt":"2022-06-20T14:20:14Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> 于2022年6月19日周日 21:50写道：\n>\n> Hi ZheNing\n>\n> On 19/06/2022 10:13, ZheNing Hu via GitGitGadget wrote:\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Add a new option --format that output index enties\n> > informations with custom format, taking inspiration\n> > from the option with the same name in the `git ls-tree`\n> > command.\n> >\n> > --format cannot used with -s, -o, -k, --resolve-undo,\n> > --deduplicate and --debug.\n>\n> I think this is an interesting feature that provides functionality that\n> is not available by feeding index entries into cat-file.\n>\n\nYeah, it cares about index state. Having this feature, maybe we can\neasier check index/work-tree state.\n\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> >   Documentation/git-ls-files.txt |  51 ++++++++++++-\n> >   builtin/ls-files.c             | 130 ++++++++++++++++++++++++++++++++-\n> >   t/t3013-ls-files-format.sh     | 130 +++++++++++++++++++++++++++++++++\n> >   3 files changed, 307 insertions(+), 4 deletions(-)\n> >   create mode 100755 t/t3013-ls-files-format.sh\n> >\n> > diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> > index 0dabf3f0ddc..9a88c92f1ad 100644\n> > --- a/Documentation/git-ls-files.txt\n> > +++ b/Documentation/git-ls-files.txt\n> > @@ -20,7 +20,7 @@ SYNOPSIS\n> >               [--exclude-standard]\n> >               [--error-unmatch] [--with-tree=<tree-ish>]\n> >               [--full-name] [--recurse-submodules]\n> > -             [--abbrev[=<n>]] [--] [<file>...]\n> > +             [--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n> >\n> >   DESCRIPTION\n> >   -----------\n> > @@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n> >       to the contained files. Sparse directories will be shown with a\n> >       trailing slash, such as \"x/\" for a sparse directory \"x\".\n> >\n> > +--format=<format>::\n> > +     A string that interpolates `%(fieldname)` from the result being shown.\n> > +     It also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n> > +     interpolates to character with hex code `xx`; for example `%00`\n> > +     interpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n> > +     --format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n> > +     `--debug`.\n> >   \\--::\n> >       Do not interpret any more arguments as options.\n> >\n> > @@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n> >   (see linkgit:git-config[1]).  Using `-z` the filename is output\n> >   verbatim and the line is terminated by a NUL byte.\n> >\n> > +It is possible to print in a custom format by using the `--format`\n> > +option, which is able to interpolate different fields using\n> > +a `%(fieldname)` notation. For example, if you only care about the\n> > +\"objectname\" and \"path\" fields, you can execute with a specific\n> > +\"--format\" like\n> > +\n> > +     git ls-files --format='%(objectname) %(path)'\n> > +\n> > +FIELD NAMES\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n> > +\n> > +tag::\n> > +     The tag of file status.\n>\n> The documentation for -t strong discourages its use, so I wonder if we\n> really want to expose it here.\n>\n\nI think it's ok to remove it.\n\n> > +objectmode::\n> > +     The mode of the object.\n> > +objectname::\n> > +     The name of the object.\n> > +stage::\n> > +     The stage of the file.\n> > +eol::\n> > +     The line endings of files.\n>\n> Every other option refers to either a \"file\" or \"object\" but here we\n> have \"files\". Looking at the implementation below this will print the\n> line ending from both the index and the worktree, it would be useful to\n> clarify that here.\n>\n\nSure...\n\n> > +path::\n> > +     The pathname of the object.\n> > +ctime::\n> > +     The create time of file.\n>\n> It is not clear from this whether this (and all the file attributes\n> below) are coming from the worktree or the index or both like eol?\n>\n\n...I think they are basically index cache_entry attributes, except eol\ncares about both\nworktree and index. I will fix them.\n\n> > +mtime::\n> > +     The modify time of file.\n> > +dev::\n> > +     The ID of device containing file.\n> > +ino::\n> > +     The inode number of file.\n> > +uid::\n> > +     The user id of file owner.\n> > +gid::\n> > +     The group id of file owner.\n> > +size::\n> > +     The size of the file.\n> > +flags::\n> > +     The flags of the file.\n>\n> What are the flags?\n>\n\nIt is cache entry flags which include In-memory only flags and some\nextended on-disk flags.\n\n> > [...]\n> > +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> > +                            void *context)\n> > +{\n> > +     struct show_index_data *data = context;\n> > +     const char *end;\n> > +     const char *p;\n> > +     unsigned int errlen;\n>  > [...]\n> > +     else if (skip_prefix(start, \"(flags)\", &p))\n> > +             strbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n> > +     else {\n> > +             errlen = (unsigned long)len;\n> > +             die(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n>\n> errlen is declared as an unsigned int, but you cast len which is a\n> size_t to unsigned long when assigning to errlen. Then errlen is used\n> where a signed int is required by die. There is also a style violation\n> as if any branch of an if needs braces then they should all be braced. I\n> think that the best solution would be to drop errlen and just write\n>\n>         else\n>                 die(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n>\n\nThis piece of code is copying from ls-tree. Maybe we should fix it too.\n\n> It would be interesting to check the performance of this implementation\n> on a large repository as it is doing a lot of branching inside a loop. I\n> don't think we should change it unless it turns out to be a problem.\n> Then we could try switching on the first character of the format\n> specifier or some other optimization.\n>\n\nJust like ref-filter or something else does, it parses atoms\nand then fills buffers with information. Maybe we need such performance\noptimization later, but for now, it's just easier to implement this patch :)\n\n> Best Wishes\n>\n> Phillip\n\nThanks\n\nZheNing Hu\n"},{"id":"457587","messageId":"pull.1262.v3.git.1655777140231.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v2.git.1655629990185.gitgitgadget@gmail.com","subject":"[PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-21T02:05:40Z","receivedAt":"2022-06-21T02:05:48Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, --resolve-undo,\n--deduplicate and --debug.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v2->v3:\n    \n     1. remove %(tag) because -t is deprecated, suggested by Phillip.\n     2. fix some description of atoms in document, suggested by Phillip..\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v2:\n\n 1:  67f2c3b8ebe ! 1:  aaafa35ffcd ls-files: introduce \"--format\" option\n     @@ Documentation/git-ls-files.txt: quoted as explained for the configuration variab\n      +into the resulting output. For each outputting line, the following\n      +names can be used:\n      +\n     -+tag::\n     -+\tThe tag of file status.\n      +objectmode::\n     -+\tThe mode of the object.\n     ++\tThe mode of the file which is in the index.\n      +objectname::\n     -+\tThe name of the object.\n     ++\tThe name of the file which is in the index.\n      +stage::\n     -+\tThe stage of the file.\n     ++\tThe stage of the file which is in the index.\n      +eol::\n     -+\tThe line endings of files.\n     ++\tThe <eolinfo> and <eolattr> of files both in the\n     ++\tindex and the work-tree.\n      +path::\n     -+\tThe pathname of the object.\n     ++\tThe pathname of the file which is in the index.\n      +ctime::\n     -+\tThe create time of file.\n     ++\tThe create time of file which is in the index.\n      +mtime::\n     -+\tThe modify time of file.\n     ++\tThe modified time of file which is in the index.\n      +dev::\n     -+\tThe ID of device containing file.\n     ++\tThe ID of device containing file which is in the index.\n      +ino::\n     -+\tThe inode number of file.\n     ++\tThe inode number of file which is in the index.\n      +uid::\n     -+\tThe user id of file owner.\n     ++\tThe user id of file owner which is in the index.\n      +gid::\n     -+\tThe group id of file owner.\n     ++\tThe group id of file owner which is in the index.\n      +size::\n     -+\tThe size of the file.\n     ++\tThe size of the file which is in the index.\n      +flags::\n     -+\tThe flags of the file.\n     ++\tThe flags of the file in the index which include\n     ++\tin-memory only flags and some extended on-disk flags.\n       \n       EXCLUDE PATTERNS\n       ----------------\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\tstruct show_index_data *data = context;\n      +\tconst char *end;\n      +\tconst char *p;\n     -+\tunsigned int errlen;\n      +\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n      +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n      +\tif (len)\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\t\t      \"does not end in ')'\"), start);\n      +\n      +\tlen = end - start + 1;\n     -+\tif (skip_prefix(start, \"(tag)\", &p))\n     -+\t\tstrbuf_addstr(sb, get_tag(data->ce, data->tag));\n     -+\telse if (skip_prefix(start, \"(objectmode)\", &p))\n     ++\tif (skip_prefix(start, \"(objectmode)\", &p))\n      +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n      +\telse if (skip_prefix(start, \"(objectname)\", &p))\n      +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n      +\telse if (skip_prefix(start, \"(flags)\", &p))\n      +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n     -+\telse {\n     -+\t\terrlen = (unsigned long)len;\n     -+\t\tdie(_(\"bad ls-files format: %%%.*s\"), errlen, start);\n     -+\t}\n     ++\telse\n     ++\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n      +\n      +\treturn len;\n      +}\n      +\n      +static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n     -+\t\t\tconst char *format, const char *fullname, const char *tag) {\n     ++\t\t\tconst char *format, const char *fullname) {\n      +\n      +\tstruct show_index_data data = {\n     -+\t\t.tag = tag,\n      +\t\t.pathname = fullname,\n      +\t\t.istate = repo->index,\n      +\t\t.ce = ce,\n     @@ builtin/ls-files.c: static void show_ce(struct repository *repo, struct dir_stru\n       \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n       \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n      +\t\tif (format) {\n     -+\t\t\tshow_ce_fmt(repo, ce, format, fullname, tag);\n     ++\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n      +\t\t\treturn;\n      +\t\t}\n      +\n     @@ t/t3013-ls-files-format.sh (new)\n      +\tgit commit -m base\n      +'\n      +\n     -+test_expect_success 'git ls-files --format tag' '\n     -+\tprintf \"H \\nH \\n\" >expect &&\n     -+\tgit ls-files --format=\"%(tag)\" -t >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n      +test_expect_success 'git ls-files --format objectmode' '\n      +\tcat >expect <<-\\EOF &&\n      +\t100755\n\n\n Documentation/git-ls-files.txt |  51 +++++++++++++-\n builtin/ls-files.c             | 124 ++++++++++++++++++++++++++++++++-\n t/t3013-ls-files-format.sh     | 124 +++++++++++++++++++++++++++++++++\n 3 files changed, 295 insertions(+), 4 deletions(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..39211bde797 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n+\t`--debug`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,48 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+objectmode::\n+\tThe mode of the file which is in the index.\n+objectname::\n+\tThe name of the file which is in the index.\n+stage::\n+\tThe stage of the file which is in the index.\n+eol::\n+\tThe <eolinfo> and <eolattr> of files both in the\n+\tindex and the work-tree.\n+path::\n+\tThe pathname of the file which is in the index.\n+ctime::\n+\tThe create time of file which is in the index.\n+mtime::\n+\tThe modified time of file which is in the index.\n+dev::\n+\tThe ID of device containing file which is in the index.\n+ino::\n+\tThe inode number of file which is in the index.\n+uid::\n+\tThe user id of file owner which is in the index.\n+gid::\n+\tThe group id of file owner which is in the index.\n+size::\n+\tThe size of the file which is in the index.\n+flags::\n+\tThe flags of the file in the index which include\n+\tin-memory only flags and some extended on-disk flags.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..387641b32df 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -58,8 +60,8 @@ static const char *tag_modified = \"\";\n static const char *tag_skip_worktree = \"\";\n static const char *tag_resolve_undo = \"\";\n \n-static void write_eolinfo(struct index_state *istate,\n-\t\t\t  const struct cache_entry *ce, const char *path)\n+static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t   const struct cache_entry *ce, const char *path)\n {\n \tif (show_eol) {\n \t\tstruct stat st;\n@@ -71,10 +73,25 @@ static void write_eolinfo(struct index_state *istate,\n \t\t\t\t\t\t\t       ce->name);\n \t\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n \t\t\tw_txt = get_wt_convert_stats_ascii(path);\n-\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\tif (sb)\n+\t\t\tstrbuf_addf(sb, \"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n+\t\telse\n+\t\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n \t}\n }\n \n+static void write_eolinfo(struct index_state *istate,\n+\t\t\t  const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(NULL, istate, ce, path);\n+}\n+\n+static void write_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t const struct cache_entry *ce, const char *path)\n+{\n+\twrite_eolinfo_internal(sb, istate, ce, path);\n+}\n+\n static void write_name(const char *name)\n {\n \t/*\n@@ -85,6 +102,15 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_add(sb, rel, strlen(rel));\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +248,85 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *tag;\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eol)\", &p))\n+\t\twrite_eolinfo_to_buf(sb, data->istate,\n+\t\t\t\t     data->ce, data->pathname);\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse if (skip_prefix(start, \"(ctime)\", &p))\n+\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n+\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n+\telse if (skip_prefix(start, \"(mtime)\", &p))\n+\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n+\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n+\telse if (skip_prefix(start, \"(dev)\", &p))\n+\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n+\telse if (skip_prefix(start, \"(ino)\", &p))\n+\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n+\telse if (skip_prefix(start, \"(uid)\", &p))\n+\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n+\telse if (skip_prefix(start, \"(gid)\", &p))\n+\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n+\telse if (skip_prefix(start, \"(size)\", &p))\n+\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n+\telse if (skip_prefix(start, \"(flags)\", &p))\n+\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+\treturn;\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +341,11 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +785,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +812,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n+\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..8c3ef2df138\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,124 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-\\EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eol' '\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n+\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format ctime' '\n+\tgit ls-files --debug >out &&\n+\tgrep ctime out >expect &&\n+\tgit ls-files --format=\"  %(ctime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format mtime' '\n+\tgit ls-files --debug >out &&\n+\tgrep mtime out >expect &&\n+\tgit ls-files --format=\"  %(mtime)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format dev and ino' '\n+\tgit ls-files --debug >out &&\n+\tgrep dev out >expect &&\n+\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format uid and gid' '\n+\tgit ls-files --debug >out &&\n+\tgrep uid out >expect &&\n+\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o3 >o3 &&\n+\tgit add o3 &&\n+\trm o3 &&\n+\tcat >expect <<-\\EOF &&\n+\to3\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format size and flags' '\n+\tgit ls-files --debug >out &&\n+\tgrep size out >expect &&\n+\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)%x0a  %(ctime)%x0a  %(mtime)%x0a  %(dev)%x09%(ino)%x0a  %(uid)%x09%(gid)%x0a  %(size)%x09%(flags)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+for flag in -s -o -k --resolve-undo --deduplicate --debug\n+do\n+\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n+\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"457785","messageId":"080f65b3-91f5-7b68-4235-4bfb956c8321@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v3.git.1655777140231.gitgitgadget@gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-06-23T14:06:33Z","receivedAt":"2022-06-23T14:06:40Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi ZheNing\n\nOn 21/06/2022 03:05, ZheNing Hu via GitGitGadget wrote:\n> From: ZheNing Hu <adlternative@gmail.com>\n> \n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n> \n> --format cannot used with -s, -o, -k, --resolve-undo,\n> --deduplicate and --debug.\n> \n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>      ls-files: introduce \"--format\" options\n>      \n>      v2->v3:\n>      \n>       1. remove %(tag) because -t is deprecated, suggested by Phillip.\n>       2. fix some description of atoms in document, suggested by Phillip..\n\nThanks for re-rolling, having taken a look a closer look at the tests \nI'm concerned about the output format for some of the specifiers, see below.\n\n> [...]  \n> +It is possible to print in a custom format by using the `--format`\n> +option, which is able to interpolate different fields using\n> +a `%(fieldname)` notation. For example, if you only care about the\n> +\"objectname\" and \"path\" fields, you can execute with a specific\n> +\"--format\" like\n> +\n> +\tgit ls-files --format='%(objectname) %(path)'\n> +\n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +objectmode::\n> +\tThe mode of the file which is in the index.\n> +objectname::\n> +\tThe name of the file which is in the index.\n> +stage::\n> +\tThe stage of the file which is in the index.\n> +eol::\n> +\tThe <eolinfo> and <eolattr> of files both in the\n> +\tindex and the work-tree.\n\nLooking at the test for this option I think it needs more work, why \nshould --format arbitrarily append a tab to the end of the output? - the \nuser should be able to specify a separator if they want one as part of \nthe format string. Also I'm not sure why there is so much whitespace in \nthe output.\n\n> +path::\n> +\tThe pathname of the file which is in the index.\n\nI think that for all these it might be clearer to say \"recorded in the \nindex\" rather than \"of the file which is in the index\"\n\n> +ctime::\n> +\tThe create time of file which is in the index.\n\nThis is printed with a prefix 'ctime:' (the same applies to the format \nspecifiers below) I think we should omit that and just print the data so \nthe user can choose the format they want.\n\n> +mtime::\n> +\tThe modified time of file which is in the index.\n> +dev::\n> +\tThe ID of device containing file which is in the index.\n> +ino::\n> +\tThe inode number of file which is in the index.\n> +uid::\n> +\tThe user id of file owner which is in the index.\n> +gid::\n> +\tThe group id of file owner which is in the index.\n> +size::\n> +\tThe size of the file which is in the index.\n> +flags::\n> +\tThe flags of the file in the index which include\n> +\tin-memory only flags and some extended on-disk flags.\n\nIf %(flags) is going to be useful then I think we need to think about \nhow they are printed and document that. At the moment they are printed \nas a hexadecimal number which is fine for debugging but probably not \ngoing to be useful for something like --format. I think printing \ndocumented symbolic names with some kind of separator (a comma maybe) \nbetween them is probably more useful\n\n > [...]\n> +test_expect_success 'git ls-files --format eol' '\n> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n> +\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n\nI'm not sure why this is passing --eol as well as --format='%(eol)' - \nshouldn't that combination of flags be an error?\n\nBest Wishes\n\nPhillip\n"},{"id":"457799","messageId":"xmqqv8sr1iex.fsf@gitster.g","threadId":"58016","inReplyTo":"080f65b3-91f5-7b68-4235-4bfb956c8321@gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-23T15:57:58Z","receivedAt":"2022-06-23T15:58:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Thanks for re-rolling, having taken a look a closer look at the tests\n> I'm concerned about the output format for some of the specifiers, see\n> below.\n\nThanks for raising these issues.  I agree with you on many of them.\nIn addition to what you covered ....\n\n>> +path::\n>> +\tThe pathname of the file which is in the index.\n> I think that for all these it might be clearer to say \"recorded in the\n> index\" rather than \"of the file which is in the index\"\n\nI think we would call this \"name\".  The name of the existing option\nthat controls how they are shown is \"--full-name\", not \"--full-path\",\nfor example.\n\n>> +ctime::\n>> +\tThe create time of file which is in the index.\n>\n> This is printed with a prefix 'ctime:' (the same applies to the format\n> specifiers below) I think we should omit that and just print the data\n> so the user can choose the format they want.\n>\n>> +mtime::\n>> +\tThe modified time of file which is in the index.\n\nThese are only the low-bits of the full timestamp, not ctime/mtime\nthemselves.\n\nBut stepping back a bit, why do we need to include them in the\noutput?  What workflow and use case are we trying to help?  Dump\noutput from \"stat <path>\" equivalent from ls-files and compare with\n\"stat .\" output to see which ones are stale?  Or is there any value\nto see the value of, say, ctime as an individual data item?\n\n>> +dev::\n>> +\tThe ID of device containing file which is in the index.\n>> +ino::\n>> +\tThe inode number of file which is in the index.\n>> +uid::\n>> +\tThe user id of file owner which is in the index.\n>> +gid::\n>> +\tThe group id of file owner which is in the index.\n\nAgain, why do we need to include these in the output?\n\nWouldn't it be sufficient, as well as a lot more useful, to show a\nsingle bit \"the cached stat info matches what is in the working tree\n(yes/no)\"?\n\n>> +size::\n>> +\tThe size of the file which is in the index.\n\nThis needs to explain what kind of size this is.  Is it the size of\nthe blob object?  Is it the size of the file in the working tree\n(i.e. not cleaned)?  Is it _always_ the size, or can it become a\nnumber that is very different from size in certain circumstances?\n\nIOW, I do not think giving this to unsuspecting users and call it\n\"size of the file\" hurts them more than it helps them, especially\nbecause it is not always the size of the file.\n\nI'd suggest getting rid of everything from ctime down to size and if\nwe really care about the freshness of the cached stat info, replace\nthem with a single bit \"up-to-date\".\n\n>> +flags::\n>> +\tThe flags of the file in the index which include\n>> +\tin-memory only flags and some extended on-disk flags.\n>\n> If %(flags) is going to be useful then I think we need to think about\n> how they are printed and document that. At the moment they are printed \n> as a hexadecimal number which is fine for debugging but probably not\n> going to be useful for something like --format. I think printing \n> documented symbolic names with some kind of separator (a comma maybe)\n> between them is probably more useful\n\nI am guessing that most of the above are only useful for curious\ngeeks and those who are debugging their new tweak to the code that\ntouches the index, i.e. a debugging feature.  But these folks can\nrun \"git\" under a debugger, and they probably have to do so when\nthey are seeing an unexpected value in the flags member of a cache\nentry anyway.  So I am not sure whom this field is intended to help.\n\n>> [...]\n>> +test_expect_success 'git ls-files --format eol' '\n>> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n>> +\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n>> +\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n>\n> I'm not sure why this is passing --eol as well as --format='%(eol)' -\n> shouldn't that combination of flags be an error?\n\nGood eyes.\n\nThanks.\n"},{"id":"457837","messageId":"db23bf2a-e661-906f-f8ed-fc2ab3d3ce41@gmail.com","threadId":"58016","inReplyTo":"xmqqv8sr1iex.fsf@gitster.g","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-06-24T10:16:53Z","receivedAt":"2022-06-24T10:16:58Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 23/06/2022 16:57, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> \n>> Thanks for re-rolling, having taken a look a closer look at the tests\n>> I'm concerned about the output format for some of the specifiers, see\n>> below.\n> \n> Thanks for raising these issues.  I agree with you on many of them.\n> In addition to what you covered ....\n> \n>>> +path::\n>>> +\tThe pathname of the file which is in the index.\n>> I think that for all these it might be clearer to say \"recorded in the\n>> index\" rather than \"of the file which is in the index\"\n> \n> I think we would call this \"name\".  The name of the existing option\n> that controls how they are shown is \"--full-name\", not \"--full-path\",\n> for example.\n\nThat's a good point, also I've just noticed that this is another case \nwhere there is a separator character is printed automatically when the \nformat string is expanded. I think it is probably right to format the \nname based on whether or not -z was passed but we should leave it up to \nthe user to supply a delimiter in the format string.\n\n>>> +ctime::\n>>> +\tThe create time of file which is in the index.\n>>\n>> This is printed with a prefix 'ctime:' (the same applies to the format\n>> specifiers below) I think we should omit that and just print the data\n>> so the user can choose the format they want.\n>>\n>>> +mtime::\n>>> +\tThe modified time of file which is in the index.\n> \n> These are only the low-bits of the full timestamp, not ctime/mtime\n> themselves.\n> \n> But stepping back a bit, why do we need to include them in the\n> output?  What workflow and use case are we trying to help?  Dump\n> output from \"stat <path>\" equivalent from ls-files and compare with\n> \"stat .\" output to see which ones are stale?  Or is there any value\n> to see the value of, say, ctime as an individual data item?\n> \n>>> +dev::\n>>> +\tThe ID of device containing file which is in the index.\n>>> +ino::\n>>> +\tThe inode number of file which is in the index.\n>>> +uid::\n>>> +\tThe user id of file owner which is in the index.\n>>> +gid::\n>>> +\tThe group id of file owner which is in the index.\n> \n> Again, why do we need to include these in the output?\n> \n> Wouldn't it be sufficient, as well as a lot more useful, to show a\n> single bit \"the cached stat info matches what is in the working tree\n> (yes/no)\"?\n\nThat does sound useful\n\n>>> +flags::\n>>> +\tThe flags of the file in the index which include\n>>> +\tin-memory only flags and some extended on-disk flags.\n>>\n>> If %(flags) is going to be useful then I think we need to think about\n>> how they are printed and document that. At the moment they are printed\n>> as a hexadecimal number which is fine for debugging but probably not\n>> going to be useful for something like --format. I think printing\n>> documented symbolic names with some kind of separator (a comma maybe)\n>> between them is probably more useful\n> \n> I am guessing that most of the above are only useful for curious\n> geeks and those who are debugging their new tweak to the code that\n> touches the index, i.e. a debugging feature.  But these folks can\n> run \"git\" under a debugger, and they probably have to do so when\n> they are seeing an unexpected value in the flags member of a cache\n> entry anyway.  So I am not sure whom this field is intended to help.\n\nI wondered about that as well, but thought there might be a plausible \nuse if someone wants to check if an entry is marked intent-to-add, or \nhas the skip-worktree/spare-index bits set (are there other ways to \ninspect those?)\n\nBest Wishes\n\nPhillip\n"},{"id":"457843","messageId":"220624.86pmiyi498.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"xmqqv8sr1iex.fsf@gitster.g","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-06-24T13:20:31Z","receivedAt":"2022-06-24T13:24:08Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jun 23 2022, Junio C Hamano wrote:\n\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n>> Thanks for re-rolling, having taken a look a closer look at the tests\n>> I'm concerned about the output format for some of the specifiers, see\n>> below.\n>\n> Thanks for raising these issues.  I agree with you on many of them.\n> In addition to what you covered ....\n>\n>>> +path::\n>>> +\tThe pathname of the file which is in the index.\n>> I think that for all these it might be clearer to say \"recorded in the\n>> index\" rather than \"of the file which is in the index\"\n>\n> I think we would call this \"name\".  The name of the existing option\n> that controls how they are shown is \"--full-name\", not \"--full-path\",\n> for example.\n\nTo the extent that we got this wrong it was me in 455923e0a15 (ls-tree:\nintroduce \"--format\" option, 2022-03-23), but given that we have that I\nthink it makes sense to have this be consistent with ls-tree.\n\nFWIW ls-tree also uses \"name\" options, but its docs talked about\n\"<path>\", so I thought it was more helpful to pick that.\n\nWe also say that we will \"show the full path names\" in that\ndocumentation.\n"},{"id":"457844","messageId":"220624.86letmi383.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"pull.1262.v3.git.1655777140231.gitgitgadget@gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-06-24T13:25:13Z","receivedAt":"2022-06-24T13:46:28Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Jun 21 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n> [...]\n> +\tif (skip_prefix(start, \"(objectmode)\", &p))\n> +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n> +\telse if (skip_prefix(start, \"(objectname)\", &p))\n> +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n> +\telse if (skip_prefix(start, \"(stage)\", &p))\n> +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n> +\telse if (skip_prefix(start, \"(eol)\", &p))\n> +\t\twrite_eolinfo_to_buf(sb, data->istate,\n> +\t\t\t\t     data->ce, data->pathname);\n> +\telse if (skip_prefix(start, \"(path)\", &p))\n> +\t\twrite_name_to_buf(sb, data->pathname);\n> +\telse if (skip_prefix(start, \"(ctime)\", &p))\n> +\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n> +\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n> +\telse if (skip_prefix(start, \"(mtime)\", &p))\n> +\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n> +\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n> +\telse if (skip_prefix(start, \"(dev)\", &p))\n> +\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n> +\telse if (skip_prefix(start, \"(ino)\", &p))\n> +\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n> +\telse if (skip_prefix(start, \"(uid)\", &p))\n> +\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n> +\telse if (skip_prefix(start, \"(gid)\", &p))\n> +\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n> +\telse if (skip_prefix(start, \"(size)\", &p))\n> +\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n> +\telse if (skip_prefix(start, \"(flags)\", &p))\n> +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n\n\nIn my mind almost the entire point of a --format is that you can\ne.g. \\0-delimit it, and don't need to do other parsing games.\n\nSo this really should be adding just e.g. \"%x\", not \"flags: %x\", \n\nSimilarly, let's no have :-delimited fields. First, for a formatted\nnumber \"1656077225:850723245\" is just bizarre for %(ctime), let's use\n\".\", not \":\", so: \"1656077225.850723245\".\n\nAnd let's call that %(ctime), then have (which is trivial to add) a\n%(ctime:sec) and %(ctime:nsec), so someone who wants to format this can\nparse it as they please, ditto for mtime.\n\nLooking at your tests it seemed you went down the route of aligning the\noutput with the --debug output, which is already pre-formatted. I.e. to\nmake what you have here match:\n\n                printf(\"  ctime: %u:%u\\n\", sd->sd_ctime.sec, sd->sd_ctime.nsec);\n                printf(\"  mtime: %u:%u\\n\", sd->sd_mtime.sec, sd->sd_mtime.nsec);\n                printf(\"  dev: %u\\tino: %u\\n\", sd->sd_dev, sd->sd_ino);\n                printf(\"  uid: %u\\tgid: %u\\n\", sd->sd_uid, sd->sd_gid);\n                printf(\"  size: %u\\tflags: %x\\n\", sd->sd_size, ce->ce_flags);\n\nI think that's a mistake, we should be able to emit those individual\n%-specifiers instead, not that line as-is without the \" \" prefix and\n\"\\n\" suffix.\n\n> +\n> +\tif (format && (show_stage || show_others || show_killed ||\n> +\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n> +\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n\nUse usage_msg_opt() or usage_msg_optf() here instead of die(), and no\nneed to include \"ls-files \" in the message.\n\nSee die_for_incompatible_opt4, maybe you can just use that instead? A\nbit painful, but:\n\n    die_for_incompatible_opt4(format, \"--format\", show_stage, \"-s\", show_others, \"-o\", show_killed, \"-k\");\n    die_for_incompatible_opt4(format, \"--format\", show_resolve_undo, \"--resolve-undo\", skipping_duplicates, \"--deduplicate\", debug_mode, \"--debug\");\n\nBut urgh, that helper really should use usage_msg_opt() instead, but\nusing it for now as-is probably sucks less.\n\nI also think we should not forbid combining this wtih --debug, it's\nhelpful to construct a format. This seems to work:\n\t\t\n\tdiff --git a/builtin/ls-files.c b/builtin/ls-files.c\n\tindex 387641b32df..82f13edef7e 100644\n\t--- a/builtin/ls-files.c\n\t+++ b/builtin/ls-files.c\n\t@@ -343,12 +343,17 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n\t \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n\t \t\tif (format) {\n\t \t\t\tshow_ce_fmt(repo, ce, format, fullname);\n\t-\t\t\treturn;\n\t+\t\t\tif (!debug_mode)\n\t+\t\t\t\treturn;\n\t \t\t}\n\t \n\t \t\ttag = get_tag(ce, tag);\n\t \n\t-\t\tif (!show_stage) {\n\t+\t\tif (format) {\n\t+\t\t\tif (!debug_mode)\n\t+\t\t\t\tBUG(\"unreachable\");\n\t+\t\t\t; /* for --debug */\n\t+\t\t} else if (!show_stage) {\n\t \t\t\tfputs(tag, stdout);\n\t \t\t} else {\n\t \t\t\tprintf(\"%s%06o %s %d\\t\",\n\t@@ -814,7 +819,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n\t \t}\n\t \n\t \tif (format && (show_stage || show_others || show_killed ||\n\t-\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n\t+\t\tshow_resolve_undo || skipping_duplicates))\n\t \t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n\t \n\t \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n\t\nI.e. we'll get:\n\t\n\t$ ./git ls-files --debug --format='<%(flags) %(path)>'  -- po/is.po\n\t<flags: 0 po/is.po>\n\tpo/is.po\n\t  ctime: 1654300098:369653868\n\t  mtime: 1654300098:369653868\n\t  dev: 2306     ino: 10487322\n\t  uid: 1001     gid: 1001\n\t  size: 3370    flags: 0\n\nWhich I think is quite useful when poking around in this an coming up\nwith a format.\n\n> +\n>  \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n>  \t\ttag_cached = \"H \";\n>  \t\ttag_unmerged = \"M \";\n> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> new file mode 100755\n> index 00000000000..8c3ef2df138\n> --- /dev/null\n> +++ b/t/t3013-ls-files-format.sh\n> @@ -0,0 +1,124 @@\n> +#!/bin/sh\n> +\n> +test_description='git ls-files --format test'\n> +\n\nAdd this line here:\n\nTEST_PASSES_SANITIZE_LEAK=true\n\nI.e. just before test-lib.sh, see other test examples. Then we'll test\nthis under SANITIZE=leak in CI, to ensure it doesn't leak memory.\n\n> +. ./test-lib.sh\n> +\n> +test_expect_success 'setup' '\n> +\techo o1 >o1 &&\n> +\techo o2 >o2 &&\n> +\tgit add o1 o2 &&\n> +\tgit add --chmod +x o1 &&\n> +\tgit commit -m base\n> +'\n> +\n> [...]\n\n> +for flag in -s -o -k --resolve-undo --deduplicate --debug\n> +do\n> +\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n> +\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n> +\t'\n> +done\n\nNit: I think it's good to move these sotrs of tests before \"setup\", and\ngive them a \"usage: \" prefix, see some other existing examples.\n\nWe usually use test_expect_code 129 for those, depending on if you'll\nend up with die() or not...\n\nnit: missing \\n before this line:\n\n> +test_done\n>\n> base-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n\n"},{"id":"457847","messageId":"xmqqy1xmw06y.fsf@gitster.g","threadId":"58016","inReplyTo":"220624.86pmiyi498.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-24T15:28:05Z","receivedAt":"2022-06-24T15:28:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:\n\n> We also say that we will \"show the full path names\" in that\n> documentation.\n\nThe primary issue is not the presence of \"name\" there, but the lack\nof \"path\" in the word chosen.\n\nMany things can have \"name\" (including \"object name\"), and \"path\",\nnot \"name\", in \"path name\" is what clarifies what kind of name it\nis.  Given that --format placeholders include \"objectname\", it does\nnot make a good design to use \"name\" alone without saying what kind\nof \"name\" it is.\n\nCalling it \"pathname\", not just \"path\", is perfectly OK.  But if\nthere is no other things the word \"path\" could refer to in this\ncontext, which I think is the case here, \"path\" would be acceptable.\n"},{"id":"457850","messageId":"xmqqsfnuvzxc.fsf@gitster.g","threadId":"58016","inReplyTo":"220624.86letmi383.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-24T15:33:51Z","receivedAt":"2022-06-24T15:34:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:\n\n> On Tue, Jun 21 2022, ZheNing Hu via GitGitGadget wrote:\n>> +\tif (skip_prefix(start, \"(objectmode)\", &p))\n>> +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n>> +\telse if (skip_prefix(start, \"(objectname)\", &p))\n>> +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n>> +\telse if (skip_prefix(start, \"(stage)\", &p))\n>> +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n>> +\telse if (skip_prefix(start, \"(path)\", &p))\n>> +\t\twrite_name_to_buf(sb, data->pathname);\n\nThese are just \"values\".\n\n>> +\telse if (skip_prefix(start, \"(ctime)\", &p))\n>> +\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n>> +\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n>> +\telse if (skip_prefix(start, \"(mtime)\", &p))\n>> +\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n>> +\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n>> +\telse if (skip_prefix(start, \"(dev)\", &p))\n>> +\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n>> +\telse if (skip_prefix(start, \"(ino)\", &p))\n>> +\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n>> +\telse if (skip_prefix(start, \"(uid)\", &p))\n>> +\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n>> +\telse if (skip_prefix(start, \"(gid)\", &p))\n>> +\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n>> +\telse if (skip_prefix(start, \"(size)\", &p))\n>> +\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n>> +\telse if (skip_prefix(start, \"(flags)\", &p))\n>> +\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n\nThese are not.\n\n> In my mind almost the entire point of a --format is that you can\n> e.g. \\0-delimit it, and don't need to do other parsing games.\n>\n> So this really should be adding just e.g. \"%x\", not \"flags: %x\", \n\nYes.  A very good point, if we were showing these fields (I already\nsaid I doubt it is useful), they should also show just \"values\"\nAfter all, people can do \"--format=mode: %(objectmode)\" if they want\nan identifying tag before the value.\n\nThanks.\n"},{"id":"457875","messageId":"CAOLTT8R1fQoH7cEWcE42+fjxu_iGBxDJy5Cgff6Bj9YH2f0--g@mail.gmail.com","threadId":"58016","inReplyTo":"080f65b3-91f5-7b68-4235-4bfb956c8321@gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-26T13:01:36Z","receivedAt":"2022-06-26T13:01:57Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> 于2022年6月23日周四 22:06写道：\n>\n> Hi ZheNing\n> > [...]\n> > +It is possible to print in a custom format by using the `--format`\n> > +option, which is able to interpolate different fields using\n> > +a `%(fieldname)` notation. For example, if you only care about the\n> > +\"objectname\" and \"path\" fields, you can execute with a specific\n> > +\"--format\" like\n> > +\n> > +     git ls-files --format='%(objectname) %(path)'\n> > +\n> > +FIELD NAMES\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n> > +\n> > +objectmode::\n> > +     The mode of the file which is in the index.\n> > +objectname::\n> > +     The name of the file which is in the index.\n> > +stage::\n> > +     The stage of the file which is in the index.\n> > +eol::\n> > +     The <eolinfo> and <eolattr> of files both in the\n> > +     index and the work-tree.\n>\n> Looking at the test for this option I think it needs more work, why\n> should --format arbitrarily append a tab to the end of the output? - the\n> user should be able to specify a separator if they want one as part of\n> the format string. Also I'm not sure why there is so much whitespace in\n> the output.\n>\n\nBecause I used old output format in write_eolinfo(), now I think it's wrong,\nI will separate it to three parts: %(eolinfo:index), %(eolinfo:worktree),\n%(eolattr).\n\n> If %(flags) is going to be useful then I think we need to think about\n> how they are printed and document that. At the moment they are printed\n> as a hexadecimal number which is fine for debugging but probably not\n> going to be useful for something like --format. I think printing\n> documented symbolic names with some kind of separator (a comma maybe)\n> between them is probably more useful\n>\n\nAgree.\n\n>  > [...]\n> > +test_expect_success 'git ls-files --format eol' '\n> > +     printf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n> > +     printf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n> > +     git ls-files --format=\"%(eol)\" --eol >actual &&\n>\n> I'm not sure why this is passing --eol as well as --format='%(eol)' -\n> shouldn't that combination of flags be an error?\n>\n\nThank you for reminding, will be corrected.\n\n> Best Wishes\n>\n> Phillip\n\nZheNing Hu\n"},{"id":"457876","messageId":"CAOLTT8Swhow5yvcTbPfmqNCedhJTxwxK0=-C7mqey8DxCA3EwA@mail.gmail.com","threadId":"58016","inReplyTo":"db23bf2a-e661-906f-f8ed-fc2ab3d3ce41@gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-26T13:05:21Z","receivedAt":"2022-06-26T13:05:34Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> 于2022年6月24日周五 18:16写道：\n\n> >>> +ctime::\n> >>> +   The create time of file which is in the index.\n> >>\n> >> This is printed with a prefix 'ctime:' (the same applies to the format\n> >> specifiers below) I think we should omit that and just print the data\n> >> so the user can choose the format they want.\n> >>\n> >>> +mtime::\n> >>> +   The modified time of file which is in the index.\n> >\n> > These are only the low-bits of the full timestamp, not ctime/mtime\n> > themselves.\n> >\n> > But stepping back a bit, why do we need to include them in the\n> > output?  What workflow and use case are we trying to help?  Dump\n> > output from \"stat <path>\" equivalent from ls-files and compare with\n> > \"stat .\" output to see which ones are stale?  Or is there any value\n> > to see the value of, say, ctime as an individual data item?\n> >\n> >>> +dev::\n> >>> +   The ID of device containing file which is in the index.\n> >>> +ino::\n> >>> +   The inode number of file which is in the index.\n> >>> +uid::\n> >>> +   The user id of file owner which is in the index.\n> >>> +gid::\n> >>> +   The group id of file owner which is in the index.\n> >\n> > Again, why do we need to include these in the output?\n> >\n> > Wouldn't it be sufficient, as well as a lot more useful, to show a\n> > single bit \"the cached stat info matches what is in the working tree\n> > (yes/no)\"?\n>\n> That does sound useful\n>\n> >>> +flags::\n> >>> +   The flags of the file in the index which include\n> >>> +   in-memory only flags and some extended on-disk flags.\n> >>\n> >> If %(flags) is going to be useful then I think we need to think about\n> >> how they are printed and document that. At the moment they are printed\n> >> as a hexadecimal number which is fine for debugging but probably not\n> >> going to be useful for something like --format. I think printing\n> >> documented symbolic names with some kind of separator (a comma maybe)\n> >> between them is probably more useful\n> >\n> > I am guessing that most of the above are only useful for curious\n> > geeks and those who are debugging their new tweak to the code that\n> > touches the index, i.e. a debugging feature.  But these folks can\n> > run \"git\" under a debugger, and they probably have to do so when\n> > they are seeing an unexpected value in the flags member of a cache\n> > entry anyway.  So I am not sure whom this field is intended to help.\n>\n> I wondered about that as well, but thought there might be a plausible\n> use if someone wants to check if an entry is marked intent-to-add, or\n> has the skip-worktree/spare-index bits set (are there other ways to\n> inspect those?)\n>\n\nI think this feature will be useful too, but it may not belong to this patch.\nWe can discuss how to implement it later.\n\n> Best Wishes\n>\n> Phillip\n\nZheNing Hu\n"},{"id":"457885","messageId":"CAOLTT8RPrtgcsrrdPwNTcD2WUozThaN_QRYu+JOO2er0F8a8-w@mail.gmail.com","threadId":"58016","inReplyTo":"220624.86letmi383.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-26T13:34:06Z","receivedAt":"2022-06-26T13:34:21Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年6月24日周五 21:46写道：\n>\n>\n> On Tue, Jun 21 2022, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> > [...]\n>\n> In my mind almost the entire point of a --format is that you can\n> e.g. \\0-delimit it, and don't need to do other parsing games.\n>\n> So this really should be adding just e.g. \"%x\", not \"flags: %x\",\n>\n\nYeah, I admit that there really shouldn't use extra formatting here.\n\n> Similarly, let's no have :-delimited fields. First, for a formatted\n> number \"1656077225:850723245\" is just bizarre for %(ctime), let's use\n> \".\", not \":\", so: \"1656077225.850723245\".\n>\n> And let's call that %(ctime), then have (which is trivial to add) a\n> %(ctime:sec) and %(ctime:nsec), so someone who wants to format this can\n> parse it as they please, ditto for mtime.\n>\n> Looking at your tests it seemed you went down the route of aligning the\n> output with the --debug output, which is already pre-formatted. I.e. to\n> make what you have here match:\n>\n>                 printf(\"  ctime: %u:%u\\n\", sd->sd_ctime.sec, sd->sd_ctime.nsec);\n>                 printf(\"  mtime: %u:%u\\n\", sd->sd_mtime.sec, sd->sd_mtime.nsec);\n>                 printf(\"  dev: %u\\tino: %u\\n\", sd->sd_dev, sd->sd_ino);\n>                 printf(\"  uid: %u\\tgid: %u\\n\", sd->sd_uid, sd->sd_gid);\n>                 printf(\"  size: %u\\tflags: %x\\n\", sd->sd_size, ce->ce_flags);\n>\n> I think that's a mistake, we should be able to emit those individual\n> %-specifiers instead, not that line as-is without the \" \" prefix and\n> \"\\n\" suffix.\n>\n\nYeah, agree. But now I just want to delete all atoms from %(ctime) to %(flags),\nand let --debug can work with --format.\n\n> > +\n> > +     if (format && (show_stage || show_others || show_killed ||\n> > +             show_resolve_undo || skipping_duplicates || debug_mode))\n> > +                     die(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n>\n> Use usage_msg_opt() or usage_msg_optf() here instead of die(), and no\n> need to include \"ls-files \" in the message.\n>\n> See die_for_incompatible_opt4, maybe you can just use that instead? A\n> bit painful, but:\n>\n>     die_for_incompatible_opt4(format, \"--format\", show_stage, \"-s\", show_others, \"-o\", show_killed, \"-k\");\n>     die_for_incompatible_opt4(format, \"--format\", show_resolve_undo, \"--resolve-undo\", skipping_duplicates, \"--deduplicate\", debug_mode, \"--debug\");\n>\n\nGood suggestion. I am curious about why there is no function like\ndie_for_incompatible_opt4() with variable parameters?\n\n> But urgh, that helper really should use usage_msg_opt() instead, but\n> using it for now as-is probably sucks less.\n>\n> I also think we should not forbid combining this wtih --debug, it's\n> helpful to construct a format. This seems to work:\n>\n>         diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n>         index 387641b32df..82f13edef7e 100644\n>         --- a/builtin/ls-files.c\n>         +++ b/builtin/ls-files.c\n>         @@ -343,12 +343,17 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n>                                           S_ISGITLINK(ce->ce_mode))) {\n>                         if (format) {\n>                                 show_ce_fmt(repo, ce, format, fullname);\n>         -                       return;\n>         +                       if (!debug_mode)\n>         +                               return;\n>                         }\n>\n>                         tag = get_tag(ce, tag);\n>\n>         -               if (!show_stage) {\n>         +               if (format) {\n>         +                       if (!debug_mode)\n>         +                               BUG(\"unreachable\");\n>         +                       ; /* for --debug */\n>         +               } else if (!show_stage) {\n>                                 fputs(tag, stdout);\n>                         } else {\n>                                 printf(\"%s%06o %s %d\\t\",\n>         @@ -814,7 +819,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n>                 }\n>\n>                 if (format && (show_stage || show_others || show_killed ||\n>         -               show_resolve_undo || skipping_duplicates || debug_mode))\n>         +               show_resolve_undo || skipping_duplicates))\n>                                 die(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n>\n>                 if (show_tag || show_valid_bit || show_fsmonitor_bit) {\n>\n> I.e. we'll get:\n>\n>         $ ./git ls-files --debug --format='<%(flags) %(path)>'  -- po/is.po\n>         <flags: 0 po/is.po>\n>         po/is.po\n>           ctime: 1654300098:369653868\n>           mtime: 1654300098:369653868\n>           dev: 2306     ino: 10487322\n>           uid: 1001     gid: 1001\n>           size: 3370    flags: 0\n>\n> Which I think is quite useful when poking around in this an coming up\n> with a format.\n>\n\nMaybe something like this will be easier?\n\n\n@@ -343,6 +335,7 @@ static void show_ce(struct repository *repo,\nstruct dir_struct *dir,\n                                  S_ISGITLINK(ce->ce_mode))) {\n                if (format) {\n                        show_ce_fmt(repo, ce, format, fullname);\n+                       print_debug(ce);\n                        return;\n                }\n\n\n> > +\n> >       if (show_tag || show_valid_bit || show_fsmonitor_bit) {\n> >               tag_cached = \"H \";\n> >               tag_unmerged = \"M \";\n> > diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> > new file mode 100755\n> > index 00000000000..8c3ef2df138\n> > --- /dev/null\n> > +++ b/t/t3013-ls-files-format.sh\n> > @@ -0,0 +1,124 @@\n> > +#!/bin/sh\n> > +\n> > +test_description='git ls-files --format test'\n> > +\n>\n> Add this line here:\n>\n> TEST_PASSES_SANITIZE_LEAK=true\n>\n> I.e. just before test-lib.sh, see other test examples. Then we'll test\n> this under SANITIZE=leak in CI, to ensure it doesn't leak memory.\n>\n> > +. ./test-lib.sh\n> > +\n> > +test_expect_success 'setup' '\n> > +     echo o1 >o1 &&\n> > +     echo o2 >o2 &&\n> > +     git add o1 o2 &&\n> > +     git add --chmod +x o1 &&\n> > +     git commit -m base\n> > +'\n> > +\n> > [...]\n>\n> > +for flag in -s -o -k --resolve-undo --deduplicate --debug\n> > +do\n> > +     test_expect_success \"git ls-files --format is incompatible with $flag\" '\n> > +             test_must_fail git ls-files --format=\"%(objectname)\" $flag\n> > +     '\n> > +done\n>\n> Nit: I think it's good to move these sotrs of tests before \"setup\", and\n> give them a \"usage: \" prefix, see some other existing examples.\n>\n\nAgree.\n\n> We usually use test_expect_code 129 for those, depending on if you'll\n> end up with die() or not...\n>\n> nit: missing \\n before this line:\n>\n> > +test_done\n> >\n> > base-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n>\n\nZheNing Hu\n"},{"id":"457886","messageId":"CAOLTT8Tc95-aUE+uN2d8QjTJpGpGw6cBJfG+bpmyE55OcXTSRA@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqsfnuvzxc.fsf@gitster.g","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-26T13:35:46Z","receivedAt":"2022-06-26T13:36:07Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年6月24日周五 23:33写道：\n>\n> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:\n>\n> > On Tue, Jun 21 2022, ZheNing Hu via GitGitGadget wrote:\n> >> +    if (skip_prefix(start, \"(objectmode)\", &p))\n> >> +            strbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n> >> +    else if (skip_prefix(start, \"(objectname)\", &p))\n> >> +            strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n> >> +    else if (skip_prefix(start, \"(stage)\", &p))\n> >> +            strbuf_addf(sb, \"%d\", ce_stage(data->ce));\n> >> +    else if (skip_prefix(start, \"(path)\", &p))\n> >> +            write_name_to_buf(sb, data->pathname);\n>\n> These are just \"values\".\n>\n> >> +    else if (skip_prefix(start, \"(ctime)\", &p))\n> >> +            strbuf_addf(sb, \"ctime: %u:%u\",\n> >> +                        sd->sd_ctime.sec, sd->sd_ctime.nsec);\n> >> +    else if (skip_prefix(start, \"(mtime)\", &p))\n> >> +            strbuf_addf(sb, \"mtime: %u:%u\",\n> >> +                        sd->sd_mtime.sec, sd->sd_mtime.nsec);\n> >> +    else if (skip_prefix(start, \"(dev)\", &p))\n> >> +            strbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n> >> +    else if (skip_prefix(start, \"(ino)\", &p))\n> >> +            strbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n> >> +    else if (skip_prefix(start, \"(uid)\", &p))\n> >> +            strbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n> >> +    else if (skip_prefix(start, \"(gid)\", &p))\n> >> +            strbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n> >> +    else if (skip_prefix(start, \"(size)\", &p))\n> >> +            strbuf_addf(sb, \"size: %u\", sd->sd_size);\n> >> +    else if (skip_prefix(start, \"(flags)\", &p))\n> >> +            strbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n>\n> These are not.\n>\nAgree. So I just remove them as you see. If someone else\nneed them for some reason, we can add them back.\n\nZheNing Hu\n"},{"id":"457888","messageId":"pull.1262.v4.git.1656257376109.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v3.git.1655777140231.gitgitgadget@gmail.com","subject":"[PATCH v4] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-06-26T15:29:35Z","receivedAt":"2022-06-26T15:29:43Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, --resolve-undo,\n--deduplicate and --eol.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v3->v4:\n    \n     1. Let --format compatible with --debug.\n     2. Let --format incompatible with --eol.\n     3. Split %(eol) to three atom: %(eolinfo:index), %(eolinfo:worktree)\n        and %(eolattr).\n     4. Remove %(ctime), %(mtime), %(dev), %(ino), %(uid), %(gid), %(size),\n        %(flags).\n     5. Fix output format without some dirty \"prefix\".\n     6. Change some test.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v4\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v3:\n\n 1:  aaafa35ffcd ! 1:  6827e44e158 ls-files: introduce \"--format\" option\n     @@ Commit message\n          command.\n      \n          --format cannot used with -s, -o, -k, --resolve-undo,\n     -    --deduplicate and --debug.\n     +    --deduplicate and --eol.\n      \n          Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n      \n     @@ Documentation/git-ls-files.txt: followed by the  (\"attr/<eolattr>\").\n      +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n      +\tinterpolates to character with hex code `xx`; for example `%00`\n      +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n     -+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo` and\n     -+\t`--debug`.\n     ++\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`\n     ++\tand `--eol`.\n       \\--::\n       \tDo not interpret any more arguments as options.\n       \n     @@ Documentation/git-ls-files.txt: quoted as explained for the configuration variab\n      +names can be used:\n      +\n      +objectmode::\n     -+\tThe mode of the file which is in the index.\n     ++\tThe mode of the file which is recorded in the index.\n      +objectname::\n     -+\tThe name of the file which is in the index.\n     ++\tThe name of the file which is recorded in the index.\n      +stage::\n     -+\tThe stage of the file which is in the index.\n     -+eol::\n     -+\tThe <eolinfo> and <eolattr> of files both in the\n     -+\tindex and the work-tree.\n     ++\tThe stage of the file which is recorded in the index.\n     ++eolinfo:index::\n     ++\tThe <eolinfo> of the file which is recorded in the index.\n     ++eolinfo:worktree::\n     ++\tThe <eolinfo> of the file which is recorded in the working tree.\n     ++eolattr::\n     ++\tThe <eolattr> of the file which is recorded in the index.\n      +path::\n     -+\tThe pathname of the file which is in the index.\n     -+ctime::\n     -+\tThe create time of file which is in the index.\n     -+mtime::\n     -+\tThe modified time of file which is in the index.\n     -+dev::\n     -+\tThe ID of device containing file which is in the index.\n     -+ino::\n     -+\tThe inode number of file which is in the index.\n     -+uid::\n     -+\tThe user id of file owner which is in the index.\n     -+gid::\n     -+\tThe group id of file owner which is in the index.\n     -+size::\n     -+\tThe size of the file which is in the index.\n     -+flags::\n     -+\tThe flags of the file in the index which include\n     -+\tin-memory only flags and some extended on-disk flags.\n     ++\tThe pathname of the file which is recorded in the index.\n       \n       EXCLUDE PATTERNS\n       ----------------\n     @@ builtin/ls-files.c: static char *ps_matched;\n       \n       static const char *tag_cached = \"\";\n       static const char *tag_unmerged = \"\";\n     -@@ builtin/ls-files.c: static const char *tag_modified = \"\";\n     - static const char *tag_skip_worktree = \"\";\n     - static const char *tag_resolve_undo = \"\";\n     - \n     --static void write_eolinfo(struct index_state *istate,\n     --\t\t\t  const struct cache_entry *ce, const char *path)\n     -+static void write_eolinfo_internal(struct strbuf *sb, struct index_state *istate,\n     -+\t\t\t\t   const struct cache_entry *ce, const char *path)\n     - {\n     - \tif (show_eol) {\n     - \t\tstruct stat st;\n      @@ builtin/ls-files.c: static void write_eolinfo(struct index_state *istate,\n     - \t\t\t\t\t\t\t       ce->name);\n     - \t\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n     - \t\t\tw_txt = get_wt_convert_stats_ascii(path);\n     --\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n     -+\t\tif (sb)\n     -+\t\t\tstrbuf_addf(sb, \"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n     -+\t\telse\n     -+\t\t\tprintf(\"i/%-5s w/%-5s attr/%-17s\\t\", i_txt, w_txt, a_txt);\n       \t}\n       }\n       \n     -+static void write_eolinfo(struct index_state *istate,\n     -+\t\t\t  const struct cache_entry *ce, const char *path)\n     ++static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n     ++\t\t\t\t       const struct cache_entry *ce)\n     ++{\n     ++\tconst char *i_txt = \"\";\n     ++\tif (ce && S_ISREG(ce->ce_mode))\n     ++\t\ti_txt = get_cached_convert_stats_ascii(istate, ce->name);\n     ++\tstrbuf_addstr(sb, i_txt);\n     ++}\n     ++\n     ++static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n      +{\n     -+\twrite_eolinfo_internal(NULL, istate, ce, path);\n     ++\tstruct stat st;\n     ++\tconst char *w_txt = \"\";\n     ++\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n     ++\t\tw_txt = get_wt_convert_stats_ascii(path);\n     ++\tstrbuf_addstr(sb, w_txt);\n      +}\n      +\n     -+static void write_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n     -+\t\t\t\t const struct cache_entry *ce, const char *path)\n     ++static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n     ++\t\t\t\t const char *path)\n      +{\n     -+\twrite_eolinfo_internal(sb, istate, ce, path);\n     ++\tstrbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n      +}\n      +\n       static void write_name(const char *name)\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n       }\n       \n      +struct show_index_data {\n     -+\tconst char *tag;\n      +\tconst char *pathname;\n      +\tstruct index_state *istate;\n      +\tconst struct cache_entry *ce;\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\tstruct show_index_data *data = context;\n      +\tconst char *end;\n      +\tconst char *p;\n     -+\tconst struct stat_data *sd = &data->ce->ce_stat_data;\n      +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n      +\tif (len)\n      +\t\treturn len;\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n      +\telse if (skip_prefix(start, \"(stage)\", &p))\n      +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n     -+\telse if (skip_prefix(start, \"(eol)\", &p))\n     -+\t\twrite_eolinfo_to_buf(sb, data->istate,\n     -+\t\t\t\t     data->ce, data->pathname);\n     ++\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n     ++\t\twrite_index_eolinfo_to_buf(sb, data->istate, data->ce);\n     ++\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n     ++\t\twrite_worktree_eolinfo_to_buf(sb, data->pathname);\n     ++\telse if (skip_prefix(start, \"(eolattr)\", &p))\n     ++\t\twrite_eolattr_to_buf(sb, data->istate, data->pathname);\n      +\telse if (skip_prefix(start, \"(path)\", &p))\n      +\t\twrite_name_to_buf(sb, data->pathname);\n     -+\telse if (skip_prefix(start, \"(ctime)\", &p))\n     -+\t\tstrbuf_addf(sb, \"ctime: %u:%u\",\n     -+\t\t\t    sd->sd_ctime.sec, sd->sd_ctime.nsec);\n     -+\telse if (skip_prefix(start, \"(mtime)\", &p))\n     -+\t\tstrbuf_addf(sb, \"mtime: %u:%u\",\n     -+\t\t\t    sd->sd_mtime.sec, sd->sd_mtime.nsec);\n     -+\telse if (skip_prefix(start, \"(dev)\", &p))\n     -+\t\tstrbuf_addf(sb, \"dev: %u\", sd->sd_dev);\n     -+\telse if (skip_prefix(start, \"(ino)\", &p))\n     -+\t\tstrbuf_addf(sb, \"ino: %u\", sd->sd_ino);\n     -+\telse if (skip_prefix(start, \"(uid)\", &p))\n     -+\t\tstrbuf_addf(sb, \"uid: %u\", sd->sd_uid);\n     -+\telse if (skip_prefix(start, \"(gid)\", &p))\n     -+\t\tstrbuf_addf(sb, \"gid: %u\", sd->sd_gid);\n     -+\telse if (skip_prefix(start, \"(size)\", &p))\n     -+\t\tstrbuf_addf(sb, \"size: %u\", sd->sd_size);\n     -+\telse if (skip_prefix(start, \"(flags)\", &p))\n     -+\t\tstrbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n      +\telse\n      +\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n      +\n     @@ builtin/ls-files.c: static void show_ce(struct repository *repo, struct dir_stru\n       \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n      +\t\tif (format) {\n      +\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n     ++\t\t\tprint_debug(ce);\n      +\t\t\treturn;\n      +\t\t}\n      +\n     @@ builtin/ls-files.c: int cmd_ls_files(int argc, const char **argv, const char *cm\n       \t}\n      +\n      +\tif (format && (show_stage || show_others || show_killed ||\n     -+\t\tshow_resolve_undo || skipping_duplicates || debug_mode))\n     -+\t\t\tdie(_(\"ls-files --format cannot used with -s, -o, -k, --resolve-undo, --deduplicate, --debug\"));\n     ++\t\tshow_resolve_undo || skipping_duplicates || show_eol))\n     ++\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, \"\n     ++\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n     ++\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n      +\n       \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n       \t\ttag_cached = \"H \";\n     @@ t/t3013-ls-files-format.sh (new)\n      +\n      +test_description='git ls-files --format test'\n      +\n     ++TEST_PASSES_SANITIZE_LEAK=true\n      +. ./test-lib.sh\n      +\n     ++for flag in -s -o -k --resolve-undo --deduplicate --eol\n     ++do\n     ++\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n     ++\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n     ++\t'\n     ++done\n     ++\n      +test_expect_success 'setup' '\n      +\techo o1 >o1 &&\n      +\techo o2 >o2 &&\n     @@ t/t3013-ls-files-format.sh (new)\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format eol' '\n     -+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >expect &&\n     -+\tprintf \"i/lf    w/lf    attr/                 \\t\\n\" >>expect &&\n     -+\tgit ls-files --format=\"%(eol)\" --eol >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format path' '\n     ++test_expect_success 'git ls-files --format eolinfo:index' '\n      +\tcat >expect <<-\\EOF &&\n     -+\to1\n     -+\to2\n     ++\tlf\n     ++\tlf\n      +\tEOF\n     -+\tgit ls-files --format=\"%(path)\" >actual &&\n     ++\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format ctime' '\n     -+\tgit ls-files --debug >out &&\n     -+\tgrep ctime out >expect &&\n     -+\tgit ls-files --format=\"  %(ctime)\" >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format mtime' '\n     -+\tgit ls-files --debug >out &&\n     -+\tgrep mtime out >expect &&\n     -+\tgit ls-files --format=\"  %(mtime)\" >actual &&\n     ++test_expect_success 'git ls-files --format eolinfo:worktree' '\n     ++\tcat >expect <<-\\EOF &&\n     ++\tlf\n     ++\tlf\n     ++\tEOF\n     ++\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format dev and ino' '\n     -+\tgit ls-files --debug >out &&\n     -+\tgrep dev out >expect &&\n     -+\tgit ls-files --format=\"  %(dev)%x09%(ino)\" >actual &&\n     ++test_expect_success 'git ls-files --format eolattr' '\n     ++\tprintf \"\\n\\n\" >expect &&\n     ++\tgit ls-files --format=\"%(eolattr)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format uid and gid' '\n     -+\tgit ls-files --debug >out &&\n     -+\tgrep uid out >expect &&\n     -+\tgit ls-files --format=\"  %(uid)%x09%(gid)\" >actual &&\n     ++test_expect_success 'git ls-files --format path' '\n     ++\tcat >expect <<-\\EOF &&\n     ++\to1\n     ++\to2\n     ++\tEOF\n     ++\tgit ls-files --format=\"%(path)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     @@ t/t3013-ls-files-format.sh (new)\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format size and flags' '\n     -+\tgit ls-files --debug >out &&\n     -+\tgrep size out >expect &&\n     -+\tgit ls-files --format=\"  %(size)%x09%(flags)\" >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n      +test_expect_success 'git ls-files --format imitate --stage' '\n      +\tgit ls-files --stage >expect &&\n      +\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format imitate --debug' '\n     ++test_expect_success 'git ls-files --format with --debug' '\n      +\tgit ls-files --debug >expect &&\n     -+\tgit ls-files --format=\"%(path)%x0a  %(ctime)%x0a  %(mtime)%x0a  %(dev)%x09%(ino)%x0a  %(uid)%x09%(gid)%x0a  %(size)%x09%(flags)\" >actual &&\n     ++\tgit ls-files --format=\"%(path)\" --debug >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+for flag in -s -o -k --resolve-undo --deduplicate --debug\n     -+do\n     -+\ttest_expect_success \"git ls-files --format is incompatible with $flag\" '\n     -+\t\ttest_must_fail git ls-files --format=\"%(objectname)\" $flag\n     -+\t'\n     -+done\n      +test_done\n\n\n Documentation/git-ls-files.txt |  37 ++++++++++-\n builtin/ls-files.c             | 113 +++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 108 +++++++++++++++++++++++++++++++\n 3 files changed, 257 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..38e81cc889f 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,34 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+\tThe <eolinfo> of the file which is recorded in the index.\n+eolinfo:worktree::\n+\tThe <eolinfo> of the file which is recorded in the working tree.\n+eolattr::\n+\tThe <eolattr> of the file which is recorded in the index.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..1d52f5cb90b 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -75,6 +77,30 @@ static void write_eolinfo(struct index_state *istate,\n \t}\n }\n \n+static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t       const struct cache_entry *ce)\n+{\n+\tconst char *i_txt = \"\";\n+\tif (ce && S_ISREG(ce->ce_mode))\n+\t\ti_txt = get_cached_convert_stats_ascii(istate, ce->name);\n+\tstrbuf_addstr(sb, i_txt);\n+}\n+\n+static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n+{\n+\tstruct stat st;\n+\tconst char *w_txt = \"\";\n+\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n+\t\tw_txt = get_wt_convert_stats_ascii(path);\n+\tstrbuf_addstr(sb, w_txt);\n+}\n+\n+static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t const char *path)\n+{\n+\tstrbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n+}\n+\n static void write_name(const char *name)\n {\n \t/*\n@@ -85,6 +111,15 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_add(sb, rel, strlen(rel));\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +257,68 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n+\t\twrite_index_eolinfo_to_buf(sb, data->istate, data->ce);\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n+\t\twrite_worktree_eolinfo_to_buf(sb, data->pathname);\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\twrite_eolattr_to_buf(sb, data->istate, data->pathname);\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+\treturn;\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +333,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +778,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +805,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol))\n+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, \"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..a186fe21126\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,108 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-\\EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolinfo:index' '\n+\tcat >expect <<-\\EOF &&\n+\tlf\n+\tlf\n+\tEOF\n+\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolinfo:worktree' '\n+\tcat >expect <<-\\EOF &&\n+\tlf\n+\tlf\n+\tEOF\n+\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolattr' '\n+\tprintf \"\\n\\n\" >expect &&\n+\tgit ls-files --format=\"%(eolattr)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o3 >o3 &&\n+\tgit add o3 &&\n+\trm o3 &&\n+\tcat >expect <<-\\EOF &&\n+\to3\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"457896","messageId":"xmqq7d52sehc.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8Tc95-aUE+uN2d8QjTJpGpGw6cBJfG+bpmyE55OcXTSRA@mail.gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-27T08:22:07Z","receivedAt":"2022-06-27T08:22:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n>> >> +    else if (skip_prefix(start, \"(path)\", &p))\n>> >> +            write_name_to_buf(sb, data->pathname);\n>>\n>> These are just \"values\".\n>> ...\n>> >> +    else if (skip_prefix(start, \"(size)\", &p))\n>> >> +            strbuf_addf(sb, \"size: %u\", sd->sd_size);\n>> >> +    else if (skip_prefix(start, \"(flags)\", &p))\n>> >> +            strbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n>>\n>> These are not.\n>>\n> ... If someone else\n> need them for some reason, we can add them back.\n\nIf someone else needs to see \"size:\" printed in front of the value\nof sd_size member, we DO NOT HAVE TO DO ANYTHING!  That someone else\ncan write \"--format=size: %(size)\" to do so themselves.\n\n\n"},{"id":"457897","messageId":"xmqq1qvase0o.fsf@gitster.g","threadId":"58016","inReplyTo":"pull.1262.v4.git.1656257376109.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-27T08:32:07Z","receivedAt":"2022-06-27T08:32:13Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Range-diff vs v3:\n> ...\n>       +test_done\n\nI omitted 300 lines of range-diff, which is not exactly illuminating\nin this case.  I wonder if there is a way to turn it off when it is\nnot helping automatically...\n\n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +objectmode::\n> +\tThe mode of the file which is recorded in the index.\n> +objectname::\n> +\tThe name of the file which is recorded in the index.\n> +stage::\n> +\tThe stage of the file which is recorded in the index.\n\n\n> +eolinfo:index::\n> +\tThe <eolinfo> of the file which is recorded in the index.\n> +eolinfo:worktree::\n> +\tThe <eolinfo> of the file which is recorded in the working tree.\n\nThese sound somewhat strange, as the above makes it sound as if we\nare recording eolinfo for something (we never record eolinfo of\nanything anywhere).\n\n\teolinfo:index::\n\teolinfo:worktree::\n        \tThe <eolinfo> (see the description of the `--eol` option) of\n                the contents in the index or in the worktree for the path\n\nperhaps?  I dunno.\n\n> +eolattr::\n> +\tThe <eolattr> of the file which is recorded in the index.\n\nLikewise, eolattr comes from the attribute subsystem and not\nrecorded in the index.  It is more like\n\n\teolattr:\n                The <eolattr> (see the description of the `--eol` option)\n                that applies to the path.\n\nBecause attribute applies to the path, it applies equally to both\nwhat is in the index and what is in the working tree.\n\n> +path::\n> +\tThe pathname of the file which is recorded in the index.\n\nAs ls-tree already uses %(path) for it, this is probably OK\n(otherwise we would probably have called it %(pathname)).\n"},{"id":"457899","messageId":"CAOLTT8RpBXR4PaHkaBADG9rWXyV0tRwsFD0VUqBEvPJVofMCNQ@mail.gmail.com","threadId":"58016","inReplyTo":"xmqq7d52sehc.fsf@gitster.g","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-27T11:06:25Z","receivedAt":"2022-06-27T11:06:40Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年6月27日周一 16:22写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> >> >> +    else if (skip_prefix(start, \"(path)\", &p))\n> >> >> +            write_name_to_buf(sb, data->pathname);\n> >>\n> >> These are just \"values\".\n> >> ...\n> >> >> +    else if (skip_prefix(start, \"(size)\", &p))\n> >> >> +            strbuf_addf(sb, \"size: %u\", sd->sd_size);\n> >> >> +    else if (skip_prefix(start, \"(flags)\", &p))\n> >> >> +            strbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n> >>\n> >> These are not.\n> >>\n> > ... If someone else\n> > need them for some reason, we can add them back.\n>\n> If someone else needs to see \"size:\" printed in front of the value\n> of sd_size member, we DO NOT HAVE TO DO ANYTHING!  That someone else\n> can write \"--format=size: %(size)\" to do so themselves.\n>\n>\n\nOh, sorry, I mean if someone need some atoms from %(size) to %(flags), we can\nadd them back.\n"},{"id":"457900","messageId":"CAOLTT8SkJgOUi5dmSQOsg=E0_WtarPWRM-YRcF96gEAkwbb2TQ@mail.gmail.com","threadId":"58016","inReplyTo":"xmqq1qvase0o.fsf@gitster.g","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-06-27T11:18:51Z","receivedAt":"2022-06-27T11:19:09Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年6月27日周一 16:32写道：\n>\n> \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > Range-diff vs v3:\n> > ...\n> >       +test_done\n>\n> I omitted 300 lines of range-diff, which is not exactly illuminating\n> in this case.  I wonder if there is a way to turn it off when it is\n> not helping automatically...\n>\n\nI have make a issue to gitgitgadget, maybe Johannes Schindelin can\ngive a help: https://github.com/gitgitgadget/gitgitgadget/issues/1024\n\n> > +FIELD NAMES\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n> > +\n> > +objectmode::\n> > +     The mode of the file which is recorded in the index.\n> > +objectname::\n> > +     The name of the file which is recorded in the index.\n> > +stage::\n> > +     The stage of the file which is recorded in the index.\n>\n>\n> > +eolinfo:index::\n> > +     The <eolinfo> of the file which is recorded in the index.\n> > +eolinfo:worktree::\n> > +     The <eolinfo> of the file which is recorded in the working tree.\n>\n> These sound somewhat strange, as the above makes it sound as if we\n> are recording eolinfo for something (we never record eolinfo of\n> anything anywhere).\n>\n>         eolinfo:index::\n>         eolinfo:worktree::\n>                 The <eolinfo> (see the description of the `--eol` option) of\n>                 the contents in the index or in the worktree for the path\n>\n> perhaps?  I dunno.\n>\n> > +eolattr::\n> > +     The <eolattr> of the file which is recorded in the index.\n>\n> Likewise, eolattr comes from the attribute subsystem and not\n> recorded in the index.  It is more like\n>\n>         eolattr:\n>                 The <eolattr> (see the description of the `--eol` option)\n>                 that applies to the path.\n>\n> Because attribute applies to the path, it applies equally to both\n> what is in the index and what is in the working tree.\n>\n\nThanks for clarifying it, I will fix it.\n\n> > +path::\n> > +     The pathname of the file which is recorded in the index.\n>\n> As ls-tree already uses %(path) for it, this is probably OK\n> (otherwise we would probably have called it %(pathname)).\n\nAgree. Unless we want to fix it in git ls-tree too.\n"},{"id":"457911","messageId":"xmqqh746qfkl.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8RpBXR4PaHkaBADG9rWXyV0tRwsFD0VUqBEvPJVofMCNQ@mail.gmail.com","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-06-27T15:41:30Z","receivedAt":"2022-06-27T15:41:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n> Junio C Hamano <gitster@pobox.com> 于2022年6月27日周一 16:22写道：\n>>\n>> ZheNing Hu <adlternative@gmail.com> writes:\n>>\n>> >> >> +    else if (skip_prefix(start, \"(path)\", &p))\n>> >> >> +            write_name_to_buf(sb, data->pathname);\n>> >>\n>> >> These are just \"values\".\n>> >> ...\n>> >> >> +    else if (skip_prefix(start, \"(size)\", &p))\n>> >> >> +            strbuf_addf(sb, \"size: %u\", sd->sd_size);\n>> >> >> +    else if (skip_prefix(start, \"(flags)\", &p))\n>> >> >> +            strbuf_addf(sb, \"flags: %x\", data->ce->ce_flags);\n>> >>\n>> >> These are not.\n>> >>\n>> > ... If someone else\n>> > need them for some reason, we can add them back.\n>>\n>> If someone else needs to see \"size:\" printed in front of the value\n>> of sd_size member, we DO NOT HAVE TO DO ANYTHING!  That someone else\n>> can write \"--format=size: %(size)\" to do so themselves.\n>\n> Oh, sorry, I mean if someone need some atoms from %(size) to %(flags), we can\n> add them back.\n\nAh, I see.  I am not sure about the %(flags) to help the debugging\nmode, but giving a single bit \"is it dirty?\" would be more useful\nthan giving the cached stat info, I would think.\n\nThanks.\n\n"},{"id":"457958","messageId":"220627.86v8smgcx2.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"pull.1262.v4.git.1656257376109.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-06-27T18:34:30Z","receivedAt":"2022-06-27T18:49:03Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Sun, Jun 26 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n>\n> --format cannot used with -s, -o, -k, --resolve-undo,\n> --deduplicate and --eol.\n>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n> [...]\n> +test_expect_success 'git ls-files --format with --debug' '\n> +\tgit ls-files --debug >expect &&\n> +\tgit ls-files --format=\"%(path)\" --debug >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_done\n\nI'm not sure what to make of this.\n\nIn some ways I think this makes more sense than what I suggested in\nhttps://lore.kernel.org/git/220624.86letmi383.gmgdl@evledraar.gmail.com/;\nbut I had to think for a second about what's going on here.\n\nIn my version I suggested having this work with --debug, but not in this\nway, in my version you'd always emit the debug output, and the format\noutput.\n\nBut here e.g.:\n\n    git ls-files -t --debug\n\nWill emit \"H tag.c\" or whatever, but if you add --format the -t option\nis silently discarded.\n\nSo the test is relying on \"%(path)\" being the default format.\n\nI think extending this to e.g. test what happens with \"-t\" would be a\ngood thing, but also in general does combining --format with -t make\nsense, and are there other such options where the combination might not\nmake sense?\n\nSo I'm not 100% sure, but I think I'd prefer my version, but I see how\nit would get hairy to support, e.g.:\n\n    git ls-files -s --debug --format=...\n\nShould work, but you'd have to special-case the logic for erroring if -s\nis combined with --format.\n\nAnyway, I think it would be fine to leave this in whatever state is\neasy, the --debug option \"just for debugging\".\n\nBut re\nhttps://lore.kernel.org/git/CAOLTT8Tc95-aUE+uN2d8QjTJpGpGw6cBJfG+bpmyE55OcXTSRA@mail.gmail.com/\nI think it might be interesting to get --format to a state where we can\nremove --debug entirely.\n\nI.e. in c2a29405105 (t1091/t3705: remove 'test-tool read-cache --table',\n2021-12-22) we could replace some similar test-only code with \"git\nls-files\". I for one wouldn't mind --debug going away entirely, and have\nthe t3705-add-sparse-checkout.sh tests use --format instead.\n\nOr we could keep --debug, but just have it powerful enough to do what\nprint_debug() is doing now, possibly without \"truly internal\" stuff like\n\"ce_flags\".\n"},{"id":"458046","messageId":"a556feaa-5f72-7c83-7156-fe91f18fc24a@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v4.git.1656257376109.gitgitgadget@gmail.com","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-06-28T15:19:25Z","receivedAt":"2022-06-28T15:19:31Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi ZheNing\n\nThis looks good, I don't have much to add beyond the comments others \nhave left.\n\nOn 26/06/2022 16:29, ZheNing Hu via GitGitGadget wrote:\n> From: ZheNing Hu <adlternative@gmail.com> \n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +objectmode::\n> +\tThe mode of the file which is recorded in the index.\n> +objectname::\n> +\tThe name of the file which is recorded in the index.\n> +stage::\n> +\tThe stage of the file which is recorded in the index.\n> +eolinfo:index::\n> +\tThe <eolinfo> of the file which is recorded in the index.\n> +eolinfo:worktree::\n> +\tThe <eolinfo> of the file which is recorded in the working tree.\n> +eolattr::\n> +\tThe <eolattr> of the file which is recorded in the index.\n> +path::\n> +\tThe pathname of the file which is recorded in the index.\n\nI think starting with this shorter list of field names is a good idea, \nwe can always add more fields later if there is a demand for %(flags) etc.\n\n> +test_expect_success 'git ls-files --format with --debug' '\n> +\tgit ls-files --debug >expect &&\n> +\tgit ls-files --format=\"%(path)\" --debug >actual &&\n> +\ttest_cmp expect actual\n> +'\n\nWhat's the motivation for being able to combine --format with --debug?\n\nBest Wishes\n\nPhillip\n"},{"id":"458381","messageId":"CAOLTT8S7PA7Aerhcf=fqDwymmwgcys7+mRby2MkWDsbSyKTLLg@mail.gmail.com","threadId":"58016","inReplyTo":"220627.86v8smgcx2.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-01T12:42:18Z","receivedAt":"2022-07-01T12:42:33Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年6月28日周二 02:48写道：\n>\n>\n> On Sun, Jun 26 2022, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Add a new option --format that output index enties\n> > informations with custom format, taking inspiration\n> > from the option with the same name in the `git ls-tree`\n> > command.\n> >\n> > --format cannot used with -s, -o, -k, --resolve-undo,\n> > --deduplicate and --eol.\n> >\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> > ---\n> > [...]\n> > +test_expect_success 'git ls-files --format with --debug' '\n> > +     git ls-files --debug >expect &&\n> > +     git ls-files --format=\"%(path)\" --debug >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_done\n>\n> I'm not sure what to make of this.\n>\n> In some ways I think this makes more sense than what I suggested in\n> https://lore.kernel.org/git/220624.86letmi383.gmgdl@evledraar.gmail.com/;\n> but I had to think for a second about what's going on here.\n>\n> In my version I suggested having this work with --debug, but not in this\n> way, in my version you'd always emit the debug output, and the format\n> output.\n>\n> But here e.g.:\n>\n>     git ls-files -t --debug\n>\n> Will emit \"H tag.c\" or whatever, but if you add --format the -t option\n> is silently discarded.\n>\n> So the test is relying on \"%(path)\" being the default format.\n>\n> I think extending this to e.g. test what happens with \"-t\" would be a\n> good thing, but also in general does combining --format with -t make\n> sense, and are there other such options where the combination might not\n> make sense?\n>\n\nWhy not we just let -t incompatible with --format? Is this because -t can also\nbe considered a “debug” message, and we often use --debug and -t together?\n\nIf so, we can just do something like:\n\n@@ -238,6 +335,13 @@ static void show_ce(struct repository *repo,\nstruct dir_struct *dir,\n                                  S_ISGITLINK(ce->ce_mode))) {\n                tag = get_tag(ce, tag);\n\n+               if (format) {\n+                       fputs(tag, stdout);\n+                       show_ce_fmt(repo, ce, format, fullname);\n+                       print_debug(ce);\n+                       return;\n+               }\n+\n\n\n> So I'm not 100% sure, but I think I'd prefer my version, but I see how\n> it would get hairy to support, e.g.:\n>\n>     git ls-files -s --debug --format=...\n>\n> Should work, but you'd have to special-case the logic for erroring if -s\n> is combined with --format.\n>\n\nAgree. it's really weird.\n\n> Anyway, I think it would be fine to leave this in whatever state is\n> easy, the --debug option \"just for debugging\".\n>\n> But re\n> https://lore.kernel.org/git/CAOLTT8Tc95-aUE+uN2d8QjTJpGpGw6cBJfG+bpmyE55OcXTSRA@mail.gmail.com/\n> I think it might be interesting to get --format to a state where we can\n> remove --debug entirely.\n>\n> I.e. in c2a29405105 (t1091/t3705: remove 'test-tool read-cache --table',\n> 2021-12-22) we could replace some similar test-only code with \"git\n> ls-files\". I for one wouldn't mind --debug going away entirely, and have\n> the t3705-add-sparse-checkout.sh tests use --format instead.\n>\n> Or we could keep --debug, but just have it powerful enough to do what\n> print_debug() is doing now, possibly without \"truly internal\" stuff like\n> \"ce_flags\".\n\nAh, though we just remove these little \"useless\" atoms, maybe we can add\nthem back later? (not in this patch?)\n\nZheNing Hu\n"},{"id":"458382","messageId":"CAOLTT8QgsdkCrfYY9Of7qgDkvvZc+=Bao4irX6xq_C5wr4vb6Q@mail.gmail.com","threadId":"58016","inReplyTo":"a556feaa-5f72-7c83-7156-fe91f18fc24a@gmail.com","subject":"Re: [PATCH v4] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-01T12:47:57Z","receivedAt":"2022-07-01T12:48:12Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> 于2022年6月28日周二 23:19写道：\n>\n> Hi ZheNing\n>\n> This looks good, I don't have much to add beyond the comments others\n> have left.\n>\n> On 26/06/2022 16:29, ZheNing Hu via GitGitGadget wrote:\n> > From: ZheNing Hu <adlternative@gmail.com>\n> > +FIELD NAMES\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n> > +\n> > +objectmode::\n> > +     The mode of the file which is recorded in the index.\n> > +objectname::\n> > +     The name of the file which is recorded in the index.\n> > +stage::\n> > +     The stage of the file which is recorded in the index.\n> > +eolinfo:index::\n> > +     The <eolinfo> of the file which is recorded in the index.\n> > +eolinfo:worktree::\n> > +     The <eolinfo> of the file which is recorded in the working tree.\n> > +eolattr::\n> > +     The <eolattr> of the file which is recorded in the index.\n> > +path::\n> > +     The pathname of the file which is recorded in the index.\n>\n> I think starting with this shorter list of field names is a good idea,\n> we can always add more fields later if there is a demand for %(flags) etc.\n>\n\nAgree.\n\n> > +test_expect_success 'git ls-files --format with --debug' '\n> > +     git ls-files --debug >expect &&\n> > +     git ls-files --format=\"%(path)\" --debug >actual &&\n> > +     test_cmp expect actual\n> > +'\n>\n> What's the motivation for being able to combine --format with --debug?\n>\n\nI guess it may help us get debug informations together with --format output\nwhen we are doing some tests.\n\n> Best Wishes\n>\n> Phillip\n\nZheNing Hu\n"},{"id":"458383","messageId":"CAOLTT8Qa-s8WEL-3r+JGfEimbXDFQCZvHNYo974_oq5UU-Dhpg@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqh746qfkl.fsf@gitster.g","subject":"Re: [PATCH v3] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-01T13:30:36Z","receivedAt":"2022-07-01T13:30:50Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年6月27日周一 23:41写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> > Oh, sorry, I mean if someone need some atoms from %(size) to %(flags), we can\n> > add them back.\n>\n> Ah, I see.  I am not sure about the %(flags) to help the debugging\n> mode, but giving a single bit \"is it dirty?\" would be more useful\n> than giving the cached stat info, I would think.\n>\n\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 061a205576..ccb3fd1676 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -296,6 +296,8 @@ static size_t expand_show_index(struct strbuf *sb,\nconst char *start,\n                write_eolattr_to_buf(sb, data->istate, data->pathname);\n        else if (skip_prefix(start, \"(path)\", &p))\n                write_name_to_buf(sb, data->pathname);\n+       else if (skip_prefix(start, \"(updatetodate)\", &p))\n+               strbuf_addstr(sb, ce_uptodate(data->ce) ? \"true\" : \"false\");\n        else\n                die(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n\n\nI try to use ce_uptodate(ce) to check its flags, but unfortunately,\ngit ls-files --format=\"%(updatetodate)\" output all files are \"false\" :(\nThat's because we have not mark some flags in the cache entry, right?\n\n> Thanks.\n>\n\nZheNing Hu\n"},{"id":"458467","messageId":"pull.1262.v5.git.1657002760815.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v4.git.1656257376109.gitgitgadget@gmail.com","subject":"[PATCH v5] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-07-05T06:32:40Z","receivedAt":"2022-07-05T06:32:47Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, -t, --resolve-undo,\n--deduplicate and --eol.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v4->v5:\n    \n     1. Let --format incompatible with -t.\n     2. Fix %(eolinfo) and %(eolattr) docs suggested by Junio.\n    \n    Looking forward to Ævar's reviewing.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v5\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v4:\n\n 1:  6827e44e158 ! 1:  1ce69d6202a ls-files: introduce \"--format\" option\n     @@ Commit message\n          from the option with the same name in the `git ls-tree`\n          command.\n      \n     -    --format cannot used with -s, -o, -k, --resolve-undo,\n     +    --format cannot used with -s, -o, -k, -t, --resolve-undo,\n          --deduplicate and --eol.\n      \n          Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n     @@ Documentation/git-ls-files.txt: followed by the  (\"attr/<eolattr>\").\n      +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n      +\tinterpolates to character with hex code `xx`; for example `%00`\n      +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n     -+\t--format cannot be combined with `-s`, `-o`, `-k`, `--resolve-undo`\n     ++\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n      +\tand `--eol`.\n       \\--::\n       \tDo not interpret any more arguments as options.\n     @@ Documentation/git-ls-files.txt: quoted as explained for the configuration variab\n      +stage::\n      +\tThe stage of the file which is recorded in the index.\n      +eolinfo:index::\n     -+\tThe <eolinfo> of the file which is recorded in the index.\n      +eolinfo:worktree::\n     -+\tThe <eolinfo> of the file which is recorded in the working tree.\n     ++\tThe <eolinfo> (see the description of the `--eol` option) of\n     ++\tthe contents in the index or in the worktree for the path.\n      +eolattr::\n     -+\tThe <eolattr> of the file which is recorded in the index.\n     ++\tThe <eolattr> (see the description of the `--eol` option)\n     ++\tthat applies to the path.\n      +path::\n      +\tThe pathname of the file which is recorded in the index.\n       \n     @@ builtin/ls-files.c: int cmd_ls_files(int argc, const char **argv, const char *cm\n       \t}\n      +\n      +\tif (format && (show_stage || show_others || show_killed ||\n     -+\t\tshow_resolve_undo || skipping_duplicates || show_eol))\n     -+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, \"\n     ++\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n     ++\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n      +\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n      +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n      +\n     @@ t/t3013-ls-files-format.sh (new)\n      +TEST_PASSES_SANITIZE_LEAK=true\n      +. ./test-lib.sh\n      +\n     -+for flag in -s -o -k --resolve-undo --deduplicate --eol\n     ++for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n      +do\n      +\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n      +\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n     @@ t/t3013-ls-files-format.sh (new)\n      +\t100755\n      +\t100644\n      +\tEOF\n     -+\tgit ls-files --format=\"%(objectmode)\" -t >actual &&\n     ++\tgit ls-files --format=\"%(objectmode)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n\n\n Documentation/git-ls-files.txt |  38 ++++++++++-\n builtin/ls-files.c             | 113 +++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 108 +++++++++++++++++++++++++++++++\n 3 files changed, 258 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..97d4cebba9f 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,35 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+eolinfo:worktree::\n+\tThe <eolinfo> (see the description of the `--eol` option) of\n+\tthe contents in the index or in the worktree for the path.\n+eolattr::\n+\tThe <eolattr> (see the description of the `--eol` option)\n+\tthat applies to the path.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..79ecdce2c9c 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -75,6 +77,30 @@ static void write_eolinfo(struct index_state *istate,\n \t}\n }\n \n+static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t       const struct cache_entry *ce)\n+{\n+\tconst char *i_txt = \"\";\n+\tif (ce && S_ISREG(ce->ce_mode))\n+\t\ti_txt = get_cached_convert_stats_ascii(istate, ce->name);\n+\tstrbuf_addstr(sb, i_txt);\n+}\n+\n+static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n+{\n+\tstruct stat st;\n+\tconst char *w_txt = \"\";\n+\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n+\t\tw_txt = get_wt_convert_stats_ascii(path);\n+\tstrbuf_addstr(sb, w_txt);\n+}\n+\n+static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n+\t\t\t\t const char *path)\n+{\n+\tstrbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n+}\n+\n static void write_name(const char *name)\n {\n \t/*\n@@ -85,6 +111,15 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_add(sb, rel, strlen(rel));\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +257,68 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n+\t\twrite_index_eolinfo_to_buf(sb, data->istate, data->ce);\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n+\t\twrite_worktree_eolinfo_to_buf(sb, data->pathname);\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\twrite_eolattr_to_buf(sb, data->istate, data->pathname);\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+\treturn;\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +333,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +778,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +805,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..60c415aafd6\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,108 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-\\EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolinfo:index' '\n+\tcat >expect <<-\\EOF &&\n+\tlf\n+\tlf\n+\tEOF\n+\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolinfo:worktree' '\n+\tcat >expect <<-\\EOF &&\n+\tlf\n+\tlf\n+\tEOF\n+\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format eolattr' '\n+\tprintf \"\\n\\n\" >expect &&\n+\tgit ls-files --format=\"%(eolattr)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o3 >o3 &&\n+\tgit add o3 &&\n+\trm o3 &&\n+\tcat >expect <<-\\EOF &&\n+\to3\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"458469","messageId":"220705.86sfng9c5a.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"pull.1262.v5.git.1657002760815.gitgitgadget@gmail.com","subject":"Re: [PATCH v5] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-07-05T08:39:18Z","receivedAt":"2022-07-05T08:50:20Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Jul 05 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n>\n> --format cannot used with -s, -o, -k, -t, --resolve-undo,\n> --deduplicate and --eol.\n>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>     ls-files: introduce \"--format\" options\n>     \n>     v4->v5:\n>     \n>      1. Let --format incompatible with -t.\n>      2. Fix %(eolinfo) and %(eolattr) docs suggested by Junio.\n>     \n>     Looking forward to Ævar's reviewing.\n\nThanks again, I took a look at this and it looks good to me as-is.\n\nIf you do want to further twiddle with it at this point I applied these\nchanges to it locally while poking around, changes:\n\n * Some trivial whitespace between variable decl.\n\n * Removed a \"return;\" at the end of a function\n\n * I found the new write_*() helpers to be uneccesary, what I did spot\n   after seeing if they could be factored out is the existing\n   write_eolinfo() function.\n\n   I see you just copied some of the code from there, but\n   e.g. initializing to \"\" and doing an unconditional strbuf_addstr()\n   looks odd IMO compared to just doing it inline as below.\n\n   I think if helpers are to be introduced here I'd think it would make\n   more sense to split out the small bits of behavior from\n   write_eolinfo() so you can call it picemeal and share the code, but\n   since it's calling trivial external functions I think just calling\n   those directly probably makes more sense...\n\n * Likewise for the test I wondered if you were adding a bug by not\n   reporting when lstat() failed, but then found that this is the same\n   thing we do on --eol.\n\n   So for the tests I think it's better just to demonstrate that we can\n   emit the exact same thing that --eol does with --format.\n\n * We've gone back & forth a bit on whether this would combine with\n   --debug, while it's an internal-only feature it would be nice to have a\n   test for it combined with --format, noting that the behavior might\n   change...\n\n * There is one subtle behavior change here in that I deleted the \"ce\n   &&\" part from write_index_eolinfo_to_buf() when moving the code\n   over. I'm 99% sure this is the right thing to do, as other code in\n   expand_show_index() unconditionally dereferences it.\n\n   So perhaps we don't need that guard in write_eolinfo() either? In any\n   case copy/pasting it over when we're already assuming a non-NULL \"ce\"\n   in the same \"if/elseif/else\" chain looks a bit odd.\n\n   Ah, I see it's because in show_dir_entry() we explicitly pass it as\n   NULL, but that doesn't apply to our new codepath, so as long as we're\n   not sharing that helper with write_eolinfo() it makes sense to not do\n   that check.\n\n   Even then the helper should probably assume \"ce\", and write_eolinfo()\n   itself should do the \"is ce NULL?\" check which is specific to its\n   use-case.\n\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 79ecdce2c9c..cc3cece3830 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -77,30 +77,6 @@ static void write_eolinfo(struct index_state *istate,\n \t}\n }\n \n-static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n-\t\t\t\t       const struct cache_entry *ce)\n-{\n-\tconst char *i_txt = \"\";\n-\tif (ce && S_ISREG(ce->ce_mode))\n-\t\ti_txt = get_cached_convert_stats_ascii(istate, ce->name);\n-\tstrbuf_addstr(sb, i_txt);\n-}\n-\n-static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n-{\n-\tstruct stat st;\n-\tconst char *w_txt = \"\";\n-\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n-\t\tw_txt = get_wt_convert_stats_ascii(path);\n-\tstrbuf_addstr(sb, w_txt);\n-}\n-\n-static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n-\t\t\t\t const char *path)\n-{\n-\tstrbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n-}\n-\n static void write_name(const char *name)\n {\n \t/*\n@@ -114,6 +90,7 @@ static void write_name(const char *name)\n static void write_name_to_buf(struct strbuf *sb, const char *name)\n {\n \tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\n \tif (line_terminator)\n \t\tquote_c_style(rel, sb, NULL, 0);\n \telse\n@@ -270,6 +247,8 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n \tconst char *end;\n \tconst char *p;\n \tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tstruct stat st;\n+\n \tif (len)\n \t\treturn len;\n \tif (*start != '(')\n@@ -288,12 +267,16 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n \t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n \telse if (skip_prefix(start, \"(stage)\", &p))\n \t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n-\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n-\t\twrite_index_eolinfo_to_buf(sb, data->istate, data->ce);\n-\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n-\t\twrite_worktree_eolinfo_to_buf(sb, data->pathname);\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n+\t\t S_ISREG(data->ce->ce_mode))\n+\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n+\t\t\t\t\t\t\t\t data->ce->name));\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n+\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n+\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n \telse if (skip_prefix(start, \"(eolattr)\", &p))\n-\t\twrite_eolattr_to_buf(sb, data->istate, data->pathname);\n+\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n+\t\t\t\t\t\t\t data->pathname));\n \telse if (skip_prefix(start, \"(path)\", &p))\n \t\twrite_name_to_buf(sb, data->pathname);\n \telse\n@@ -310,13 +293,12 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n \t\t.istate = repo->index,\n \t\t.ce = ce,\n \t};\n-\n \tstruct strbuf sb = STRBUF_INIT;\n+\n \tstrbuf_expand(&sb, format, expand_show_index, &data);\n \tstrbuf_addch(&sb, line_terminator);\n \tfwrite(sb.buf, sb.len, 1, stdout);\n \tstrbuf_release(&sb);\n-\treturn;\n }\n \n static void show_ce(struct repository *repo, struct dir_struct *dir,\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nindex 60c415aafd6..baf03f9096e 100755\n--- a/t/t3013-ls-files-format.sh\n+++ b/t/t3013-ls-files-format.sh\n@@ -40,27 +40,13 @@ test_expect_success 'git ls-files --format objectname' '\n \ttest_cmp expect actual\n '\n \n-test_expect_success 'git ls-files --format eolinfo:index' '\n-\tcat >expect <<-\\EOF &&\n-\tlf\n-\tlf\n-\tEOF\n-\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n-\ttest_cmp expect actual\n-'\n-\n-test_expect_success 'git ls-files --format eolinfo:worktree' '\n-\tcat >expect <<-\\EOF &&\n-\tlf\n-\tlf\n-\tEOF\n-\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n-\ttest_cmp expect actual\n-'\n-\n-test_expect_success 'git ls-files --format eolattr' '\n-\tprintf \"\\n\\n\" >expect &&\n-\tgit ls-files --format=\"%(eolattr)\" >actual &&\n+HT='\t'\n+WS='    '\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+\tgit ls-files --eol >expect 2>err &&\n+\ttest_must_be_empty err &&\n+\tgit ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n+\ttest_must_be_empty err &&\n \ttest_cmp expect actual\n '\n \n"},{"id":"458493","messageId":"20220705192839.irfrfx4z6jwimcmu@tb-raspi4","threadId":"58016","inReplyTo":"pull.1262.v5.git.1657002760815.gitgitgadget@gmail.com","subject":"Re: [PATCH v5] ls-files: introduce \"--format\" option","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2022-07-05T19:28:40Z","receivedAt":"2022-07-05T19:34:02Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"On Tue, Jul 05, 2022 at 06:32:40AM +0000, ZheNing Hu via GitGitGadget wrote:\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add a new option --format that output index enties\n> informations with custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n[]\n> +FIELD NAMES\n\nNice\n\n> +-----------\n> +Various values from structured fields can be used to interpolate\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n> +\n> +objectmode::\n> +\tThe mode of the file which is recorded in the index.\n> +objectname::\n> +\tThe name of the file which is recorded in the index.\n> +stage::\n> +\tThe stage of the file which is recorded in the index.\n> +eolinfo:index::\n> +eolinfo:worktree::\n> +\tThe <eolinfo> (see the description of the `--eol` option) of\n> +\tthe contents in the index or in the worktree for the path.\n> +eolattr::\n> +\tThe <eolattr> (see the description of the `--eol` option)\n> +\tthat applies to the path.\n\nThis may be a matter of taste, looking at the eol-stuff:\nShould the ':' be dropped and we have 3 fieldnames like this:\n\neolindex\neolworktree\neolattr\n\n> +test_expect_success 'git ls-files --format eolinfo:index' '\n> +\tcat >expect <<-\\EOF &&\n> +\tlf\n> +\tlf\n> +\tEOF\n> +\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format eolinfo:worktree' '\n> +\tcat >expect <<-\\EOF &&\n> +\tlf\n> +\tlf\n> +\tEOF\n> +\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format eolattr' '\n> +\tprintf \"\\n\\n\" >expect &&\n> +\tgit ls-files --format=\"%(eolattr)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n\nWhat exactly should this testcases test ?\nDoes it make sense to set up a combination of index, worktree, attr,\nwhich are happening in real live ?\n\nThere are some tests in t0025, t0027 and t0028 that do more\nrealistic tests of different combinations.\n\n\n"},{"id":"458799","messageId":"CAOLTT8Q+-Tr9-X=DBmf7wExT3L3DtAXzpmoV+dqfrY-nouP5pg@mail.gmail.com","threadId":"58016","inReplyTo":"220705.86sfng9c5a.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v5] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-11T15:14:49Z","receivedAt":"2022-07-11T15:15:04Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年7月5日周二 16:50写道：\n>\n>\n> On Tue, Jul 05 2022, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Add a new option --format that output index enties\n> > informations with custom format, taking inspiration\n> > from the option with the same name in the `git ls-tree`\n> > command.\n> >\n> > --format cannot used with -s, -o, -k, -t, --resolve-undo,\n> > --deduplicate and --eol.\n> >\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> > ---\n> >     ls-files: introduce \"--format\" options\n> >\n> >     v4->v5:\n> >\n> >      1. Let --format incompatible with -t.\n> >      2. Fix %(eolinfo) and %(eolattr) docs suggested by Junio.\n> >\n> >     Looking forward to Ævar's reviewing.\n>\n> Thanks again, I took a look at this and it looks good to me as-is.\n>\n> If you do want to further twiddle with it at this point I applied these\n> changes to it locally while poking around, changes:\n>\n>  * Some trivial whitespace between variable decl.\n>\n>  * Removed a \"return;\" at the end of a function\n>\n>  * I found the new write_*() helpers to be uneccesary, what I did spot\n>    after seeing if they could be factored out is the existing\n>    write_eolinfo() function.\n>\n>    I see you just copied some of the code from there, but\n>    e.g. initializing to \"\" and doing an unconditional strbuf_addstr()\n>    looks odd IMO compared to just doing it inline as below.\n>\n\nIndeed, it may be a little inelegant...\n\n>    I think if helpers are to be introduced here I'd think it would make\n>    more sense to split out the small bits of behavior from\n>    write_eolinfo() so you can call it picemeal and share the code, but\n>    since it's calling trivial external functions I think just calling\n>    those directly probably makes more sense...\n>\n>  * Likewise for the test I wondered if you were adding a bug by not\n>    reporting when lstat() failed, but then found that this is the same\n>    thing we do on --eol.\n>\n\nYes, write_eolinfo() ignore lstat() error too, so this would not be a problem.\n\n>    So for the tests I think it's better just to demonstrate that we can\n>    emit the exact same thing that --eol does with --format.\n>\n>  * We've gone back & forth a bit on whether this would combine with\n>    --debug, while it's an internal-only feature it would be nice to have a\n>    test for it combined with --format, noting that the behavior might\n>    change...\n>\n\nOh, if we really want --format, --debug used with --eol, -t, some user\nmay curious about why --format can not used with  --eol, -t (without --debug),\nand I think this will make it interface more complicated. So now I pefer to keep\norigin design.\n\n>  * There is one subtle behavior change here in that I deleted the \"ce\n>    &&\" part from write_index_eolinfo_to_buf() when moving the code\n>    over. I'm 99% sure this is the right thing to do, as other code in\n>    expand_show_index() unconditionally dereferences it.\n>\n>    So perhaps we don't need that guard in write_eolinfo() either? In any\n>    case copy/pasting it over when we're already assuming a non-NULL \"ce\"\n>    in the same \"if/elseif/else\" chain looks a bit odd.\n>\n>    Ah, I see it's because in show_dir_entry() we explicitly pass it as\n>    NULL, but that doesn't apply to our new codepath, so as long as we're\n>    not sharing that helper with write_eolinfo() it makes sense to not do\n>    that check.\n>\n\nAgree.\n\n>    Even then the helper should probably assume \"ce\", and write_eolinfo()\n>    itself should do the \"is ce NULL?\" check which is specific to its\n>    use-case.\n>\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index 79ecdce2c9c..cc3cece3830 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -77,30 +77,6 @@ static void write_eolinfo(struct index_state *istate,\n>         }\n>  }\n>\n> -static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n> -                                      const struct cache_entry *ce)\n> -{\n> -       const char *i_txt = \"\";\n> -       if (ce && S_ISREG(ce->ce_mode))\n> -               i_txt = get_cached_convert_stats_ascii(istate, ce->name);\n> -       strbuf_addstr(sb, i_txt);\n> -}\n> -\n> -static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n> -{\n> -       struct stat st;\n> -       const char *w_txt = \"\";\n> -       if (!lstat(path, &st) && S_ISREG(st.st_mode))\n> -               w_txt = get_wt_convert_stats_ascii(path);\n> -       strbuf_addstr(sb, w_txt);\n> -}\n> -\n> -static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n> -                                const char *path)\n> -{\n> -       strbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n> -}\n> -\n>  static void write_name(const char *name)\n>  {\n>         /*\n> @@ -114,6 +90,7 @@ static void write_name(const char *name)\n>  static void write_name_to_buf(struct strbuf *sb, const char *name)\n>  {\n>         const char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n> +\n>         if (line_terminator)\n>                 quote_c_style(rel, sb, NULL, 0);\n>         else\n> @@ -270,6 +247,8 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n>         const char *end;\n>         const char *p;\n>         size_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> +       struct stat st;\n> +\n>         if (len)\n>                 return len;\n>         if (*start != '(')\n> @@ -288,12 +267,16 @@ static size_t expand_show_index(struct strbuf *sb, const char *start,\n>                 strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n>         else if (skip_prefix(start, \"(stage)\", &p))\n>                 strbuf_addf(sb, \"%d\", ce_stage(data->ce));\n> -       else if (skip_prefix(start, \"(eolinfo:index)\", &p))\n> -               write_index_eolinfo_to_buf(sb, data->istate, data->ce);\n> -       else if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n> -               write_worktree_eolinfo_to_buf(sb, data->pathname);\n> +       else if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n> +                S_ISREG(data->ce->ce_mode))\n> +               strbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n> +                                                                data->ce->name));\n> +       else if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n> +                !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n> +               strbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n>         else if (skip_prefix(start, \"(eolattr)\", &p))\n> -               write_eolattr_to_buf(sb, data->istate, data->pathname);\n> +               strbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n> +                                                        data->pathname));\n>         else if (skip_prefix(start, \"(path)\", &p))\n>                 write_name_to_buf(sb, data->pathname);\n>         else\n> @@ -310,13 +293,12 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n>                 .istate = repo->index,\n>                 .ce = ce,\n>         };\n> -\n>         struct strbuf sb = STRBUF_INIT;\n> +\n>         strbuf_expand(&sb, format, expand_show_index, &data);\n>         strbuf_addch(&sb, line_terminator);\n>         fwrite(sb.buf, sb.len, 1, stdout);\n>         strbuf_release(&sb);\n> -       return;\n>  }\n>\n>  static void show_ce(struct repository *repo, struct dir_struct *dir,\n> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> index 60c415aafd6..baf03f9096e 100755\n> --- a/t/t3013-ls-files-format.sh\n> +++ b/t/t3013-ls-files-format.sh\n> @@ -40,27 +40,13 @@ test_expect_success 'git ls-files --format objectname' '\n>         test_cmp expect actual\n>  '\n>\n> -test_expect_success 'git ls-files --format eolinfo:index' '\n> -       cat >expect <<-\\EOF &&\n> -       lf\n> -       lf\n> -       EOF\n> -       git ls-files --format=\"%(eolinfo:index)\" >actual &&\n> -       test_cmp expect actual\n> -'\n> -\n> -test_expect_success 'git ls-files --format eolinfo:worktree' '\n> -       cat >expect <<-\\EOF &&\n> -       lf\n> -       lf\n> -       EOF\n> -       git ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n> -       test_cmp expect actual\n> -'\n> -\n> -test_expect_success 'git ls-files --format eolattr' '\n> -       printf \"\\n\\n\" >expect &&\n> -       git ls-files --format=\"%(eolattr)\" >actual &&\n> +HT='   '\n> +WS='    '\n> +test_expect_success 'git ls-files --format v.s. --eol' '\n> +       git ls-files --eol >expect 2>err &&\n> +       test_must_be_empty err &&\n> +       git ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n> +       test_must_be_empty err &&\n>         test_cmp expect actual\n>  '\n>\n\nThanks for review and help :-)\n\nZheNing Hu\n"},{"id":"458801","messageId":"CAOLTT8Ss53yXw3bo58dHwoTWaX3wS880qKF8AXQ5KbZeTB2eUA@mail.gmail.com","threadId":"58016","inReplyTo":"20220705192839.irfrfx4z6jwimcmu@tb-raspi4","subject":"Re: [PATCH v5] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-11T15:27:03Z","receivedAt":"2022-07-11T15:27:18Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Torsten Bögershausen <tboegi@web.de> 于2022年7月6日周三 03:28写道：\n>\n> On Tue, Jul 05, 2022 at 06:32:40AM +0000, ZheNing Hu via GitGitGadget wrote:\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Add a new option --format that output index enties\n> > informations with custom format, taking inspiration\n> > from the option with the same name in the `git ls-tree`\n> > command.\n> []\n> > +FIELD NAMES\n>\n> Nice\n>\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n> > +\n> > +objectmode::\n> > +     The mode of the file which is recorded in the index.\n> > +objectname::\n> > +     The name of the file which is recorded in the index.\n> > +stage::\n> > +     The stage of the file which is recorded in the index.\n> > +eolinfo:index::\n> > +eolinfo:worktree::\n> > +     The <eolinfo> (see the description of the `--eol` option) of\n> > +     the contents in the index or in the worktree for the path.\n> > +eolattr::\n> > +     The <eolattr> (see the description of the `--eol` option)\n> > +     that applies to the path.\n>\n> This may be a matter of taste, looking at the eol-stuff:\n> Should the ':' be dropped and we have 3 fieldnames like this:\n>\n> eolindex\n> eolworktree\n> eolattr\n>\n\nLet's see the document of --eol in git-ls-files.txt:\n\n--eol::\n     Show <eolinfo> and <eolattr> of files.\n     <eolinfo> is the file content identification used by Git when\n     the \"text\" attribute is \"auto\" (or not set and core.autocrlf is not false).\n     <eolinfo> is either \"-text\", \"none\", \"lf\", \"crlf\", \"mixed\" or \"\".\n\nThere mentioned eolinfo and eolattr many times, so let's keep it.\n\n> > +test_expect_success 'git ls-files --format eolinfo:index' '\n> > +     cat >expect <<-\\EOF &&\n> > +     lf\n> > +     lf\n> > +     EOF\n> > +     git ls-files --format=\"%(eolinfo:index)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format eolinfo:worktree' '\n> > +     cat >expect <<-\\EOF &&\n> > +     lf\n> > +     lf\n> > +     EOF\n> > +     git ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format eolattr' '\n> > +     printf \"\\n\\n\" >expect &&\n> > +     git ls-files --format=\"%(eolattr)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n>\n> What exactly should this testcases test ?\n> Does it make sense to set up a combination of index, worktree, attr,\n> which are happening in real live ?\n>\n> There are some tests in t0025, t0027 and t0028 that do more\n> realistic tests of different combinations.\n>\n>\n\nOrigin test is not good, But now I decide use Avar's patch version:\n\n-test_expect_success 'git ls-files --format eolattr' '\n-       printf \"\\n\\n\" >expect &&\n-       git ls-files --format=\"%(eolattr)\" >actual &&\n+HT='   '\n+WS='    '\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+       git ls-files --eol >expect 2>err &&\n+       test_must_be_empty err &&\n+       git ls-files\n--format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS}\n${HT}%(path)\" >actual 2>err &&\n+       test_must_be_empty err &&\n        test_cmp expect actual\n\nit can compare the output of git ls-files --format with git ls-files --eol.\n\nThanks for review!\n\nZheNing Hu\n"},{"id":"458803","messageId":"pull.1262.v6.git.1657558435532.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v5.git.1657002760815.gitgitgadget@gmail.com","subject":"[PATCH v6] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-07-11T16:53:55Z","receivedAt":"2022-07-11T16:54:07Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option --format that output index enties\ninformations with custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n--format cannot used with -s, -o, -k, -t, --resolve-undo,\n--deduplicate and --eol.\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v5->v6:\n    \n     1. Some code cleaning suggested by Ævar.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v6\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v6\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v5:\n\n 1:  1ce69d6202a ! 1:  57ed2c15728 ls-files: introduce \"--format\" option\n     @@ builtin/ls-files.c: static char *ps_matched;\n       \n       static const char *tag_cached = \"\";\n       static const char *tag_unmerged = \"\";\n     -@@ builtin/ls-files.c: static void write_eolinfo(struct index_state *istate,\n     - \t}\n     - }\n     - \n     -+static void write_index_eolinfo_to_buf(struct strbuf *sb, struct index_state *istate,\n     -+\t\t\t\t       const struct cache_entry *ce)\n     -+{\n     -+\tconst char *i_txt = \"\";\n     -+\tif (ce && S_ISREG(ce->ce_mode))\n     -+\t\ti_txt = get_cached_convert_stats_ascii(istate, ce->name);\n     -+\tstrbuf_addstr(sb, i_txt);\n     -+}\n     -+\n     -+static void write_worktree_eolinfo_to_buf(struct strbuf *sb, const char *path)\n     -+{\n     -+\tstruct stat st;\n     -+\tconst char *w_txt = \"\";\n     -+\tif (!lstat(path, &st) && S_ISREG(st.st_mode))\n     -+\t\tw_txt = get_wt_convert_stats_ascii(path);\n     -+\tstrbuf_addstr(sb, w_txt);\n     -+}\n     -+\n     -+static void write_eolattr_to_buf(struct strbuf *sb, struct index_state *istate,\n     -+\t\t\t\t const char *path)\n     -+{\n     -+\tstrbuf_addstr(sb, get_convert_attr_ascii(istate, path));\n     -+}\n     -+\n     - static void write_name(const char *name)\n     - {\n     - \t/*\n      @@ builtin/ls-files.c: static void write_name(const char *name)\n       \t\t\t\t   stdout, line_terminator);\n       }\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\tconst char *end;\n      +\tconst char *p;\n      +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n     ++\tstruct stat st;\n     ++\n      +\tif (len)\n      +\t\treturn len;\n      +\tif (*start != '(')\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n      +\telse if (skip_prefix(start, \"(stage)\", &p))\n      +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n     -+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n     -+\t\twrite_index_eolinfo_to_buf(sb, data->istate, data->ce);\n     -+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n     -+\t\twrite_worktree_eolinfo_to_buf(sb, data->pathname);\n     ++\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n     ++\t\t S_ISREG(data->ce->ce_mode))\n     ++\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n     ++\t\t\t\t\t\t\t\t data->ce->name));\n     ++\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n     ++\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n     ++\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n      +\telse if (skip_prefix(start, \"(eolattr)\", &p))\n     -+\t\twrite_eolattr_to_buf(sb, data->istate, data->pathname);\n     ++\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n     ++\t\t\t      data->pathname));\n      +\telse if (skip_prefix(start, \"(path)\", &p))\n      +\t\twrite_name_to_buf(sb, data->pathname);\n      +\telse\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\n      +static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n      +\t\t\tconst char *format, const char *fullname) {\n     -+\n      +\tstruct show_index_data data = {\n      +\t\t.pathname = fullname,\n      +\t\t.istate = repo->index,\n      +\t\t.ce = ce,\n      +\t};\n     -+\n      +\tstruct strbuf sb = STRBUF_INIT;\n     ++\n      +\tstrbuf_expand(&sb, format, expand_show_index, &data);\n      +\tstrbuf_addch(&sb, line_terminator);\n      +\tfwrite(sb.buf, sb.len, 1, stdout);\n      +\tstrbuf_release(&sb);\n     -+\treturn;\n      +}\n      +\n       static void show_ce(struct repository *repo, struct dir_struct *dir,\n     @@ t/t3013-ls-files-format.sh (new)\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format eolinfo:index' '\n     -+\tcat >expect <<-\\EOF &&\n     -+\tlf\n     -+\tlf\n     -+\tEOF\n     -+\tgit ls-files --format=\"%(eolinfo:index)\" >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format eolinfo:worktree' '\n     -+\tcat >expect <<-\\EOF &&\n     -+\tlf\n     -+\tlf\n     -+\tEOF\n     -+\tgit ls-files --format=\"%(eolinfo:worktree)\" >actual &&\n     -+\ttest_cmp expect actual\n     -+'\n     -+\n     -+test_expect_success 'git ls-files --format eolattr' '\n     -+\tprintf \"\\n\\n\" >expect &&\n     -+\tgit ls-files --format=\"%(eolattr)\" >actual &&\n     ++HT='\t'\n     ++WS='    '\n     ++test_expect_success 'git ls-files --format v.s. --eol' '\n     ++\tgit ls-files --eol >expect 2>err &&\n     ++\ttest_must_be_empty err &&\n     ++\tgit ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n     ++\ttest_must_be_empty err &&\n      +\ttest_cmp expect actual\n      +'\n      +\n\n\n Documentation/git-ls-files.txt | 38 +++++++++++++-\n builtin/ls-files.c             | 93 +++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 94 ++++++++++++++++++++++++++++++++++\n 3 files changed, 224 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..97d4cebba9f 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,35 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+Various values from structured fields can be used to interpolate\n+into the resulting output. For each outputting line, the following\n+names can be used:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+eolinfo:worktree::\n+\tThe <eolinfo> (see the description of the `--eol` option) of\n+\tthe contents in the index or in the worktree for the path.\n+eolattr::\n+\tThe <eolattr> (see the description of the `--eol` option)\n+\tthat applies to the path.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..6376dbcccc6 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -85,6 +87,15 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_add(sb, rel, strlen(rel));\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +233,72 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t       void *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tstruct stat st;\n+\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n+\t\t S_ISREG(data->ce->ce_mode))\n+\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n+\t\t\t\t\t\t\t\t data->ce->name));\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n+\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n+\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n+\t\t\t      data->pathname));\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +313,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +758,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +785,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..baf03f9096e\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,94 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\techo o1 >o1 &&\n+\techo o2 >o2 &&\n+\tgit add o1 o2 &&\n+\tgit add --chmod +x o1 &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode' '\n+\tcat >expect <<-\\EOF &&\n+\t100755\n+\t100644\n+\tEOF\n+\tgit ls-files --format=\"%(objectmode)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname' '\n+\toid1=$(git hash-object o1) &&\n+\toid2=$(git hash-object o2) &&\n+\tcat >expect <<-EOF &&\n+\t$oid1\n+\t$oid2\n+\tEOF\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+HT='\t'\n+WS='    '\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+\tgit ls-files --eol >expect 2>err &&\n+\ttest_must_be_empty err &&\n+\tgit ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path' '\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\to2\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1 &&\n+\tcat >expect <<-\\EOF &&\n+\to1\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o3 >o3 &&\n+\tgit add o3 &&\n+\trm o3 &&\n+\tcat >expect <<-\\EOF &&\n+\to3\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"458828","messageId":"xmqqleszl2p0.fsf@gitster.g","threadId":"58016","inReplyTo":"pull.1262.v6.git.1657558435532.gitgitgadget@gmail.com","subject":"Re: [PATCH v6] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-11T22:11:39Z","receivedAt":"2022-07-11T22:11:48Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add a new option --format that output index enties ...\n\nLet's quote the options and use the Oxford comma.\n\n    ls-files: introduce \"--format\" option\n\n    Add a new option \"--format\" that outputs index entries in a\n    custom format, taking inspiration from the option with the same\n    name in the `git ls-tree` command.\n\n    \"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\", \"--resolve-undo\",\n    \"--deduplicate\", and \"--eol\".\n\n> +It is possible to print in a custom format by using the `--format`\n> +option, which is able to interpolate different fields using\n\nSo we use the term \"field\" to mean different piece of information we\ncan present.  The definition of what fields are available come later\nand the presentation order is a bit awkward, but hopefully the text\nis understandable as-is.\n\n> +a `%(fieldname)` notation. For example, if you only care about the\n> +\"objectname\" and \"path\" fields, you can execute with a specific\n> +\"--format\" like\n> +\n> +\tgit ls-files --format='%(objectname) %(path)'\n\nAnd the example makes it pretty clear.  OK.\n\n> +FIELD NAMES\n> +-----------\n> +Various values from structured fields can be used to interpolate\n\nAre we dealing with unstructured fields, too?  If not, let's drop\n\"structured\".\n\n> +into the resulting output. For each outputting line, the following\n> +names can be used:\n\n\"outputting line\" sounds like a non language.\n\n\n    The way each path is shown can be customized by using the\n    `--format=<format>` option, where the %(fieldname) in the\n    <format> string for various aspects of the index entry are\n    interpolated.  The following \"fieldname\" are understood:\n\nperhaps?\n\n> +\n> +objectmode::\n> +\tThe mode of the file which is recorded in the index.\n> +objectname::\n> +\tThe name of the file which is recorded in the index.\n> +stage::\n> +\tThe stage of the file which is recorded in the index.\n> +eolinfo:index::\n> +eolinfo:worktree::\n> +\tThe <eolinfo> (see the description of the `--eol` option) of\n> +\tthe contents in the index or in the worktree for the path.\n> +eolattr::\n> +\tThe <eolattr> (see the description of the `--eol` option)\n> +\tthat applies to the path.\n\n> +path::\n> +\tThe pathname of the file which is recorded in the index.\n\nSince we are mutually exclusive with \"--other\", the output always\ncomes from the index, so this is OK.\n\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index e791b65e7e9..6376dbcccc6 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -11,6 +11,7 @@\n>  #include \"quote.h\"\n>  #include \"dir.h\"\n>  #include \"builtin.h\"\n> +#include \"strbuf.h\"\n\nThis is not strictly needed (we have users of strbuf in this file\nwithout this patch already), but OK.\n\n> @@ -48,6 +49,7 @@ static char *ps_matched;\n>  static const char *with_tree;\n>  static int exc_given;\n>  static int exclude_args;\n> +static const char *format;\n>  \n>  static const char *tag_cached = \"\";\n>  static const char *tag_unmerged = \"\";\n> @@ -85,6 +87,15 @@ static void write_name(const char *name)\n>  \t\t\t\t   stdout, line_terminator);\n>  }\n>  \n> +static void write_name_to_buf(struct strbuf *sb, const char *name)\n> +{\n> +\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n\nA blank line here between the decl and the first statement.\n\n> +\tif (line_terminator)\n> +\t\tquote_c_style(rel, sb, NULL, 0);\n> +\telse\n> +\t\tstrbuf_add(sb, rel, strlen(rel));\n\nIt's the same thing, but strbuf_addstr() is less error prone.\n\n> @@ -222,6 +233,72 @@ static void show_submodule(struct repository *superproject,\n>  \trepo_clear(&subrepo);\n>  }\n>  \n> +struct show_index_data {\n> +\tconst char *pathname;\n> +\tstruct index_state *istate;\n> +\tconst struct cache_entry *ce;\n> +};\n> +\n> +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> +\t\t\t       void *context)\n\nI think this does not make \"struct\" and \"void\" align (one more SP needed).\n\n> +{\n> +\tstruct show_index_data *data = context;\n> +\tconst char *end;\n> +\tconst char *p;\n> +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> +\tstruct stat st;\n> +\n> +\tif (len)\n> +\t\treturn len;\n> +\tif (*start != '(')\n> +\t\tdie(_(\"bad ls-files format: element '%s' \"\n> +\t\t      \"does not start with '('\"), start);\n> +\n> +\tend = strchr(start + 1, ')');\n> +\tif (!end)\n> +\t\tdie(_(\"bad ls-files format: element '%s'\"\n> +\t\t      \"does not end in ')'\"), start);\n> +\n> +\tlen = end - start + 1;\n> +\tif (skip_prefix(start, \"(objectmode)\", &p))\n\n\nUsing skip_prefix() not for the purpose of skipping (notice that\nnobody uses p at all) is ugly.  We already computed start and end\n(hence the length), so we should be able to do much better than\nthis.\n\nBut let's let it pass, as it was copy-pasted from existing code in\nls-tree.c::expand_show_tree().\n\n> +\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n> +\t\t S_ISREG(data->ce->ce_mode))\n> +\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n> +\t\t\t\t\t\t\t\t data->ce->name));\n\nThis is outright wrong, isn't it?\n\nIt is unlikely to see such a trivial error in the 6th round of a\nseries after other reviewers looked at it many times, so perhaps I\nam missing something?  Or perhaps this is a new code added in this\nround.\n\nIf you ask for %(eolinfo:index) for an index entry that is not a\nregular file, this \"else if\" will not trigger, and the control will\neventually fall through to hit \"bad ls-files format\" but what you\ndetected is not a bad format at all.  Once the skip_prefix() hits,\nyou should be committed to handle that \"field\" and never let the\nother choices in this if/elif/ cascade to see it.\n\nIt is OK to interpolate %(eolinfo:index) to an empty string for a\ngitlink and a symbolic link, but the right way to do so would\nprobably be:\n\n\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) {\n\t\tif (S_ISREG(data->ce->ce_mode))\n\t\t\tstrbuf_addstr(...);\n\t} else ...\n\n> +\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n> +\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n> +\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n\nLikewise.\n\n> +test_expect_success 'setup' '\n> +\techo o1 >o1 &&\n> +\techo o2 >o2 &&\n> +\tgit add o1 o2 &&\n> +\tgit add --chmod +x o1 &&\n> +\tgit commit -m base\n> +'\n\nApparently, this set-up is too trivial to uncover the above bug that\ncan be spotted in 10 seconds of staring at the code.  Perhaps add a\nsymbolic link (use \"git update-index --cacheinfo\" and you do not\nhave to worry about Windows), a subdirectory and a submodule?\n\n"},{"id":"458886","messageId":"CAOLTT8RSo83ZBXbT_MLigD946_xHjnX-aS76D_K7=ScbMR=nYw@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqleszl2p0.fsf@gitster.g","subject":"Re: [PATCH v6] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-12T13:53:57Z","receivedAt":"2022-07-12T13:54:12Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年7月12日周二 06:11写道：\n>\n> \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Add a new option --format that output index enties ...\n>\n> Let's quote the options and use the Oxford comma.\n>\n>     ls-files: introduce \"--format\" option\n>\n>     Add a new option \"--format\" that outputs index entries in a\n>     custom format, taking inspiration from the option with the same\n>     name in the `git ls-tree` command.\n>\n>     \"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\", \"--resolve-undo\",\n>     \"--deduplicate\", and \"--eol\".\n>\n> > +It is possible to print in a custom format by using the `--format`\n> > +option, which is able to interpolate different fields using\n>\n> So we use the term \"field\" to mean different piece of information we\n> can present.  The definition of what fields are available come later\n> and the presentation order is a bit awkward, but hopefully the text\n> is understandable as-is.\n>\n\nOK.\n\n> > +a `%(fieldname)` notation. For example, if you only care about the\n> > +\"objectname\" and \"path\" fields, you can execute with a specific\n> > +\"--format\" like\n> > +\n> > +     git ls-files --format='%(objectname) %(path)'\n>\n> And the example makes it pretty clear.  OK.\n>\n> > +FIELD NAMES\n> > +-----------\n> > +Various values from structured fields can be used to interpolate\n>\n> Are we dealing with unstructured fields, too?  If not, let's drop\n> \"structured\".\n>\n\nOK (copy from git-ls-tree.txt too)\n\n> > +into the resulting output. For each outputting line, the following\n> > +names can be used:\n>\n> \"outputting line\" sounds like a non language.\n>\n>\n>     The way each path is shown can be customized by using the\n>     `--format=<format>` option, where the %(fieldname) in the\n>     <format> string for various aspects of the index entry are\n>     interpolated.  The following \"fieldname\" are understood:\n>\n> perhaps?\n>\n\nThis will indeed be better.\n\n> > +{\n> > +     struct show_index_data *data = context;\n> > +     const char *end;\n> > +     const char *p;\n> > +     size_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> > +     struct stat st;\n> > +\n> > +     if (len)\n> > +             return len;\n> > +     if (*start != '(')\n> > +             die(_(\"bad ls-files format: element '%s' \"\n> > +                   \"does not start with '('\"), start);\n> > +\n> > +     end = strchr(start + 1, ')');\n> > +     if (!end)\n> > +             die(_(\"bad ls-files format: element '%s'\"\n> > +                   \"does not end in ')'\"), start);\n> > +\n> > +     len = end - start + 1;\n> > +     if (skip_prefix(start, \"(objectmode)\", &p))\n>\n>\n> Using skip_prefix() not for the purpose of skipping (notice that\n> nobody uses p at all) is ugly.  We already computed start and end\n> (hence the length), so we should be able to do much better than\n> this.\n>\n\nAgree. I check the parsing format part of ref-filter.c, we just need to find the\natom's begin pos and end pos, then we can use memcmp() to know what's the\ntype of atom.\n\n> But let's let it pass, as it was copy-pasted from existing code in\n> ls-tree.c::expand_show_tree().\n>\n\nYeah, maybe we can optimize it later.\n\n> > +     else if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n> > +              S_ISREG(data->ce->ce_mode))\n> > +             strbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n> > +                                                              data->ce->name));\n>\n> This is outright wrong, isn't it?\n>\n> It is unlikely to see such a trivial error in the 6th round of a\n> series after other reviewers looked at it many times, so perhaps I\n> am missing something?  Or perhaps this is a new code added in this\n> round.\n>\n> If you ask for %(eolinfo:index) for an index entry that is not a\n> regular file, this \"else if\" will not trigger, and the control will\n> eventually fall through to hit \"bad ls-files format\" but what you\n> detected is not a bad format at all.  Once the skip_prefix() hits,\n> you should be committed to handle that \"field\" and never let the\n> other choices in this if/elif/ cascade to see it.\n>\n> It is OK to interpolate %(eolinfo:index) to an empty string for a\n> gitlink and a symbolic link, but the right way to do so would\n> probably be:\n>\n>         else if (skip_prefix(start, \"(eolinfo:index)\", &p) {\n>                 if (S_ISREG(data->ce->ce_mode))\n>                         strbuf_addstr(...);\n>         } else ...\n>\n\nYeah, but we would use \"{\", \"}\" again, so just revert this code to v5,\nwhich uses a\n wrap function.\n\n> > +     else if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n> > +              !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n> > +             strbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n>\n> Likewise.\n>\n> > +test_expect_success 'setup' '\n> > +     echo o1 >o1 &&\n> > +     echo o2 >o2 &&\n> > +     git add o1 o2 &&\n> > +     git add --chmod +x o1 &&\n> > +     git commit -m base\n> > +'\n>\n> Apparently, this set-up is too trivial to uncover the above bug that\n> can be spotted in 10 seconds of staring at the code.  Perhaps add a\n> symbolic link (use \"git update-index --cacheinfo\" and you do not\n> have to worry about Windows), a subdirectory and a submodule?\n>\n\nAh, Just looking at the c code, I took a long time (more than 10 minutes) to\nfind out where the mistake was. But yeah, use a subdirectory can quickly\nmeet the error,  so I need to add more cases here.\n\nThanks for your review.\n\nZheNing Hu\n"},{"id":"458891","messageId":"xmqqh73miemj.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8RSo83ZBXbT_MLigD946_xHjnX-aS76D_K7=ScbMR=nYw@mail.gmail.com","subject":"Re: [PATCH v6] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-12T14:34:28Z","receivedAt":"2022-07-12T14:35:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n>> > +test_expect_success 'setup' '\n>> > +     echo o1 >o1 &&\n>> > +     echo o2 >o2 &&\n>> > +     git add o1 o2 &&\n>> > +     git add --chmod +x o1 &&\n>> > +     git commit -m base\n>> > +'\n>>\n>> Apparently, this set-up is too trivial to uncover the above bug that\n>> can be spotted in 10 seconds of staring at the code.  Perhaps add a\n>> symbolic link (use \"git update-index --cacheinfo\" and you do not\n>> have to worry about Windows), a subdirectory and a submodule?\n>\n> Ah, Just looking at the c code, I took a long time (more than 10 minutes) to\n> find out where the mistake was.\n\nIt is OK---it tends to take a lot more time than it should for all\nof us, experienced developers included, to find mistakes in our own\ncode than code written by other people.\n\n> But yeah, use a subdirectory can quickly meet the error, so I need\n> to add more cases here.\n\nA pure sub-\"directory\" would not, I suspect.  A submodule or a\nsymbolic link would. \n"},{"id":"458938","messageId":"pull.1262.v7.git.1657692472994.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v6.git.1657558435532.gitgitgadget@gmail.com","subject":"[PATCH v7] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-07-13T06:07:52Z","receivedAt":"2022-07-13T06:08:02Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option \"--format\" that outputs index entries\ninformations in a custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n\"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n\" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v6->v7:\n    \n     1. Change documents helped by Junio.\n     2. Fix bug of parsing format.\n     3. Add more test cases for other mode index entries (120000 and\n        160000).\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v7\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v7\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v6:\n\n 1:  57ed2c15728 ! 1:  9ca22edba94 ls-files: introduce \"--format\" option\n     @@ Metadata\n       ## Commit message ##\n          ls-files: introduce \"--format\" option\n      \n     -    Add a new option --format that output index enties\n     -    informations with custom format, taking inspiration\n     +    Add a new option \"--format\" that outputs index entries\n     +    informations in a custom format, taking inspiration\n          from the option with the same name in the `git ls-tree`\n          command.\n      \n     -    --format cannot used with -s, -o, -k, -t, --resolve-undo,\n     -    --deduplicate and --eol.\n     +    \"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n     +    \" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n      \n          Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n      \n     @@ Documentation/git-ls-files.txt: quoted as explained for the configuration variab\n      +\n      +FIELD NAMES\n      +-----------\n     -+Various values from structured fields can be used to interpolate\n     -+into the resulting output. For each outputting line, the following\n     -+names can be used:\n     ++The way each path is shown can be customized by using the\n     ++`--format=<format>` option, where the %(fieldname) in the\n     ++<format> string for various aspects of the index entry are\n     ++interpolated.  The following \"fieldname\" are understood:\n      +\n      +objectmode::\n      +\tThe mode of the file which is recorded in the index.\n     @@ builtin/ls-files.c: static void write_name(const char *name)\n      +static void write_name_to_buf(struct strbuf *sb, const char *name)\n      +{\n      +\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n     ++\n      +\tif (line_terminator)\n      +\t\tquote_c_style(rel, sb, NULL, 0);\n      +\telse\n     -+\t\tstrbuf_add(sb, rel, strlen(rel));\n     ++\t\tstrbuf_addstr(sb, rel);\n      +}\n      +\n       static const char *get_tag(const struct cache_entry *ce, const char *tag)\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +};\n      +\n      +static size_t expand_show_index(struct strbuf *sb, const char *start,\n     -+\t\t\t       void *context)\n     ++\t\t\t\tvoid *context)\n      +{\n      +\tstruct show_index_data *data = context;\n      +\tconst char *end;\n     @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n      +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n      +\telse if (skip_prefix(start, \"(stage)\", &p))\n      +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n     -+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n     -+\t\t S_ISREG(data->ce->ce_mode))\n     -+\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n     -+\t\t\t\t\t\t\t\t data->ce->name));\n     -+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n     -+\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n     -+\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n     ++\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n     ++\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n     ++\t\t\t      get_cached_convert_stats_ascii(data->istate,\n     ++\t\t\t      data->ce->name) : \"\");\n     ++\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n     ++\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n     ++\t\t\t      S_ISREG(st.st_mode) ?\n     ++\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n      +\telse if (skip_prefix(start, \"(eolattr)\", &p))\n      +\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n      +\t\t\t      data->pathname));\n     @@ t/t3013-ls-files-format.sh (new)\n      +done\n      +\n      +test_expect_success 'setup' '\n     -+\techo o1 >o1 &&\n     -+\techo o2 >o2 &&\n     -+\tgit add o1 o2 &&\n     -+\tgit add --chmod +x o1 &&\n     ++\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n     ++\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n     ++\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n     ++\tln -s o3.txt o4.txt &&\n     ++\tgit add \"*.txt\" &&\n     ++\tgit add --chmod +x o1.txt &&\n     ++\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n      +\tgit commit -m base\n      +'\n      +\n     -+test_expect_success 'git ls-files --format objectmode' '\n     -+\tcat >expect <<-\\EOF &&\n     -+\t100755\n     -+\t100644\n     -+\tEOF\n     ++test_expect_success 'git ls-files --format objectmode v.s. -s' '\n     ++\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n      +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format objectname' '\n     -+\toid1=$(git hash-object o1) &&\n     -+\toid2=$(git hash-object o2) &&\n     -+\tcat >expect <<-EOF &&\n     -+\t$oid1\n     -+\t$oid2\n     -+\tEOF\n     ++test_expect_success 'git ls-files --format objectname v.s. -s' '\n     ++\tgit ls-files -s | awk \"{print \\$2}\" >expect &&\n      +\tgit ls-files --format=\"%(objectname)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+HT='\t'\n     -+WS='    '\n      +test_expect_success 'git ls-files --format v.s. --eol' '\n     -+\tgit ls-files --eol >expect 2>err &&\n     ++\tgit ls-files --eol >tmp &&\n     ++\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n      +\ttest_must_be_empty err &&\n     -+\tgit ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n     ++\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n      +\ttest_must_be_empty err &&\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format path' '\n     -+\tcat >expect <<-\\EOF &&\n     -+\to1\n     -+\to2\n     -+\tEOF\n     ++test_expect_success 'git ls-files --format path v.s. -s' '\n     ++\tgit ls-files -s | awk \"{print \\$4}\" >expect &&\n      +\tgit ls-files --format=\"%(path)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format with -m' '\n     -+\techo change >o1 &&\n     ++\techo change >o1.txt &&\n      +\tcat >expect <<-\\EOF &&\n     -+\to1\n     ++\to1.txt\n     ++\to5.txt\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -m >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format with -d' '\n     -+\techo o3 >o3 &&\n     -+\tgit add o3 &&\n     -+\trm o3 &&\n     ++\techo o6 >o6.txt &&\n     ++\tgit add o6.txt &&\n     ++\trm o6.txt &&\n      +\tcat >expect <<-\\EOF &&\n     -+\to3\n     ++\to5.txt\n     ++\to6.txt\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -d >actual &&\n      +\ttest_cmp expect actual\n\n\n Documentation/git-ls-files.txt | 39 +++++++++++++-\n builtin/ls-files.c             | 95 ++++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 87 +++++++++++++++++++++++++++++++\n 3 files changed, 220 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..d7986419c25 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,36 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+The way each path is shown can be customized by using the\n+`--format=<format>` option, where the %(fieldname) in the\n+<format> string for various aspects of the index entry are\n+interpolated.  The following \"fieldname\" are understood:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+eolinfo:worktree::\n+\tThe <eolinfo> (see the description of the `--eol` option) of\n+\tthe contents in the index or in the worktree for the path.\n+eolattr::\n+\tThe <eolattr> (see the description of the `--eol` option)\n+\tthat applies to the path.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..6f3ebcaaff7 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -85,6 +87,16 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_addstr(sb, rel);\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t\tvoid *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tstruct stat st;\n+\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n+\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n+\t\t\t      get_cached_convert_stats_ascii(data->istate,\n+\t\t\t      data->ce->name) : \"\");\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n+\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n+\t\t\t      S_ISREG(st.st_mode) ?\n+\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n+\t\t\t      data->pathname));\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +315,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +760,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +787,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..e62bce70f3b\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,87 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n+\tln -s o3.txt o4.txt &&\n+\tgit add \"*.txt\" &&\n+\tgit add --chmod +x o1.txt &&\n+\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode v.s. -s' '\n+\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n+\tgit ls-files --format=\"%(objectmode)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname v.s. -s' '\n+\tgit ls-files -s | awk \"{print \\$2}\" >expect &&\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+\tgit ls-files --eol >tmp &&\n+\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n+\ttest_must_be_empty err &&\n+\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path v.s. -s' '\n+\tgit ls-files -s | awk \"{print \\$4}\" >expect &&\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to1.txt\n+\to5.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o6 >o6.txt &&\n+\tgit add o6.txt &&\n+\trm o6.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to5.txt\n+\to6.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"459240","messageId":"220718.86pmi2ygbt.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"pull.1262.v7.git.1657692472994.gitgitgadget@gmail.com","subject":"Re: [PATCH v7] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-07-18T08:09:15Z","receivedAt":"2022-07-18T08:29:18Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Wed, Jul 13 2022, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add a new option \"--format\" that outputs index entries\n> informations in a custom format, taking inspiration\n> from the option with the same name in the `git ls-tree`\n> command.\n>\n> \"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n> \" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>     ls-files: introduce \"--format\" options\n>     \n>     v6->v7:\n>     \n>      1. Change documents helped by Junio.\n>      2. Fix bug of parsing format.\n>      3. Add more test cases for other mode index entries (120000 and\n>         160000).\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v7\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v7\n> Pull-Request: https://github.com/gitgitgadget/git/pull/1262\n>\n> Range-diff vs v6:\n>\n>  1:  57ed2c15728 ! 1:  9ca22edba94 ls-files: introduce \"--format\" option\n>      @@ Metadata\n>        ## Commit message ##\n>           ls-files: introduce \"--format\" option\n>       \n>      -    Add a new option --format that output index enties\n>      -    informations with custom format, taking inspiration\n>      +    Add a new option \"--format\" that outputs index entries\n>      +    informations in a custom format, taking inspiration\n>           from the option with the same name in the `git ls-tree`\n>           command.\n>       \n>      -    --format cannot used with -s, -o, -k, -t, --resolve-undo,\n>      -    --deduplicate and --eol.\n>      +    \"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n>      +    \" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n>       \n>           Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n>       \n>      @@ Documentation/git-ls-files.txt: quoted as explained for the configuration variab\n>       +\n>       +FIELD NAMES\n>       +-----------\n>      -+Various values from structured fields can be used to interpolate\n>      -+into the resulting output. For each outputting line, the following\n>      -+names can be used:\n>      ++The way each path is shown can be customized by using the\n>      ++`--format=<format>` option, where the %(fieldname) in the\n>      ++<format> string for various aspects of the index entry are\n>      ++interpolated.  The following \"fieldname\" are understood:\n>       +\n>       +objectmode::\n>       +\tThe mode of the file which is recorded in the index.\n>      @@ builtin/ls-files.c: static void write_name(const char *name)\n>       +static void write_name_to_buf(struct strbuf *sb, const char *name)\n>       +{\n>       +\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n>      ++\n>       +\tif (line_terminator)\n>       +\t\tquote_c_style(rel, sb, NULL, 0);\n>       +\telse\n>      -+\t\tstrbuf_add(sb, rel, strlen(rel));\n>      ++\t\tstrbuf_addstr(sb, rel);\n>       +}\n>       +\n>        static const char *get_tag(const struct cache_entry *ce, const char *tag)\n>      @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n>       +};\n>       +\n>       +static size_t expand_show_index(struct strbuf *sb, const char *start,\n>      -+\t\t\t       void *context)\n>      ++\t\t\t\tvoid *context)\n>       +{\n>       +\tstruct show_index_data *data = context;\n>       +\tconst char *end;\n>      @@ builtin/ls-files.c: static void show_submodule(struct repository *superproject,\n>       +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n>       +\telse if (skip_prefix(start, \"(stage)\", &p))\n>       +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n>      -+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p) &&\n>      -+\t\t S_ISREG(data->ce->ce_mode))\n>      -+\t\tstrbuf_addstr(sb, get_cached_convert_stats_ascii(data->istate,\n>      -+\t\t\t\t\t\t\t\t data->ce->name));\n>      -+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p) &&\n>      -+\t\t !lstat(data->pathname, &st) && S_ISREG(st.st_mode))\n>      -+\t\tstrbuf_addstr(sb, get_wt_convert_stats_ascii(data->pathname));\n>      ++\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n>      ++\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n>      ++\t\t\t      get_cached_convert_stats_ascii(data->istate,\n>      ++\t\t\t      data->ce->name) : \"\");\n>      ++\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n>      ++\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n>      ++\t\t\t      S_ISREG(st.st_mode) ?\n>      ++\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n>       +\telse if (skip_prefix(start, \"(eolattr)\", &p))\n>       +\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n>       +\t\t\t      data->pathname));\n>      @@ t/t3013-ls-files-format.sh (new)\n>       +done\n>       +\n>       +test_expect_success 'setup' '\n>      -+\techo o1 >o1 &&\n>      -+\techo o2 >o2 &&\n>      -+\tgit add o1 o2 &&\n>      -+\tgit add --chmod +x o1 &&\n>      ++\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n>      ++\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n>      ++\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n>      ++\tln -s o3.txt o4.txt &&\n>      ++\tgit add \"*.txt\" &&\n>      ++\tgit add --chmod +x o1.txt &&\n>      ++\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n>       +\tgit commit -m base\n>       +'\n>       +\n>      -+test_expect_success 'git ls-files --format objectmode' '\n>      -+\tcat >expect <<-\\EOF &&\n>      -+\t100755\n>      -+\t100644\n>      -+\tEOF\n>      ++test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>      ++\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n>       +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n>       +\ttest_cmp expect actual\n>       +'\n>       +\n>      -+test_expect_success 'git ls-files --format objectname' '\n>      -+\toid1=$(git hash-object o1) &&\n>      -+\toid2=$(git hash-object o2) &&\n>      -+\tcat >expect <<-EOF &&\n>      -+\t$oid1\n>      -+\t$oid2\n>      -+\tEOF\n>      ++test_expect_success 'git ls-files --format objectname v.s. -s' '\n>      ++\tgit ls-files -s | awk \"{print \\$2}\" >expect &&\n>       +\tgit ls-files --format=\"%(objectname)\" >actual &&\n>       +\ttest_cmp expect actual\n>       +'\n>       +\n>      -+HT='\t'\n>      -+WS='    '\n>       +test_expect_success 'git ls-files --format v.s. --eol' '\n>      -+\tgit ls-files --eol >expect 2>err &&\n>      ++\tgit ls-files --eol >tmp &&\n>      ++\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n>       +\ttest_must_be_empty err &&\n>      -+\tgit ls-files --format=\"i/%(eolinfo:index)${WS}w/%(eolinfo:worktree)${WS}attr/${WS}${WS}${WS}${WS} ${HT}%(path)\" >actual 2>err &&\n>      ++\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n>       +\ttest_must_be_empty err &&\n>       +\ttest_cmp expect actual\n>       +'\n>       +\n>      -+test_expect_success 'git ls-files --format path' '\n>      -+\tcat >expect <<-\\EOF &&\n>      -+\to1\n>      -+\to2\n>      -+\tEOF\n>      ++test_expect_success 'git ls-files --format path v.s. -s' '\n>      ++\tgit ls-files -s | awk \"{print \\$4}\" >expect &&\n>       +\tgit ls-files --format=\"%(path)\" >actual &&\n>       +\ttest_cmp expect actual\n>       +'\n>       +\n>       +test_expect_success 'git ls-files --format with -m' '\n>      -+\techo change >o1 &&\n>      ++\techo change >o1.txt &&\n>       +\tcat >expect <<-\\EOF &&\n>      -+\to1\n>      ++\to1.txt\n>      ++\to5.txt\n>       +\tEOF\n>       +\tgit ls-files --format=\"%(path)\" -m >actual &&\n>       +\ttest_cmp expect actual\n>       +'\n>       +\n>       +test_expect_success 'git ls-files --format with -d' '\n>      -+\techo o3 >o3 &&\n>      -+\tgit add o3 &&\n>      -+\trm o3 &&\n>      ++\techo o6 >o6.txt &&\n>      ++\tgit add o6.txt &&\n>      ++\trm o6.txt &&\n>       +\tcat >expect <<-\\EOF &&\n>      -+\to3\n>      ++\to5.txt\n>      ++\to6.txt\n>       +\tEOF\n>       +\tgit ls-files --format=\"%(path)\" -d >actual &&\n>       +\ttest_cmp expect actual\n>\n>\n>  Documentation/git-ls-files.txt | 39 +++++++++++++-\n>  builtin/ls-files.c             | 95 ++++++++++++++++++++++++++++++++++\n>  t/t3013-ls-files-format.sh     | 87 +++++++++++++++++++++++++++++++\n>  3 files changed, 220 insertions(+), 1 deletion(-)\n>  create mode 100755 t/t3013-ls-files-format.sh\n>\n> diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\n> index 0dabf3f0ddc..d7986419c25 100644\n> --- a/Documentation/git-ls-files.txt\n> +++ b/Documentation/git-ls-files.txt\n> @@ -20,7 +20,7 @@ SYNOPSIS\n>  \t\t[--exclude-standard]\n>  \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n>  \t\t[--full-name] [--recurse-submodules]\n> -\t\t[--abbrev[=<n>]] [--] [<file>...]\n> +\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n>  \n>  DESCRIPTION\n>  -----------\n> @@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n>  \tto the contained files. Sparse directories will be shown with a\n>  \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n>  \n> +--format=<format>::\n> +\tA string that interpolates `%(fieldname)` from the result being shown.\n> +\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n> +\tinterpolates to character with hex code `xx`; for example `%00`\n> +\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n> +\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n> +\tand `--eol`.\n>  \\--::\n>  \tDo not interpret any more arguments as options.\n>  \n> @@ -223,6 +230,36 @@ quoted as explained for the configuration variable `core.quotePath`\n>  (see linkgit:git-config[1]).  Using `-z` the filename is output\n>  verbatim and the line is terminated by a NUL byte.\n>  \n> +It is possible to print in a custom format by using the `--format`\n> +option, which is able to interpolate different fields using\n> +a `%(fieldname)` notation. For example, if you only care about the\n> +\"objectname\" and \"path\" fields, you can execute with a specific\n> +\"--format\" like\n> +\n> +\tgit ls-files --format='%(objectname) %(path)'\n> +\n> +FIELD NAMES\n> +-----------\n> +The way each path is shown can be customized by using the\n> +`--format=<format>` option, where the %(fieldname) in the\n> +<format> string for various aspects of the index entry are\n> +interpolated.  The following \"fieldname\" are understood:\n> +\n> +objectmode::\n> +\tThe mode of the file which is recorded in the index.\n> +objectname::\n> +\tThe name of the file which is recorded in the index.\n> +stage::\n> +\tThe stage of the file which is recorded in the index.\n> +eolinfo:index::\n> +eolinfo:worktree::\n> +\tThe <eolinfo> (see the description of the `--eol` option) of\n> +\tthe contents in the index or in the worktree for the path.\n> +eolattr::\n> +\tThe <eolattr> (see the description of the `--eol` option)\n> +\tthat applies to the path.\n> +path::\n> +\tThe pathname of the file which is recorded in the index.\n>  \n>  EXCLUDE PATTERNS\n>  ----------------\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index e791b65e7e9..6f3ebcaaff7 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -11,6 +11,7 @@\n>  #include \"quote.h\"\n>  #include \"dir.h\"\n>  #include \"builtin.h\"\n> +#include \"strbuf.h\"\n>  #include \"tree.h\"\n>  #include \"cache-tree.h\"\n>  #include \"parse-options.h\"\n> @@ -48,6 +49,7 @@ static char *ps_matched;\n>  static const char *with_tree;\n>  static int exc_given;\n>  static int exclude_args;\n> +static const char *format;\n>  \n>  static const char *tag_cached = \"\";\n>  static const char *tag_unmerged = \"\";\n> @@ -85,6 +87,16 @@ static void write_name(const char *name)\n>  \t\t\t\t   stdout, line_terminator);\n>  }\n>  \n> +static void write_name_to_buf(struct strbuf *sb, const char *name)\n> +{\n> +\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n> +\n> +\tif (line_terminator)\n> +\t\tquote_c_style(rel, sb, NULL, 0);\n> +\telse\n> +\t\tstrbuf_addstr(sb, rel);\n> +}\n> +\n>  static const char *get_tag(const struct cache_entry *ce, const char *tag)\n>  {\n>  \tstatic char alttag[4];\n> @@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n>  \trepo_clear(&subrepo);\n>  }\n>  \n> +struct show_index_data {\n> +\tconst char *pathname;\n> +\tstruct index_state *istate;\n> +\tconst struct cache_entry *ce;\n> +};\n> +\n> +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> +\t\t\t\tvoid *context)\n> +{\n> +\tstruct show_index_data *data = context;\n> +\tconst char *end;\n> +\tconst char *p;\n> +\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> +\tstruct stat st;\n> +\n> +\tif (len)\n> +\t\treturn len;\n> +\tif (*start != '(')\n> +\t\tdie(_(\"bad ls-files format: element '%s' \"\n> +\t\t      \"does not start with '('\"), start);\n> +\n> +\tend = strchr(start + 1, ')');\n> +\tif (!end)\n> +\t\tdie(_(\"bad ls-files format: element '%s'\"\n> +\t\t      \"does not end in ')'\"), start);\n> +\n> +\tlen = end - start + 1;\n> +\tif (skip_prefix(start, \"(objectmode)\", &p))\n> +\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n> +\telse if (skip_prefix(start, \"(objectname)\", &p))\n> +\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n> +\telse if (skip_prefix(start, \"(stage)\", &p))\n> +\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n> +\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n> +\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n> +\t\t\t      get_cached_convert_stats_ascii(data->istate,\n> +\t\t\t      data->ce->name) : \"\");\n> +\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n> +\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n> +\t\t\t      S_ISREG(st.st_mode) ?\n> +\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n> +\telse if (skip_prefix(start, \"(eolattr)\", &p))\n> +\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n> +\t\t\t      data->pathname));\n> +\telse if (skip_prefix(start, \"(path)\", &p))\n> +\t\twrite_name_to_buf(sb, data->pathname);\n> +\telse\n> +\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n> +\n> +\treturn len;\n> +}\n> +\n> +static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n> +\t\t\tconst char *format, const char *fullname) {\n> +\tstruct show_index_data data = {\n> +\t\t.pathname = fullname,\n> +\t\t.istate = repo->index,\n> +\t\t.ce = ce,\n> +\t};\n> +\tstruct strbuf sb = STRBUF_INIT;\n> +\n> +\tstrbuf_expand(&sb, format, expand_show_index, &data);\n> +\tstrbuf_addch(&sb, line_terminator);\n> +\tfwrite(sb.buf, sb.len, 1, stdout);\n> +\tstrbuf_release(&sb);\n> +}\n> +\n>  static void show_ce(struct repository *repo, struct dir_struct *dir,\n>  \t\t    const struct cache_entry *ce, const char *fullname,\n>  \t\t    const char *tag)\n> @@ -236,6 +315,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n>  \t\t\t\t  max_prefix_len, ps_matched,\n>  \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n>  \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n> +\t\tif (format) {\n> +\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n> +\t\t\tprint_debug(ce);\n> +\t\t\treturn;\n> +\t\t}\n> +\n>  \t\ttag = get_tag(ce, tag);\n>  \n>  \t\tif (!show_stage) {\n> @@ -675,6 +760,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n>  \t\t\t N_(\"suppress duplicate entries\")),\n>  \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n>  \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n> +\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n> +\t\t\t     N_(\"format to use for the output\"),\n> +\t\t\t     PARSE_OPT_NONEG),\n>  \t\tOPT_END()\n>  \t};\n>  \tint ret = 0;\n> @@ -699,6 +787,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n>  \tfor (i = 0; i < exclude_list.nr; i++) {\n>  \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n>  \t}\n> +\n> +\tif (format && (show_stage || show_others || show_killed ||\n> +\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n> +\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n> +\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n> +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n\nThere's a whitespace issue here, you need to add a space after \"-t\",\notherwise we emit:\n\n\tfatal: --format cannot used with -s, -o, -k, -t--resolve-undo, --deduplicate, --eol\n> +\n>  \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n>  \t\ttag_cached = \"H \";\n>  \t\ttag_unmerged = \"M \";\n> diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n> new file mode 100755\n> index 00000000000..e62bce70f3b\n> --- /dev/null\n> +++ b/t/t3013-ls-files-format.sh\n> @@ -0,0 +1,87 @@\n> +#!/bin/sh\n> +\n> +test_description='git ls-files --format test'\n> +\n> +TEST_PASSES_SANITIZE_LEAK=true\n> +. ./test-lib.sh\n> +\n> +for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n> +do\n> +\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n> +\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n> +\t'\n> +done\n> +\n> +test_expect_success 'setup' '\n> +\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n> +\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n> +\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n\nIf you want to do this sort of thing in general this pattern is better:\n\n\tx=\"a b c\" &&\n\tprintf \"%s\\n\" $x\n        printf \"%s\\r\\n\" $x\n\nI.e. you can use printf's auto-repeating, or test_write_lines[1]. But in\nthis case I tried:\n\n\tdiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n\tindex e62bce70f3b..e4c3a788acb 100755\n\t--- a/t/t3013-ls-files-format.sh\n\t+++ b/t/t3013-ls-files-format.sh\n\t@@ -13,9 +13,11 @@ do\n\t done\n\t \n\t test_expect_success 'setup' '\n\t-\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n\t-\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n\t-\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n\t+\tlines=\"LO LINETWO LINETHREE\" &&\n\t+\ttest_write_lines $lines >o1.txt &&\n\t+\t# Even this passes!\n\t+\t#>o1.txt &&\n\t+\n\t \tln -s o3.txt o4.txt &&\n\t \tgit add \"*.txt\" &&\n\t \tgit add --chmod +x o1.txt &&\n\nI.e. all tests pass if we don't write o2.txt and o3.txt, and continue to\npass if you uncomment that and make o1.txt an empty file.\n\nSo is this some incomplete test setup code that was never used & we\ncould drop?\n\t\n\n> +\tln -s o3.txt o4.txt &&\n> +\tgit add \"*.txt\" &&\n> +\tgit add --chmod +x o1.txt &&\n> +\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n\nDo:\n\n\toid=$(git hash-object ..) &&\n\tgit update-index ... \"$(oid)\"\n\nOtherwise we hide the exit code of \"git-hash-object\", e.g. if it returns\nthe hash and then segfaults.\n\n> +\tgit commit -m base\n> +'\n> +\n> +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> +\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n\nSame in this case and below, i.e. let's not hide \"git\" on the lhs of a\npipe. So:\n\n\tgit ls-files >files &&\n\tawk ... <files >expect\n\nIn this case all your awk-ing can be replaced with (continued)...\n\n> +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format objectname v.s. -s' '\n> +\tgit ls-files -s | awk \"{print \\$2}\" >expect &&\n\n...\n\n\tcut -d\" \" -f2\n\n...\n\n> +\tgit ls-files --format=\"%(objectname)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format v.s. --eol' '\n> +\tgit ls-files --eol >tmp &&\n> +\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n> +\ttest_must_be_empty err &&\n> +\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n> +\ttest_must_be_empty err &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format path v.s. -s' '\n> +\tgit ls-files -s | awk \"{print \\$4}\" >expect &&\n\n...\n\n\tcut -f2\n\nI.e. instead of the 4th whitespace field ask for the 2nd \\t-delimited\nfield. There's nothing wrong with using awk per-se, but let's use the\nsimpler \"cut\" for such a simple use-case.\n\n> +\tgit ls-files --format=\"%(path)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -m' '\n> +\techo change >o1.txt &&\n> +\tcat >expect <<-\\EOF &&\n> +\to1.txt\n> +\to5.txt\n> +\tEOF\n> +\tgit ls-files --format=\"%(path)\" -m >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format with -d' '\n> +\techo o6 >o6.txt &&\n> +\tgit add o6.txt &&\n> +\trm o6.txt &&\n> +\tcat >expect <<-\\EOF &&\n> +\to5.txt\n> +\to6.txt\n> +\tEOF\n> +\tgit ls-files --format=\"%(path)\" -d >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format imitate --stage' '\n> +\tgit ls-files --stage >expect &&\n> +\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'git ls-files --format with --debug' '\n> +\tgit ls-files --debug >expect &&\n> +\tgit ls-files --format=\"%(path)\" --debug >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_done\n\nThe rest of this (and especially the C code) all looks good to me at\nthis point, thanks!\n\n1. Aside: I've found the test_write_lines helper to be rather strange\n   for us to carry. I.e. most helpers provide a briefer or less\n   buggy/tricky way to do something, but in that case:\n\n\ttest_write_lines\n\tprintf \"%s\\n\"\n\n   So we have it to write something in a more verbose way than we need,\n   as we can see experimentally from all tests passing with:\n\n\tperl -pi -e 's[test_write_lines ][printf \"%s\\\\n\" ]g' t/t[0-9]*.sh\n\n   It seems to me that per\n   https://lore.kernel.org/git/xmqqioqu5fr3.fsf@gitster.dls.corp.google.com/\n   and\n   https://lore.kernel.org/git/1398255277-26303-2-git-send-email-mst@redhat.com/\n   it was suggested without knowing that we could use printf to do the\n   same.\n\n   The implementation that landed in ac9afcc31cd (test: add\n   test_write_lines helper, 2014-04-27) was fixed up to use printf,\n   without re-visiting why we were carrying a helper when an even\n   shorter printf would do...\n"},{"id":"459381","messageId":"CAOLTT8R_=hmwDab1Tq+qy1vXUZ4C1z6+9xTyR8WSzQ=JOBnhjg@mail.gmail.com","threadId":"58016","inReplyTo":"220718.86pmi2ygbt.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v7] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-19T16:19:36Z","receivedAt":"2022-07-19T16:19:59Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年7月18日周一 16:29写道：\n>\n>\n> On Wed, Jul 13 2022, ZheNing Hu via GitGitGadget wrote:\n>\n> > +test_expect_success 'setup' '\n> > +     printf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n> > +     printf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n> > +     printf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n>\n> If you want to do this sort of thing in general this pattern is better:\n>\n>         x=\"a b c\" &&\n>         printf \"%s\\n\" $x\n>         printf \"%s\\r\\n\" $x\n>\n\nLet see what's these cmd output:\n\nx=\"a b c\" &&\nprintf \"%s\\n\" $x &&\nprintf \"%s\\r\\n\" $x\n\na b c\na b c\n\nI guess what we expect is:\n\na\nb\nc\na\nb\nc\n\ntest_write_lines() can do this:\n\ntest_write_lines a b c\ntest_write_lines a b c\n\nyeah, maybe printf do this too:\n\n# x=\"a b c\" we don't use a variable\nprintf \"%s\\n\" a b c &&\nprintf \"%s\\r\\n\" a b c\n\n> I.e. you can use printf's auto-repeating, or test_write_lines[1]. But in\n> this case I tried:\n>\n>         diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\n>         index e62bce70f3b..e4c3a788acb 100755\n>         --- a/t/t3013-ls-files-format.sh\n>         +++ b/t/t3013-ls-files-format.sh\n>         @@ -13,9 +13,11 @@ do\n>          done\n>\n>          test_expect_success 'setup' '\n>         -       printf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n>         -       printf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n>         -       printf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n>         +       lines=\"LO LINETWO LINETHREE\" &&\n>         +       test_write_lines $lines >o1.txt &&\n>         +       # Even this passes!\n>         +       #>o1.txt &&\n>         +\n>                 ln -s o3.txt o4.txt &&\n>                 git add \"*.txt\" &&\n>                 git add --chmod +x o1.txt &&\n>\n> I.e. all tests pass if we don't write o2.txt and o3.txt, and continue to\n> pass if you uncomment that and make o1.txt an empty file.\n>\n> So is this some incomplete test setup code that was never used & we\n> could drop?\n\nNo, o2.txt, o3.txt just for test some file with different\neolinfo/eolattr, so if we just\nkeep o1,txt and no o2.txt, o3.txt, it can certainly work with this test case:\n'git ls-files --format v.s. --eol'. Other cases don't really need\no2.txt, o3.txt.\n\nBy the way: this part of code was copied from t/t0025-crlf-renormalize.sh.\nit want to test for three kind file, end with \"\\n\", \"\\r\\n\", or mix\nwith \"\\n\", \"\\r\\n\".\n\n>\n>\n> > +     ln -s o3.txt o4.txt &&\n> > +     git add \"*.txt\" &&\n> > +     git add --chmod +x o1.txt &&\n> > +     git update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n>\n> Do:\n>\n>         oid=$(git hash-object ..) &&\n>         git update-index ... \"$(oid)\"\n>\n> Otherwise we hide the exit code of \"git-hash-object\", e.g. if it returns\n> the hash and then segfaults.\n>\n\nThanks. I will keep this in my mind.\n\n> > +     git commit -m base\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> > +     git ls-files -s | awk \"{print \\$1}\" >expect &&\n>\n> Same in this case and below, i.e. let's not hide \"git\" on the lhs of a\n> pipe. So:\n>\n>         git ls-files >files &&\n>         awk ... <files >expect\n>\n> In this case all your awk-ing can be replaced with (continued)...\n>\n> > +     git ls-files --format=\"%(objectmode)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format objectname v.s. -s' '\n> > +     git ls-files -s | awk \"{print \\$2}\" >expect &&\n>\n> ...\n>\n>         cut -d\" \" -f2\n>\n> ...\n>\n> > +     git ls-files --format=\"%(objectname)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format v.s. --eol' '\n> > +     git ls-files --eol >tmp &&\n> > +     sed -e \"s/      / /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n> > +     test_must_be_empty err &&\n> > +     git ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n> > +     test_must_be_empty err &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format path v.s. -s' '\n> > +     git ls-files -s | awk \"{print \\$4}\" >expect &&\n>\n> ...\n>\n>         cut -f2\n>\n> I.e. instead of the 4th whitespace field ask for the 2nd \\t-delimited\n> field. There's nothing wrong with using awk per-se, but let's use the\n> simpler \"cut\" for such a simple use-case.\n>\n> > +     git ls-files --format=\"%(path)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with -m' '\n> > +     echo change >o1.txt &&\n> > +     cat >expect <<-\\EOF &&\n> > +     o1.txt\n> > +     o5.txt\n> > +     EOF\n> > +     git ls-files --format=\"%(path)\" -m >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with -d' '\n> > +     echo o6 >o6.txt &&\n> > +     git add o6.txt &&\n> > +     rm o6.txt &&\n> > +     cat >expect <<-\\EOF &&\n> > +     o5.txt\n> > +     o6.txt\n> > +     EOF\n> > +     git ls-files --format=\"%(path)\" -d >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format imitate --stage' '\n> > +     git ls-files --stage >expect &&\n> > +     git ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_expect_success 'git ls-files --format with --debug' '\n> > +     git ls-files --debug >expect &&\n> > +     git ls-files --format=\"%(path)\" --debug >actual &&\n> > +     test_cmp expect actual\n> > +'\n> > +\n> > +test_done\n>\n> The rest of this (and especially the C code) all looks good to me at\n> this point, thanks!\n>\n> 1. Aside: I've found the test_write_lines helper to be rather strange\n>    for us to carry. I.e. most helpers provide a briefer or less\n>    buggy/tricky way to do something, but in that case:\n>\n>         test_write_lines\n>         printf \"%s\\n\"\n>\n>    So we have it to write something in a more verbose way than we need,\n>    as we can see experimentally from all tests passing with:\n>\n>         perl -pi -e 's[test_write_lines ][printf \"%s\\\\n\" ]g' t/t[0-9]*.sh\n>\n>    It seems to me that per\n>    https://lore.kernel.org/git/xmqqioqu5fr3.fsf@gitster.dls.corp.google.com/\n>    and\n>    https://lore.kernel.org/git/1398255277-26303-2-git-send-email-mst@redhat.com/\n>    it was suggested without knowing that we could use printf to do the\n>    same.\n>\n>    The implementation that landed in ac9afcc31cd (test: add\n>    test_write_lines helper, 2014-04-27) was fixed up to use printf,\n>    without re-visiting why we were carrying a helper when an even\n>    shorter printf would do...\n\nI guess it's just for letting contributers use the same way output multiple\nline, otherwise contributers (like me) want to use echo or other commands\nsometimes?\n\nBut in my test case here, I need a file mixed with \"r\\n\", \"\\n\" instread of\nonly \"\\n\", so I think maybe we should keep it.\n\nZheNing Hu\n"},{"id":"459386","messageId":"xmqqh73daw1z.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8R_=hmwDab1Tq+qy1vXUZ4C1z6+9xTyR8WSzQ=JOBnhjg@mail.gmail.com","subject":"Re: [PATCH v7] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-19T16:47:52Z","receivedAt":"2022-07-19T16:48:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n> Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年7月18日周一 16:29写道：\n>>\n>>\n>> On Wed, Jul 13 2022, ZheNing Hu via GitGitGadget wrote:\n>>\n>> > +test_expect_success 'setup' '\n>> > +     printf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n>> > +     printf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n>> > +     printf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n>>\n>> If you want to do this sort of thing in general this pattern is better:\n>>\n>>         x=\"a b c\" &&\n>>         printf \"%s\\n\" $x\n>>         printf \"%s\\r\\n\" $x\n>>\n>\n> Let see what's these cmd output:\n>\n> x=\"a b c\" &&\n> printf \"%s\\n\" $x &&\n> printf \"%s\\r\\n\" $x\n>\n> a b c\n> a b c\n\nThe above makes it look as if your shell is broken or you have an\nunusual IFS that does not have space in it.  Are you sure you did\nnot place anything around $x on the second and the third line, which\nis given to printf after its contents split into words at $IFS?\n"},{"id":"459387","messageId":"CAOLTT8SW4RNjGoKGi=FPoDmPVvie_tADeex+NXE3M7a_j6V4qA@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqh73daw1z.fsf@gitster.g","subject":"Re: [PATCH v7] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-19T17:21:39Z","receivedAt":"2022-07-19T17:22:00Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年7月20日周三 00:47写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> > Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2022年7月18日周一 16:29写道：\n> >>\n> >>\n> >> On Wed, Jul 13 2022, ZheNing Hu via GitGitGadget wrote:\n> >>\n> >> > +test_expect_success 'setup' '\n> >> > +     printf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n> >> > +     printf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n> >> > +     printf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n> >>\n> >> If you want to do this sort of thing in general this pattern is better:\n> >>\n> >>         x=\"a b c\" &&\n> >>         printf \"%s\\n\" $x\n> >>         printf \"%s\\r\\n\" $x\n> >>\n> >\n> > Let see what's these cmd output:\n> >\n> > x=\"a b c\" &&\n> > printf \"%s\\n\" $x &&\n> > printf \"%s\\r\\n\" $x\n> >\n> > a b c\n> > a b c\n>\n> The above makes it look as if your shell is broken or you have an\n> unusual IFS that does not have space in it.  Are you sure you did\n> not place anything around $x on the second and the third line, which\n> is given to printf after its contents split into words at $IFS?\n\nOk... That's zsh's strange feature.... I turn to use bash, it's find.\n"},{"id":"459516","messageId":"pull.1262.v8.git.1658334983053.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v7.git.1657692472994.gitgitgadget@gmail.com","subject":"[PATCH v8] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-07-20T16:36:22Z","receivedAt":"2022-07-20T16:36:34Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option \"--format\" that outputs index entries\ninformations in a custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n\"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n\" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v6->v7:\n    \n     1. fix the usage which leak of one white blank.\n     2. fix some test cases.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v8\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v8\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v7:\n\n 1:  9ca22edba94 ! 1:  e765ee28e90 ls-files: introduce \"--format\" option\n     @@ builtin/ls-files.c: int cmd_ls_files(int argc, const char **argv, const char *cm\n      +\n      +\tif (format && (show_stage || show_others || show_killed ||\n      +\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n     -+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n     ++\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n      +\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n      +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n      +\n     @@ t/t3013-ls-files-format.sh (new)\n      +\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n      +\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n      +\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n     -+\tln -s o3.txt o4.txt &&\n     -+\tgit add \"*.txt\" &&\n     -+\tgit add --chmod +x o1.txt &&\n     -+\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n     ++\tgit add . &&\n     ++\toid=$(git hash-object o1.txt) &&\n     ++\tgit update-index --add --cacheinfo 120000 $oid o4.txt &&\n     ++\tgit update-index --add --cacheinfo 160000 $oid o5.txt &&\n     ++\tgit update-index --add --cacheinfo 100755 $oid o6.txt &&\n      +\tgit commit -m base\n      +'\n      +\n      +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n     -+\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n     ++\tgit ls-files -s >files &&\n     ++\tcut -d\" \" -f1 files >expect &&\n      +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format objectname v.s. -s' '\n     -+\tgit ls-files -s | awk \"{print \\$2}\" >expect &&\n     ++\tgit ls-files -s >files &&\n     ++\tcut -d\" \" -f2 files >expect &&\n      +\tgit ls-files --format=\"%(objectname)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n     @@ t/t3013-ls-files-format.sh (new)\n      +'\n      +\n      +test_expect_success 'git ls-files --format path v.s. -s' '\n     -+\tgit ls-files -s | awk \"{print \\$4}\" >expect &&\n     ++\tgit ls-files -s >files &&\n     ++\tcut -f2 files >expect &&\n      +\tgit ls-files --format=\"%(path)\" >actual &&\n      +\ttest_cmp expect actual\n      +'\n     @@ t/t3013-ls-files-format.sh (new)\n      +\techo change >o1.txt &&\n      +\tcat >expect <<-\\EOF &&\n      +\to1.txt\n     ++\to4.txt\n      +\to5.txt\n     ++\to6.txt\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -m >actual &&\n      +\ttest_cmp expect actual\n      +'\n      +\n      +test_expect_success 'git ls-files --format with -d' '\n     -+\techo o6 >o6.txt &&\n     -+\tgit add o6.txt &&\n     -+\trm o6.txt &&\n     ++\techo o7 >o7.txt &&\n     ++\tgit add o7.txt &&\n     ++\trm o7.txt &&\n      +\tcat >expect <<-\\EOF &&\n     ++\to4.txt\n      +\to5.txt\n      +\to6.txt\n     ++\to7.txt\n      +\tEOF\n      +\tgit ls-files --format=\"%(path)\" -d >actual &&\n      +\ttest_cmp expect actual\n\n\n Documentation/git-ls-files.txt | 39 +++++++++++++-\n builtin/ls-files.c             | 95 ++++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 95 ++++++++++++++++++++++++++++++++++\n 3 files changed, 228 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..d7986419c25 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,36 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+The way each path is shown can be customized by using the\n+`--format=<format>` option, where the %(fieldname) in the\n+<format> string for various aspects of the index entry are\n+interpolated.  The following \"fieldname\" are understood:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+eolinfo:worktree::\n+\tThe <eolinfo> (see the description of the `--eol` option) of\n+\tthe contents in the index or in the worktree for the path.\n+eolattr::\n+\tThe <eolattr> (see the description of the `--eol` option)\n+\tthat applies to the path.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..44a2c1cb425 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -85,6 +87,16 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_addstr(sb, rel);\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t\tvoid *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tstruct stat st;\n+\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n+\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n+\t\t\t      get_cached_convert_stats_ascii(data->istate,\n+\t\t\t      data->ce->name) : \"\");\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n+\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n+\t\t\t      S_ISREG(st.st_mode) ?\n+\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n+\t\t\t      data->pathname));\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +315,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +760,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +787,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..92d378ad1df\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,95 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n+\tgit add . &&\n+\toid=$(git hash-object o1.txt) &&\n+\tgit update-index --add --cacheinfo 120000 $oid o4.txt &&\n+\tgit update-index --add --cacheinfo 160000 $oid o5.txt &&\n+\tgit update-index --add --cacheinfo 100755 $oid o6.txt &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -d\" \" -f1 files >expect &&\n+\tgit ls-files --format=\"%(objectmode)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -d\" \" -f2 files >expect &&\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+\tgit ls-files --eol >tmp &&\n+\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n+\ttest_must_be_empty err &&\n+\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -f2 files >expect &&\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to1.txt\n+\to4.txt\n+\to5.txt\n+\to6.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o7 >o7.txt &&\n+\tgit add o7.txt &&\n+\trm o7.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to4.txt\n+\to5.txt\n+\to6.txt\n+\to7.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format imitate --stage' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"459522","messageId":"xmqqbktj3ct7.fsf@gitster.g","threadId":"58016","inReplyTo":"pull.1262.v8.git.1658334983053.gitgitgadget@gmail.com","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-20T17:37:40Z","receivedAt":"2022-07-20T17:38:04Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\nIt's been quite many iterations, so I'll just comment on the range-diff.\n\n>      -+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n>      ++\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n>       +\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n>       +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n\nLooks good.\n\n>      @@ t/t3013-ls-files-format.sh (new)\n>       +\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n>       +\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n>       +\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n>      -+\tln -s o3.txt o4.txt &&\n>      -+\tgit add \"*.txt\" &&\n>      -+\tgit add --chmod +x o1.txt &&\n>      -+\tgit update-index --add --cacheinfo 160000 $(git hash-object o1.txt) o5.txt &&\n>      ++\tgit add . &&\n\nWe may want to be a bit more strict (like \"o?.txt\") but because we\nknow this is the first 'setup' step, let's let it pass.\n\n>      ++\toid=$(git hash-object o1.txt) &&\n>      ++\tgit update-index --add --cacheinfo 120000 $oid o4.txt &&\n>      ++\tgit update-index --add --cacheinfo 160000 $oid o5.txt &&\n>      ++\tgit update-index --add --cacheinfo 100755 $oid o6.txt &&\n\nIt is a bit inconvenient that --cacheinfo takes only fully-spelled\nraw object name that we need to use $oid like this (otherwise we\nwould be able to write \":o1.txt\" instead), but (1) it is not a fault\nof this patch, and (2) update-index is a plumbing command meant for\nscripts, so it is not too big a problem.\n\n>       +\tgit commit -m base\n>       +'\n>       +\n>       +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>      -+\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n>      ++\tgit ls-files -s >files &&\n>      ++\tcut -d\" \" -f1 files >expect &&\n\nEither \"awk\" or \"cut\" is fine and flipping between them is a bit\ndistracting.  Cutting the pipe into two is a good move.\n\nBut is this testing the right thing?\n\n> +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> +\tgit ls-files -s >files &&\n> +\tcut -d\" \" -f1 files >expect &&\n> +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n> +\ttest_cmp expect actual\n> +'\n\nIt only looks at the first column of the \"-s\" output, and we are\nimplicitly assuming that the order of output does not change between\nthe \"-s\" output and \"--format=<format>\" output.  I wonder if it is\nmore useful and less error prone to come up with a format string\nthat 100% reproduces the \"ls-files -s\" output and compare the two,\ne.g. \n\n\tformat=\"%(objectmode) %(objectname) %(stage)\t%(path)\" &&\n\tgit ls-files -s >expect &&\n\tgit ls-files --format=\"$format\" >actual &&\n\ttest_cmp expect actual\n\nI do not know if the $format I wrote without looking at the doc is\ncorrect, but you get the idea.\n\nThanks.\n\n\n"},{"id":"459639","messageId":"220721.86ilnqtq8f.gmgdl@evledraar.gmail.com","threadId":"58016","inReplyTo":"xmqqbktj3ct7.fsf@gitster.g","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-07-21T15:54:07Z","receivedAt":"2022-07-21T15:55:35Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Wed, Jul 20 2022, Junio C Hamano wrote:\n\n> \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> It's been quite many iterations, so I'll just comment on the range-diff.\n>\n>>      -+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n>>      ++\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n>>       +\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n>>       +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n>\n> Looks good.\n\nAlthough a nit I didn't spot before: missing _() & this should be marked\nfor translation, surely...\n\n>>       +\tgit commit -m base\n>>       +'\n>>       +\n>>       +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>>      -+\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n>>      ++\tgit ls-files -s >files &&\n>>      ++\tcut -d\" \" -f1 files >expect &&\n>\n> Either \"awk\" or \"cut\" is fine and flipping between them is a bit\n> distracting.  Cutting the pipe into two is a good move.\n\nThat \"cut\" suggestion saw mine, sorry about the churn...\n\n> But is this testing the right thing?\n\nOn this...\n\n>> +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>> +\tgit ls-files -s >files &&\n>> +\tcut -d\" \" -f1 files >expect &&\n>> +\tgit ls-files --format=\"%(objectmode)\" >actual &&\n>> +\ttest_cmp expect actual\n>> +'\n>\n> It only looks at the first column of the \"-s\" output, and we are\n> implicitly assuming that the order of output does not change between\n> the \"-s\" output and \"--format=<format>\" output.  I wonder if it is\n> more useful and less error prone to come up with a format string\n> that 100% reproduces the \"ls-files -s\" output and compare the two,\n> e.g. \n>\n> \tformat=\"%(objectmode) %(objectname) %(stage)\t%(path)\" &&\n> \tgit ls-files -s >expect &&\n> \tgit ls-files --format=\"$format\" >actual &&\n> \ttest_cmp expect actual\n>\n> I do not know if the $format I wrote without looking at the doc is\n> correct, but you get the idea.\n\nPast rounds moved some tests towards that, maybe that's a good thing\nhere too I didn't look deeply this time around...\n"},{"id":"459664","messageId":"CAPig+cTAnVjk9gepW33AW9SSpCTJ7FpNSaB_7M-wV+tTe6D04A@mail.gmail.com","threadId":"58016","inReplyTo":"220721.86ilnqtq8f.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2022-07-21T17:22:17Z","receivedAt":"2022-07-21T17:22:36Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jul 21, 2022 at 12:07 PM Ævar Arnfjörð Bjarmason\n<avarab@gmail.com> wrote:\n> On Wed, Jul 20 2022, Junio C Hamano wrote:\n> > \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n> > It's been quite many iterations, so I'll just comment on the range-diff.\n> >\n> >>      -+                      usage_msg_opt(\"--format cannot used with -s, -o, -k, -t\"\n> >>      ++                      usage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n> >>       +                                    \"--resolve-undo, --deduplicate, --eol\",\n> >>       +                                    ls_files_usage, builtin_ls_files_options);\n> >\n> > Looks good.\n>\n> Although a nit I didn't spot before: missing _() & this should be marked\n> for translation, surely...\n\nProbably also want: s/cannot used/cannot be used/\n"},{"id":"459665","messageId":"xmqqsfmuidmp.fsf@gitster.g","threadId":"58016","inReplyTo":"220721.86ilnqtq8f.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-21T17:23:10Z","receivedAt":"2022-07-21T17:23:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:\n\n>>>       +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>>>      -+\tgit ls-files -s | awk \"{print \\$1}\" >expect &&\n>>>      ++\tgit ls-files -s >files &&\n>>>      ++\tcut -d\" \" -f1 files >expect &&\n>>\n>> Either \"awk\" or \"cut\" is fine and flipping between them is a bit\n>> distracting.  Cutting the pipe into two is a good move.\n>\n> That \"cut\" suggestion saw mine, sorry about the churn...\n\nAs I said \"cut\" is perfectly fine.  Unless this part goes away,\n(i.e. perhaps we may decide that it is a bad idea to check only the\npieces of lines), let's not flip back to awk ;-)\n\n>> \tformat=\"%(objectmode) %(objectname) %(stage)\t%(path)\" &&\n>> \tgit ls-files -s >expect &&\n>> \tgit ls-files --format=\"$format\" >actual &&\n>> \ttest_cmp expect actual\n>>\n>> I do not know if the $format I wrote without looking at the doc is\n>> correct, but you get the idea.\n>\n> Past rounds moved some tests towards that, maybe that's a good thing\n> here too I didn't look deeply this time around...\n\nOK, thanks for reviewing.\n"},{"id":"459756","messageId":"CAOLTT8RjLoooT7t+ucFqa9P=8TiVL3M+ZgcEY7qVhRbjB=9OhA@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqbktj3ct7.fsf@gitster.g","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-22T06:44:42Z","receivedAt":"2022-07-22T06:44:56Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年7月21日周四 01:37写道：\n>\n> >       +       git commit -m base\n> >       +'\n> >       +\n> >       +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> >      -+       git ls-files -s | awk \"{print \\$1}\" >expect &&\n> >      ++       git ls-files -s >files &&\n> >      ++       cut -d\" \" -f1 files >expect &&\n>\n> Either \"awk\" or \"cut\" is fine and flipping between them is a bit\n> distracting.  Cutting the pipe into two is a good move.\n>\n> But is this testing the right thing?\n>\n\nYes, I am sure about that cut can do the same thing as awk, and it can\nspecify its delimiter.\n\n> > +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> > +     git ls-files -s >files &&\n> > +     cut -d\" \" -f1 files >expect &&\n> > +     git ls-files --format=\"%(objectmode)\" >actual &&\n> > +     test_cmp expect actual\n> > +'\n>\n> It only looks at the first column of the \"-s\" output, and we are\n> implicitly assuming that the order of output does not change between\n> the \"-s\" output and \"--format=<format>\" output.  I wonder if it is\n> more useful and less error prone to come up with a format string\n> that 100% reproduces the \"ls-files -s\" output and compare the two,\n> e.g.\n>\n>         format=\"%(objectmode) %(objectname) %(stage)    %(path)\" &&\n>         git ls-files -s >expect &&\n>         git ls-files --format=\"$format\" >actual &&\n>         test_cmp expect actual\n>\n\nSee test case: 'git ls-files --format imitate --stage' which just do such thing,\nmaybe I should change its name to 'git ls-files --format v.s. -s'?\n\n> I do not know if the $format I wrote without looking at the doc is\n> correct, but you get the idea.\n>\n> Thanks.\n>\n>\n\nThanks\n"},{"id":"459830","messageId":"pull.1262.v9.git.1658558685407.gitgitgadget@gmail.com","threadId":"58016","inReplyTo":"pull.1262.v8.git.1658334983053.gitgitgadget@gmail.com","subject":"[PATCH v9] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-07-23T06:44:45Z","receivedAt":"2022-07-23T06:44:54Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAdd a new option \"--format\" that outputs index entries\ninformations in a custom format, taking inspiration\nfrom the option with the same name in the `git ls-tree`\ncommand.\n\n\"--format\" cannot used with \"-s\", \"-o\", \"-k\", \"-t\",\n\" --resolve-undo\",\"--deduplicate\" and \"--eol\".\n\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n    ls-files: introduce \"--format\" options\n    \n    v7->v8:\n    \n     1. wrap the usage with _() and fix grammar error.\n     2. fix test case title.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1262%2Fadlternative%2Fzh%2Fls-file-format-v9\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1262/adlternative/zh/ls-file-format-v9\nPull-Request: https://github.com/gitgitgadget/git/pull/1262\n\nRange-diff vs v8:\n\n 1:  e765ee28e90 ! 1:  df602d30bf4 ls-files: introduce \"--format\" option\n     @@ builtin/ls-files.c: int cmd_ls_files(int argc, const char **argv, const char *cm\n      +\n      +\tif (format && (show_stage || show_others || show_killed ||\n      +\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n     -+\t\t\tusage_msg_opt(\"--format cannot used with -s, -o, -k, -t, \"\n     -+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\",\n     ++\t\t\tusage_msg_opt(_(\"--format cannot be used with -s, -o, -k, -t, \"\n     ++\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\"),\n      +\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n      +\n       \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n     @@ t/t3013-ls-files-format.sh (new)\n      +\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n      +\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n      +\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n     -+\tgit add . &&\n     ++\tgit add o?.txt &&\n      +\toid=$(git hash-object o1.txt) &&\n      +\tgit update-index --add --cacheinfo 120000 $oid o4.txt &&\n      +\tgit update-index --add --cacheinfo 160000 $oid o5.txt &&\n     @@ t/t3013-ls-files-format.sh (new)\n      +\ttest_cmp expect actual\n      +'\n      +\n     -+test_expect_success 'git ls-files --format imitate --stage' '\n     ++test_expect_success 'git ls-files --format v.s -s' '\n      +\tgit ls-files --stage >expect &&\n      +\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n      +\ttest_cmp expect actual\n\n\n Documentation/git-ls-files.txt | 39 +++++++++++++-\n builtin/ls-files.c             | 95 ++++++++++++++++++++++++++++++++++\n t/t3013-ls-files-format.sh     | 95 ++++++++++++++++++++++++++++++++++\n 3 files changed, 228 insertions(+), 1 deletion(-)\n create mode 100755 t/t3013-ls-files-format.sh\n\ndiff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt\nindex 0dabf3f0ddc..d7986419c25 100644\n--- a/Documentation/git-ls-files.txt\n+++ b/Documentation/git-ls-files.txt\n@@ -20,7 +20,7 @@ SYNOPSIS\n \t\t[--exclude-standard]\n \t\t[--error-unmatch] [--with-tree=<tree-ish>]\n \t\t[--full-name] [--recurse-submodules]\n-\t\t[--abbrev[=<n>]] [--] [<file>...]\n+\t\t[--abbrev[=<n>]] [--format=<format>] [--] [<file>...]\n \n DESCRIPTION\n -----------\n@@ -192,6 +192,13 @@ followed by the  (\"attr/<eolattr>\").\n \tto the contained files. Sparse directories will be shown with a\n \ttrailing slash, such as \"x/\" for a sparse directory \"x\".\n \n+--format=<format>::\n+\tA string that interpolates `%(fieldname)` from the result being shown.\n+\tIt also interpolates `%%` to `%`, and `%xx` where `xx` are hex digits\n+\tinterpolates to character with hex code `xx`; for example `%00`\n+\tinterpolates to `\\0` (NUL), `%09` to `\\t` (TAB) and %0a to `\\n` (LF).\n+\t--format cannot be combined with `-s`, `-o`, `-k`, `-t`, `--resolve-undo`\n+\tand `--eol`.\n \\--::\n \tDo not interpret any more arguments as options.\n \n@@ -223,6 +230,36 @@ quoted as explained for the configuration variable `core.quotePath`\n (see linkgit:git-config[1]).  Using `-z` the filename is output\n verbatim and the line is terminated by a NUL byte.\n \n+It is possible to print in a custom format by using the `--format`\n+option, which is able to interpolate different fields using\n+a `%(fieldname)` notation. For example, if you only care about the\n+\"objectname\" and \"path\" fields, you can execute with a specific\n+\"--format\" like\n+\n+\tgit ls-files --format='%(objectname) %(path)'\n+\n+FIELD NAMES\n+-----------\n+The way each path is shown can be customized by using the\n+`--format=<format>` option, where the %(fieldname) in the\n+<format> string for various aspects of the index entry are\n+interpolated.  The following \"fieldname\" are understood:\n+\n+objectmode::\n+\tThe mode of the file which is recorded in the index.\n+objectname::\n+\tThe name of the file which is recorded in the index.\n+stage::\n+\tThe stage of the file which is recorded in the index.\n+eolinfo:index::\n+eolinfo:worktree::\n+\tThe <eolinfo> (see the description of the `--eol` option) of\n+\tthe contents in the index or in the worktree for the path.\n+eolattr::\n+\tThe <eolattr> (see the description of the `--eol` option)\n+\tthat applies to the path.\n+path::\n+\tThe pathname of the file which is recorded in the index.\n \n EXCLUDE PATTERNS\n ----------------\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex e791b65e7e9..779dc18e59d 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -11,6 +11,7 @@\n #include \"quote.h\"\n #include \"dir.h\"\n #include \"builtin.h\"\n+#include \"strbuf.h\"\n #include \"tree.h\"\n #include \"cache-tree.h\"\n #include \"parse-options.h\"\n@@ -48,6 +49,7 @@ static char *ps_matched;\n static const char *with_tree;\n static int exc_given;\n static int exclude_args;\n+static const char *format;\n \n static const char *tag_cached = \"\";\n static const char *tag_unmerged = \"\";\n@@ -85,6 +87,16 @@ static void write_name(const char *name)\n \t\t\t\t   stdout, line_terminator);\n }\n \n+static void write_name_to_buf(struct strbuf *sb, const char *name)\n+{\n+\tconst char *rel = relative_path(name, prefix_len ? prefix : NULL, sb);\n+\n+\tif (line_terminator)\n+\t\tquote_c_style(rel, sb, NULL, 0);\n+\telse\n+\t\tstrbuf_addstr(sb, rel);\n+}\n+\n static const char *get_tag(const struct cache_entry *ce, const char *tag)\n {\n \tstatic char alttag[4];\n@@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n \trepo_clear(&subrepo);\n }\n \n+struct show_index_data {\n+\tconst char *pathname;\n+\tstruct index_state *istate;\n+\tconst struct cache_entry *ce;\n+};\n+\n+static size_t expand_show_index(struct strbuf *sb, const char *start,\n+\t\t\t\tvoid *context)\n+{\n+\tstruct show_index_data *data = context;\n+\tconst char *end;\n+\tconst char *p;\n+\tsize_t len = strbuf_expand_literal_cb(sb, start, NULL);\n+\tstruct stat st;\n+\n+\tif (len)\n+\t\treturn len;\n+\tif (*start != '(')\n+\t\tdie(_(\"bad ls-files format: element '%s' \"\n+\t\t      \"does not start with '('\"), start);\n+\n+\tend = strchr(start + 1, ')');\n+\tif (!end)\n+\t\tdie(_(\"bad ls-files format: element '%s'\"\n+\t\t      \"does not end in ')'\"), start);\n+\n+\tlen = end - start + 1;\n+\tif (skip_prefix(start, \"(objectmode)\", &p))\n+\t\tstrbuf_addf(sb, \"%06o\", data->ce->ce_mode);\n+\telse if (skip_prefix(start, \"(objectname)\", &p))\n+\t\tstrbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev);\n+\telse if (skip_prefix(start, \"(stage)\", &p))\n+\t\tstrbuf_addf(sb, \"%d\", ce_stage(data->ce));\n+\telse if (skip_prefix(start, \"(eolinfo:index)\", &p))\n+\t\tstrbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ?\n+\t\t\t      get_cached_convert_stats_ascii(data->istate,\n+\t\t\t      data->ce->name) : \"\");\n+\telse if (skip_prefix(start, \"(eolinfo:worktree)\", &p))\n+\t\tstrbuf_addstr(sb, !lstat(data->pathname, &st) &&\n+\t\t\t      S_ISREG(st.st_mode) ?\n+\t\t\t      get_wt_convert_stats_ascii(data->pathname) : \"\");\n+\telse if (skip_prefix(start, \"(eolattr)\", &p))\n+\t\tstrbuf_addstr(sb, get_convert_attr_ascii(data->istate,\n+\t\t\t      data->pathname));\n+\telse if (skip_prefix(start, \"(path)\", &p))\n+\t\twrite_name_to_buf(sb, data->pathname);\n+\telse\n+\t\tdie(_(\"bad ls-files format: %%%.*s\"), (int)len, start);\n+\n+\treturn len;\n+}\n+\n+static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,\n+\t\t\tconst char *format, const char *fullname) {\n+\tstruct show_index_data data = {\n+\t\t.pathname = fullname,\n+\t\t.istate = repo->index,\n+\t\t.ce = ce,\n+\t};\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tstrbuf_expand(&sb, format, expand_show_index, &data);\n+\tstrbuf_addch(&sb, line_terminator);\n+\tfwrite(sb.buf, sb.len, 1, stdout);\n+\tstrbuf_release(&sb);\n+}\n+\n static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t    const struct cache_entry *ce, const char *fullname,\n \t\t    const char *tag)\n@@ -236,6 +315,12 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,\n \t\t\t\t  max_prefix_len, ps_matched,\n \t\t\t\t  S_ISDIR(ce->ce_mode) ||\n \t\t\t\t  S_ISGITLINK(ce->ce_mode))) {\n+\t\tif (format) {\n+\t\t\tshow_ce_fmt(repo, ce, format, fullname);\n+\t\t\tprint_debug(ce);\n+\t\t\treturn;\n+\t\t}\n+\n \t\ttag = get_tag(ce, tag);\n \n \t\tif (!show_stage) {\n@@ -675,6 +760,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t\t N_(\"suppress duplicate entries\")),\n \t\tOPT_BOOL(0, \"sparse\", &show_sparse_dirs,\n \t\t\t N_(\"show sparse directories in the presence of a sparse index\")),\n+\t\tOPT_STRING_F(0, \"format\", &format, N_(\"format\"),\n+\t\t\t     N_(\"format to use for the output\"),\n+\t\t\t     PARSE_OPT_NONEG),\n \t\tOPT_END()\n \t};\n \tint ret = 0;\n@@ -699,6 +787,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tfor (i = 0; i < exclude_list.nr; i++) {\n \t\tadd_pattern(exclude_list.items[i].string, \"\", 0, pl, --exclude_args);\n \t}\n+\n+\tif (format && (show_stage || show_others || show_killed ||\n+\t\tshow_resolve_undo || skipping_duplicates || show_eol || show_tag))\n+\t\t\tusage_msg_opt(_(\"--format cannot be used with -s, -o, -k, -t, \"\n+\t\t\t\t      \"--resolve-undo, --deduplicate, --eol\"),\n+\t\t\t\t      ls_files_usage, builtin_ls_files_options);\n+\n \tif (show_tag || show_valid_bit || show_fsmonitor_bit) {\n \t\ttag_cached = \"H \";\n \t\ttag_unmerged = \"M \";\ndiff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh\nnew file mode 100755\nindex 00000000000..efb7450bf1e\n--- /dev/null\n+++ b/t/t3013-ls-files-format.sh\n@@ -0,0 +1,95 @@\n+#!/bin/sh\n+\n+test_description='git ls-files --format test'\n+\n+TEST_PASSES_SANITIZE_LEAK=true\n+. ./test-lib.sh\n+\n+for flag in -s -o -k -t --resolve-undo --deduplicate --eol\n+do\n+\ttest_expect_success \"usage: --format is incompatible with $flag\" '\n+\t\ttest_expect_code 129 git ls-files --format=\"%(objectname)\" $flag\n+\t'\n+done\n+\n+test_expect_success 'setup' '\n+\tprintf \"LINEONE\\nLINETWO\\nLINETHREE\\n\" >o1.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\r\\nLINETHREE\\r\\n\" >o2.txt &&\n+\tprintf \"LINEONE\\r\\nLINETWO\\nLINETHREE\\n\" >o3.txt &&\n+\tgit add o?.txt &&\n+\toid=$(git hash-object o1.txt) &&\n+\tgit update-index --add --cacheinfo 120000 $oid o4.txt &&\n+\tgit update-index --add --cacheinfo 160000 $oid o5.txt &&\n+\tgit update-index --add --cacheinfo 100755 $oid o6.txt &&\n+\tgit commit -m base\n+'\n+\n+test_expect_success 'git ls-files --format objectmode v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -d\" \" -f1 files >expect &&\n+\tgit ls-files --format=\"%(objectmode)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format objectname v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -d\" \" -f2 files >expect &&\n+\tgit ls-files --format=\"%(objectname)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format v.s. --eol' '\n+\tgit ls-files --eol >tmp &&\n+\tsed -e \"s/\t/ /g\" -e \"s/  */ /g\" tmp >expect 2>err &&\n+\ttest_must_be_empty err &&\n+\tgit ls-files --format=\"i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)\" >actual 2>err &&\n+\ttest_must_be_empty err &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format path v.s. -s' '\n+\tgit ls-files -s >files &&\n+\tcut -f2 files >expect &&\n+\tgit ls-files --format=\"%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -m' '\n+\techo change >o1.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to1.txt\n+\to4.txt\n+\to5.txt\n+\to6.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -m >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with -d' '\n+\techo o7 >o7.txt &&\n+\tgit add o7.txt &&\n+\trm o7.txt &&\n+\tcat >expect <<-\\EOF &&\n+\to4.txt\n+\to5.txt\n+\to6.txt\n+\to7.txt\n+\tEOF\n+\tgit ls-files --format=\"%(path)\" -d >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format v.s -s' '\n+\tgit ls-files --stage >expect &&\n+\tgit ls-files --format=\"%(objectmode) %(objectname) %(stage)%x09%(path)\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'git ls-files --format with --debug' '\n+\tgit ls-files --debug >expect &&\n+\tgit ls-files --format=\"%(path)\" --debug >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\n\nbase-commit: ab336e8f1c8009c8b1aab8deb592148e69217085\n-- \ngitgitgadget\n"},{"id":"459841","messageId":"xmqqsfmr8ygp.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8RjLoooT7t+ucFqa9P=8TiVL3M+ZgcEY7qVhRbjB=9OhA@mail.gmail.com","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-23T18:40:06Z","receivedAt":"2022-07-23T18:44:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n>> But is this testing the right thing?\n>\n> Yes, I am sure about that cut can do the same thing as awk, and it can\n> specify its delimiter.\n\nThat is not an answer to \"is this testing the right thing?\"\nquestion, though ;-)\n\n>> > +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n>> > +     git ls-files -s >files &&\n>> > +     cut -d\" \" -f1 files >expect &&\n>> > +     git ls-files --format=\"%(objectmode)\" >actual &&\n>> > +     test_cmp expect actual\n>> > +'\n>>\n>> It only looks at the first column of the \"-s\" output, and we are\n>> implicitly assuming that the order of output does not change between\n>> the \"-s\" output and \"--format=<format>\" output.  I wonder if it is\n>> more useful and less error prone to come up with a format string\n>> that 100% reproduces the \"ls-files -s\" output and compare the two,\n>> e.g.\n>>\n>>         format=\"%(objectmode) %(objectname) %(stage)    %(path)\" &&\n>>         git ls-files -s >expect &&\n>>         git ls-files --format=\"$format\" >actual &&\n>>         test_cmp expect actual\n>>\n>\n> See test case: 'git ls-files --format imitate --stage' which just do such thing,\n\n\nThat was not the point.  By extracting only \"%(objectmode)\" without\nhaving any other clues (like \"%(path)\") on the same line, the test\nis assuming that ls-files will always sort its output in the same\norder regardless of the output format, whether it is \"--stage\" or\n\"--format=<spec>\", and that was what the \"is this testing the right\nthing?\" question was about.\n\nThe other test that makes sure --format=<spec> can recreate --stage\noutput is fine.  If some future developer breaks the output order by\nmistake for --format=<spec>, we will catch such a mistake with it.\n\n\n> maybe I should change its name to 'git ls-files --format v.s. -s'?\n\nI do not think you should.  \"A v.s. B\" does not imply \"A and B\nshould create identical result\".  The original title describes what\nit does much more clearly.\n"},{"id":"459842","messageId":"xmqqo7xf8y5r.fsf@gitster.g","threadId":"58016","inReplyTo":"xmqqsfmr8ygp.fsf@gitster.g","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-23T18:46:40Z","receivedAt":"2022-07-23T18:46:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> That was not the point.  By extracting only \"%(objectmode)\" without\n> having any other clues (like \"%(path)\") on the same line, the test\n> is assuming that ls-files will always sort its output in the same\n> order regardless of the output format, whether it is \"--stage\" or\n> \"--format=<spec>\", and that was what the \"is this testing the right\n> thing?\" question was about.\n>\n> The other test that makes sure --format=<spec> can recreate --stage\n> output is fine.  If some future developer breaks the output order by\n> mistake for --format=<spec>, we will catch such a mistake with it.\n\nHaving said that, let's stop rerolling the series just for this.  An\nextra test that may not catch potential breakage is fine and it is\nnot worth spending an extra review cycle only to remove it.\n\nThanks.\n"},{"id":"459849","messageId":"CAOLTT8RqMU-k85LmcpY0wATHSDoWDEQLnPtfuZ2OC2nWN9305A@mail.gmail.com","threadId":"58016","inReplyTo":"xmqqsfmr8ygp.fsf@gitster.g","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-24T11:08:00Z","receivedAt":"2022-07-24T11:08:16Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年7月24日周日 02:40写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> >> But is this testing the right thing?\n> >\n> > Yes, I am sure about that cut can do the same thing as awk, and it can\n> > specify its delimiter.\n>\n> That is not an answer to \"is this testing the right thing?\"\n> question, though ;-)\n>\n> >> > +test_expect_success 'git ls-files --format objectmode v.s. -s' '\n> >> > +     git ls-files -s >files &&\n> >> > +     cut -d\" \" -f1 files >expect &&\n> >> > +     git ls-files --format=\"%(objectmode)\" >actual &&\n> >> > +     test_cmp expect actual\n> >> > +'\n> >>\n> >> It only looks at the first column of the \"-s\" output, and we are\n> >> implicitly assuming that the order of output does not change between\n> >> the \"-s\" output and \"--format=<format>\" output.  I wonder if it is\n> >> more useful and less error prone to come up with a format string\n> >> that 100% reproduces the \"ls-files -s\" output and compare the two,\n> >> e.g.\n> >>\n> >>         format=\"%(objectmode) %(objectname) %(stage)    %(path)\" &&\n> >>         git ls-files -s >expect &&\n> >>         git ls-files --format=\"$format\" >actual &&\n> >>         test_cmp expect actual\n> >>\n> >\n> > See test case: 'git ls-files --format imitate --stage' which just do such thing,\n>\n>\n> That was not the point.  By extracting only \"%(objectmode)\" without\n> having any other clues (like \"%(path)\") on the same line, the test\n> is assuming that ls-files will always sort its output in the same\n> order regardless of the output format, whether it is \"--stage\" or\n> \"--format=<spec>\", and that was what the \"is this testing the right\n> thing?\" question was about.\n>\n\nAh, so that we should sort the ls-files output first, and then compare them.\n\n> The other test that makes sure --format=<spec> can recreate --stage\n> output is fine.  If some future developer breaks the output order by\n> mistake for --format=<spec>, we will catch such a mistake with it.\n>\n>\n> > maybe I should change its name to 'git ls-files --format v.s. -s'?\n>\n> I do not think you should.  \"A v.s. B\" does not imply \"A and B\n> should create identical result\".  The original title describes what\n> it does much more clearly.\n\nOk, here I don't need another rerolling to revert it, right?\n\nThanks for all the reviews!\n\nZheNing Hu\n"},{"id":"459853","messageId":"xmqq7d4280m8.fsf@gitster.g","threadId":"58016","inReplyTo":"CAOLTT8RqMU-k85LmcpY0wATHSDoWDEQLnPtfuZ2OC2nWN9305A@mail.gmail.com","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-07-25T01:03:27Z","receivedAt":"2022-07-25T01:03:37Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> writes:\n\n>> That was not the point.  By extracting only \"%(objectmode)\" without\n>> having any other clues (like \"%(path)\") on the same line, the test\n>> is assuming that ls-files will always sort its output in the same\n>> order regardless of the output format, whether it is \"--stage\" or\n>> \"--format=<spec>\", and that was what the \"is this testing the right\n>> thing?\" question was about.\n>>\n>\n> Ah, so that we should sort the ls-files output first, and then compare them.\n\nImagine that there are three paths in the index and \"ls-files -s\"\ngives\n\n    100644 1234... 0 path1\n    100644 2345... 0 path2\n    100755 3456... 0 path3\n\nbut a bug causes \"ls-files --format=<spec>\" to show entries in a\nwrong order, e.g. first for path2 and then for path1 and then for\npath3.  If the test used enough fields (like the one that mimics the\nfull output of \"ls-files -s\"), then the output may be\n\n    100644 2345... 0 path2\n    100644 1234... 0 path1\n    100755 3456... 0 path3\n\nand you would notice that it is different from \"ls-files -s\".\n\nBut if the test only used %(objectmode), then the faulty output from\n\"ls-files --format=%(objectmode)\" would be\n\n    100644\n    100644\n    100755\n\nthat matches the \"ls-files -s | cut -d' ' -f1\"\n\nIf you sort, then such a breakage will become even harder to\nnotice.  If the faulty output showed path3 first and then path2 and\nthen path1, the raw output from \"ls-files --format=%(objectmode)\" may\nbe 100755/100644/100644, but if you sort it, no matter what the\nbroken order is, you will always get 100644/100644/100755.\n\nSo, no, we shouldn't sort.  If ls-files were allowed to show output\nin any random order, then sorting the output before comparing is a\ngood strategy, but that does not apply here.\n"},{"id":"459859","messageId":"CAOLTT8SKW1RCB1Md0DOxKikwq3mgsKWp4stR62YyMyGxjn56aA@mail.gmail.com","threadId":"58016","inReplyTo":"xmqq7d4280m8.fsf@gitster.g","subject":"Re: [PATCH v8] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-07-25T11:00:55Z","receivedAt":"2022-07-25T11:01:11Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2022年7月25日周一 09:03写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> >> That was not the point.  By extracting only \"%(objectmode)\" without\n> >> having any other clues (like \"%(path)\") on the same line, the test\n> >> is assuming that ls-files will always sort its output in the same\n> >> order regardless of the output format, whether it is \"--stage\" or\n> >> \"--format=<spec>\", and that was what the \"is this testing the right\n> >> thing?\" question was about.\n> >>\n> >\n> > Ah, so that we should sort the ls-files output first, and then compare them.\n>\n> Imagine that there are three paths in the index and \"ls-files -s\"\n> gives\n>\n>     100644 1234... 0 path1\n>     100644 2345... 0 path2\n>     100755 3456... 0 path3\n>\n> but a bug causes \"ls-files --format=<spec>\" to show entries in a\n> wrong order, e.g. first for path2 and then for path1 and then for\n> path3.  If the test used enough fields (like the one that mimics the\n> full output of \"ls-files -s\"), then the output may be\n>\n>     100644 2345... 0 path2\n>     100644 1234... 0 path1\n>     100755 3456... 0 path3\n>\n> and you would notice that it is different from \"ls-files -s\".\n>\n> But if the test only used %(objectmode), then the faulty output from\n> \"ls-files --format=%(objectmode)\" would be\n>\n>     100644\n>     100644\n>     100755\n>\n> that matches the \"ls-files -s | cut -d' ' -f1\"\n>\n> If you sort, then such a breakage will become even harder to\n> notice.  If the faulty output showed path3 first and then path2 and\n> then path1, the raw output from \"ls-files --format=%(objectmode)\" may\n> be 100755/100644/100644, but if you sort it, no matter what the\n> broken order is, you will always get 100644/100644/100755.\n>\n> So, no, we shouldn't sort.  If ls-files were allowed to show output\n> in any random order, then sorting the output before comparing is a\n> good strategy, but that does not apply here.\n\nI get what you mean. So test 'git ls-files --format imitate --stage'\ncan help for\nchecking it, because every line content is different (maybe different <path>,\nor the same <path> with different <stage>,<objectmode>...), we can find the\n--format \"disorder bug\" with ease.\n\nZheNing Hu\n"},{"id":"462782","messageId":"CANYiYbFGSfg+iFV1ovhCSxW0YQSpemKUN-sS+F0BHee7KD5arA@mail.gmail.com","threadId":"58016","inReplyTo":"pull.1262.v9.git.1658558685407.gitgitgadget@gmail.com","subject":"Re: [PATCH v9] ls-files: introduce \"--format\" option","fromName":"Jiang Xin","fromEmail":"worldhello.net@gmail.com","sentAt":"2022-09-08T02:01:07Z","receivedAt":"2022-09-08T02:01:24Z","isPatch":true,"sender":{"key":"worldhello.net@gmail.com","avatar":"https://avatars.githubusercontent.com/u/183860?v=4"},"body":"On Sat, Jul 23, 2022 at 2:54 PM ZheNing Hu via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n\n> diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> index e791b65e7e9..779dc18e59d 100644\n> --- a/builtin/ls-files.c\n> +++ b/builtin/ls-files.c\n> @@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n>         repo_clear(&subrepo);\n>  }\n>\n> +struct show_index_data {\n> +       const char *pathname;\n> +       struct index_state *istate;\n> +       const struct cache_entry *ce;\n> +};\n> +\n> +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> +                               void *context)\n> +{\n> +       struct show_index_data *data = context;\n> +       const char *end;\n> +       const char *p;\n> +       size_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> +       struct stat st;\n> +\n> +       if (len)\n> +               return len;\n> +       if (*start != '(')\n> +               die(_(\"bad ls-files format: element '%s' \"\n\nGood, the last space acts as a separator between two lines.\n\n> +                     \"does not start with '('\"), start);\n> +\n> +       end = strchr(start + 1, ')');\n> +       if (!end)\n> +               die(_(\"bad ls-files format: element '%s'\"\n\nMissing the last space to seperate two lines, and this leads to wrong\nl10n message. See:\n\n    https://github.com/git-l10n/pot-changes/blob/pot/main/2022-08-03.diff#L70\n\n--\nJiang Xin\n"},{"id":"462916","messageId":"CAOLTT8RFWJ=LdZr1s5DWE7C2Uu9juzzfuhbXvQPe=b+ppXsPsw@mail.gmail.com","threadId":"58016","inReplyTo":"CANYiYbFGSfg+iFV1ovhCSxW0YQSpemKUN-sS+F0BHee7KD5arA@mail.gmail.com","subject":"Re: [PATCH v9] ls-files: introduce \"--format\" option","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-09-11T11:01:05Z","receivedAt":"2022-09-11T11:01:20Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Jiang Xin <worldhello.net@gmail.com> 于2022年9月8日周四 10:01写道：\n>\n> On Sat, Jul 23, 2022 at 2:54 PM ZheNing Hu via GitGitGadget\n> <gitgitgadget@gmail.com> wrote:\n>\n> > diff --git a/builtin/ls-files.c b/builtin/ls-files.c\n> > index e791b65e7e9..779dc18e59d 100644\n> > --- a/builtin/ls-files.c\n> > +++ b/builtin/ls-files.c\n> > @@ -222,6 +234,73 @@ static void show_submodule(struct repository *superproject,\n> >         repo_clear(&subrepo);\n> >  }\n> >\n> > +struct show_index_data {\n> > +       const char *pathname;\n> > +       struct index_state *istate;\n> > +       const struct cache_entry *ce;\n> > +};\n> > +\n> > +static size_t expand_show_index(struct strbuf *sb, const char *start,\n> > +                               void *context)\n> > +{\n> > +       struct show_index_data *data = context;\n> > +       const char *end;\n> > +       const char *p;\n> > +       size_t len = strbuf_expand_literal_cb(sb, start, NULL);\n> > +       struct stat st;\n> > +\n> > +       if (len)\n> > +               return len;\n> > +       if (*start != '(')\n> > +               die(_(\"bad ls-files format: element '%s' \"\n>\n> Good, the last space acts as a separator between two lines.\n>\n> > +                     \"does not start with '('\"), start);\n> > +\n> > +       end = strchr(start + 1, ')');\n> > +       if (!end)\n> > +               die(_(\"bad ls-files format: element '%s'\"\n>\n> Missing the last space to seperate two lines, and this leads to wrong\n> l10n message. See:\n>\n\nThank you for pointing out the error, I will fix it quickly.\n\n>     https://github.com/git-l10n/pot-changes/blob/pot/main/2022-08-03.diff#L70\n>\n> --\n> Jiang Xin\n\nThanks,\nZheNing Hu\n"}]}