{"thread":{"id":"56038","subject":"[PATCH 01/15] [GSOC] ref-filter: add obj-type check in grab contents","startedAt":"2021-07-01T16:08:18Z","lastAt":"2021-07-05T07:25:56Z","messageCount":39,"participants":["ZheNing Hu via GitGitGadget","Junio C Hamano","ZheNing Hu","Ævar Arnfjörð Bjarmason","Christian Couder","Eric Sunshine"],"isPatch":true,"patchVersion":1,"patchTotal":15},"messages":[{"id":"428950","messageId":"e0c12cd1301de79097fff1a4cb909a2c4386cfc7.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 01/15] [GSOC] ref-filter: add obj-type check in grab contents","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:07:59Z","receivedAt":"2021-07-01T16:08:18Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nOnly tag and commit objects use `grab_sub_body_contents()` to grab\nobject contents in the current codebase.  We want to teach the\nfunction to also handle blobs and trees to get their raw data,\nwithout parsing a blob (whose contents looks like a commit or a tag)\nincorrectly as a commit or a tag.\n\nSkip the block of code that is specific to handling commits and tags\nearly when the given object is of a wrong type to help later\naddition to handle other types of objects in this function.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nHelped-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c | 24 +++++++++++++++---------\n 1 file changed, 15 insertions(+), 9 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 4db0e40ff4c..5cee6512fba 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1356,11 +1356,12 @@ static void append_lines(struct strbuf *out, const char *buf, unsigned long size\n }\n \n /* See grab_values */\n-static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)\n+static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data)\n {\n \tint i;\n \tconst char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;\n \tsize_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;\n+\tvoid *buf = data->content;\n \n \tfor (i = 0; i < used_atom_cnt; i++) {\n \t\tstruct used_atom *atom = &used_atom[i];\n@@ -1371,10 +1372,13 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)\n \t\t\tcontinue;\n \t\tif (deref)\n \t\t\tname++;\n-\t\tif (strcmp(name, \"body\") &&\n-\t\t    !starts_with(name, \"subject\") &&\n-\t\t    !starts_with(name, \"trailers\") &&\n-\t\t    !starts_with(name, \"contents\"))\n+\n+\t\tif ((data->type != OBJ_TAG &&\n+\t\t     data->type != OBJ_COMMIT) ||\n+\t\t    (strcmp(name, \"body\") &&\n+\t\t     !starts_with(name, \"subject\") &&\n+\t\t     !starts_with(name, \"trailers\") &&\n+\t\t     !starts_with(name, \"contents\")))\n \t\t\tcontinue;\n \t\tif (!subpos)\n \t\t\tfind_subpos(buf,\n@@ -1438,17 +1442,19 @@ static void fill_missing_values(struct atom_value *val)\n  * pointed at by the ref itself; otherwise it is the object the\n  * ref (which is a tag) refers to.\n  */\n-static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)\n+static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data)\n {\n+\tvoid *buf = data->content;\n+\n \tswitch (obj->type) {\n \tcase OBJ_TAG:\n \t\tgrab_tag_values(val, deref, obj);\n-\t\tgrab_sub_body_contents(val, deref, buf);\n+\t\tgrab_sub_body_contents(val, deref, data);\n \t\tgrab_person(\"tagger\", val, deref, buf);\n \t\tbreak;\n \tcase OBJ_COMMIT:\n \t\tgrab_commit_values(val, deref, obj);\n-\t\tgrab_sub_body_contents(val, deref, buf);\n+\t\tgrab_sub_body_contents(val, deref, data);\n \t\tgrab_person(\"author\", val, deref, buf);\n \t\tgrab_person(\"committer\", val, deref, buf);\n \t\tbreak;\n@@ -1678,7 +1684,7 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj\n \t\t\treturn strbuf_addf_ret(err, -1, _(\"parse_object_buffer failed on %s for %s\"),\n \t\t\t\t\t       oid_to_hex(&oi->oid), ref->refname);\n \t\t}\n-\t\tgrab_values(ref->value, deref, *obj, oi->content);\n+\t\tgrab_values(ref->value, deref, *obj, oi);\n \t}\n \n \tgrab_common_values(ref->value, deref, oi);\n-- \ngitgitgadget\n\n"},{"id":"428951","messageId":"pull.989.git.1625155693.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":null,"subject":"[PATCH 00/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:07:58Z","receivedAt":"2021-07-01T16:08:19Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"This patch series make cat-file reuse ref-filter logic.\n\nChange from last version:\n\n 1. Amend part of the description of git for-each-ref.txt.\n 2. Modify commit messages about why cat-file reject some atoms.\n 3. Resolve conflicts with the 'seen' branch, rebase this patch on\n    (bb9a3a8f77 Merge branch 'zh/cat-file-batch-fix' into jch).\n\nZheNing Hu (15):\n  [GSOC] ref-filter: add obj-type check in grab contents\n  [GSOC] ref-filter: add %(raw) atom\n  [GSOC] ref-filter: --format=%(raw) re-support --perl\n  [GSOC] ref-filter: use non-const ref_format in *_atom_parser()\n  [GSOC] ref-filter: add %(rest) atom\n  [GSOC] ref-filter: pass get_object() return value to their callers\n  [GSOC] ref-filter: introduce free_ref_array_item_value() function\n  [GSOC] ref-filter: add cat_file_mode in struct ref_format\n  [GSOC] ref-filter: modify the error message and value in get_object\n  [GSOC] cat-file: add has_object_file() check\n  [GSOC] cat-file: change batch_objects parameter name\n  [GSOC] cat-file: reuse ref-filter logic\n  [GSOC] cat-file: reuse err buf in batch_object_write()\n  [GSOC] cat-file: re-implement --textconv, --filters options\n  [GSOC] ref-filter: remove grab_oid() function\n\n Documentation/git-cat-file.txt     |   6 +\n Documentation/git-for-each-ref.txt |   9 +\n builtin/cat-file.c                 | 275 ++++++----------------\n builtin/tag.c                      |   2 +-\n quote.c                            |  17 ++\n quote.h                            |   1 +\n ref-filter.c                       | 357 ++++++++++++++++++++++-------\n ref-filter.h                       |  14 +-\n t/t1006-cat-file.sh                | 251 ++++++++++++++++++++\n t/t3203-branch-output.sh           |   4 +\n t/t6300-for-each-ref.sh            | 235 +++++++++++++++++++\n t/t6301-for-each-ref-errors.sh     |   2 +-\n t/t7004-tag.sh                     |   4 +\n t/t7030-verify-tag.sh              |   4 +\n 14 files changed, 887 insertions(+), 294 deletions(-)\n\n\nbase-commit: bb9a3a8f77e13b7a414cf64bf294c3f59f2444c9\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-989%2Fadlternative%2Fcat-file-batch-refactor-rebase-version-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-989/adlternative/cat-file-batch-refactor-rebase-version-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/989\n-- \ngitgitgadget\n"},{"id":"428952","messageId":"7d2314b43f21a470667971f1fdcb382f43bf51ed.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 03/15] [GSOC] ref-filter: --format=%(raw) re-support --perl","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:01Z","receivedAt":"2021-07-01T16:08:20Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nBecause the perl language can handle binary data correctly,\nadd the function perl_quote_buf_with_len(), which can specify\nthe length of the data and prevent the data from being truncated\nat '\\0' to help `--format=\"%(raw)\"` re-support `--perl`.\n\nHelped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  4 ++--\n quote.c                            | 17 +++++++++++++++++\n quote.h                            |  1 +\n ref-filter.c                       | 15 +++++++++++----\n t/t6300-for-each-ref.sh            | 19 +++++++++++++++++--\n 5 files changed, 48 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex cbb6f87d13f..6da899c6296 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -241,8 +241,8 @@ raw:size::\n \tThe raw data size of the object.\n \n Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,\n-`--perl` because such language may not support arbitrary binary data in their\n-string variable type.\n+because such language may not support arbitrary binary data in their string\n+variable type.\n \n The message in a commit or a tag object is `contents`, from which\n `contents:<part>` can be used to extract various parts out of:\ndiff --git a/quote.c b/quote.c\nindex 8a3a5e39eb1..26719d21d1e 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -471,6 +471,23 @@ void perl_quote_buf(struct strbuf *sb, const char *src)\n \tstrbuf_addch(sb, sq);\n }\n \n+void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len)\n+{\n+\tconst char sq = '\\'';\n+\tconst char bq = '\\\\';\n+\tconst char *c = src;\n+\tconst char *end = src + len;\n+\n+\tstrbuf_addch(sb, sq);\n+\twhile (c != end) {\n+\t\tif (*c == sq || *c == bq)\n+\t\t\tstrbuf_addch(sb, bq);\n+\t\tstrbuf_addch(sb, *c);\n+\t\tc++;\n+\t}\n+\tstrbuf_addch(sb, sq);\n+}\n+\n void python_quote_buf(struct strbuf *sb, const char *src)\n {\n \tconst char sq = '\\'';\ndiff --git a/quote.h b/quote.h\nindex 768cc6338e2..0fe69e264b0 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -94,6 +94,7 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne\n \n /* quoting as a string literal for other languages */\n void perl_quote_buf(struct strbuf *sb, const char *src);\n+void perl_quote_buf_with_len(struct strbuf *sb, const char *src, size_t len);\n void python_quote_buf(struct strbuf *sb, const char *src);\n void tcl_quote_buf(struct strbuf *sb, const char *src);\n void basic_regex_quote_buf(struct strbuf *sb, const char *src);\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 7822be90307..797b20ffa61 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -742,7 +742,10 @@ static void quote_formatting(struct strbuf *s, const char *str, size_t len, int\n \t\tsq_quote_buf(s, str);\n \t\tbreak;\n \tcase QUOTE_PERL:\n-\t\tperl_quote_buf(s, str);\n+\t\tif (len != ATOM_VALUE_S_SIZE_INIT)\n+\t\t\tperl_quote_buf_with_len(s, str, len);\n+\t\telse\n+\t\t\tperl_quote_buf(s, str);\n \t\tbreak;\n \tcase QUOTE_PYTHON:\n \t\tpython_quote_buf(s, str);\n@@ -1006,10 +1009,14 @@ int verify_ref_format(struct ref_format *format)\n \t\tat = parse_ref_filter_atom(format, sp + 2, ep, &err);\n \t\tif (at < 0)\n \t\t\tdie(\"%s\", err.buf);\n-\t\tif (format->quote_style && used_atom[at].atom_type == ATOM_RAW &&\n-\t\t    used_atom[at].u.raw_data.option == RAW_BARE)\n+\n+\t\tif ((format->quote_style == QUOTE_PYTHON ||\n+\t\t     format->quote_style == QUOTE_SHELL ||\n+\t\t     format->quote_style == QUOTE_TCL) &&\n+\t\t     used_atom[at].atom_type == ATOM_RAW &&\n+\t\t     used_atom[at].u.raw_data.option == RAW_BARE)\n \t\t\tdie(_(\"--format=%.*s cannot be used with\"\n-\t\t\t      \"--python, --shell, --tcl, --perl\"), (int)(ep - sp - 2), sp + 2);\n+\t\t\t      \"--python, --shell, --tcl\"), (int)(ep - sp - 2), sp + 2);\n \t\tcp = ep + 1;\n \n \t\tif (skip_prefix(used_atom[at].name, \"color:\", &color))\ndiff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\nindex 18554f62d94..0b66e743c58 100755\n--- a/t/t6300-for-each-ref.sh\n+++ b/t/t6300-for-each-ref.sh\n@@ -915,8 +915,23 @@ test_expect_success '%(raw) with --tcl must fail' '\n \ttest_must_fail git for-each-ref --format=\"%(raw)\" --tcl\n '\n \n-test_expect_success '%(raw) with --perl must fail' '\n-\ttest_must_fail git for-each-ref --format=\"%(raw)\" --perl\n+test_expect_success '%(raw) with --perl' '\n+\tgit for-each-ref --format=\"\\$name= %(raw);\n+print \\\"\\$name\\\"\" refs/myblobs/blob1 --perl | perl > actual &&\n+\tcmp blob1 actual &&\n+\tgit for-each-ref --format=\"\\$name= %(raw);\n+print \\\"\\$name\\\"\" refs/myblobs/blob3 --perl | perl > actual &&\n+\tcmp blob3 actual &&\n+\tgit for-each-ref --format=\"\\$name= %(raw);\n+print \\\"\\$name\\\"\" refs/myblobs/blob8 --perl | perl > actual &&\n+\tcmp blob8 actual &&\n+\tgit for-each-ref --format=\"\\$name= %(raw);\n+print \\\"\\$name\\\"\" refs/myblobs/first --perl | perl > actual &&\n+\tcmp one actual &&\n+\tgit cat-file tree refs/mytrees/first > expected &&\n+\tgit for-each-ref --format=\"\\$name= %(raw);\n+print \\\"\\$name\\\"\" refs/mytrees/first --perl | perl > actual &&\n+\tcmp expected actual\n '\n \n test_expect_success '%(raw) with --shell must fail' '\n-- \ngitgitgadget\n\n"},{"id":"428953","messageId":"ad71510feda0a100920a99de41db0ab137787796.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 02/15] [GSOC] ref-filter: add %(raw) atom","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:00Z","receivedAt":"2021-07-01T16:08:21Z","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 new formatting option `%(raw)`, which will print the raw\nobject data without any changes. It will help further to migrate\nall cat-file formatting logic from cat-file to ref-filter.\n\nThe raw data of blob, tree objects may contain '\\0', but most of\nthe logic in `ref-filter` depends on the output of the atom being\ntext (specifically, no embedded NULs in it).\n\nE.g. `quote_formatting()` use `strbuf_addstr()` or `*._quote_buf()`\nadd the data to the buffer. The raw data of a tree object is\n`100644 one\\0...`, only the `100644 one` will be added to the buffer,\nwhich is incorrect.\n\nTherefore, we need to find a way to record the length of the\natom_value's member `s`. Although strbuf can already record the\nstring and its length, if we want to replace the type of atom_value's\nmember `s` with strbuf, many places in ref-filter that are filled\nwith dynamically allocated mermory in `v->s` are not easy to replace.\nAt the same time, we need to check if `v->s == NULL` in\npopulate_value(), and strbuf cannot easily distinguish NULL and empty\nstrings, but c-style \"const char *\" can do it. So add a new member in\n`struct atom_value`: `s_size`, which can record raw object size, it\ncan help us add raw object data to the buffer or compare two buffers\nwhich contain raw object data.\n\nNote that `--format=%(raw)` cannot be used with `--python`, `--shell`,\n`--tcl`, and `--perl` because if the binary raw data is passed to a\nvariable in such languages, these may not support arbitrary binary data\nin their string variable type.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nHelped-by: Bagas Sanjaya <bagasdotme@gmail.com>\nHelped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nHelped-by: Felipe Contreras <felipe.contreras@gmail.com>\nHelped-by: Phillip Wood <phillip.wood@dunelm.org.uk>\nHelped-by: Junio C Hamano <gitster@pobox.com>\nBased-on-patch-by: Olga Telezhnaya <olyatelezhnaya@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-for-each-ref.txt |   9 ++\n ref-filter.c                       | 139 +++++++++++++++----\n t/t6300-for-each-ref.sh            | 216 +++++++++++++++++++++++++++++\n 3 files changed, 337 insertions(+), 27 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 2ae2478de70..cbb6f87d13f 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -235,6 +235,15 @@ and `date` to extract the named component.  For email fields (`authoremail`,\n without angle brackets, and `:localpart` to get the part before the `@` symbol\n out of the trimmed email.\n \n+The raw data in an object is `raw`.\n+\n+raw:size::\n+\tThe raw data size of the object.\n+\n+Note that `--format=%(raw)` can not be used with `--python`, `--shell`, `--tcl`,\n+`--perl` because such language may not support arbitrary binary data in their\n+string variable type.\n+\n The message in a commit or a tag object is `contents`, from which\n `contents:<part>` can be used to extract various parts out of:\n \ndiff --git a/ref-filter.c b/ref-filter.c\nindex 5cee6512fba..7822be90307 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -144,6 +144,7 @@ enum atom_type {\n \tATOM_BODY,\n \tATOM_TRAILERS,\n \tATOM_CONTENTS,\n+\tATOM_RAW,\n \tATOM_UPSTREAM,\n \tATOM_PUSH,\n \tATOM_SYMREF,\n@@ -189,6 +190,9 @@ static struct used_atom {\n \t\t\tstruct process_trailer_options trailer_opts;\n \t\t\tunsigned int nlines;\n \t\t} contents;\n+\t\tstruct {\n+\t\t\tenum { RAW_BARE, RAW_LENGTH } option;\n+\t\t} raw_data;\n \t\tstruct {\n \t\t\tcmp_status cmp_status;\n \t\t\tconst char *str;\n@@ -426,6 +430,18 @@ static int contents_atom_parser(const struct ref_format *format, struct used_ato\n \treturn 0;\n }\n \n+static int raw_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+\t\t\t\tconst char *arg, struct strbuf *err)\n+{\n+\tif (!arg)\n+\t\tatom->u.raw_data.option = RAW_BARE;\n+\telse if (!strcmp(arg, \"size\"))\n+\t\tatom->u.raw_data.option = RAW_LENGTH;\n+\telse\n+\t\treturn strbuf_addf_ret(err, -1, _(\"unrecognized %%(raw) argument: %s\"), arg);\n+\treturn 0;\n+}\n+\n static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,\n \t\t\t   const char *arg, struct strbuf *err)\n {\n@@ -586,6 +602,7 @@ static struct {\n \t[ATOM_BODY] = { \"body\", SOURCE_OBJ, FIELD_STR, body_atom_parser },\n \t[ATOM_TRAILERS] = { \"trailers\", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },\n \t[ATOM_CONTENTS] = { \"contents\", SOURCE_OBJ, FIELD_STR, contents_atom_parser },\n+\t[ATOM_RAW] = { \"raw\", SOURCE_OBJ, FIELD_STR, raw_atom_parser },\n \t[ATOM_UPSTREAM] = { \"upstream\", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },\n \t[ATOM_PUSH] = { \"push\", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },\n \t[ATOM_SYMREF] = { \"symref\", SOURCE_NONE, FIELD_STR, refname_atom_parser },\n@@ -620,12 +637,15 @@ struct ref_formatting_state {\n \n struct atom_value {\n \tconst char *s;\n+\tsize_t s_size;\n \tint (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,\n \t\t       struct strbuf *err);\n \tuintmax_t value; /* used for sorting when not FIELD_STR */\n \tstruct used_atom *atom;\n };\n \n+#define ATOM_VALUE_S_SIZE_INIT (-1)\n+\n /*\n  * Used to parse format string and sort specifiers\n  */\n@@ -644,13 +664,6 @@ static int parse_ref_filter_atom(const struct ref_format *format,\n \t\treturn strbuf_addf_ret(err, -1, _(\"malformed field name: %.*s\"),\n \t\t\t\t       (int)(ep-atom), atom);\n \n-\t/* Do we have the atom already used elsewhere? */\n-\tfor (i = 0; i < used_atom_cnt; i++) {\n-\t\tint len = strlen(used_atom[i].name);\n-\t\tif (len == ep - atom && !memcmp(used_atom[i].name, atom, len))\n-\t\t\treturn i;\n-\t}\n-\n \t/*\n \t * If the atom name has a colon, strip it and everything after\n \t * it off - it specifies the format for this entry, and\n@@ -660,6 +673,13 @@ static int parse_ref_filter_atom(const struct ref_format *format,\n \targ = memchr(sp, ':', ep - sp);\n \tatom_len = (arg ? arg : ep) - sp;\n \n+\t/* Do we have the atom already used elsewhere? */\n+\tfor (i = 0; i < used_atom_cnt; i++) {\n+\t\tint len = strlen(used_atom[i].name);\n+\t\tif (len == ep - atom && !memcmp(used_atom[i].name, atom, len))\n+\t\t\treturn i;\n+\t}\n+\n \t/* Is the atom a valid one? */\n \tfor (i = 0; i < ARRAY_SIZE(valid_atom); i++) {\n \t\tint len = strlen(valid_atom[i].name);\n@@ -709,11 +729,14 @@ static int parse_ref_filter_atom(const struct ref_format *format,\n \treturn at;\n }\n \n-static void quote_formatting(struct strbuf *s, const char *str, int quote_style)\n+static void quote_formatting(struct strbuf *s, const char *str, size_t len, int quote_style)\n {\n \tswitch (quote_style) {\n \tcase QUOTE_NONE:\n-\t\tstrbuf_addstr(s, str);\n+\t\tif (len != ATOM_VALUE_S_SIZE_INIT)\n+\t\t\tstrbuf_add(s, str, len);\n+\t\telse\n+\t\t\tstrbuf_addstr(s, str);\n \t\tbreak;\n \tcase QUOTE_SHELL:\n \t\tsq_quote_buf(s, str);\n@@ -740,9 +763,12 @@ static int append_atom(struct atom_value *v, struct ref_formatting_state *state,\n \t * encountered.\n \t */\n \tif (!state->stack->prev)\n-\t\tquote_formatting(&state->stack->output, v->s, state->quote_style);\n+\t\tquote_formatting(&state->stack->output, v->s, v->s_size, state->quote_style);\n \telse\n-\t\tstrbuf_addstr(&state->stack->output, v->s);\n+\t\tif (v->s_size != ATOM_VALUE_S_SIZE_INIT)\n+\t\t\tstrbuf_add(&state->stack->output, v->s, v->s_size);\n+\t\telse\n+\t\t\tstrbuf_addstr(&state->stack->output, v->s);\n \treturn 0;\n }\n \n@@ -842,21 +868,23 @@ static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state\n \treturn 0;\n }\n \n-static int is_empty(const char *s)\n+static int is_empty(struct strbuf *buf)\n {\n-\twhile (*s != '\\0') {\n-\t\tif (!isspace(*s))\n-\t\t\treturn 0;\n-\t\ts++;\n-\t}\n-\treturn 1;\n-}\n+\tconst char *cur = buf->buf;\n+\tconst char *end = buf->buf + buf->len;\n+\n+\twhile (cur != end && (isspace(*cur)))\n+\t\tcur++;\n+\n+\treturn cur == end;\n+ }\n \n static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,\n \t\t\t     struct strbuf *err)\n {\n \tstruct ref_formatting_stack *cur = state->stack;\n \tstruct if_then_else *if_then_else = NULL;\n+\tsize_t str_len = 0;\n \n \tif (cur->at_end == if_then_else_handler)\n \t\tif_then_else = (struct if_then_else *)cur->at_end_data;\n@@ -867,18 +895,22 @@ static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_sta\n \tif (if_then_else->else_atom_seen)\n \t\treturn strbuf_addf_ret(err, -1, _(\"format: %%(then) atom used after %%(else)\"));\n \tif_then_else->then_atom_seen = 1;\n+\tif (if_then_else->str)\n+\t\tstr_len = strlen(if_then_else->str);\n \t/*\n \t * If the 'equals' or 'notequals' attribute is used then\n \t * perform the required comparison. If not, only non-empty\n \t * strings satisfy the 'if' condition.\n \t */\n \tif (if_then_else->cmp_status == COMPARE_EQUAL) {\n-\t\tif (!strcmp(if_then_else->str, cur->output.buf))\n+\t\tif (str_len == cur->output.len &&\n+\t\t    !memcmp(if_then_else->str, cur->output.buf, cur->output.len))\n \t\t\tif_then_else->condition_satisfied = 1;\n \t} else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {\n-\t\tif (strcmp(if_then_else->str, cur->output.buf))\n+\t\tif (str_len != cur->output.len ||\n+\t\t    memcmp(if_then_else->str, cur->output.buf, cur->output.len))\n \t\t\tif_then_else->condition_satisfied = 1;\n-\t} else if (cur->output.len && !is_empty(cur->output.buf))\n+\t} else if (cur->output.len && !is_empty(&cur->output))\n \t\tif_then_else->condition_satisfied = 1;\n \tstrbuf_reset(&cur->output);\n \treturn 0;\n@@ -924,7 +956,7 @@ static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_stat\n \t * only on the topmost supporting atom.\n \t */\n \tif (!current->prev->prev) {\n-\t\tquote_formatting(&s, current->output.buf, state->quote_style);\n+\t\tquote_formatting(&s, current->output.buf, current->output.len, state->quote_style);\n \t\tstrbuf_swap(&current->output, &s);\n \t}\n \tstrbuf_release(&s);\n@@ -974,6 +1006,10 @@ int verify_ref_format(struct ref_format *format)\n \t\tat = parse_ref_filter_atom(format, sp + 2, ep, &err);\n \t\tif (at < 0)\n \t\t\tdie(\"%s\", err.buf);\n+\t\tif (format->quote_style && used_atom[at].atom_type == ATOM_RAW &&\n+\t\t    used_atom[at].u.raw_data.option == RAW_BARE)\n+\t\t\tdie(_(\"--format=%.*s cannot be used with\"\n+\t\t\t      \"--python, --shell, --tcl, --perl\"), (int)(ep - sp - 2), sp + 2);\n \t\tcp = ep + 1;\n \n \t\tif (skip_prefix(used_atom[at].name, \"color:\", &color))\n@@ -1362,17 +1398,29 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp\n \tconst char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;\n \tsize_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;\n \tvoid *buf = data->content;\n+\tunsigned long buf_size = data->size;\n \n \tfor (i = 0; i < used_atom_cnt; i++) {\n \t\tstruct used_atom *atom = &used_atom[i];\n \t\tconst char *name = atom->name;\n \t\tstruct atom_value *v = &val[i];\n+\t\tenum atom_type atom_type = atom->atom_type;\n \n \t\tif (!!deref != (*name == '*'))\n \t\t\tcontinue;\n \t\tif (deref)\n \t\t\tname++;\n \n+\t\tif (atom_type == ATOM_RAW) {\n+\t\t\tif (atom->u.raw_data.option == RAW_BARE) {\n+\t\t\t\tv->s = xmemdupz(buf, buf_size);\n+\t\t\t\tv->s_size = buf_size;\n+\t\t\t} else if (atom->u.raw_data.option == RAW_LENGTH) {\n+\t\t\t\tv->s = xstrfmt(\"%\"PRIuMAX, (uintmax_t)buf_size);\n+\t\t\t}\n+\t\t\tcontinue;\n+\t\t}\n+\n \t\tif ((data->type != OBJ_TAG &&\n \t\t     data->type != OBJ_COMMIT) ||\n \t\t    (strcmp(name, \"body\") &&\n@@ -1460,9 +1508,11 @@ static void grab_values(struct atom_value *val, int deref, struct object *obj, s\n \t\tbreak;\n \tcase OBJ_TREE:\n \t\t/* grab_tree_values(val, deref, obj, buf, sz); */\n+\t\tgrab_sub_body_contents(val, deref, data);\n \t\tbreak;\n \tcase OBJ_BLOB:\n \t\t/* grab_blob_values(val, deref, obj, buf, sz); */\n+\t\tgrab_sub_body_contents(val, deref, data);\n \t\tbreak;\n \tdefault:\n \t\tdie(\"Eh?  Object of type %d?\", obj->type);\n@@ -1766,6 +1816,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)\n \t\tconst char *refname;\n \t\tstruct branch *branch = NULL;\n \n+\t\tv->s_size = ATOM_VALUE_S_SIZE_INIT;\n \t\tv->handler = append_atom;\n \t\tv->atom = atom;\n \n@@ -2369,6 +2420,19 @@ static int compare_detached_head(struct ref_array_item *a, struct ref_array_item\n \treturn 0;\n }\n \n+static int memcasecmp(const void *vs1, const void *vs2, size_t n)\n+{\n+\tconst char *s1 = vs1, *s2 = vs2;\n+\tconst char *end = s1 + n;\n+\n+\tfor (; s1 < end; s1++, s2++) {\n+\t\tint diff = tolower(*s1) - tolower(*s2);\n+\t\tif (diff)\n+\t\t\treturn diff;\n+\t}\n+\treturn 0;\n+}\n+\n static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)\n {\n \tstruct atom_value *va, *vb;\n@@ -2389,10 +2453,30 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru\n \t} else if (s->sort_flags & REF_SORTING_VERSION) {\n \t\tcmp = versioncmp(va->s, vb->s);\n \t} else if (cmp_type == FIELD_STR) {\n-\t\tint (*cmp_fn)(const char *, const char *);\n-\t\tcmp_fn = s->sort_flags & REF_SORTING_ICASE\n-\t\t\t? strcasecmp : strcmp;\n-\t\tcmp = cmp_fn(va->s, vb->s);\n+\t\tif (va->s_size == ATOM_VALUE_S_SIZE_INIT &&\n+\t\t    vb->s_size == ATOM_VALUE_S_SIZE_INIT) {\n+\t\t\tint (*cmp_fn)(const char *, const char *);\n+\t\t\tcmp_fn = s->sort_flags & REF_SORTING_ICASE\n+\t\t\t\t? strcasecmp : strcmp;\n+\t\t\tcmp = cmp_fn(va->s, vb->s);\n+\t\t} else {\n+\t\t\tsize_t a_size = va->s_size == ATOM_VALUE_S_SIZE_INIT ?\n+\t\t\t\t\tstrlen(va->s) : va->s_size;\n+\t\t\tsize_t b_size = vb->s_size == ATOM_VALUE_S_SIZE_INIT ?\n+\t\t\t\t\tstrlen(vb->s) : vb->s_size;\n+\t\t\tint (*cmp_fn)(const void *, const void *, size_t);\n+\t\t\tcmp_fn = s->sort_flags & REF_SORTING_ICASE\n+\t\t\t\t? memcasecmp : memcmp;\n+\n+\t\t\tcmp = cmp_fn(va->s, vb->s, b_size > a_size ?\n+\t\t\t\t     a_size : b_size);\n+\t\t\tif (!cmp) {\n+\t\t\t\tif (a_size > b_size)\n+\t\t\t\t\tcmp = 1;\n+\t\t\t\telse if (a_size < b_size)\n+\t\t\t\t\tcmp = -1;\n+\t\t\t}\n+\t\t}\n \t} else {\n \t\tif (va->value < vb->value)\n \t\t\tcmp = -1;\n@@ -2492,6 +2576,7 @@ int format_ref_array_item(struct ref_array_item *info,\n \t}\n \tif (format->need_color_reset_at_eol) {\n \t\tstruct atom_value resetv;\n+\t\tresetv.s_size = ATOM_VALUE_S_SIZE_INIT;\n \t\tresetv.s = GIT_COLOR_RESET;\n \t\tif (append_atom(&resetv, &state, error_buf)) {\n \t\t\tpop_stack_element(&state.stack);\ndiff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\nindex 9e0214076b4..18554f62d94 100755\n--- a/t/t6300-for-each-ref.sh\n+++ b/t/t6300-for-each-ref.sh\n@@ -130,6 +130,8 @@ test_atom head parent:short=10 ''\n test_atom head numparent 0\n test_atom head object ''\n test_atom head type ''\n+test_atom head raw \"$(git cat-file commit refs/heads/main)\n+\"\n test_atom head '*objectname' ''\n test_atom head '*objecttype' ''\n test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'\n@@ -221,6 +223,15 @@ test_atom tag contents 'Tagging at 1151968727\n '\n test_atom tag HEAD ' '\n \n+test_expect_success 'basic atom: refs/tags/testtag *raw' '\n+\tgit cat-file commit refs/tags/testtag^{} >expected &&\n+\tgit for-each-ref --format=\"%(*raw)\" refs/tags/testtag >actual &&\n+\tsanitize_pgp <expected >expected.clean &&\n+\techo >>expected.clean &&\n+\tsanitize_pgp <actual >actual.clean &&\n+\ttest_cmp expected.clean actual.clean\n+'\n+\n test_expect_success 'Check invalid atoms names are errors' '\n \ttest_must_fail git for-each-ref --format=\"%(INVALID)\" refs/heads\n '\n@@ -686,6 +697,15 @@ test_atom refs/tags/signed-empty contents:body ''\n test_atom refs/tags/signed-empty contents:signature \"$sig\"\n test_atom refs/tags/signed-empty contents \"$sig\"\n \n+test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '\n+\tgit cat-file tag refs/tags/signed-empty >expected &&\n+\tgit for-each-ref --format=\"%(raw)\" refs/tags/signed-empty >actual &&\n+\tsanitize_pgp <expected >expected.clean &&\n+\techo >>expected.clean &&\n+\tsanitize_pgp <actual >actual.clean &&\n+\ttest_cmp expected.clean actual.clean\n+'\n+\n test_atom refs/tags/signed-short subject 'subject line'\n test_atom refs/tags/signed-short subject:sanitize 'subject-line'\n test_atom refs/tags/signed-short contents:subject 'subject line'\n@@ -695,6 +715,15 @@ test_atom refs/tags/signed-short contents:signature \"$sig\"\n test_atom refs/tags/signed-short contents \"subject line\n $sig\"\n \n+test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '\n+\tgit cat-file tag refs/tags/signed-short >expected &&\n+\tgit for-each-ref --format=\"%(raw)\" refs/tags/signed-short >actual &&\n+\tsanitize_pgp <expected >expected.clean &&\n+\techo >>expected.clean &&\n+\tsanitize_pgp <actual >actual.clean &&\n+\ttest_cmp expected.clean actual.clean\n+'\n+\n test_atom refs/tags/signed-long subject 'subject line'\n test_atom refs/tags/signed-long subject:sanitize 'subject-line'\n test_atom refs/tags/signed-long contents:subject 'subject line'\n@@ -708,6 +737,15 @@ test_atom refs/tags/signed-long contents \"subject line\n body contents\n $sig\"\n \n+test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '\n+\tgit cat-file tag refs/tags/signed-long >expected &&\n+\tgit for-each-ref --format=\"%(raw)\" refs/tags/signed-long >actual &&\n+\tsanitize_pgp <expected >expected.clean &&\n+\techo >>expected.clean &&\n+\tsanitize_pgp <actual >actual.clean &&\n+\ttest_cmp expected.clean actual.clean\n+'\n+\n test_expect_success 'set up refs pointing to tree and blob' '\n \tgit update-ref refs/mytrees/first refs/heads/main^{tree} &&\n \tgit update-ref refs/myblobs/first refs/heads/main:one\n@@ -720,6 +758,16 @@ test_atom refs/mytrees/first contents:body \"\"\n test_atom refs/mytrees/first contents:signature \"\"\n test_atom refs/mytrees/first contents \"\"\n \n+test_expect_success 'basic atom: refs/mytrees/first raw' '\n+\tgit cat-file tree refs/mytrees/first >expected &&\n+\techo >>expected &&\n+\tgit for-each-ref --format=\"%(raw)\" refs/mytrees/first >actual &&\n+\ttest_cmp expected actual &&\n+\tgit cat-file -s refs/mytrees/first >expected &&\n+\tgit for-each-ref --format=\"%(raw:size)\" refs/mytrees/first >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_atom refs/myblobs/first subject \"\"\n test_atom refs/myblobs/first contents:subject \"\"\n test_atom refs/myblobs/first body \"\"\n@@ -727,6 +775,174 @@ test_atom refs/myblobs/first contents:body \"\"\n test_atom refs/myblobs/first contents:signature \"\"\n test_atom refs/myblobs/first contents \"\"\n \n+test_expect_success 'basic atom: refs/myblobs/first raw' '\n+\tgit cat-file blob refs/myblobs/first >expected &&\n+\techo >>expected &&\n+\tgit for-each-ref --format=\"%(raw)\" refs/myblobs/first >actual &&\n+\ttest_cmp expected actual &&\n+\tgit cat-file -s refs/myblobs/first >expected &&\n+\tgit for-each-ref --format=\"%(raw:size)\" refs/myblobs/first >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'set up refs pointing to binary blob' '\n+\tprintf \"a\\0b\\0c\" >blob1 &&\n+\tprintf \"a\\0c\\0b\" >blob2 &&\n+\tprintf \"\\0a\\0b\\0c\" >blob3 &&\n+\tprintf \"abc\" >blob4 &&\n+\tprintf \"\\0 \\0 \\0 \" >blob5 &&\n+\tprintf \"\\0 \\0a\\0 \" >blob6 &&\n+\tprintf \"  \" >blob7 &&\n+\t>blob8 &&\n+\tobj=$(git hash-object -w blob1) &&\n+\tgit update-ref refs/myblobs/blob1 \"$obj\" &&\n+\tobj=$(git hash-object -w blob2) &&\n+\tgit update-ref refs/myblobs/blob2 \"$obj\" &&\n+\tobj=$(git hash-object -w blob3) &&\n+\tgit update-ref refs/myblobs/blob3 \"$obj\" &&\n+\tobj=$(git hash-object -w blob4) &&\n+\tgit update-ref refs/myblobs/blob4 \"$obj\" &&\n+\tobj=$(git hash-object -w blob5) &&\n+\tgit update-ref refs/myblobs/blob5 \"$obj\" &&\n+\tobj=$(git hash-object -w blob6) &&\n+\tgit update-ref refs/myblobs/blob6 \"$obj\" &&\n+\tobj=$(git hash-object -w blob7) &&\n+\tgit update-ref refs/myblobs/blob7 \"$obj\" &&\n+\tobj=$(git hash-object -w blob8) &&\n+\tgit update-ref refs/myblobs/blob8 \"$obj\"\n+'\n+\n+test_expect_success 'Verify sorts with raw' '\n+\tcat >expected <<-EOF &&\n+\trefs/myblobs/blob8\n+\trefs/myblobs/blob5\n+\trefs/myblobs/blob6\n+\trefs/myblobs/blob3\n+\trefs/myblobs/blob7\n+\trefs/mytrees/first\n+\trefs/myblobs/first\n+\trefs/myblobs/blob1\n+\trefs/myblobs/blob2\n+\trefs/myblobs/blob4\n+\trefs/heads/main\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname)\" --sort=raw \\\n+\t\trefs/heads/main refs/myblobs/ refs/mytrees/first >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'Verify sorts with raw:size' '\n+\tcat >expected <<-EOF &&\n+\trefs/myblobs/blob8\n+\trefs/myblobs/first\n+\trefs/myblobs/blob7\n+\trefs/heads/main\n+\trefs/myblobs/blob4\n+\trefs/myblobs/blob1\n+\trefs/myblobs/blob2\n+\trefs/myblobs/blob3\n+\trefs/myblobs/blob5\n+\trefs/myblobs/blob6\n+\trefs/mytrees/first\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname)\" --sort=raw:size \\\n+\t\trefs/heads/main refs/myblobs/ refs/mytrees/first >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'validate raw atom with %(if:equals)' '\n+\tcat >expected <<-EOF &&\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\trefs/myblobs/blob4\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tnot equals\n+\tEOF\n+\tgit for-each-ref --format=\"%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)\" \\\n+\t\trefs/myblobs/ refs/heads/ >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'validate raw atom with %(if:notequals)' '\n+\tcat >expected <<-EOF &&\n+\trefs/heads/ambiguous\n+\trefs/heads/main\n+\trefs/heads/newtag\n+\trefs/myblobs/blob1\n+\trefs/myblobs/blob2\n+\trefs/myblobs/blob3\n+\tequals\n+\trefs/myblobs/blob5\n+\trefs/myblobs/blob6\n+\trefs/myblobs/blob7\n+\trefs/myblobs/blob8\n+\trefs/myblobs/first\n+\tEOF\n+\tgit for-each-ref --format=\"%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)\" \\\n+\t\trefs/myblobs/ refs/heads/ >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'empty raw refs with %(if)' '\n+\tcat >expected <<-EOF &&\n+\trefs/myblobs/blob1 not empty\n+\trefs/myblobs/blob2 not empty\n+\trefs/myblobs/blob3 not empty\n+\trefs/myblobs/blob4 not empty\n+\trefs/myblobs/blob5 not empty\n+\trefs/myblobs/blob6 not empty\n+\trefs/myblobs/blob7 empty\n+\trefs/myblobs/blob8 empty\n+\trefs/myblobs/first not empty\n+\tEOF\n+\tgit for-each-ref --format=\"%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)\" \\\n+\t\trefs/myblobs/ >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success '%(raw) with --python must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(raw)\" --python\n+'\n+\n+test_expect_success '%(raw) with --tcl must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(raw)\" --tcl\n+'\n+\n+test_expect_success '%(raw) with --perl must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(raw)\" --perl\n+'\n+\n+test_expect_success '%(raw) with --shell must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(raw)\" --shell\n+'\n+\n+test_expect_success '%(raw) with --shell and --sort=raw must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(raw)\" --sort=raw --shell\n+'\n+\n+test_expect_success '%(raw:size) with --shell' '\n+\tgit for-each-ref --format=\"%(raw:size)\" | while read line\n+\tdo\n+\t\techo \"'\\''$line'\\''\" >>expect\n+\tdone &&\n+\tgit for-each-ref --format=\"%(raw:size)\" --shell >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'for-each-ref --format compare with cat-file --batch' '\n+\tgit rev-parse refs/mytrees/first | git cat-file --batch >expected &&\n+\tgit for-each-ref --format=\"%(objectname) %(objecttype) %(objectsize)\n+%(raw)\" refs/mytrees/first >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_expect_success 'set up multiple-sort tags' '\n \tfor when in 100000 200000\n \tdo\n-- \ngitgitgadget\n\n"},{"id":"428954","messageId":"82ffe602759f1819060b655e0f5488a74f16aaf7.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 05/15] [GSOC] ref-filter: add %(rest) atom","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:03Z","receivedAt":"2021-07-01T16:08:23Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nIn order to let \"cat-file --batch=%(rest)\" use the ref-filter\ninterface, add %(rest) atom for ref-filter. \"git for-each-ref\",\n\"git branch\", \"git tag\" and \"git verify-tag\" will reject %(rest)\nby default.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c             | 21 +++++++++++++++++++++\n ref-filter.h             |  5 ++++-\n t/t3203-branch-output.sh |  4 ++++\n t/t6300-for-each-ref.sh  |  4 ++++\n t/t7004-tag.sh           |  4 ++++\n t/t7030-verify-tag.sh    |  4 ++++\n 6 files changed, 41 insertions(+), 1 deletion(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex d01a0266fb8..10c78de9cfa 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -157,6 +157,7 @@ enum atom_type {\n \tATOM_IF,\n \tATOM_THEN,\n \tATOM_ELSE,\n+\tATOM_REST,\n };\n \n /*\n@@ -559,6 +560,15 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,\n \treturn 0;\n }\n \n+static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,\n+\t\t\t    const char *arg, struct strbuf *err)\n+{\n+\tif (arg)\n+\t\treturn strbuf_addf_ret(err, -1, _(\"%%(rest) does not take arguments\"));\n+\tformat->use_rest = 1;\n+\treturn 0;\n+}\n+\n static int head_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t    const char *arg, struct strbuf *unused_err)\n {\n@@ -615,6 +625,7 @@ static struct {\n \t[ATOM_IF] = { \"if\", SOURCE_NONE, FIELD_STR, if_atom_parser },\n \t[ATOM_THEN] = { \"then\", SOURCE_NONE },\n \t[ATOM_ELSE] = { \"else\", SOURCE_NONE },\n+\t[ATOM_REST] = { \"rest\", SOURCE_NONE, FIELD_STR, rest_atom_parser },\n \t/*\n \t * Please update $__git_ref_fieldlist in git-completion.bash\n \t * when you add new atoms\n@@ -1010,6 +1021,9 @@ int verify_ref_format(struct ref_format *format)\n \t\tif (at < 0)\n \t\t\tdie(\"%s\", err.buf);\n \n+\t\tif (used_atom[at].atom_type == ATOM_REST)\n+\t\t\tdie(\"this command reject atom %%(%.*s)\", (int)(ep - sp - 2), sp + 2);\n+\n \t\tif ((format->quote_style == QUOTE_PYTHON ||\n \t\t     format->quote_style == QUOTE_SHELL ||\n \t\t     format->quote_style == QUOTE_TCL) &&\n@@ -1927,6 +1941,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)\n \t\t\tv->handler = else_atom_handler;\n \t\t\tv->s = xstrdup(\"\");\n \t\t\tcontinue;\n+\t\t} else if (atom_type == ATOM_REST) {\n+\t\t\tif (ref->rest)\n+\t\t\t\tv->s = xstrdup(ref->rest);\n+\t\t\telse\n+\t\t\t\tv->s = xstrdup(\"\");\n+\t\t\tcontinue;\n \t\t} else\n \t\t\tcontinue;\n \n@@ -2144,6 +2164,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,\n \n \tFLEX_ALLOC_STR(ref, refname, refname);\n \toidcpy(&ref->objectname, oid);\n+\tref->rest = NULL;\n \n \treturn ref;\n }\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 74fb423fc89..c15dee8d6b9 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -38,6 +38,7 @@ struct ref_sorting {\n \n struct ref_array_item {\n \tstruct object_id objectname;\n+\tconst char *rest;\n \tint flag;\n \tunsigned int kind;\n \tconst char *symref;\n@@ -76,14 +77,16 @@ struct ref_format {\n \t * verify_ref_format() afterwards to finalize.\n \t */\n \tconst char *format;\n+\tconst char *rest;\n \tint quote_style;\n+\tint use_rest;\n \tint use_color;\n \n \t/* Internal state to ref-filter */\n \tint need_color_reset_at_eol;\n };\n \n-#define REF_FORMAT_INIT { NULL, 0, -1 }\n+#define REF_FORMAT_INIT { .use_color = -1 }\n \n /*  Macros for checking --merged and --no-merged options */\n #define _OPT_MERGED_NO_MERGED(option, filter, h) \\\ndiff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh\nindex 5325b9f67a0..6e94c6db7b5 100755\n--- a/t/t3203-branch-output.sh\n+++ b/t/t3203-branch-output.sh\n@@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'git branch with --format=%(rest) must fail' '\n+\ttest_must_fail git branch --format=\"%(rest)\" >actual\n+'\n+\n test_expect_success 'worktree colors correct' '\n \tcat >expect <<-EOF &&\n \t* <GREEN>(HEAD detached from fromtag)<RESET>\ndiff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\nindex 0b66e743c58..6ca5c2cc19c 100755\n--- a/t/t6300-for-each-ref.sh\n+++ b/t/t6300-for-each-ref.sh\n@@ -1211,6 +1211,10 @@ test_expect_success 'basic atom: head contents:trailers' '\n \ttest_cmp expect actual.clean\n '\n \n+test_expect_success 'basic atom: rest must fail' '\n+\ttest_must_fail git for-each-ref --format=\"%(rest)\" refs/heads/main\n+'\n+\n test_expect_success 'trailer parsing not fooled by --- line' '\n \tgit commit --allow-empty -F - <<-\\EOF &&\n \tthis is the subject\ndiff --git a/t/t7004-tag.sh b/t/t7004-tag.sh\nindex 2f72c5c6883..082be85dffc 100755\n--- a/t/t7004-tag.sh\n+++ b/t/t7004-tag.sh\n@@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'git tag -l with --format=\"%(rest)\" must fail' '\n+\ttest_must_fail git tag -l --format=\"%(rest)\" \"v1*\"\n+'\n+\n test_expect_success \"set up color tests\" '\n \techo \"<RED>v1.0<RESET>\" >expect.color &&\n \techo \"v1.0\" >expect.bare &&\ndiff --git a/t/t7030-verify-tag.sh b/t/t7030-verify-tag.sh\nindex 3cefde9602b..10faa645157 100755\n--- a/t/t7030-verify-tag.sh\n+++ b/t/t7030-verify-tag.sh\n@@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success GPG 'verifying tag with --format=\"%(rest)\" must fail' '\n+\ttest_must_fail git verify-tag --format=\"%(rest)\" \"fourth-signed\"\n+'\n+\n test_expect_success GPG 'verifying a forged tag with --format should fail silently' '\n \ttest_must_fail git verify-tag --format=\"tagname : %(tag)\" $(cat forged1.tag) >actual-forged &&\n \ttest_must_be_empty actual-forged\n-- \ngitgitgadget\n\n"},{"id":"428955","messageId":"1ae58077355f2dfe50f2ce3c2c095e4ab7c6f80e.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 04/15] [GSOC] ref-filter: use non-const ref_format in *_atom_parser()","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:02Z","receivedAt":"2021-07-01T16:08:24Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nUse non-const ref_format in *_atom_parser(), which can help us\nmodify the members of ref_format in *_atom_parser().\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n builtin/tag.c |  2 +-\n ref-filter.c  | 44 ++++++++++++++++++++++----------------------\n ref-filter.h  |  4 ++--\n 3 files changed, 25 insertions(+), 25 deletions(-)\n\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex 82fcfc09824..452558ec957 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -146,7 +146,7 @@ static int verify_tag(const char *name, const char *ref,\n \t\t      const struct object_id *oid, void *cb_data)\n {\n \tint flags;\n-\tconst struct ref_format *format = cb_data;\n+\tstruct ref_format *format = cb_data;\n \tflags = GPG_VERIFY_VERBOSE;\n \n \tif (format->format)\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 797b20ffa61..d01a0266fb8 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -226,7 +226,7 @@ static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)\n \treturn ret;\n }\n \n-static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int color_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t     const char *color_value, struct strbuf *err)\n {\n \tif (!color_value)\n@@ -264,7 +264,7 @@ static int refname_atom_parser_internal(struct refname_atom *atom, const char *a\n \treturn 0;\n }\n \n-static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int remote_ref_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\t  const char *arg, struct strbuf *err)\n {\n \tstruct string_list params = STRING_LIST_INIT_DUP;\n@@ -311,7 +311,7 @@ static int remote_ref_atom_parser(const struct ref_format *format, struct used_a\n \treturn 0;\n }\n \n-static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int objecttype_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\t  const char *arg, struct strbuf *err)\n {\n \tif (arg)\n@@ -323,7 +323,7 @@ static int objecttype_atom_parser(const struct ref_format *format, struct used_a\n \treturn 0;\n }\n \n-static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int objectsize_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\t  const char *arg, struct strbuf *err)\n {\n \tif (!arg) {\n@@ -343,7 +343,7 @@ static int objectsize_atom_parser(const struct ref_format *format, struct used_a\n \treturn 0;\n }\n \n-static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int deltabase_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\t const char *arg, struct strbuf *err)\n {\n \tif (arg)\n@@ -355,7 +355,7 @@ static int deltabase_atom_parser(const struct ref_format *format, struct used_at\n \treturn 0;\n }\n \n-static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int body_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t    const char *arg, struct strbuf *err)\n {\n \tif (arg)\n@@ -364,7 +364,7 @@ static int body_atom_parser(const struct ref_format *format, struct used_atom *a\n \treturn 0;\n }\n \n-static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int subject_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t       const char *arg, struct strbuf *err)\n {\n \tif (!arg)\n@@ -376,7 +376,7 @@ static int subject_atom_parser(const struct ref_format *format, struct used_atom\n \treturn 0;\n }\n \n-static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int trailers_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\tconst char *arg, struct strbuf *err)\n {\n \tatom->u.contents.trailer_opts.no_divider = 1;\n@@ -402,7 +402,7 @@ static int trailers_atom_parser(const struct ref_format *format, struct used_ato\n \treturn 0;\n }\n \n-static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int contents_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\tconst char *arg, struct strbuf *err)\n {\n \tif (!arg)\n@@ -430,7 +430,7 @@ static int contents_atom_parser(const struct ref_format *format, struct used_ato\n \treturn 0;\n }\n \n-static int raw_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int raw_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\tconst char *arg, struct strbuf *err)\n {\n \tif (!arg)\n@@ -442,7 +442,7 @@ static int raw_atom_parser(const struct ref_format *format, struct used_atom *at\n \treturn 0;\n }\n \n-static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int oid_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t   const char *arg, struct strbuf *err)\n {\n \tif (!arg)\n@@ -461,7 +461,7 @@ static int oid_atom_parser(const struct ref_format *format, struct used_atom *at\n \treturn 0;\n }\n \n-static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int person_email_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t\t    const char *arg, struct strbuf *err)\n {\n \tif (!arg)\n@@ -475,7 +475,7 @@ static int person_email_atom_parser(const struct ref_format *format, struct used\n \treturn 0;\n }\n \n-static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int refname_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t       const char *arg, struct strbuf *err)\n {\n \treturn refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);\n@@ -492,7 +492,7 @@ static align_type parse_align_position(const char *s)\n \treturn -1;\n }\n \n-static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int align_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t     const char *arg, struct strbuf *err)\n {\n \tstruct align *align = &atom->u.align;\n@@ -544,7 +544,7 @@ static int align_atom_parser(const struct ref_format *format, struct used_atom *\n \treturn 0;\n }\n \n-static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int if_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t  const char *arg, struct strbuf *err)\n {\n \tif (!arg) {\n@@ -559,7 +559,7 @@ static int if_atom_parser(const struct ref_format *format, struct used_atom *ato\n \treturn 0;\n }\n \n-static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,\n+static int head_atom_parser(struct ref_format *format, struct used_atom *atom,\n \t\t\t    const char *arg, struct strbuf *unused_err)\n {\n \tatom->u.head = resolve_refdup(\"HEAD\", RESOLVE_REF_READING, NULL, NULL);\n@@ -570,7 +570,7 @@ static struct {\n \tconst char *name;\n \tinfo_source source;\n \tcmp_type cmp_type;\n-\tint (*parser)(const struct ref_format *format, struct used_atom *atom,\n+\tint (*parser)(struct ref_format *format, struct used_atom *atom,\n \t\t      const char *arg, struct strbuf *err);\n } valid_atom[] = {\n \t[ATOM_REFNAME] = { \"refname\", SOURCE_NONE, FIELD_STR, refname_atom_parser },\n@@ -649,7 +649,7 @@ struct atom_value {\n /*\n  * Used to parse format string and sort specifiers\n  */\n-static int parse_ref_filter_atom(const struct ref_format *format,\n+static int parse_ref_filter_atom(struct ref_format *format,\n \t\t\t\t const char *atom, const char *ep,\n \t\t\t\t struct strbuf *err)\n {\n@@ -2553,9 +2553,9 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting\n }\n \n int format_ref_array_item(struct ref_array_item *info,\n-\t\t\t   const struct ref_format *format,\n-\t\t\t   struct strbuf *final_buf,\n-\t\t\t   struct strbuf *error_buf)\n+\t\t\t  struct ref_format *format,\n+\t\t\t  struct strbuf *final_buf,\n+\t\t\t  struct strbuf *error_buf)\n {\n \tconst char *cp, *sp, *ep;\n \tstruct ref_formatting_state state = REF_FORMATTING_STATE_INIT;\n@@ -2600,7 +2600,7 @@ int format_ref_array_item(struct ref_array_item *info,\n }\n \n void pretty_print_ref(const char *name, const struct object_id *oid,\n-\t\t      const struct ref_format *format)\n+\t\t      struct ref_format *format)\n {\n \tstruct ref_array_item *ref_item;\n \tstruct strbuf output = STRBUF_INIT;\ndiff --git a/ref-filter.h b/ref-filter.h\nindex baf72a71896..74fb423fc89 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -116,7 +116,7 @@ void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);\n void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting, unsigned int mask, int on);\n /*  Based on the given format and quote_style, fill the strbuf */\n int format_ref_array_item(struct ref_array_item *info,\n-\t\t\t  const struct ref_format *format,\n+\t\t\t  struct ref_format *format,\n \t\t\t  struct strbuf *final_buf,\n \t\t\t  struct strbuf *error_buf);\n /*  Parse a single sort specifier and add it to the list */\n@@ -137,7 +137,7 @@ void setup_ref_filter_porcelain_msg(void);\n  * name must be a fully qualified refname.\n  */\n void pretty_print_ref(const char *name, const struct object_id *oid,\n-\t\t      const struct ref_format *format);\n+\t\t      struct ref_format *format);\n \n /*\n  * Push a single ref onto the array; this can be used to construct your own\n-- \ngitgitgadget\n\n"},{"id":"428956","messageId":"d7af86dc1cb6ccee5b9ad6d4da13334c0d4c7dcc.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 06/15] [GSOC] ref-filter: pass get_object() return value to their callers","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:04Z","receivedAt":"2021-07-01T16:08:26Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nBecause in the refactor of `git cat-file --batch` later,\noid_object_info_extended() in get_object() will be used to obtain\nthe info of an object with it's oid. When the object cannot be\nobtained in the git repository, `cat-file --batch` expects to output\n\"<oid> missing\" and continue the next oid query instead of letting\nGit exit. In other error conditions, Git should exit normally. So we\ncan achieve this function by passing the return value of get_object().\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nHelped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c | 17 +++++++++++------\n 1 file changed, 11 insertions(+), 6 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 10c78de9cfa..e4988aa8a24 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1816,6 +1816,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)\n {\n \tstruct object *obj;\n \tint i;\n+\tint ret;\n \tstruct object_info empty = OBJECT_INFO_INIT;\n \n \tCALLOC_ARRAY(ref->value, used_atom_cnt);\n@@ -1972,8 +1973,9 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)\n \n \n \toi.oid = ref->objectname;\n-\tif (get_object(ref, 0, &obj, &oi, err))\n-\t\treturn -1;\n+\tret = get_object(ref, 0, &obj, &oi, err);\n+\tif (ret)\n+\t\treturn ret;\n \n \t/*\n \t * If there is no atom that wants to know about tagged\n@@ -2005,8 +2007,10 @@ static int get_ref_atom_value(struct ref_array_item *ref, int atom,\n \t\t\t      struct atom_value **v, struct strbuf *err)\n {\n \tif (!ref->value) {\n-\t\tif (populate_value(ref, err))\n-\t\t\treturn -1;\n+\t\tint ret = populate_value(ref, err);\n+\n+\t\tif (ret)\n+\t\t\treturn ret;\n \t\tfill_missing_values(ref->value);\n \t}\n \t*v = &ref->value[atom];\n@@ -2580,6 +2584,7 @@ int format_ref_array_item(struct ref_array_item *info,\n {\n \tconst char *cp, *sp, *ep;\n \tstruct ref_formatting_state state = REF_FORMATTING_STATE_INIT;\n+\tint ret;\n \n \tstate.quote_style = format->quote_style;\n \tpush_stack_element(&state.stack);\n@@ -2592,10 +2597,10 @@ int format_ref_array_item(struct ref_array_item *info,\n \t\tif (cp < sp)\n \t\t\tappend_literal(cp, sp, &state);\n \t\tpos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);\n-\t\tif (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||\n+\t\tif (pos < 0 || (ret = get_ref_atom_value(info, pos, &atomv, error_buf)) ||\n \t\t    atomv->handler(atomv, &state, error_buf)) {\n \t\t\tpop_stack_element(&state.stack);\n-\t\t\treturn -1;\n+\t\t\treturn ret ? ret : -1;\n \t\t}\n \t}\n \tif (*cp) {\n-- \ngitgitgadget\n\n"},{"id":"428957","messageId":"9568baf5dddcc0637c15f698aea24d230c4d01b2.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:06Z","receivedAt":"2021-07-01T16:08:27Z","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 `cat_file_mode` member in struct `ref_format`, when\n`cat-file --batch` use ref-filter logic later, it can help\nus reject atoms in verify_ref_format() which cat-file cannot\nuse, e.g. `%(refname)`, `%(push)`, `%(upstream)`...\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c | 11 +++++++++--\n ref-filter.h |  1 +\n 2 files changed, 10 insertions(+), 2 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 731e596eaa6..45122959eef 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1021,8 +1021,15 @@ int verify_ref_format(struct ref_format *format)\n \t\tif (at < 0)\n \t\t\tdie(\"%s\", err.buf);\n \n-\t\tif (used_atom[at].atom_type == ATOM_REST)\n-\t\t\tdie(\"this command reject atom %%(%.*s)\", (int)(ep - sp - 2), sp + 2);\n+\t\tif ((!format->cat_file_mode && used_atom[at].atom_type == ATOM_REST) ||\n+\t\t    (format->cat_file_mode && (used_atom[at].atom_type == ATOM_FLAG ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_HEAD ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_PUSH ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_REFNAME ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_SYMREF ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_UPSTREAM ||\n+\t\t\t\t\t       used_atom[at].atom_type == ATOM_WORKTREEPATH)))\n+\t\t\tdie(_(\"this command reject atom %%(%.*s)\"), (int)(ep - sp - 2), sp + 2);\n \n \t\tif ((format->quote_style == QUOTE_PYTHON ||\n \t\t     format->quote_style == QUOTE_SHELL ||\ndiff --git a/ref-filter.h b/ref-filter.h\nindex 44e6dc05ac2..053980a6a42 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -78,6 +78,7 @@ struct ref_format {\n \t */\n \tconst char *format;\n \tconst char *rest;\n+\tint cat_file_mode;\n \tint quote_style;\n \tint use_rest;\n \tint use_color;\n-- \ngitgitgadget\n\n"},{"id":"428958","messageId":"ac97ca70234240f9b67892c9a53b431f972a290e.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 07/15] [GSOC] ref-filter: introduce free_ref_array_item_value() function","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:05Z","receivedAt":"2021-07-01T16:08:28Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nWhen we use ref_array_item which is not dynamically allocated and\nwant to free the space of its member \"value\" after the end of use,\nfree_array_item() does not meet our needs, because it tries to free\nref_array_item itself and its member \"symref\".\n\nIntroduce free_ref_array_item_value() for freeing ref_array_item value.\nIt will be called internally by free_array_item(), and it will help\n`cat-file --batch` free ref_array_item's value memory later.\n\nHelped-by: Junio C Hamano <gitster@pobox.com>\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c | 11 ++++++++---\n ref-filter.h |  2 ++\n 2 files changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex e4988aa8a24..731e596eaa6 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -2291,16 +2291,21 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,\n \treturn 0;\n }\n \n-/*  Free memory allocated for a ref_array_item */\n-static void free_array_item(struct ref_array_item *item)\n+void free_ref_array_item_value(struct ref_array_item *item)\n {\n-\tfree((char *)item->symref);\n \tif (item->value) {\n \t\tint i;\n \t\tfor (i = 0; i < used_atom_cnt; i++)\n \t\t\tfree((char *)item->value[i].s);\n \t\tfree(item->value);\n \t}\n+}\n+\n+/*  Free memory allocated for a ref_array_item */\n+static void free_array_item(struct ref_array_item *item)\n+{\n+\tfree((char *)item->symref);\n+\tfree_ref_array_item_value(item);\n \tfree(item);\n }\n \ndiff --git a/ref-filter.h b/ref-filter.h\nindex c15dee8d6b9..44e6dc05ac2 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -111,6 +111,8 @@ struct ref_format {\n int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);\n /*  Clear all memory allocated to ref_array */\n void ref_array_clear(struct ref_array *array);\n+/* Free ref_array_item's value */\n+void free_ref_array_item_value(struct ref_array_item *item);\n /*  Used to verify if the given format is correct and to parse out the used atoms */\n int verify_ref_format(struct ref_format *format);\n /*  Sort the given ref_array as per the ref_sorting provided */\n-- \ngitgitgadget\n\n"},{"id":"428959","messageId":"0149940ee4d7282360234f1ef1ec270def83c907.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 09/15] [GSOC] ref-filter: modify the error message and value in get_object","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:07Z","receivedAt":"2021-07-01T16:08:29Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nLet get_object() return 1 and print \"<oid> missing\" instead\nof returning -1 and printing \"missing object <oid> for <refname>\"\nif oid_object_info_extended() unable to find the data corresponding\nto oid. When `cat-file --batch` use ref-filter logic later it can\nhelp `format_ref_array_item()` just report that the object is missing\nwithout letting Git exit.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c                   | 4 ++--\n t/t6301-for-each-ref-errors.sh | 2 +-\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 45122959eef..9ca3dd5557d 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1749,8 +1749,8 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj\n \t}\n \tif (oid_object_info_extended(the_repository, &oi->oid, &oi->info,\n \t\t\t\t     OBJECT_INFO_LOOKUP_REPLACE))\n-\t\treturn strbuf_addf_ret(err, -1, _(\"missing object %s for %s\"),\n-\t\t\t\t       oid_to_hex(&oi->oid), ref->refname);\n+\t\treturn strbuf_addf_ret(err, 1, _(\"%s missing\"),\n+\t\t\t\t       oid_to_hex(&oi->oid));\n \tif (oi->info.disk_sizep && oi->disk_size < 0)\n \t\tBUG(\"Object size is less than zero.\");\n \ndiff --git a/t/t6301-for-each-ref-errors.sh b/t/t6301-for-each-ref-errors.sh\nindex 40edf9dab53..3553f84a00c 100755\n--- a/t/t6301-for-each-ref-errors.sh\n+++ b/t/t6301-for-each-ref-errors.sh\n@@ -41,7 +41,7 @@ test_expect_success 'Missing objects are reported correctly' '\n \tr=refs/heads/missing &&\n \techo $MISSING >.git/$r &&\n \ttest_when_finished \"rm -f .git/$r\" &&\n-\techo \"fatal: missing object $MISSING for $r\" >missing-err &&\n+\techo \"fatal: $MISSING missing\" >missing-err &&\n \ttest_must_fail git for-each-ref 2>err &&\n \ttest_cmp missing-err err &&\n \t(\n-- \ngitgitgadget\n\n"},{"id":"428960","messageId":"0bef02ce5a465a3c968b975a9ba75afb335e6a53.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 11/15] [GSOC] cat-file: change batch_objects parameter name","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:09Z","receivedAt":"2021-07-01T16:08:31Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nBecause later cat-file reuses ref-filter logic that will add\nparameter \"const struct option *options\" to batch_objects(),\nthe two synonymous parameters of \"opt\" and \"options\" may\nconfuse readers, so change batch_options parameter of\nbatch_objects() from \"opt\" to \"batch\".\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n builtin/cat-file.c | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 59a86412fd0..41d407638d5 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -495,7 +495,7 @@ static int batch_unordered_packed(const struct object_id *oid,\n \treturn batch_unordered_object(oid, data);\n }\n \n-static int batch_objects(struct batch_options *opt)\n+static int batch_objects(struct batch_options *batch)\n {\n \tstruct strbuf input = STRBUF_INIT;\n \tstruct strbuf output = STRBUF_INIT;\n@@ -503,8 +503,8 @@ static int batch_objects(struct batch_options *opt)\n \tint save_warning;\n \tint retval = 0;\n \n-\tif (!opt->format)\n-\t\topt->format = \"%(objectname) %(objecttype) %(objectsize)\";\n+\tif (!batch->format)\n+\t\tbatch->format = \"%(objectname) %(objecttype) %(objectsize)\";\n \n \t/*\n \t * Expand once with our special mark_query flag, which will prime the\n@@ -513,20 +513,20 @@ static int batch_objects(struct batch_options *opt)\n \t */\n \tmemset(&data, 0, sizeof(data));\n \tdata.mark_query = 1;\n-\tstrbuf_expand(&output, opt->format, expand_format, &data);\n+\tstrbuf_expand(&output, batch->format, expand_format, &data);\n \tdata.mark_query = 0;\n \tstrbuf_release(&output);\n-\tif (opt->cmdmode)\n+\tif (batch->cmdmode)\n \t\tdata.split_on_whitespace = 1;\n \n \t/*\n \t * If we are printing out the object, then always fill in the type,\n \t * since we will want to decide whether or not to stream.\n \t */\n-\tif (opt->print_contents)\n+\tif (batch->print_contents)\n \t\tdata.info.typep = &data.type;\n \n-\tif (opt->all_objects) {\n+\tif (batch->all_objects) {\n \t\tstruct object_cb_data cb;\n \t\tstruct object_info empty = OBJECT_INFO_INIT;\n \n@@ -536,11 +536,11 @@ static int batch_objects(struct batch_options *opt)\n \t\tif (has_promisor_remote())\n \t\t\twarning(\"This repository uses promisor remotes. Some objects may not be loaded.\");\n \n-\t\tcb.opt = opt;\n+\t\tcb.opt = batch;\n \t\tcb.expand = &data;\n \t\tcb.scratch = &output;\n \n-\t\tif (opt->unordered) {\n+\t\tif (batch->unordered) {\n \t\t\tstruct oidset seen = OIDSET_INIT;\n \n \t\t\tcb.seen = &seen;\n@@ -590,7 +590,7 @@ static int batch_objects(struct batch_options *opt)\n \t\t\tdata.rest = p;\n \t\t}\n \n-\t\tbatch_one_object(input.buf, &output, opt, &data);\n+\t\tbatch_one_object(input.buf, &output, batch, &data);\n \t}\n \n \tstrbuf_release(&input);\n-- \ngitgitgadget\n\n"},{"id":"428961","messageId":"e04b970ccb0cad8c0b651ab11f5f52063bd84606.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:10Z","receivedAt":"2021-07-01T16:08:32Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nIn order to let cat-file use ref-filter logic, let's do the\nfollowing:\n\n1. Change the type of member `format` in struct `batch_options`\nto `ref_format`, we will pass it to ref-filter later.\n2. Let `batch_objects()` add atoms to format, and use\n`verify_ref_format()` to check atoms.\n3. Use `format_ref_array_item()` in `batch_object_write()` to\nget the formatted data corresponding to the object. If the\nreturn value of `format_ref_array_item()` is equals to zero,\nuse `batch_write()` to print object data; else if the return\nvalue is less than zero, use `die()` to print the error message\nand exit; else if return value is greater than zero, only print\nthe error message, but don't exit.\n4. Use free_ref_array_item_value() to free ref_array_item's\nvalue.\n\nMost of the atoms in `for-each-ref --format` are now supported,\nsuch as `%(tree)`, `%(parent)`, `%(author)`, `%(tagger)`, `%(if)`,\n`%(then)`, `%(else)`, `%(end)`. But these atoms will be rejected:\n`%(refname)`, `%(symref)`, `%(upstream)`, `%(push)`, `%(worktreepath)`,\n`%(flag)`, `%(HEAD)`, because these atoms are unique to those objects\nthat pointed to by a ref, \"for-each-ref\"'s family can naturally use\nthese atoms, but not all objects are pointed to be a ref, so \"cat-file\"\nwill not be able to use them.\n\nThe performance for `git cat-file --batch-all-objects\n--batch-check` on the Git repository itself with performance\ntesting tool `hyperfine` changes from 669.4 ms ±  31.1 ms to\n1.134 s ±  0.063 s.\n\nThe performance for `git cat-file --batch-all-objects --batch\n>/dev/null` on the Git repository itself with performance testing\ntool `time` change from \"27.37s user 0.29s system 98% cpu 28.089\ntotal\" to \"33.69s user 1.54s system 87% cpu 40.258 total\".\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n Documentation/git-cat-file.txt |   6 +\n builtin/cat-file.c             | 242 ++++++-------------------------\n t/t1006-cat-file.sh            | 251 +++++++++++++++++++++++++++++++++\n 3 files changed, 304 insertions(+), 195 deletions(-)\n\ndiff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt\nindex 4eb0421b3fd..ef8ab952b2f 100644\n--- a/Documentation/git-cat-file.txt\n+++ b/Documentation/git-cat-file.txt\n@@ -226,6 +226,12 @@ newline. The available atoms are:\n \tafter that first run of whitespace (i.e., the \"rest\" of the\n \tline) are output in place of the `%(rest)` atom.\n \n+Note that most of the atoms in `for-each-ref --format` are now supported,\n+such as `%(tree)`, `%(parent)`, `%(author)`, `%(tagger)`, `%(if)`,\n+`%(then)`, `%(else)`, `%(end)`. But these atoms will be rejected:\n+`%(refname)`, `%(symref)`, `%(upstream)`, `%(push)`, `%(worktreepath)`,\n+`%(flag)`, `%(HEAD)`. See linkgit:git-for-each-ref[1].\n+\n If no format is specified, the default format is `%(objectname)\n %(objecttype) %(objectsize)`.\n \ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 41d407638d5..5b163551fc6 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -16,6 +16,7 @@\n #include \"packfile.h\"\n #include \"object-store.h\"\n #include \"promisor-remote.h\"\n+#include \"ref-filter.h\"\n \n struct batch_options {\n \tint enabled;\n@@ -25,7 +26,7 @@ struct batch_options {\n \tint all_objects;\n \tint unordered;\n \tint cmdmode; /* may be 'w' or 'c' for --filters or --textconv */\n-\tconst char *format;\n+\tstruct ref_format format;\n };\n \n static const char *force_path;\n@@ -195,99 +196,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,\n \n struct expand_data {\n \tstruct object_id oid;\n-\tenum object_type type;\n-\tunsigned long size;\n-\toff_t disk_size;\n \tconst char *rest;\n-\tstruct object_id delta_base_oid;\n-\n-\t/*\n-\t * If mark_query is true, we do not expand anything, but rather\n-\t * just mark the object_info with items we wish to query.\n-\t */\n-\tint mark_query;\n-\n-\t/*\n-\t * Whether to split the input on whitespace before feeding it to\n-\t * get_sha1; this is decided during the mark_query phase based on\n-\t * whether we have a %(rest) token in our format.\n-\t */\n \tint split_on_whitespace;\n-\n-\t/*\n-\t * After a mark_query run, this object_info is set up to be\n-\t * passed to oid_object_info_extended. It will point to the data\n-\t * elements above, so you can retrieve the response from there.\n-\t */\n-\tstruct object_info info;\n-\n-\t/*\n-\t * This flag will be true if the requested batch format and options\n-\t * don't require us to call oid_object_info, which can then be\n-\t * optimized out.\n-\t */\n-\tunsigned skip_object_info : 1;\n };\n \n-static int is_atom(const char *atom, const char *s, int slen)\n-{\n-\tint alen = strlen(atom);\n-\treturn alen == slen && !memcmp(atom, s, alen);\n-}\n-\n-static void expand_atom(struct strbuf *sb, const char *atom, int len,\n-\t\t\tvoid *vdata)\n-{\n-\tstruct expand_data *data = vdata;\n-\n-\tif (is_atom(\"objectname\", atom, len)) {\n-\t\tif (!data->mark_query)\n-\t\t\tstrbuf_addstr(sb, oid_to_hex(&data->oid));\n-\t} else if (is_atom(\"objecttype\", atom, len)) {\n-\t\tif (data->mark_query)\n-\t\t\tdata->info.typep = &data->type;\n-\t\telse\n-\t\t\tstrbuf_addstr(sb, type_name(data->type));\n-\t} else if (is_atom(\"objectsize\", atom, len)) {\n-\t\tif (data->mark_query)\n-\t\t\tdata->info.sizep = &data->size;\n-\t\telse\n-\t\t\tstrbuf_addf(sb, \"%\"PRIuMAX , (uintmax_t)data->size);\n-\t} else if (is_atom(\"objectsize:disk\", atom, len)) {\n-\t\tif (data->mark_query)\n-\t\t\tdata->info.disk_sizep = &data->disk_size;\n-\t\telse\n-\t\t\tstrbuf_addf(sb, \"%\"PRIuMAX, (uintmax_t)data->disk_size);\n-\t} else if (is_atom(\"rest\", atom, len)) {\n-\t\tif (data->mark_query)\n-\t\t\tdata->split_on_whitespace = 1;\n-\t\telse if (data->rest)\n-\t\t\tstrbuf_addstr(sb, data->rest);\n-\t} else if (is_atom(\"deltabase\", atom, len)) {\n-\t\tif (data->mark_query)\n-\t\t\tdata->info.delta_base_oid = &data->delta_base_oid;\n-\t\telse\n-\t\t\tstrbuf_addstr(sb,\n-\t\t\t\t      oid_to_hex(&data->delta_base_oid));\n-\t} else\n-\t\tdie(\"unknown format element: %.*s\", len, atom);\n-}\n-\n-static size_t expand_format(struct strbuf *sb, const char *start, void *data)\n-{\n-\tconst char *end;\n-\n-\tif (*start != '(')\n-\t\treturn 0;\n-\tend = strchr(start + 1, ')');\n-\tif (!end)\n-\t\tdie(\"format element '%s' does not end in ')'\", start);\n-\n-\texpand_atom(sb, start + 1, end - start - 1, data);\n-\n-\treturn end - start + 1;\n-}\n-\n static void batch_write(struct batch_options *opt, const void *data, int len)\n {\n \tif (opt->buffer_output) {\n@@ -297,87 +209,34 @@ static void batch_write(struct batch_options *opt, const void *data, int len)\n \t\twrite_or_die(1, data, len);\n }\n \n-static void print_object_or_die(struct batch_options *opt, struct expand_data *data)\n-{\n-\tconst struct object_id *oid = &data->oid;\n-\n-\tassert(data->info.typep);\n-\n-\tif (data->type == OBJ_BLOB) {\n-\t\tif (opt->buffer_output)\n-\t\t\tfflush(stdout);\n-\t\tif (opt->cmdmode) {\n-\t\t\tchar *contents;\n-\t\t\tunsigned long size;\n-\n-\t\t\tif (!data->rest)\n-\t\t\t\tdie(\"missing path for '%s'\", oid_to_hex(oid));\n-\n-\t\t\tif (opt->cmdmode == 'w') {\n-\t\t\t\tif (filter_object(data->rest, 0100644, oid,\n-\t\t\t\t\t\t  &contents, &size))\n-\t\t\t\t\tdie(\"could not convert '%s' %s\",\n-\t\t\t\t\t    oid_to_hex(oid), data->rest);\n-\t\t\t} else if (opt->cmdmode == 'c') {\n-\t\t\t\tenum object_type type;\n-\t\t\t\tif (!textconv_object(the_repository,\n-\t\t\t\t\t\t     data->rest, 0100644, oid,\n-\t\t\t\t\t\t     1, &contents, &size))\n-\t\t\t\t\tcontents = read_object_file(oid,\n-\t\t\t\t\t\t\t\t    &type,\n-\t\t\t\t\t\t\t\t    &size);\n-\t\t\t\tif (!contents)\n-\t\t\t\t\tdie(\"could not convert '%s' %s\",\n-\t\t\t\t\t    oid_to_hex(oid), data->rest);\n-\t\t\t} else\n-\t\t\t\tBUG(\"invalid cmdmode: %c\", opt->cmdmode);\n-\t\t\tbatch_write(opt, contents, size);\n-\t\t\tfree(contents);\n-\t\t} else {\n-\t\t\tstream_blob(oid);\n-\t\t}\n-\t}\n-\telse {\n-\t\tenum object_type type;\n-\t\tunsigned long size;\n-\t\tvoid *contents;\n-\n-\t\tcontents = read_object_file(oid, &type, &size);\n-\t\tif (!contents)\n-\t\t\tdie(\"object %s disappeared\", oid_to_hex(oid));\n-\t\tif (type != data->type)\n-\t\t\tdie(\"object %s changed type!?\", oid_to_hex(oid));\n-\t\tif (data->info.sizep && size != data->size)\n-\t\t\tdie(\"object %s changed size!?\", oid_to_hex(oid));\n-\n-\t\tbatch_write(opt, contents, size);\n-\t\tfree(contents);\n-\t}\n-}\n \n static void batch_object_write(const char *obj_name,\n \t\t\t       struct strbuf *scratch,\n \t\t\t       struct batch_options *opt,\n \t\t\t       struct expand_data *data)\n {\n-\tif (!data->skip_object_info &&\n-\t    oid_object_info_extended(the_repository, &data->oid, &data->info,\n-\t\t\t\t     OBJECT_INFO_LOOKUP_REPLACE) < 0) {\n-\t\tprintf(\"%s missing\\n\",\n-\t\t       obj_name ? obj_name : oid_to_hex(&data->oid));\n-\t\tfflush(stdout);\n-\t\treturn;\n-\t}\n+\tint ret;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_array_item item = { data->oid, data->rest };\n \n \tstrbuf_reset(scratch);\n-\tstrbuf_expand(scratch, opt->format, expand_format, data);\n-\tstrbuf_addch(scratch, '\\n');\n-\tbatch_write(opt, scratch->buf, scratch->len);\n \n-\tif (opt->print_contents) {\n-\t\tprint_object_or_die(opt, data);\n-\t\tbatch_write(opt, \"\\n\", 1);\n+\tret = format_ref_array_item(&item, &opt->format, scratch, &err);\n+\tif (ret < 0)\n+\t\tdie(\"%s\\n\", err.buf);\n+\tif (ret) {\n+\t\t/* ret > 0 means when the object corresponding to oid\n+\t\t * cannot be found in format_ref_array_item(), we only print\n+\t\t * the error message.\n+\t\t */\n+\t\tprintf(\"%s\\n\", err.buf);\n+\t\tfflush(stdout);\n+\t} else {\n+\t\tstrbuf_addch(scratch, '\\n');\n+\t\tbatch_write(opt, scratch->buf, scratch->len);\n \t}\n+\tfree_ref_array_item_value(&item);\n+\tstrbuf_release(&err);\n }\n \n static void batch_one_object(const char *obj_name,\n@@ -495,43 +354,37 @@ static int batch_unordered_packed(const struct object_id *oid,\n \treturn batch_unordered_object(oid, data);\n }\n \n-static int batch_objects(struct batch_options *batch)\n+static const char * const cat_file_usage[] = {\n+\tN_(\"git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>\"),\n+\tN_(\"git cat-file (--batch[=<format>] | --batch-check[=<format>]) [--follow-symlinks] [--textconv | --filters]\"),\n+\tNULL\n+};\n+\n+static int batch_objects(struct batch_options *batch, const struct option *options)\n {\n \tstruct strbuf input = STRBUF_INIT;\n \tstruct strbuf output = STRBUF_INIT;\n+\tstruct strbuf format = STRBUF_INIT;\n \tstruct expand_data data;\n \tint save_warning;\n \tint retval = 0;\n \n-\tif (!batch->format)\n-\t\tbatch->format = \"%(objectname) %(objecttype) %(objectsize)\";\n-\n-\t/*\n-\t * Expand once with our special mark_query flag, which will prime the\n-\t * object_info to be handed to oid_object_info_extended for each\n-\t * object.\n-\t */\n \tmemset(&data, 0, sizeof(data));\n-\tdata.mark_query = 1;\n-\tstrbuf_expand(&output, batch->format, expand_format, &data);\n-\tdata.mark_query = 0;\n-\tstrbuf_release(&output);\n-\tif (batch->cmdmode)\n-\t\tdata.split_on_whitespace = 1;\n-\n-\t/*\n-\t * If we are printing out the object, then always fill in the type,\n-\t * since we will want to decide whether or not to stream.\n-\t */\n+\tif (batch->format.format)\n+\t\tstrbuf_addstr(&format, batch->format.format);\n+\telse\n+\t\tstrbuf_addstr(&format, \"%(objectname) %(objecttype) %(objectsize)\");\n \tif (batch->print_contents)\n-\t\tdata.info.typep = &data.type;\n+\t\tstrbuf_addstr(&format, \"\\n%(raw)\");\n+\tbatch->format.format = format.buf;\n+\tif (verify_ref_format(&batch->format))\n+\t\tusage_with_options(cat_file_usage, options);\n+\n+\tif (batch->cmdmode || batch->format.use_rest)\n+\t\tdata.split_on_whitespace = 1;\n \n \tif (batch->all_objects) {\n \t\tstruct object_cb_data cb;\n-\t\tstruct object_info empty = OBJECT_INFO_INIT;\n-\n-\t\tif (!memcmp(&data.info, &empty, sizeof(empty)))\n-\t\t\tdata.skip_object_info = 1;\n \n \t\tif (has_promisor_remote())\n \t\t\twarning(\"This repository uses promisor remotes. Some objects may not be loaded.\");\n@@ -561,6 +414,7 @@ static int batch_objects(struct batch_options *batch)\n \t\t\toid_array_clear(&sa);\n \t\t}\n \n+\t\tstrbuf_release(&format);\n \t\tstrbuf_release(&output);\n \t\treturn 0;\n \t}\n@@ -593,18 +447,13 @@ static int batch_objects(struct batch_options *batch)\n \t\tbatch_one_object(input.buf, &output, batch, &data);\n \t}\n \n+\tstrbuf_release(&format);\n \tstrbuf_release(&input);\n \tstrbuf_release(&output);\n \twarn_on_object_refname_ambiguity = save_warning;\n \treturn retval;\n }\n \n-static const char * const cat_file_usage[] = {\n-\tN_(\"git cat-file (-t [--allow-unknown-type] | -s [--allow-unknown-type] | -e | -p | <type> | --textconv | --filters) [--path=<path>] <object>\"),\n-\tN_(\"git cat-file (--batch[=<format>] | --batch-check[=<format>]) [--follow-symlinks] [--textconv | --filters]\"),\n-\tNULL\n-};\n-\n static int git_cat_file_config(const char *var, const char *value, void *cb)\n {\n \tif (userdiff_config(var, value) < 0)\n@@ -627,7 +476,7 @@ static int batch_option_callback(const struct option *opt,\n \n \tbo->enabled = 1;\n \tbo->print_contents = !strcmp(opt->long_name, \"batch\");\n-\tbo->format = arg;\n+\tbo->format.format = arg;\n \n \treturn 0;\n }\n@@ -636,7 +485,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n {\n \tint opt = 0;\n \tconst char *exp_type = NULL, *obj_name = NULL;\n-\tstruct batch_options batch = {0};\n+\tstruct batch_options batch = {\n+\t\t.format = REF_FORMAT_INIT\n+\t};\n \tint unknown_type = 0;\n \n \tconst struct option options[] = {\n@@ -675,6 +526,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \tgit_config(git_cat_file_config, NULL);\n \n \tbatch.buffer_output = -1;\n+\tbatch.format.cat_file_mode = 1;\n \targc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);\n \n \tif (opt) {\n@@ -718,7 +570,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \t\tbatch.buffer_output = batch.all_objects;\n \n \tif (batch.enabled)\n-\t\treturn batch_objects(&batch);\n+\t\treturn batch_objects(&batch, options);\n \n \tif (unknown_type && opt != 't' && opt != 's')\n \t\tdie(\"git cat-file --allow-unknown-type: use with -s or -t\");\ndiff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh\nindex 18b3779ccb6..7452404f24a 100755\n--- a/t/t1006-cat-file.sh\n+++ b/t/t1006-cat-file.sh\n@@ -607,5 +607,256 @@ test_expect_success 'cat-file --batch=\"batman\" with --batch-all-objects will wor\n \tgit -C all-two cat-file --batch-all-objects --batch=\"batman\" >actual &&\n \tcmp expect actual\n '\n+. \"$TEST_DIRECTORY\"/lib-gpg.sh\n+. \"$TEST_DIRECTORY\"/lib-terminal.sh\n+\n+test_expect_success 'cat-file --batch|--batch-check setup' '\n+\techo 1>blob1 &&\n+\tprintf \"a\\0b\\0\\c\" >blob2 &&\n+\tgit add blob1 blob2 &&\n+\tgit commit -m \"Commit Message\" &&\n+\tgit branch -M main &&\n+\tgit tag -a -m \"v0.0.0\" testtag &&\n+\tgit update-ref refs/myblobs/blob1 HEAD:blob1 &&\n+\tgit update-ref refs/myblobs/blob2 HEAD:blob2 &&\n+\tgit update-ref refs/mytrees/tree1 HEAD^{tree}\n+'\n+\n+batch_test_atom() {\n+\tif test \"$3\" = \"fail\"\n+\tthen\n+\t\ttest_expect_${4:-success} $PREREQ \"basic atom: $1 $2 must fail\" \"\n+\t\t\ttest_must_fail git cat-file --batch-check='$2' >bad <<-EOF\n+\t\t\t$1\n+\t\t\tEOF\n+\t\t\"\n+\telse\n+\t\ttest_expect_${4:-success} $PREREQ \"basic atom: $1 $2\" \"\n+\t\t\tgit for-each-ref --format='$2' $1 >expected &&\n+\t\t\tgit cat-file --batch-check='$2' >actual <<-EOF &&\n+\t\t\t$1\n+\t\t\tEOF\n+\t\t\tsanitize_pgp <actual >actual.clean &&\n+\t\t\tcmp expected actual.clean\n+\t\t\"\n+\tfi\n+}\n+\n+batch_test_atom refs/heads/main '%(refname)' fail\n+batch_test_atom refs/heads/main '%(refname:)' fail\n+batch_test_atom refs/heads/main '%(refname:short)' fail\n+batch_test_atom refs/heads/main '%(refname:lstrip=1)' fail\n+batch_test_atom refs/heads/main '%(refname:lstrip=2)' fail\n+batch_test_atom refs/heads/main '%(refname:lstrip=-1)' fail\n+batch_test_atom refs/heads/main '%(refname:lstrip=-2)' fail\n+batch_test_atom refs/heads/main '%(refname:rstrip=1)' fail\n+batch_test_atom refs/heads/main '%(refname:rstrip=2)' fail\n+batch_test_atom refs/heads/main '%(refname:rstrip=-1)' fail\n+batch_test_atom refs/heads/main '%(refname:rstrip=-2)' fail\n+batch_test_atom refs/heads/main '%(refname:strip=1)' fail\n+batch_test_atom refs/heads/main '%(refname:strip=2)' fail\n+batch_test_atom refs/heads/main '%(refname:strip=-1)' fail\n+batch_test_atom refs/heads/main '%(refname:strip=-2)' fail\n+batch_test_atom refs/heads/main '%(upstream)' fail\n+batch_test_atom refs/heads/main '%(upstream:short)' fail\n+batch_test_atom refs/heads/main '%(upstream:lstrip=2)' fail\n+batch_test_atom refs/heads/main '%(upstream:lstrip=-2)' fail\n+batch_test_atom refs/heads/main '%(upstream:rstrip=2)' fail\n+batch_test_atom refs/heads/main '%(upstream:rstrip=-2)' fail\n+batch_test_atom refs/heads/main '%(upstream:strip=2)' fail\n+batch_test_atom refs/heads/main '%(upstream:strip=-2)' fail\n+batch_test_atom refs/heads/main '%(push)' fail\n+batch_test_atom refs/heads/main '%(push:short)' fail\n+batch_test_atom refs/heads/main '%(push:lstrip=1)' fail\n+batch_test_atom refs/heads/main '%(push:lstrip=-1)' fail\n+batch_test_atom refs/heads/main '%(push:rstrip=1)' fail\n+batch_test_atom refs/heads/main '%(push:rstrip=-1)' fail\n+batch_test_atom refs/heads/main '%(push:strip=1)' fail\n+batch_test_atom refs/heads/main '%(push:strip=-1)' fail\n+batch_test_atom refs/heads/main '%(objecttype)'\n+batch_test_atom refs/heads/main '%(objectsize)'\n+batch_test_atom refs/heads/main '%(objectsize:disk)'\n+batch_test_atom refs/heads/main '%(deltabase)'\n+batch_test_atom refs/heads/main '%(objectname)'\n+batch_test_atom refs/heads/main '%(objectname:short)'\n+batch_test_atom refs/heads/main '%(objectname:short=1)'\n+batch_test_atom refs/heads/main '%(objectname:short=10)'\n+batch_test_atom refs/heads/main '%(tree)'\n+batch_test_atom refs/heads/main '%(tree:short)'\n+batch_test_atom refs/heads/main '%(tree:short=1)'\n+batch_test_atom refs/heads/main '%(tree:short=10)'\n+batch_test_atom refs/heads/main '%(parent)'\n+batch_test_atom refs/heads/main '%(parent:short)'\n+batch_test_atom refs/heads/main '%(parent:short=1)'\n+batch_test_atom refs/heads/main '%(parent:short=10)'\n+batch_test_atom refs/heads/main '%(numparent)'\n+batch_test_atom refs/heads/main '%(object)'\n+batch_test_atom refs/heads/main '%(type)'\n+batch_test_atom refs/heads/main '%(raw)'\n+batch_test_atom refs/heads/main '%(*objectname)'\n+batch_test_atom refs/heads/main '%(*objecttype)'\n+batch_test_atom refs/heads/main '%(author)'\n+batch_test_atom refs/heads/main '%(authorname)'\n+batch_test_atom refs/heads/main '%(authoremail)'\n+batch_test_atom refs/heads/main '%(authoremail:trim)'\n+batch_test_atom refs/heads/main '%(authoremail:localpart)'\n+batch_test_atom refs/heads/main '%(authordate)'\n+batch_test_atom refs/heads/main '%(committer)'\n+batch_test_atom refs/heads/main '%(committername)'\n+batch_test_atom refs/heads/main '%(committeremail)'\n+batch_test_atom refs/heads/main '%(committeremail:trim)'\n+batch_test_atom refs/heads/main '%(committeremail:localpart)'\n+batch_test_atom refs/heads/main '%(committerdate)'\n+batch_test_atom refs/heads/main '%(tag)'\n+batch_test_atom refs/heads/main '%(tagger)'\n+batch_test_atom refs/heads/main '%(taggername)'\n+batch_test_atom refs/heads/main '%(taggeremail)'\n+batch_test_atom refs/heads/main '%(taggeremail:trim)'\n+batch_test_atom refs/heads/main '%(taggeremail:localpart)'\n+batch_test_atom refs/heads/main '%(taggerdate)'\n+batch_test_atom refs/heads/main '%(creator)'\n+batch_test_atom refs/heads/main '%(creatordate)'\n+batch_test_atom refs/heads/main '%(subject)'\n+batch_test_atom refs/heads/main '%(subject:sanitize)'\n+batch_test_atom refs/heads/main '%(contents:subject)'\n+batch_test_atom refs/heads/main '%(body)'\n+batch_test_atom refs/heads/main '%(contents:body)'\n+batch_test_atom refs/heads/main '%(contents:signature)'\n+batch_test_atom refs/heads/main '%(contents)'\n+batch_test_atom refs/heads/main '%(HEAD)' fail\n+batch_test_atom refs/heads/main '%(upstream:track)' fail\n+batch_test_atom refs/heads/main '%(upstream:trackshort)' fail\n+batch_test_atom refs/heads/main '%(upstream:track,nobracket)' fail\n+batch_test_atom refs/heads/main '%(upstream:nobracket,track)' fail\n+batch_test_atom refs/heads/main '%(push:track)' fail\n+batch_test_atom refs/heads/main '%(push:trackshort)' fail\n+batch_test_atom refs/heads/main '%(worktreepath)' fail\n+batch_test_atom refs/heads/main '%(symref)' fail\n+batch_test_atom refs/heads/main '%(flag)' fail\n+\n+batch_test_atom refs/tags/testtag '%(refname)' fail\n+batch_test_atom refs/tags/testtag '%(refname:short)' fail\n+batch_test_atom refs/tags/testtag '%(upstream)' fail\n+batch_test_atom refs/tags/testtag '%(push)' fail\n+batch_test_atom refs/tags/testtag '%(objecttype)'\n+batch_test_atom refs/tags/testtag '%(objectsize)'\n+batch_test_atom refs/tags/testtag '%(objectsize:disk)'\n+batch_test_atom refs/tags/testtag '%(*objectsize:disk)'\n+batch_test_atom refs/tags/testtag '%(deltabase)'\n+batch_test_atom refs/tags/testtag '%(*deltabase)'\n+batch_test_atom refs/tags/testtag '%(objectname)'\n+batch_test_atom refs/tags/testtag '%(objectname:short)'\n+batch_test_atom refs/tags/testtag '%(tree)'\n+batch_test_atom refs/tags/testtag '%(tree:short)'\n+batch_test_atom refs/tags/testtag '%(tree:short=1)'\n+batch_test_atom refs/tags/testtag '%(tree:short=10)'\n+batch_test_atom refs/tags/testtag '%(parent)'\n+batch_test_atom refs/tags/testtag '%(parent:short)'\n+batch_test_atom refs/tags/testtag '%(parent:short=1)'\n+batch_test_atom refs/tags/testtag '%(parent:short=10)'\n+batch_test_atom refs/tags/testtag '%(numparent)'\n+batch_test_atom refs/tags/testtag '%(object)'\n+batch_test_atom refs/tags/testtag '%(type)'\n+batch_test_atom refs/tags/testtag '%(*objectname)'\n+batch_test_atom refs/tags/testtag '%(*objecttype)'\n+batch_test_atom refs/tags/testtag '%(author)'\n+batch_test_atom refs/tags/testtag '%(authorname)'\n+batch_test_atom refs/tags/testtag '%(authoremail)'\n+batch_test_atom refs/tags/testtag '%(authoremail:trim)'\n+batch_test_atom refs/tags/testtag '%(authoremail:localpart)'\n+batch_test_atom refs/tags/testtag '%(authordate)'\n+batch_test_atom refs/tags/testtag '%(committer)'\n+batch_test_atom refs/tags/testtag '%(committername)'\n+batch_test_atom refs/tags/testtag '%(committeremail)'\n+batch_test_atom refs/tags/testtag '%(committeremail:trim)'\n+batch_test_atom refs/tags/testtag '%(committeremail:localpart)'\n+batch_test_atom refs/tags/testtag '%(committerdate)'\n+batch_test_atom refs/tags/testtag '%(tag)'\n+batch_test_atom refs/tags/testtag '%(tagger)'\n+batch_test_atom refs/tags/testtag '%(taggername)'\n+batch_test_atom refs/tags/testtag '%(taggeremail)'\n+batch_test_atom refs/tags/testtag '%(taggeremail:trim)'\n+batch_test_atom refs/tags/testtag '%(taggeremail:localpart)'\n+batch_test_atom refs/tags/testtag '%(taggerdate)'\n+batch_test_atom refs/tags/testtag '%(creator)'\n+batch_test_atom refs/tags/testtag '%(creatordate)'\n+batch_test_atom refs/tags/testtag '%(subject)'\n+batch_test_atom refs/tags/testtag '%(subject:sanitize)'\n+batch_test_atom refs/tags/testtag '%(contents:subject)'\n+batch_test_atom refs/tags/testtag '%(body)'\n+batch_test_atom refs/tags/testtag '%(contents:body)'\n+batch_test_atom refs/tags/testtag '%(contents:signature)'\n+batch_test_atom refs/tags/testtag '%(contents)'\n+batch_test_atom refs/tags/testtag '%(HEAD)' fail\n+\n+batch_test_atom refs/myblobs/blob1 '%(refname)' fail\n+batch_test_atom refs/myblobs/blob1 '%(upstream)' fail\n+batch_test_atom refs/myblobs/blob1 '%(push)' fail\n+batch_test_atom refs/myblobs/blob1 '%(HEAD)' fail\n+\n+batch_test_atom refs/myblobs/blob1 '%(objectname)'\n+batch_test_atom refs/myblobs/blob1 '%(objecttype)'\n+batch_test_atom refs/myblobs/blob1 '%(objectsize)'\n+batch_test_atom refs/myblobs/blob1 '%(objectsize:disk)'\n+batch_test_atom refs/myblobs/blob1 '%(deltabase)'\n+\n+batch_test_atom refs/myblobs/blob1 '%(contents)'\n+batch_test_atom refs/myblobs/blob2 '%(contents)'\n+\n+batch_test_atom refs/myblobs/blob1 '%(raw)'\n+batch_test_atom refs/myblobs/blob2 '%(raw)'\n+batch_test_atom refs/mytrees/tree1 '%(raw)'\n+\n+batch_test_atom refs/myblobs/blob1 '%(raw:size)'\n+batch_test_atom refs/myblobs/blob2 '%(raw:size)'\n+batch_test_atom refs/mytrees/tree1 '%(raw:size)'\n+\n+batch_test_atom refs/myblobs/blob1 '%(if:equals=blob)%(objecttype)%(then)commit%(else)not commit%(end)'\n+batch_test_atom refs/myblobs/blob2 '%(if:equals=blob)%(objecttype)%(then)commit%(else)not commit%(end)'\n+batch_test_atom refs/mytrees/tree1 '%(if:equals=tree)%(objecttype)%(then)tree%(else)not tree%(end)'\n+\n+batch_test_atom refs/heads/main '%(align:60) objectname is %(objectname)%(end)|%(objectname)'\n+batch_test_atom refs/heads/main '%(align:left,60) objectname is %(objectname)%(end)|%(objectname)'\n+batch_test_atom refs/heads/main '%(align:middle,60) objectname is %(objectname)%(end)|%(objectname)'\n+batch_test_atom refs/heads/main '%(align:60,right) objectname is %(objectname)%(end)|%(objectname)'\n+\n+batch_test_atom refs/heads/main 'VALID'\n+batch_test_atom refs/heads/main '%(INVALID)' fail\n+batch_test_atom refs/heads/main '%(authordate:INVALID)' fail\n+\n+test_expect_success '%(rest) works with both a branch and a tag' '\n+\tcat >expected <<-EOF &&\n+\t123 commit 123\n+\t456 tag 456\n+\tEOF\n+\tgit cat-file --batch-check=\"%(rest) %(objecttype) %(rest)\" >actual <<-EOF &&\n+\trefs/heads/main 123\n+\trefs/tags/testtag 456\n+\tEOF\n+\ttest_cmp expected actual\n+'\n+\n+batch_test_atom refs/heads/main '%(objectname) %(objecttype) %(objectsize)\n+%(raw)'\n+batch_test_atom refs/tags/testtag '%(objectname) %(objecttype) %(objectsize)\n+%(raw)'\n+batch_test_atom refs/myblobs/blob1 '%(objectname) %(objecttype) %(objectsize)\n+%(raw)'\n+batch_test_atom refs/myblobs/blob2 '%(objectname) %(objecttype) %(objectsize)\n+%(raw)'\n+\n+\n+test_expect_success 'cat-file --batch equals to --batch-check with atoms' '\n+\tgit cat-file --batch-check=\"%(objectname) %(objecttype) %(objectsize)\n+%(raw)\" >expected <<-EOF &&\n+\trefs/heads/main\n+\trefs/tags/testtag\n+\tEOF\n+\tgit cat-file --batch >actual <<-EOF &&\n+\trefs/heads/main\n+\trefs/tags/testtag\n+\tEOF\n+\tcmp expected actual\n+'\n \n test_done\n-- \ngitgitgadget\n\n"},{"id":"428962","messageId":"18f38075b3c70748894ad6b2e1158f2b17460845.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 10/15] [GSOC] cat-file: add has_object_file() check","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:08Z","receivedAt":"2021-07-01T16:08:33Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nUse `has_object_file()` in `batch_one_object()` to check\nwhether the input object exists. This can help us reject\nthe missing oid when we let `cat-file --batch` use ref-filter\nlogic later.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n builtin/cat-file.c | 7 +++++++\n 1 file changed, 7 insertions(+)\n\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 243fe6844bc..59a86412fd0 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -428,6 +428,13 @@ static void batch_one_object(const char *obj_name,\n \t\treturn;\n \t}\n \n+\tif (!has_object_file(&data->oid)) {\n+\t\tprintf(\"%s missing\\n\",\n+\t\t       obj_name ? obj_name : oid_to_hex(&data->oid));\n+\t\tfflush(stdout);\n+\t\treturn;\n+\t}\n+\n \tbatch_object_write(obj_name, scratch, opt, data);\n }\n \n-- \ngitgitgadget\n\n"},{"id":"428963","messageId":"86e4620663e012b12abfc6b0850c4611f607d2b9.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 13/15] [GSOC] cat-file: reuse err buf in batch_object_write()","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:11Z","receivedAt":"2021-07-01T16:08:35Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nReuse the `err` buffer in batch_object_write(), as the\nbuffer `scratch` does. This will reduce the overhead\nof multiple allocations of memory of the err buffer.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n builtin/cat-file.c | 22 ++++++++++++++--------\n 1 file changed, 14 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 5b163551fc6..dc604a9879d 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -212,35 +212,36 @@ static void batch_write(struct batch_options *opt, const void *data, int len)\n \n static void batch_object_write(const char *obj_name,\n \t\t\t       struct strbuf *scratch,\n+\t\t\t       struct strbuf *err,\n \t\t\t       struct batch_options *opt,\n \t\t\t       struct expand_data *data)\n {\n \tint ret;\n-\tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_array_item item = { data->oid, data->rest };\n \n \tstrbuf_reset(scratch);\n+\tstrbuf_reset(err);\n \n-\tret = format_ref_array_item(&item, &opt->format, scratch, &err);\n+\tret = format_ref_array_item(&item, &opt->format, scratch, err);\n \tif (ret < 0)\n-\t\tdie(\"%s\\n\", err.buf);\n+\t\tdie(\"%s\\n\", err->buf);\n \tif (ret) {\n \t\t/* ret > 0 means when the object corresponding to oid\n \t\t * cannot be found in format_ref_array_item(), we only print\n \t\t * the error message.\n \t\t */\n-\t\tprintf(\"%s\\n\", err.buf);\n+\t\tprintf(\"%s\\n\", err->buf);\n \t\tfflush(stdout);\n \t} else {\n \t\tstrbuf_addch(scratch, '\\n');\n \t\tbatch_write(opt, scratch->buf, scratch->len);\n \t}\n \tfree_ref_array_item_value(&item);\n-\tstrbuf_release(&err);\n }\n \n static void batch_one_object(const char *obj_name,\n \t\t\t     struct strbuf *scratch,\n+\t\t\t     struct strbuf *err,\n \t\t\t     struct batch_options *opt,\n \t\t\t     struct expand_data *data)\n {\n@@ -294,7 +295,7 @@ static void batch_one_object(const char *obj_name,\n \t\treturn;\n \t}\n \n-\tbatch_object_write(obj_name, scratch, opt, data);\n+\tbatch_object_write(obj_name, scratch, err, opt, data);\n }\n \n struct object_cb_data {\n@@ -302,13 +303,14 @@ struct object_cb_data {\n \tstruct expand_data *expand;\n \tstruct oidset *seen;\n \tstruct strbuf *scratch;\n+\tstruct strbuf *err;\n };\n \n static int batch_object_cb(const struct object_id *oid, void *vdata)\n {\n \tstruct object_cb_data *data = vdata;\n \toidcpy(&data->expand->oid, oid);\n-\tbatch_object_write(NULL, data->scratch, data->opt, data->expand);\n+\tbatch_object_write(NULL, data->scratch, data->err, data->opt, data->expand);\n \treturn 0;\n }\n \n@@ -364,6 +366,7 @@ static int batch_objects(struct batch_options *batch, const struct option *optio\n {\n \tstruct strbuf input = STRBUF_INIT;\n \tstruct strbuf output = STRBUF_INIT;\n+\tstruct strbuf err = STRBUF_INIT;\n \tstruct strbuf format = STRBUF_INIT;\n \tstruct expand_data data;\n \tint save_warning;\n@@ -392,6 +395,7 @@ static int batch_objects(struct batch_options *batch, const struct option *optio\n \t\tcb.opt = batch;\n \t\tcb.expand = &data;\n \t\tcb.scratch = &output;\n+\t\tcb.err = &err;\n \n \t\tif (batch->unordered) {\n \t\t\tstruct oidset seen = OIDSET_INIT;\n@@ -416,6 +420,7 @@ static int batch_objects(struct batch_options *batch, const struct option *optio\n \n \t\tstrbuf_release(&format);\n \t\tstrbuf_release(&output);\n+\t\tstrbuf_release(&err);\n \t\treturn 0;\n \t}\n \n@@ -444,12 +449,13 @@ static int batch_objects(struct batch_options *batch, const struct option *optio\n \t\t\tdata.rest = p;\n \t\t}\n \n-\t\tbatch_one_object(input.buf, &output, batch, &data);\n+\t\tbatch_one_object(input.buf, &output, &err, batch, &data);\n \t}\n \n \tstrbuf_release(&format);\n \tstrbuf_release(&input);\n \tstrbuf_release(&output);\n+\tstrbuf_release(&err);\n \twarn_on_object_refname_ambiguity = save_warning;\n \treturn retval;\n }\n-- \ngitgitgadget\n\n"},{"id":"428964","messageId":"3aeb4d3d3ec9dc112a19a8b4adeaf213577b4028.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:12Z","receivedAt":"2021-07-01T16:08:35Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nAfter cat-file reuses the ref-filter logic, we re-implement the\nfunctions of --textconv and --filters options.\n\nAdd members `use_textconv` and `use_filters` in struct `ref_format`,\nand use global variables `use_filters` and `use_textconv` in\n`ref-filter.c`, so that we can filter the content of the object\nin get_object(). Use `actual_oi` to record the real expand_data:\nit may point to the original `oi` or the `act_oi` processed by\n`textconv_object()` or `convert_to_working_tree()`. `grab_values()`\nwill grab the contents of `actual_oi` and `grab_common_values()`\nto grab the contents of origin `oi`, this ensures that `%(objectsize)`\nstill uses the size of the unfiltered data.\n\nIn `get_object()`, we made an optimization: Firstly, get the size and\ntype of the object instead of directly getting the object data.\nIf using --textconv, after successfully obtaining the filtered object\ndata, an extra oid_object_info_extended() will be skipped, which can\nreduce the cost of object data copy; If using --filter, the data of\nthe object first will be getted first, and then convert_to_working_tree()\nwill be used to get the filtered object data.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n builtin/cat-file.c |  6 +++++\n ref-filter.c       | 59 ++++++++++++++++++++++++++++++++++++++++++++--\n ref-filter.h       |  2 ++\n 3 files changed, 65 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex dc604a9879d..710085b7d72 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -380,6 +380,12 @@ static int batch_objects(struct batch_options *batch, const struct option *optio\n \tif (batch->print_contents)\n \t\tstrbuf_addstr(&format, \"\\n%(raw)\");\n \tbatch->format.format = format.buf;\n+\n+\tif (batch->cmdmode == 'c')\n+\t\tbatch->format.use_textconv = 1;\n+\telse if (batch->cmdmode == 'w')\n+\t\tbatch->format.use_filters = 1;\n+\n \tif (verify_ref_format(&batch->format))\n \t\tusage_with_options(cat_file_usage, options);\n \ndiff --git a/ref-filter.c b/ref-filter.c\nindex 9ca3dd5557d..427552b8108 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1,3 +1,4 @@\n+#define USE_THE_INDEX_COMPATIBILITY_MACROS\n #include \"builtin.h\"\n #include \"cache.h\"\n #include \"parse-options.h\"\n@@ -84,6 +85,9 @@ static struct expand_data {\n \tstruct object_info info;\n } oi, oi_deref;\n \n+int use_filters;\n+int use_textconv;\n+\n struct ref_to_worktree_entry {\n \tstruct hashmap_entry ent;\n \tstruct worktree *wt; /* key is wt->head_ref */\n@@ -1031,6 +1035,9 @@ int verify_ref_format(struct ref_format *format)\n \t\t\t\t\t       used_atom[at].atom_type == ATOM_WORKTREEPATH)))\n \t\t\tdie(_(\"this command reject atom %%(%.*s)\"), (int)(ep - sp - 2), sp + 2);\n \n+\t\tuse_filters = format->use_filters;\n+\t\tuse_textconv = format->use_textconv;\n+\n \t\tif ((format->quote_style == QUOTE_PYTHON ||\n \t\t     format->quote_style == QUOTE_SHELL ||\n \t\t     format->quote_style == QUOTE_TCL) &&\n@@ -1742,10 +1749,38 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj\n {\n \t/* parse_object_buffer() will set eaten to 0 if free() will be needed */\n \tint eaten = 1;\n+\tstruct expand_data *actual_oi = oi;\n+\tstruct expand_data act_oi = {0};\n+\n \tif (oi->info.contentp) {\n \t\t/* We need to know that to use parse_object_buffer properly */\n+\t\tvoid **temp_contentp = oi->info.contentp;\n+\t\toi->info.contentp = NULL;\n \t\toi->info.sizep = &oi->size;\n \t\toi->info.typep = &oi->type;\n+\n+\t\t/* get the type and size */\n+\t\tif (oid_object_info_extended(the_repository, &oi->oid, &oi->info,\n+\t\t\t\t\tOBJECT_INFO_LOOKUP_REPLACE))\n+\t\t\treturn strbuf_addf_ret(err, 1, _(\"%s missing\"),\n+\t\t\t\t\t       oid_to_hex(&oi->oid));\n+\n+\t\toi->info.sizep = NULL;\n+\t\toi->info.typep = NULL;\n+\t\toi->info.contentp = temp_contentp;\n+\n+\t\tif (use_textconv && !ref->rest)\n+\t\t\treturn strbuf_addf_ret(err, -1, _(\"missing path for '%s'\"),\n+\t\t\t\t\t       oid_to_hex(&act_oi.oid));\n+\t\tif (use_textconv && oi->type == OBJ_BLOB) {\n+\t\t\tact_oi = *oi;\n+\t\t\tif (textconv_object(the_repository,\n+\t\t\t\t\t    ref->rest, 0100644, &act_oi.oid,\n+\t\t\t\t\t    1, (char **)(&act_oi.content), &act_oi.size)) {\n+\t\t\t\tactual_oi = &act_oi;\n+\t\t\t\tgoto success;\n+\t\t\t}\n+\t\t}\n \t}\n \tif (oid_object_info_extended(the_repository, &oi->oid, &oi->info,\n \t\t\t\t     OBJECT_INFO_LOOKUP_REPLACE))\n@@ -1755,19 +1790,39 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj\n \t\tBUG(\"Object size is less than zero.\");\n \n \tif (oi->info.contentp) {\n-\t\t*obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);\n+\t\tif (use_filters && !ref->rest)\n+\t\t\treturn strbuf_addf_ret(err, -1, _(\"missing path for '%s'\"),\n+\t\t\t\t\t       oid_to_hex(&oi->oid));\n+\t\tif (use_filters && oi->type == OBJ_BLOB) {\n+\t\t\tstruct strbuf strbuf = STRBUF_INIT;\n+\t\t\tstruct checkout_metadata meta;\n+\t\t\tact_oi = *oi;\n+\n+\t\t\tinit_checkout_metadata(&meta, NULL, NULL, &act_oi.oid);\n+\t\t\tif (!convert_to_working_tree(&the_index, ref->rest, act_oi.content, act_oi.size, &strbuf, &meta))\n+\t\t\t\tdie(\"could not convert '%s' %s\",\n+\t\t\t\t\toid_to_hex(&oi->oid), ref->rest);\n+\t\t\tact_oi.size = strbuf.len;\n+\t\t\tact_oi.content = strbuf_detach(&strbuf, NULL);\n+\t\t\tactual_oi = &act_oi;\n+\t\t}\n+\n+success:\n+\t\t*obj = parse_object_buffer(the_repository, &actual_oi->oid, actual_oi->type, actual_oi->size, actual_oi->content, &eaten);\n \t\tif (!*obj) {\n \t\t\tif (!eaten)\n \t\t\t\tfree(oi->content);\n \t\t\treturn strbuf_addf_ret(err, -1, _(\"parse_object_buffer failed on %s for %s\"),\n \t\t\t\t\t       oid_to_hex(&oi->oid), ref->refname);\n \t\t}\n-\t\tgrab_values(ref->value, deref, *obj, oi);\n+\t\tgrab_values(ref->value, deref, *obj, actual_oi);\n \t}\n \n \tgrab_common_values(ref->value, deref, oi);\n \tif (!eaten)\n \t\tfree(oi->content);\n+\tif (actual_oi != oi)\n+\t\tfree(actual_oi->content);\n \treturn 0;\n }\n \ndiff --git a/ref-filter.h b/ref-filter.h\nindex 053980a6a42..497e3e93632 100644\n--- a/ref-filter.h\n+++ b/ref-filter.h\n@@ -80,6 +80,8 @@ struct ref_format {\n \tconst char *rest;\n \tint cat_file_mode;\n \tint quote_style;\n+\tint use_textconv;\n+\tint use_filters;\n \tint use_rest;\n \tint use_color;\n \n-- \ngitgitgadget\n\n"},{"id":"428965","messageId":"117e195fe5d64110fc0f03e87b839b8517e67f12.1625155693.git.gitgitgadget@gmail.com","threadId":"56038","inReplyTo":"pull.989.git.1625155693.gitgitgadget@gmail.com","subject":"[PATCH 15/15] [GSOC] ref-filter: remove grab_oid() function","fromName":"ZheNing Hu via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-07-01T16:08:13Z","receivedAt":"2021-07-01T16:08:36Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"From: ZheNing Hu <adlternative@gmail.com>\n\nBecause \"atom_type == ATOM_OBJECTNAME\" implies the condition\nof `starts_with(name, \"objectname\")`, \"atom_type == ATOM_TREE\"\nimplies the condition of `starts_with(name, \"tree\")`, so the\ncheck for `starts_with(name, field)` in grab_oid() is redundant.\n\nSo Remove the grab_oid() from ref-filter, to reduce repeated check.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Hariom Verma <hariom18599@gmail.com>\nSigned-off-by: ZheNing Hu <adlternative@gmail.com>\n---\n ref-filter.c | 26 +++++++++-----------------\n 1 file changed, 9 insertions(+), 17 deletions(-)\n\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 427552b8108..55bff4ed9e7 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -1071,16 +1071,6 @@ static const char *do_grab_oid(const char *field, const struct object_id *oid,\n \t}\n }\n \n-static int grab_oid(const char *name, const char *field, const struct object_id *oid,\n-\t\t    struct atom_value *v, struct used_atom *atom)\n-{\n-\tif (starts_with(name, field)) {\n-\t\tv->s = xstrdup(do_grab_oid(field, oid, atom));\n-\t\treturn 1;\n-\t}\n-\treturn 0;\n-}\n-\n /* See grab_values */\n static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)\n {\n@@ -1106,8 +1096,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_\n \t\t\t}\n \t\t} else if (atom_type == ATOM_DELTABASE)\n \t\t\tv->s = xstrdup(oid_to_hex(&oi->delta_base_oid));\n-\t\telse if (atom_type == ATOM_OBJECTNAME && deref)\n-\t\t\tgrab_oid(name, \"objectname\", &oi->oid, v, &used_atom[i]);\n+\t\telse if (atom_type == ATOM_OBJECTNAME && deref) {\n+\t\t\tv->s = xstrdup(do_grab_oid(\"objectname\", &oi->oid, &used_atom[i]));\n+\t\t}\n \t}\n }\n \n@@ -1148,9 +1139,10 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object\n \t\t\tcontinue;\n \t\tif (deref)\n \t\t\tname++;\n-\t\tif (atom_type == ATOM_TREE &&\n-\t\t    grab_oid(name, \"tree\", get_commit_tree_oid(commit), v, &used_atom[i]))\n+\t\tif (atom_type == ATOM_TREE) {\n+\t\t\tv->s = xstrdup(do_grab_oid(\"tree\", get_commit_tree_oid(commit), &used_atom[i]));\n \t\t\tcontinue;\n+\t\t}\n \t\tif (atom_type == ATOM_NUMPARENT) {\n \t\t\tv->value = commit_list_count(commit->parents);\n \t\t\tv->s = xstrfmt(\"%lu\", (unsigned long)v->value);\n@@ -1971,9 +1963,9 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)\n \t\t\t\tv->s = xstrdup(buf + 1);\n \t\t\t}\n \t\t\tcontinue;\n-\t\t} else if (!deref && atom_type == ATOM_OBJECTNAME &&\n-\t\t\t   grab_oid(name, \"objectname\", &ref->objectname, v, atom)) {\n-\t\t\t\tcontinue;\n+\t\t} else if (!deref && atom_type == ATOM_OBJECTNAME) {\n+\t\t\t   v->s = xstrdup(do_grab_oid(\"objectname\", &ref->objectname, atom));\n+\t\t\t   continue;\n \t\t} else if (atom_type == ATOM_HEAD) {\n \t\t\tif (atom->u.head && !strcmp(ref->refname, atom->u.head))\n \t\t\t\tv->s = xstrdup(\"*\");\n-- \ngitgitgadget\n"},{"id":"428980","messageId":"xmqqwnq9reqr.fsf@gitster.g","threadId":"56038","inReplyTo":"3aeb4d3d3ec9dc112a19a8b4adeaf213577b4028.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-07-01T19:55:08Z","receivedAt":"2021-07-01T19:55:12Z","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> +#define USE_THE_INDEX_COMPATIBILITY_MACROS\n>  #include \"builtin.h\"\n>  #include \"cache.h\"\n>  #include \"parse-options.h\"\n> @@ -84,6 +85,9 @@ static struct expand_data {\n>  \tstruct object_info info;\n>  } oi, oi_deref;\n>  \n> +int use_filters;\n> +int use_textconv;\n\nWith this commit in, \"make sparse\" dies like so:\n\n    SP ref-filter.c\nref-filter.c:88:5: error: symbol 'use_filters' was not declared. Should it be static?\nref-filter.c:89:5: error: symbol 'use_textconv' was not declared. Should it be static?\n"},{"id":"428982","messageId":"xmqqo8blrdzr.fsf@gitster.g","threadId":"56038","inReplyTo":"3aeb4d3d3ec9dc112a19a8b4adeaf213577b4028.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-07-01T20:11:20Z","receivedAt":"2021-07-01T20:11:23Z","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> After cat-file reuses the ref-filter logic, we re-implement the\n> functions of --textconv and --filters options.\n>\n> Add members `use_textconv` and `use_filters` in struct `ref_format`,\n> and use global variables `use_filters` and `use_textconv` in\n> `ref-filter.c`, so that we can filter the content of the object\n> in get_object().\n\nThese global variables are problematic.  The places that check them\nare quite isolated, and it makes me wonder why we cannot pass a\nformat instance through the callchain down to get_object().\n\n \n"},{"id":"429036","messageId":"CAOLTT8Qj0BO35+gTFdZ4jkNP=n8TarQX+s0PeCz2i+5yCCj-4w@mail.gmail.com","threadId":"56038","inReplyTo":"xmqqo8blrdzr.fsf@gitster.g","subject":"Re: [PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-02T12:46:15Z","receivedAt":"2021-07-02T12:46:23Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2021年7月2日周五 上午4:11写道：\n>\n> \"ZheNing Hu via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > After cat-file reuses the ref-filter logic, we re-implement the\n> > functions of --textconv and --filters options.\n> >\n> > Add members `use_textconv` and `use_filters` in struct `ref_format`,\n> > and use global variables `use_filters` and `use_textconv` in\n> > `ref-filter.c`, so that we can filter the content of the object\n> > in get_object().\n>\n> These global variables are problematic.  The places that check them\n> are quite isolated, and it makes me wonder why we cannot pass a\n> format instance through the callchain down to get_object().\n\nIf we pass such two options In such a deep call chain, it seems not\nworth to do it.\n\nformat_ref_array_item()->get_ref_atom_value()->populate_value()-->get_object()\n\nBut if we puts them in struct ref_array_item, It may be a solution,\nbut it may not be\nvery good.\n\nOr just use \"static\" prefix?\n\n>\n>\n\nThanks.\n--\nZheNing Hu\n"},{"id":"429042","messageId":"87im1salrd.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"ad71510feda0a100920a99de41db0ab137787796.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 02/15] [GSOC] ref-filter: add %(raw) atom","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-02T13:22:29Z","receivedAt":"2021-07-02T13:27:55Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> @@ -1362,17 +1398,29 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp\n>  \tconst char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;\n>  \tsize_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;\n>  \tvoid *buf = data->content;\n> +\tunsigned long buf_size = data->size;\n\nLet's declare this.\n\n>  \tfor (i = 0; i < used_atom_cnt; i++) {\n>  \t\tstruct used_atom *atom = &used_atom[i];\n>  \t\tconst char *name = atom->name;\n>  \t\tstruct atom_value *v = &val[i];\n> +\t\tenum atom_type atom_type = atom->atom_type;\n>  \n>  \t\tif (!!deref != (*name == '*'))\n>  \t\t\tcontinue;\n>  \t\tif (deref)\n>  \t\t\tname++;\n>  \n> +\t\tif (atom_type == ATOM_RAW) {\n\nHere, where it's going to be used.\n\n> +static int memcasecmp(const void *vs1, const void *vs2, size_t n)\n> +{\n> +\tconst char *s1 = vs1, *s2 = vs2;\n> +\tconst char *end = s1 + n;\n> +\n> +\tfor (; s1 < end; s1++, s2++) {\n> +\t\tint diff = tolower(*s1) - tolower(*s2);\n> +\t\tif (diff)\n> +\t\t\treturn diff;\n> +\t}\n> +\treturn 0;\n> +}\n\nI thought we had such a thing already, but clearly not...\n\n> diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\n> index 9e0214076b4..18554f62d94 100755\n> --- a/t/t6300-for-each-ref.sh\n> +++ b/t/t6300-for-each-ref.sh\n> @@ -130,6 +130,8 @@ test_atom head parent:short=10 ''\n>  test_atom head numparent 0\n>  test_atom head object ''\n>  test_atom head type ''\n> +test_atom head raw \"$(git cat-file commit refs/heads/main)\n> +\"\n\nExtra stay line here, is the \\n intentional?\n"},{"id":"429043","messageId":"87fswwalmv.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"7d2314b43f21a470667971f1fdcb382f43bf51ed.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 03/15] [GSOC] ref-filter: --format=%(raw) re-support --perl","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-02T13:29:49Z","receivedAt":"2021-07-02T13:30:37Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Because the perl language can handle binary data correctly,\n> add the function perl_quote_buf_with_len(), which can specify\n> the length of the data and prevent the data from being truncated\n> at '\\0' to help `--format=\"%(raw)\"` re-support `--perl`.\n>\n> Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n\nThanks!\n\n> -test_expect_success '%(raw) with --perl must fail' '\n> -\ttest_must_fail git for-each-ref --format=\"%(raw)\" --perl\n> +test_expect_success '%(raw) with --perl' '\n> +\tgit for-each-ref --format=\"\\$name= %(raw);\n> +print \\\"\\$name\\\"\" refs/myblobs/blob1 --perl | perl > actual &&\n> +\tcmp blob1 actual &&\n> +\tgit for-each-ref --format=\"\\$name= %(raw);\n> +print \\\"\\$name\\\"\" refs/myblobs/blob3 --perl | perl > actual &&\n> +\tcmp blob3 actual &&\n> +\tgit for-each-ref --format=\"\\$name= %(raw);\n> +print \\\"\\$name\\\"\" refs/myblobs/blob8 --perl | perl > actual &&\n> +\tcmp blob8 actual &&\n> +\tgit for-each-ref --format=\"\\$name= %(raw);\n> +print \\\"\\$name\\\"\" refs/myblobs/first --perl | perl > actual &&\n> +\tcmp one actual &&\n> +\tgit cat-file tree refs/mytrees/first > expected &&\n> +\tgit for-each-ref --format=\"\\$name= %(raw);\n> +print \\\"\\$name\\\"\" refs/mytrees/first --perl | perl > actual &&\n> +\tcmp expected actual\n>  '\n>  \n>  test_expect_success '%(raw) with --shell must fail' '\n\n\nNit: Formatting can be \"| perl >actual\", not \"| perl > actual\", like the\nusual style...\n"},{"id":"429044","messageId":"87czs0alhg.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"9568baf5dddcc0637c15f698aea24d230c4d01b2.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-02T13:32:38Z","receivedAt":"2021-07-02T13:33:51Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Add `cat_file_mode` member in struct `ref_format`, when\n> `cat-file --batch` use ref-filter logic later, it can help\n> us reject atoms in verify_ref_format() which cat-file cannot\n> use, e.g. `%(refname)`, `%(push)`, `%(upstream)`...\n>\n> Mentored-by: Christian Couder <christian.couder@gmail.com>\n> Mentored-by: Hariom Verma <hariom18599@gmail.com>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>  ref-filter.c | 11 +++++++++--\n>  ref-filter.h |  1 +\n>  2 files changed, 10 insertions(+), 2 deletions(-)\n>\n> diff --git a/ref-filter.c b/ref-filter.c\n> index 731e596eaa6..45122959eef 100644\n> --- a/ref-filter.c\n> +++ b/ref-filter.c\n> @@ -1021,8 +1021,15 @@ int verify_ref_format(struct ref_format *format)\n>  \t\tif (at < 0)\n>  \t\t\tdie(\"%s\", err.buf);\n>  \n> -\t\tif (used_atom[at].atom_type == ATOM_REST)\n> -\t\t\tdie(\"this command reject atom %%(%.*s)\", (int)(ep - sp - 2), sp + 2);\n> +\t\tif ((!format->cat_file_mode && used_atom[at].atom_type == ATOM_REST) ||\n> +\t\t    (format->cat_file_mode && (used_atom[at].atom_type == ATOM_FLAG ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_HEAD ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_PUSH ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_REFNAME ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_SYMREF ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_UPSTREAM ||\n> +\t\t\t\t\t       used_atom[at].atom_type == ATOM_WORKTREEPATH)))\n> +\t\t\tdie(_(\"this command reject atom %%(%.*s)\"), (int)(ep - sp - 2), sp + 2);\n\nMaybe it was brought up already, but those ATOM_* are mostly continuous\nwhere they're declared, Maybe worth making it so having a comment like:\n\n    /* These need to be in order, used in verify_ref_format */\n\nAnd then just doing:\n\n    used_atom[at].atom_type >= FIRST_OF_THOSE &&\n    used_atom[at].atom_type >= LAST_OF_THOSE\n\nMaybe it's too magical and peeking under the hood of an enum...\n"},{"id":"429045","messageId":"87a6n4alfk.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"18f38075b3c70748894ad6b2e1158f2b17460845.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 10/15] [GSOC] cat-file: add has_object_file() check","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-02T13:34:19Z","receivedAt":"2021-07-02T13:34:59Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> Use `has_object_file()` in `batch_one_object()` to check\n> whether the input object exists. This can help us reject\n> the missing oid when we let `cat-file --batch` use ref-filter\n> logic later.\n>\n> Mentored-by: Christian Couder <christian.couder@gmail.com>\n> Mentored-by: Hariom Verma <hariom18599@gmail.com>\n> Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> ---\n>  builtin/cat-file.c | 7 +++++++\n>  1 file changed, 7 insertions(+)\n>\n> diff --git a/builtin/cat-file.c b/builtin/cat-file.c\n> index 243fe6844bc..59a86412fd0 100644\n> --- a/builtin/cat-file.c\n> +++ b/builtin/cat-file.c\n> @@ -428,6 +428,13 @@ static void batch_one_object(const char *obj_name,\n>  \t\treturn;\n>  \t}\n>  \n> +\tif (!has_object_file(&data->oid)) {\n> +\t\tprintf(\"%s missing\\n\",\n> +\t\t       obj_name ? obj_name : oid_to_hex(&data->oid));\n> +\t\tfflush(stdout);\n> +\t\treturn;\n> +\t}\n> +\n>  \tbatch_object_write(obj_name, scratch, opt, data);\n>  }\n\nIn 12/15 the old \"missing\" is removed, so is this strictly redundant to\nwhat's in batch_object_write() for now, does it change anything?\n"},{"id":"429046","messageId":"877di8al8n.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"e04b970ccb0cad8c0b651ab11f5f52063bd84606.1625155693.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-02T13:36:32Z","receivedAt":"2021-07-02T13:39:08Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> From: ZheNing Hu <adlternative@gmail.com>\n>\n> In order to let cat-file use ref-filter logic, let's do the\n> following:\n>\n> 1. Change the type of member `format` in struct `batch_options`\n> to `ref_format`, we will pass it to ref-filter later.\n> 2. Let `batch_objects()` add atoms to format, and use\n> `verify_ref_format()` to check atoms.\n> 3. Use `format_ref_array_item()` in `batch_object_write()` to\n> get the formatted data corresponding to the object. If the\n> return value of `format_ref_array_item()` is equals to zero,\n> use `batch_write()` to print object data; else if the return\n> value is less than zero, use `die()` to print the error message\n> and exit; else if return value is greater than zero, only print\n> the error message, but don't exit.\n> 4. Use free_ref_array_item_value() to free ref_array_item's\n> value.\n>\n> Most of the atoms in `for-each-ref --format` are now supported,\n> such as `%(tree)`, `%(parent)`, `%(author)`, `%(tagger)`, `%(if)`,\n> `%(then)`, `%(else)`, `%(end)`. But these atoms will be rejected:\n> `%(refname)`, `%(symref)`, `%(upstream)`, `%(push)`, `%(worktreepath)`,\n> `%(flag)`, `%(HEAD)`, because these atoms are unique to those objects\n> that pointed to by a ref, \"for-each-ref\"'s family can naturally use\n> these atoms, but not all objects are pointed to be a ref, so \"cat-file\"\n> will not be able to use them.\n>\n> The performance for `git cat-file --batch-all-objects\n> --batch-check` on the Git repository itself with performance\n> testing tool `hyperfine` changes from 669.4 ms ±  31.1 ms to\n> 1.134 s ±  0.063 s.\n>\n> The performance for `git cat-file --batch-all-objects --batch\n>>/dev/null` on the Git repository itself with performance testing\n> tool `time` change from \"27.37s user 0.29s system 98% cpu 28.089\n> total\" to \"33.69s user 1.54s system 87% cpu 40.258 total\".\n\nThis new feature is really nice, but that's a really bad performance\nregression. A lot of software in the wild relies on \"cat-file --batch\"\nto be *the* performant interface to git for mass-extrction of object\ndata.\n\nThat's in increase of ~70% and ~20%, respectively. Have you dug into\n(e.g. with a profiler) where we're now spending all this time?\n"},{"id":"429048","messageId":"CAP8UFD0P5eiwxKvot_5+izEjscyGw+F4QFN72BmgHNhi9V5S1w@mail.gmail.com","threadId":"56038","inReplyTo":"877di8al8n.fsf@evledraar.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2021-07-02T13:45:13Z","receivedAt":"2021-07-02T13:45:27Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Fri, Jul 2, 2021 at 3:39 PM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:\n>\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n\n> > The performance for `git cat-file --batch-all-objects\n> > --batch-check` on the Git repository itself with performance\n> > testing tool `hyperfine` changes from 669.4 ms ±  31.1 ms to\n> > 1.134 s ±  0.063 s.\n> >\n> > The performance for `git cat-file --batch-all-objects --batch\n> >>/dev/null` on the Git repository itself with performance testing\n> > tool `time` change from \"27.37s user 0.29s system 98% cpu 28.089\n> > total\" to \"33.69s user 1.54s system 87% cpu 40.258 total\".\n>\n> This new feature is really nice, but that's a really bad performance\n> regression. A lot of software in the wild relies on \"cat-file --batch\"\n> to be *the* performant interface to git for mass-extrction of object\n> data.\n>\n> That's in increase of ~70% and ~20%, respectively. Have you dug into\n> (e.g. with a profiler) where we're now spending all this time?\n\nYeah, I think it's better to discuss this performance issue as part of\nthe discussion of this series, instead of as part of the discussion of\nthe blog posts.\n"},{"id":"429058","messageId":"xmqqy2aopwg2.fsf@gitster.g","threadId":"56038","inReplyTo":"CAOLTT8Qj0BO35+gTFdZ4jkNP=n8TarQX+s0PeCz2i+5yCCj-4w@mail.gmail.com","subject":"Re: [PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-07-02T15:27:57Z","receivedAt":"2021-07-02T15:28:02Z","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>> These global variables are problematic.  The places that check them\n>> are quite isolated, and it makes me wonder why we cannot pass a\n>> format instance through the callchain down to get_object().\n>\n> If we pass such two options In such a deep call chain, it seems not\n> worth to do it.\n\nOK.  After all, ref-filter.c has too many existing global variables\n(starting from the used_atom[] array) that makes it impossible to be\na proper API that can be used to handle more than one invocations at\nonce, so two more globals aren't making it any more hopeless than it\ncurrently is.  If we wanted to make the API reentrant, we'd need a\nbigger change anyway.\n\n> Or just use \"static\" prefix?\n\nYes, that is needed regardless to pass \"make sparse\".\n\nTShanks.\n"},{"id":"429067","messageId":"CAPig+cQinUsEZqEDb2_zhi37SdYuBCVEznmFbXPJuFB5cFznKg@mail.gmail.com","threadId":"56038","inReplyTo":"87czs0alhg.fsf@evledraar.gmail.com","subject":"Re: [PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2021-07-02T19:28:33Z","receivedAt":"2021-07-02T19:28:49Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Jul 2, 2021 at 9:33 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n> > +             if ((!format->cat_file_mode && used_atom[at].atom_type == ATOM_REST) ||\n> > +                 (format->cat_file_mode && (used_atom[at].atom_type == ATOM_FLAG ||\n> > +                                            used_atom[at].atom_type == ATOM_HEAD ||\n> > +                                            used_atom[at].atom_type == ATOM_PUSH ||\n> > +                                            used_atom[at].atom_type == ATOM_REFNAME ||\n> > +                                            used_atom[at].atom_type == ATOM_SYMREF ||\n> > +                                            used_atom[at].atom_type == ATOM_UPSTREAM ||\n> > +                                            used_atom[at].atom_type == ATOM_WORKTREEPATH)))\n> > +                     die(_(\"this command reject atom %%(%.*s)\"), (int)(ep - sp - 2), sp + 2);\n>\n> Maybe it was brought up already, but those ATOM_* are mostly continuous\n> where they're declared, Maybe worth making it so having a comment like:\n>\n>     /* These need to be in order, used in verify_ref_format */\n>\n> And then just doing:\n>\n>     used_atom[at].atom_type >= FIRST_OF_THOSE &&\n>     used_atom[at].atom_type >= LAST_OF_THOSE\n>\n> Maybe it's too magical and peeking under the hood of an enum...\n\nAside from the potential maintenance burden of the `atom_type >= ...\n&& atom_type <= ...` approach, another problem is that it increases\ncognitive load. The reader has to go reference the enum to fully\nunderstand the cases to which this code applies. On the other hand,\nthe way the patch mentions the enumeration items explicitly, it is\nobvious at-a-glance to which cases the code applies. An additional\ndownside of the suggestion is that the range specified by `>=` and\n`<=` may cause some readers to think that there is some sort of\nimplicit relationship between the items in the range, which doesn't\nseem to be the case. So, I find the way it's done in the patch\npresently easier to comprehend.\n"},{"id":"429082","messageId":"CAP8UFD2FjY1mY=GS4ZZHiAxTpsqq7+1ywHfCvdRvJrr8Oo9-qg@mail.gmail.com","threadId":"56038","inReplyTo":"CAPig+cQinUsEZqEDb2_zhi37SdYuBCVEznmFbXPJuFB5cFznKg@mail.gmail.com","subject":"Re: [PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2021-07-02T22:11:29Z","receivedAt":"2021-07-02T22:11:50Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Fri, Jul 2, 2021 at 9:28 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>\n> Aside from the potential maintenance burden of the `atom_type >= ...\n> && atom_type <= ...` approach, another problem is that it increases\n> cognitive load. The reader has to go reference the enum to fully\n> understand the cases to which this code applies. On the other hand,\n> the way the patch mentions the enumeration items explicitly, it is\n> obvious at-a-glance to which cases the code applies. An additional\n> downside of the suggestion is that the range specified by `>=` and\n> `<=` may cause some readers to think that there is some sort of\n> implicit relationship between the items in the range, which doesn't\n> seem to be the case. So, I find the way it's done in the patch\n> presently easier to comprehend.\n\nI agree that it's less cognitive load, but maybe it could be improved\nusing a separate function like:\n\nstatic int reject_atom(int cat_file_mode, enum atom_type atom_type)\n{\n    if (!cat_file_mode)\n        return atom_type == ATOM_REST;\n\n    /* cat_file_mode */\n    switch (atom_type) {\n    case ATOM_FLAG:\n    case ATOM_HEAD:\n    case ATOM_PUSH:\n    case ATOM_REFNAME:\n    case ATOM_SYMREF:\n    case ATOM_UPSTREAM:\n    case ATOM_WORKTREEPATH:\n        return 1;\n    default:\n        return 0;\n    }\n}\n"},{"id":"429098","messageId":"CAOLTT8QjGekriRWFp_HM0Cqiz4TYB_=cN7bBxhCSdZqzg4BG0w@mail.gmail.com","threadId":"56038","inReplyTo":"87im1salrd.fsf@evledraar.gmail.com","subject":"Re: [PATCH 02/15] [GSOC] ref-filter: add %(raw) atom","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T05:14:09Z","receivedAt":"2021-07-03T05:14: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> 于2021年7月2日周五 下午9:27写道：\n>\n>\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n>\n> > @@ -1362,17 +1398,29 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp\n> >       const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;\n> >       size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;\n> >       void *buf = data->content;\n> > +     unsigned long buf_size = data->size;\n>\n> Let's declare this.\n>\n> >       for (i = 0; i < used_atom_cnt; i++) {\n> >               struct used_atom *atom = &used_atom[i];\n> >               const char *name = atom->name;\n> >               struct atom_value *v = &val[i];\n> > +             enum atom_type atom_type = atom->atom_type;\n> >\n> >               if (!!deref != (*name == '*'))\n> >                       continue;\n> >               if (deref)\n> >                       name++;\n> >\n> > +             if (atom_type == ATOM_RAW) {\n>\n> Here, where it's going to be used.\n>\n\nYeah, buf_size is only used here.\n\n> > diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh\n> > index 9e0214076b4..18554f62d94 100755\n> > --- a/t/t6300-for-each-ref.sh\n> > +++ b/t/t6300-for-each-ref.sh\n> > @@ -130,6 +130,8 @@ test_atom head parent:short=10 ''\n> >  test_atom head numparent 0\n> >  test_atom head object ''\n> >  test_atom head type ''\n> > +test_atom head raw \"$(git cat-file commit refs/heads/main)\n> > +\"\n>\n> Extra stay line here, is the \\n intentional?\n\nI think so. `\\n` is necessary.\nIn cmd_for_each_ref(), there have a  extern \"putchar('\\n');\"\nIn cat_one_file(), write_or_die() output origin data of the object.\n\nThanks.\n--\nZheNing Hu\n"},{"id":"429099","messageId":"CAOLTT8RK4y76XAzFf+X=Dh7ysO4a-2-F08hdaof8gYWL8Kuv3w@mail.gmail.com","threadId":"56038","inReplyTo":"87fswwalmv.fsf@evledraar.gmail.com","subject":"Re: [PATCH 03/15] [GSOC] ref-filter: --format=%(raw) re-support --perl","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T05:14:54Z","receivedAt":"2021-07-03T05:15:03Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2021年7月2日周五 下午9:30写道：\n>\n>\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Because the perl language can handle binary data correctly,\n> > add the function perl_quote_buf_with_len(), which can specify\n> > the length of the data and prevent the data from being truncated\n> > at '\\0' to help `--format=\"%(raw)\"` re-support `--perl`.\n> >\n> > Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n>\n> Thanks!\n>\n> > -test_expect_success '%(raw) with --perl must fail' '\n> > -     test_must_fail git for-each-ref --format=\"%(raw)\" --perl\n> > +test_expect_success '%(raw) with --perl' '\n> > +     git for-each-ref --format=\"\\$name= %(raw);\n> > +print \\\"\\$name\\\"\" refs/myblobs/blob1 --perl | perl > actual &&\n> > +     cmp blob1 actual &&\n> > +     git for-each-ref --format=\"\\$name= %(raw);\n> > +print \\\"\\$name\\\"\" refs/myblobs/blob3 --perl | perl > actual &&\n> > +     cmp blob3 actual &&\n> > +     git for-each-ref --format=\"\\$name= %(raw);\n> > +print \\\"\\$name\\\"\" refs/myblobs/blob8 --perl | perl > actual &&\n> > +     cmp blob8 actual &&\n> > +     git for-each-ref --format=\"\\$name= %(raw);\n> > +print \\\"\\$name\\\"\" refs/myblobs/first --perl | perl > actual &&\n> > +     cmp one actual &&\n> > +     git cat-file tree refs/mytrees/first > expected &&\n> > +     git for-each-ref --format=\"\\$name= %(raw);\n> > +print \\\"\\$name\\\"\" refs/mytrees/first --perl | perl > actual &&\n> > +     cmp expected actual\n> >  '\n> >\n> >  test_expect_success '%(raw) with --shell must fail' '\n>\n>\n> Nit: Formatting can be \"| perl >actual\", not \"| perl > actual\", like the\n> usual style...\n\nYeah, thanks.\n--\nZheNing Hu\n"},{"id":"429100","messageId":"CAOLTT8SD1eSNy1w8HBt0Z2BA=YqiUYrWsnAA9cDhBOdff=U=Yg@mail.gmail.com","threadId":"56038","inReplyTo":"87a6n4alfk.fsf@evledraar.gmail.com","subject":"Re: [PATCH 10/15] [GSOC] cat-file: add has_object_file() check","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T05:50:05Z","receivedAt":"2021-07-03T05:51:32Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2021年7月2日周五 下午9:34写道：\n>\n>\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > Use `has_object_file()` in `batch_one_object()` to check\n> > whether the input object exists. This can help us reject\n> > the missing oid when we let `cat-file --batch` use ref-filter\n> > logic later.\n> >\n> > Mentored-by: Christian Couder <christian.couder@gmail.com>\n> > Mentored-by: Hariom Verma <hariom18599@gmail.com>\n> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>\n> > ---\n> >  builtin/cat-file.c | 7 +++++++\n> >  1 file changed, 7 insertions(+)\n> >\n> > diff --git a/builtin/cat-file.c b/builtin/cat-file.c\n> > index 243fe6844bc..59a86412fd0 100644\n> > --- a/builtin/cat-file.c\n> > +++ b/builtin/cat-file.c\n> > @@ -428,6 +428,13 @@ static void batch_one_object(const char *obj_name,\n> >               return;\n> >       }\n> >\n> > +     if (!has_object_file(&data->oid)) {\n> > +             printf(\"%s missing\\n\",\n> > +                    obj_name ? obj_name : oid_to_hex(&data->oid));\n> > +             fflush(stdout);\n> > +             return;\n> > +     }\n> > +\n> >       batch_object_write(obj_name, scratch, opt, data);\n> >  }\n>\n> In 12/15 the old \"missing\" is removed, so is this strictly redundant to\n> what's in batch_object_write() for now, does it change anything?\n\nBoth of these commits are used to pass some tests for checking missing objects.\n\nIf we don't have this commit, \"empty --batch-check notices missing\nobject\" in \"t/t1006-cat-file.sh\"\nwill fail;\nIf we don't have the commit \"[GSOC] ref-filter: modify the error\nmessage and value in get_object\",\n\"bogus OFS_DELTA in packfile\" in \"t/t5313-pack-bounds-checks.sh\" will fail.\n\nThanks.\n--\nZheNing Hu\n"},{"id":"429102","messageId":"CAOLTT8SHAxtQFNYAhJcfpbv8P7k5xq2id8Bvwp8vwnLYm+2beg@mail.gmail.com","threadId":"56038","inReplyTo":"CAP8UFD2FjY1mY=GS4ZZHiAxTpsqq7+1ywHfCvdRvJrr8Oo9-qg@mail.gmail.com","subject":"Re: [PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T05:55:48Z","receivedAt":"2021-07-03T05:55:57Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Christian Couder <christian.couder@gmail.com> 于2021年7月3日周六 上午6:11写道：\n>\n> On Fri, Jul 2, 2021 at 9:28 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n> >\n> > Aside from the potential maintenance burden of the `atom_type >= ...\n> > && atom_type <= ...` approach, another problem is that it increases\n> > cognitive load. The reader has to go reference the enum to fully\n> > understand the cases to which this code applies. On the other hand,\n> > the way the patch mentions the enumeration items explicitly, it is\n> > obvious at-a-glance to which cases the code applies. An additional\n> > downside of the suggestion is that the range specified by `>=` and\n> > `<=` may cause some readers to think that there is some sort of\n> > implicit relationship between the items in the range, which doesn't\n> > seem to be the case. So, I find the way it's done in the patch\n> > presently easier to comprehend.\n>\n> I agree that it's less cognitive load, but maybe it could be improved\n> using a separate function like:\n>\n> static int reject_atom(int cat_file_mode, enum atom_type atom_type)\n> {\n>     if (!cat_file_mode)\n>         return atom_type == ATOM_REST;\n>\n>     /* cat_file_mode */\n>     switch (atom_type) {\n>     case ATOM_FLAG:\n>     case ATOM_HEAD:\n>     case ATOM_PUSH:\n>     case ATOM_REFNAME:\n>     case ATOM_SYMREF:\n>     case ATOM_UPSTREAM:\n>     case ATOM_WORKTREEPATH:\n>         return 1;\n>     default:\n>         return 0;\n>     }\n> }\n\nI agree. It is clearer to use a function to wrap origin reject atoms step.\n\nThanks.\n--\nZheNIng Hu\n"},{"id":"429103","messageId":"CAOLTT8S4XnekSCAFNgPeQZD7JU2p3bm==9vHRDxcir-K+w4rqA@mail.gmail.com","threadId":"56038","inReplyTo":"xmqqy2aopwg2.fsf@gitster.g","subject":"Re: [PATCH 14/15] [GSOC] cat-file: re-implement --textconv, --filters options","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T06:17:40Z","receivedAt":"2021-07-03T06:17:51Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Junio C Hamano <gitster@pobox.com> 于2021年7月2日周五 下午11:27写道：\n>\n> ZheNing Hu <adlternative@gmail.com> writes:\n>\n> >> These global variables are problematic.  The places that check them\n> >> are quite isolated, and it makes me wonder why we cannot pass a\n> >> format instance through the callchain down to get_object().\n> >\n> > If we pass such two options In such a deep call chain, it seems not\n> > worth to do it.\n>\n> OK.  After all, ref-filter.c has too many existing global variables\n> (starting from the used_atom[] array) that makes it impossible to be\n> a proper API that can be used to handle more than one invocations at\n> once, so two more globals aren't making it any more hopeless than it\n> currently is.  If we wanted to make the API reentrant, we'd need a\n> bigger change anyway.\n>\n\nYes, Using global variables may make the code non-reentrant.\nNow I think because the \"rest\" is already in struct ref_array_item,\nwe can emulate \"rest\", adding these two flags \"use_textconv\"\nand \"use_filter\" to struct ref_array_item. Then we can use them\nin get_object().\n\n> > Or just use \"static\" prefix?\n>\n> Yes, that is needed regardless to pass \"make sparse\".\n>\n> TShanks.\n\nThanks.\n--\nZheNIng Hu\n"},{"id":"429903","messageId":"CAOLTT8RdujpQ2uKEWPyG0HGkUz_EsONw3hEZ6YAhpmQc5rgohA@mail.gmail.com","threadId":"56038","inReplyTo":"877di8al8n.fsf@evledraar.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T11:45:30Z","receivedAt":"2021-07-03T11:45:42Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2021年7月2日周五 下午9:39写道：\n>\n>\n> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n>\n> > From: ZheNing Hu <adlternative@gmail.com>\n> >\n> > In order to let cat-file use ref-filter logic, let's do the\n> > following:\n> >\n> > 1. Change the type of member `format` in struct `batch_options`\n> > to `ref_format`, we will pass it to ref-filter later.\n> > 2. Let `batch_objects()` add atoms to format, and use\n> > `verify_ref_format()` to check atoms.\n> > 3. Use `format_ref_array_item()` in `batch_object_write()` to\n> > get the formatted data corresponding to the object. If the\n> > return value of `format_ref_array_item()` is equals to zero,\n> > use `batch_write()` to print object data; else if the return\n> > value is less than zero, use `die()` to print the error message\n> > and exit; else if return value is greater than zero, only print\n> > the error message, but don't exit.\n> > 4. Use free_ref_array_item_value() to free ref_array_item's\n> > value.\n> >\n> > Most of the atoms in `for-each-ref --format` are now supported,\n> > such as `%(tree)`, `%(parent)`, `%(author)`, `%(tagger)`, `%(if)`,\n> > `%(then)`, `%(else)`, `%(end)`. But these atoms will be rejected:\n> > `%(refname)`, `%(symref)`, `%(upstream)`, `%(push)`, `%(worktreepath)`,\n> > `%(flag)`, `%(HEAD)`, because these atoms are unique to those objects\n> > that pointed to by a ref, \"for-each-ref\"'s family can naturally use\n> > these atoms, but not all objects are pointed to be a ref, so \"cat-file\"\n> > will not be able to use them.\n> >\n> > The performance for `git cat-file --batch-all-objects\n> > --batch-check` on the Git repository itself with performance\n> > testing tool `hyperfine` changes from 669.4 ms ±  31.1 ms to\n> > 1.134 s ±  0.063 s.\n> >\n> > The performance for `git cat-file --batch-all-objects --batch\n> >>/dev/null` on the Git repository itself with performance testing\n> > tool `time` change from \"27.37s user 0.29s system 98% cpu 28.089\n> > total\" to \"33.69s user 1.54s system 87% cpu 40.258 total\".\n>\n> This new feature is really nice, but that's a really bad performance\n> regression. A lot of software in the wild relies on \"cat-file --batch\"\n> to be *the* performant interface to git for mass-extrction of object\n> data.\n>\n\nThanks, this performance is indeed worrying.\n\n> That's in increase of ~70% and ~20%, respectively. Have you dug into\n> (e.g. with a profiler) where we're now spending all this time?\n\nSee this two attachment about performance flame graph,\noid_object_info_extended() in get_object() is the key to performance\nlimitations.\n\n--\nZheNing Hu\n"},{"id":"429126","messageId":"CAOLTT8QdPXzkEak4HpHdxhSmpgDe+FEeRKeHkG8c_u7QV=BWDg@mail.gmail.com","threadId":"56038","inReplyTo":"CAOLTT8RdujpQ2uKEWPyG0HGkUz_EsONw3hEZ6YAhpmQc5rgohA@mail.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-03T14:17:38Z","receivedAt":"2021-07-03T14:17:48Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"ZheNing Hu <adlternative@gmail.com> 于2021年7月3日周六 下午7:45写道：\n>\n> See this two attachment about performance flame graph,\n> oid_object_info_extended() in get_object() is the key to performance\n> limitations.\n>\n\nWell, from the current point of view, The time of oid_object_info_extended()\nbefore and after reconstruction is similar. But after refactoring,\nthere are a lot\nof additional detections in ref-filter.\n\nE.g. append_literal() 1.68%  append_atom 1.63% ...\n\n--\nZheNing Hu\n"},{"id":"429127","messageId":"87eecf8ork.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"CAOLTT8RdujpQ2uKEWPyG0HGkUz_EsONw3hEZ6YAhpmQc5rgohA@mail.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-03T13:37:33Z","receivedAt":"2021-07-03T14:18:13Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Sat, Jul 03 2021, ZheNing Hu wrote:\n\n> Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2021年7月2日周五 下午9:39写道：\n>>\n>>\n>> On Thu, Jul 01 2021, ZheNing Hu via GitGitGadget wrote:\n>>\n>> > From: ZheNing Hu <adlternative@gmail.com>\n>> >\n>> > In order to let cat-file use ref-filter logic, let's do the\n>> > following:\n>> >\n>> > 1. Change the type of member `format` in struct `batch_options`\n>> > to `ref_format`, we will pass it to ref-filter later.\n>> > 2. Let `batch_objects()` add atoms to format, and use\n>> > `verify_ref_format()` to check atoms.\n>> > 3. Use `format_ref_array_item()` in `batch_object_write()` to\n>> > get the formatted data corresponding to the object. If the\n>> > return value of `format_ref_array_item()` is equals to zero,\n>> > use `batch_write()` to print object data; else if the return\n>> > value is less than zero, use `die()` to print the error message\n>> > and exit; else if return value is greater than zero, only print\n>> > the error message, but don't exit.\n>> > 4. Use free_ref_array_item_value() to free ref_array_item's\n>> > value.\n>> >\n>> > Most of the atoms in `for-each-ref --format` are now supported,\n>> > such as `%(tree)`, `%(parent)`, `%(author)`, `%(tagger)`, `%(if)`,\n>> > `%(then)`, `%(else)`, `%(end)`. But these atoms will be rejected:\n>> > `%(refname)`, `%(symref)`, `%(upstream)`, `%(push)`, `%(worktreepath)`,\n>> > `%(flag)`, `%(HEAD)`, because these atoms are unique to those objects\n>> > that pointed to by a ref, \"for-each-ref\"'s family can naturally use\n>> > these atoms, but not all objects are pointed to be a ref, so \"cat-file\"\n>> > will not be able to use them.\n>> >\n>> > The performance for `git cat-file --batch-all-objects\n>> > --batch-check` on the Git repository itself with performance\n>> > testing tool `hyperfine` changes from 669.4 ms ±  31.1 ms to\n>> > 1.134 s ±  0.063 s.\n>> >\n>> > The performance for `git cat-file --batch-all-objects --batch\n>> >>/dev/null` on the Git repository itself with performance testing\n>> > tool `time` change from \"27.37s user 0.29s system 98% cpu 28.089\n>> > total\" to \"33.69s user 1.54s system 87% cpu 40.258 total\".\n>>\n>> This new feature is really nice, but that's a really bad performance\n>> regression. A lot of software in the wild relies on \"cat-file --batch\"\n>> to be *the* performant interface to git for mass-extrction of object\n>> data.\n>>\n>\n> Thanks, this performance is indeed worrying.\n>\n>> That's in increase of ~70% and ~20%, respectively. Have you dug into\n>> (e.g. with a profiler) where we're now spending all this time?\n>\n> See this two attachment about performance flame graph,\n> oid_object_info_extended() in get_object() is the key to performance\n> limitations.\n\nMost of the problem, although this may not entirely fix the performance\nregression, is that you're either looking up everything twice now, or\ntaking a much more expensive path.\n\nI think using gprof is probably much more handy here. See [1. I did a\n`git rev-list --all >rla` and ran that piped into 'git cat-file --batch'\nwith/without your pathces. Results:\n    \n    $ gprof ./git-master ./gmon-master.out | head -n 10\n    Flat profile:\n    \n    Each sample counts as 0.01 seconds.\n      %   cumulative   self              self     total           \n     time   seconds   seconds    calls  ms/call  ms/call  name    \n     14.29      0.02     0.02   475186     0.00     0.00  nth_packed_object_offset\n     14.29      0.04     0.02   237835     0.00     0.00  hash_to_hex_algop_r\n      7.14      0.05     0.01  5220425     0.00     0.00  hashcmp_algop\n      7.14      0.06     0.01  4757120     0.00     0.00  hex2chr\n      7.14      0.07     0.01  1732023     0.00     0.00  find_entry_ptr\n\nAnd:\n    \n    $ gprof ./git-new ./gmon-new.out |head -n 10\n    Flat profile:\n    \n    Each sample counts as 0.01 seconds.\n      %   cumulative   self              self     total\n     time   seconds   seconds    calls  ms/call  ms/call  name\n      7.32      0.06     0.06   764570     0.00     0.00  lookup_object\n      7.32      0.12     0.06   237835     0.00     0.00  parse_commit_date\n      4.88      0.16     0.04   712779     0.00     0.00  nth_packed_object_offset\n      3.66      0.19     0.03   964574     0.00     0.00  bsearch_hash\n      3.66      0.22     0.03   237835     0.00     0.00  grab_sub_body_contents\n\nIf you e.g. make lookup_object() simply die when it's called you'll see\nthat before we don't call it at all, after your patch it's our #1\nfunction.\n\nBefore when we have the simplest case of writing out an object this is\nour callstack:\n    \n    (gdb) bt\n    #0  batch_write (opt=0x7fffffffde50, data=0x555555ab9470, len=52) at builtin/cat-file.c:298\n    #1  0x000055555558b160 in batch_object_write (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0,\n        opt=0x7fffffffde50, data=0x7fffffffd7f0) at builtin/cat-file.c:375\n    #2  0x000055555558b36e in batch_one_object (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0, opt=0x7fffffffde50,\n        data=0x7fffffffd7f0) at builtin/cat-file.c:431\n    #3  0x000055555558b8ed in batch_objects (opt=0x7fffffffde50) at builtin/cat-file.c:588\n    #4  0x000055555558c0d3 in cmd_cat_file (argc=0, argv=0x7fffffffe1e0, prefix=0x0) at builtin/cat-file.c:716\n    #5  0x0000555555573adb in run_builtin (p=0x555555941870 <commands+240>, argc=2, argv=0x7fffffffe1e0) at git.c:461\n    #6  0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1e0) at git.c:714\n    #7  0x0000555555574182 in run_argv (argcp=0x7fffffffe08c, argv=0x7fffffffe080) at git.c:781\n    #8  0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1e0) at git.c:912\n    #9  0x000055555565b508 in main (argc=3, argv=0x7fffffffe1d8) at common-main.c:52\n\nAfter (well, here we're not even to writing it, just looking it up), the\nBUG() is my addition:\n    \n    (gdb) bt\n    #0  BUG_fl (file=0x5555558ade71 \"object.c\", line=91, fmt=0x5555558ade6e \"yo\") at usage.c:290\n    #1  0x00005555557441ca in lookup_object (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at object.c:91\n    #2  0x000055555569dfc8 in lookup_commit (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at commit.c:62\n    #3  0x00005555557445f5 in parse_object_buffer (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>, type=OBJ_COMMIT, size=342, buffer=0x555555ab48e0,\n        eaten_p=0x7fffffffd36c) at object.c:215\n    #4  0x0000555555785094 in get_object (ref=0x7fffffffd6b0, deref=0, obj=0x7fffffffd520, oi=0x555555975160 <oi>, err=0x7fffffffd860) at ref-filter.c:1803\n    #5  0x0000555555785c99 in populate_value (ref=0x7fffffffd6b0, err=0x7fffffffd860) at ref-filter.c:2030\n    #6  0x0000555555785d7b in get_ref_atom_value (ref=0x7fffffffd6b0, atom=0, v=0x7fffffffd628, err=0x7fffffffd860) at ref-filter.c:2064\n    #7  0x000055555578742f in format_ref_array_item (info=0x7fffffffd6b0, format=0x7fffffffde30, final_buf=0x7fffffffd880, error_buf=0x7fffffffd860)\n        at ref-filter.c:2659\n    #8  0x000055555558ab1c in batch_object_write (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880,\n        err=0x7fffffffd860, opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:225\n    #9  0x000055555558ade5 in batch_one_object (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880, err=0x7fffffffd860,\n        opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:298\n    #10 0x000055555558b394 in batch_objects (batch=0x7fffffffde10, options=0x7fffffffd900) at builtin/cat-file.c:458\n    #11 0x000055555558bbd5 in cmd_cat_file (argc=0, argv=0x7fffffffe1d0, prefix=0x0) at builtin/cat-file.c:585\n    #12 0x0000555555573adb in run_builtin (p=0x555555942850 <commands+240>, argc=2, argv=0x7fffffffe1d0) at git.c:461\n    #13 0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1d0) at git.c:714\n    #14 0x0000555555574182 in run_argv (argcp=0x7fffffffe07c, argv=0x7fffffffe070) at git.c:781\n    #15 0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1d0) at git.c:912\n    #16 0x000055555565afc1 in main (argc=3, argv=0x7fffffffe1c8) at common-main.c:52\n\nI.e. before in batch_object_write() we could use a cheap path of doing\noid_object_info_extended() and directly emitting the content. With your\nversion we're all the way down to parse_object_buffer(). Meaning that\nwe're going to be creating a \"struct commit\" or whatever if we're\nlooking at a commit, just to print out the raw contents.\n\nI think the best next step here is to add a t/perf/t1006-cat-file.sh\ntest to stress these various cases, i.e. a plain --batch without a\nformat, with format, with --batch-all-objects etc. Try to then run that\non each of your commits against the preceding one and see if/when you\nhave regressions.\n\nAside from any double-lookups etc, the problem is also that you're\ntrying to handle a really general case (e.g. with textconv) in a\ncodepath that needs to be really fast. If anything we should be\ninserting some more more optimization shortcuts for common cases into\nit. E.g. I was able to trivially speed up 'cat-file --batch-check' on\n\"master\" by hardcoding a path for our default format (patch at the end\nof this mail):\n    \n    # passed all 2 test(s)\n    1..2\n    Test                             origin/master     HEAD\n    -------------------------------------------------------------------------\n    1006.2: cat-file --batch-check   0.60(0.37+0.23)   0.35(0.33+0.02) -41.7%\n\nAnything that needs to handle general format patching is going to be\nslower. I think /some/ performance regression if we're using something\nthat's not just the current light strbuf_expand() probably can't be\navoided, but we could/should try to make up the difference at least for\nthe common case of --batch or --batch-check without --textconv and\nperhaps hardcode (and document that it's faster) a path for the default\nformats).\n\n1. https://sourceware.org/binutils/docs/gprof/Output.html\n\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 5ebf13359e8..775b7dd1b01 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -360,6 +360,11 @@ static void batch_object_write(const char *obj_name,\n \t\t\t       struct batch_options *opt,\n \t\t\t       struct expand_data *data)\n {\n+\tint default_format = !strcmp(opt->format, \"%(objectname) %(objecttype) %(objectsize)\");\n+\tstruct strbuf type_name = STRBUF_INIT;\n+\tif (default_format)\n+\t\tdata->info.type_name = &type_name;\n+\t\t\n \tif (!data->skip_object_info &&\n \t    oid_object_info_extended(the_repository, &data->oid, &data->info,\n \t\t\t\t     OBJECT_INFO_LOOKUP_REPLACE) < 0) {\n@@ -369,14 +374,20 @@ static void batch_object_write(const char *obj_name,\n \t\treturn;\n \t}\n \n-\tstrbuf_reset(scratch);\n-\tstrbuf_expand(scratch, opt->format, expand_format, data);\n-\tstrbuf_addch(scratch, '\\n');\n-\tbatch_write(opt, scratch->buf, scratch->len);\n-\n-\tif (opt->print_contents) {\n-\t\tprint_object_or_die(opt, data);\n-\t\tbatch_write(opt, \"\\n\", 1);\n+\tif (default_format && !opt->print_contents) {\n+\t\tfprintf(stdout, \"%s %s %\"PRIuMAX\"\\n\", oid_to_hex(&data->oid),\n+\t\t\tdata->info.type_name->buf, \n+\t\t\t(uintmax_t)*data->info.sizep);\n+\t} else {\n+\t\tstrbuf_reset(scratch);\n+\t\tstrbuf_expand(scratch, opt->format, expand_format, data);\n+\t\tstrbuf_addch(scratch, '\\n');\n+\t\tbatch_write(opt, scratch->buf, scratch->len);\n+\n+\t\tif (opt->print_contents) {\n+\t\t\tprint_object_or_die(opt, data);\n+\t\t\tbatch_write(opt, \"\\n\", 1);\n+\t\t}\n \t}\n }\n \ndiff --git a/t/perf/p1006-cat-file.sh b/t/perf/p1006-cat-file.sh\nnew file mode 100755\nindex 00000000000..a295d334715\n--- /dev/null\n+++ b/t/perf/p1006-cat-file.sh\n@@ -0,0 +1,16 @@\n+#!/bin/sh\n+\n+test_description='Basic sort performance tests'\n+. ./perf-lib.sh\n+\n+test_perf_default_repo\n+\n+test_expect_success 'setup' '\n+\tgit rev-list --all >rla\n+'\n+\n+test_perf 'cat-file --batch-check' '\n+\tgit cat-file --batch-check <rla\n+'\n+\n+test_done\n"},{"id":"429163","messageId":"CAOLTT8TM8b2o535S5Xvd+=__xPH+oLOTeooXSwQJjj3O1SCVSA@mail.gmail.com","threadId":"56038","inReplyTo":"87eecf8ork.fsf@evledraar.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2021-07-04T11:10:14Z","receivedAt":"2021-07-04T11:10: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> 于2021年7月3日周六 下午10:18写道：\n\n> Most of the problem, although this may not entirely fix the performance\n> regression, is that you're either looking up everything twice now, or\n> taking a much more expensive path.\n>\n\nYeah, In get_object(), oid_object_info_extended() is called twice when we\nwant print object's contents. The original intention is to reduce the call of\noid_object_info_extended() once when using --textconv. Should we make\n--textconv and --filters have the same logic? In this way, without using\n--textconv and --filters, we can call oid_object_info_extended() only once.\n\n> I think using gprof is probably much more handy here. See [1. I did a\n> `git rev-list --all >rla` and ran that piped into 'git cat-file --batch'\n> with/without your pathces. Results:\n>\n>     $ gprof ./git-master ./gmon-master.out | head -n 10\n>     Flat profile:\n>\n>     Each sample counts as 0.01 seconds.\n>       %   cumulative   self              self     total\n>      time   seconds   seconds    calls  ms/call  ms/call  name\n>      14.29      0.02     0.02   475186     0.00     0.00  nth_packed_object_offset\n>      14.29      0.04     0.02   237835     0.00     0.00  hash_to_hex_algop_r\n>       7.14      0.05     0.01  5220425     0.00     0.00  hashcmp_algop\n>       7.14      0.06     0.01  4757120     0.00     0.00  hex2chr\n>       7.14      0.07     0.01  1732023     0.00     0.00  find_entry_ptr\n>\n> And:\n>\n>     $ gprof ./git-new ./gmon-new.out |head -n 10\n>     Flat profile:\n>\n>     Each sample counts as 0.01 seconds.\n>       %   cumulative   self              self     total\n>      time   seconds   seconds    calls  ms/call  ms/call  name\n>       7.32      0.06     0.06   764570     0.00     0.00  lookup_object\n>       7.32      0.12     0.06   237835     0.00     0.00  parse_commit_date\n>       4.88      0.16     0.04   712779     0.00     0.00  nth_packed_object_offset\n>       3.66      0.19     0.03   964574     0.00     0.00  bsearch_hash\n>       3.66      0.22     0.03   237835     0.00     0.00  grab_sub_body_contents\n>\n\nIt seems that lookup_object() took a lot of time with the patch of my version .\n\n> If you e.g. make lookup_object() simply die when it's called you'll see\n> that before we don't call it at all, after your patch it's our #1\n> function.\n>\n> Before when we have the simplest case of writing out an object this is\n> our callstack:\n>\n>     (gdb) bt\n>     #0  batch_write (opt=0x7fffffffde50, data=0x555555ab9470, len=52) at builtin/cat-file.c:298\n>     #1  0x000055555558b160 in batch_object_write (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0,\n>         opt=0x7fffffffde50, data=0x7fffffffd7f0) at builtin/cat-file.c:375\n>     #2  0x000055555558b36e in batch_one_object (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0, opt=0x7fffffffde50,\n>         data=0x7fffffffd7f0) at builtin/cat-file.c:431\n>     #3  0x000055555558b8ed in batch_objects (opt=0x7fffffffde50) at builtin/cat-file.c:588\n>     #4  0x000055555558c0d3 in cmd_cat_file (argc=0, argv=0x7fffffffe1e0, prefix=0x0) at builtin/cat-file.c:716\n>     #5  0x0000555555573adb in run_builtin (p=0x555555941870 <commands+240>, argc=2, argv=0x7fffffffe1e0) at git.c:461\n>     #6  0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1e0) at git.c:714\n>     #7  0x0000555555574182 in run_argv (argcp=0x7fffffffe08c, argv=0x7fffffffe080) at git.c:781\n>     #8  0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1e0) at git.c:912\n>     #9  0x000055555565b508 in main (argc=3, argv=0x7fffffffe1d8) at common-main.c:52\n>\n> After (well, here we're not even to writing it, just looking it up), the\n> BUG() is my addition:\n>\n>     (gdb) bt\n>     #0  BUG_fl (file=0x5555558ade71 \"object.c\", line=91, fmt=0x5555558ade6e \"yo\") at usage.c:290\n>     #1  0x00005555557441ca in lookup_object (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at object.c:91\n>     #2  0x000055555569dfc8 in lookup_commit (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at commit.c:62\n>     #3  0x00005555557445f5 in parse_object_buffer (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>, type=OBJ_COMMIT, size=342, buffer=0x555555ab48e0,\n>         eaten_p=0x7fffffffd36c) at object.c:215\n>     #4  0x0000555555785094 in get_object (ref=0x7fffffffd6b0, deref=0, obj=0x7fffffffd520, oi=0x555555975160 <oi>, err=0x7fffffffd860) at ref-filter.c:1803\n>     #5  0x0000555555785c99 in populate_value (ref=0x7fffffffd6b0, err=0x7fffffffd860) at ref-filter.c:2030\n>     #6  0x0000555555785d7b in get_ref_atom_value (ref=0x7fffffffd6b0, atom=0, v=0x7fffffffd628, err=0x7fffffffd860) at ref-filter.c:2064\n>     #7  0x000055555578742f in format_ref_array_item (info=0x7fffffffd6b0, format=0x7fffffffde30, final_buf=0x7fffffffd880, error_buf=0x7fffffffd860)\n>         at ref-filter.c:2659\n>     #8  0x000055555558ab1c in batch_object_write (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880,\n>         err=0x7fffffffd860, opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:225\n>     #9  0x000055555558ade5 in batch_one_object (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880, err=0x7fffffffd860,\n>         opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:298\n>     #10 0x000055555558b394 in batch_objects (batch=0x7fffffffde10, options=0x7fffffffd900) at builtin/cat-file.c:458\n>     #11 0x000055555558bbd5 in cmd_cat_file (argc=0, argv=0x7fffffffe1d0, prefix=0x0) at builtin/cat-file.c:585\n>     #12 0x0000555555573adb in run_builtin (p=0x555555942850 <commands+240>, argc=2, argv=0x7fffffffe1d0) at git.c:461\n>     #13 0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1d0) at git.c:714\n>     #14 0x0000555555574182 in run_argv (argcp=0x7fffffffe07c, argv=0x7fffffffe070) at git.c:781\n>     #15 0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1d0) at git.c:912\n>     #16 0x000055555565afc1 in main (argc=3, argv=0x7fffffffe1c8) at common-main.c:52\n>\n\nIt seems that the call stack is very deep after using my version.\n\n> I.e. before in batch_object_write() we could use a cheap path of doing\n> oid_object_info_extended() and directly emitting the content. With your\n> version we're all the way down to parse_object_buffer(). Meaning that\n> we're going to be creating a \"struct commit\" or whatever if we're\n> looking at a commit, just to print out the raw contents.\n>\n\nI agree that the logic in ref-filter is too complicated in order to be\nable to print\nthe structured object data.\n\n> I think the best next step here is to add a t/perf/t1006-cat-file.sh\n> test to stress these various cases, i.e. a plain --batch without a\n> format, with format, with --batch-all-objects etc. Try to then run that\n> on each of your commits against the preceding one and see if/when you\n> have regressions.\n>\n\nMake sence.\n\n> Aside from any double-lookups etc, the problem is also that you're\n> trying to handle a really general case (e.g. with textconv) in a\n\nWell, \"--textconv\" is a common situation? Here I may not be sure which\nscenarios where the upper application calls \"git cat-file --batch\" are the\nmost common.\n\n> codepath that needs to be really fast. If anything we should be\n> inserting some more more optimization shortcuts for common cases into\n> it. E.g. I was able to trivially speed up 'cat-file --batch-check' on\n> \"master\" by hardcoding a path for our default format (patch at the end\n> of this mail):\n>\n>     # passed all 2 test(s)\n>     1..2\n>     Test                             origin/master     HEAD\n>     -------------------------------------------------------------------------\n>     1006.2: cat-file --batch-check   0.60(0.37+0.23)   0.35(0.33+0.02) -41.7%\n>\n> Anything that needs to handle general format patching is going to be\n> slower. I think /some/ performance regression if we're using something\n> that's not just the current light strbuf_expand() probably can't be\n> avoided, but we could/should try to make up the difference at least for\n> the common case of --batch or --batch-check without --textconv and\n> perhaps hardcode (and document that it's faster) a path for the default\n> formats).\n>\n\nYeah, Like the hardcode in your patch may be a solution to the performance\ndegradation. This will indeed help those upper-level applications that use the\n common case.\n\n> 1. https://sourceware.org/binutils/docs/gprof/Output.html\n>\n> diff --git a/builtin/cat-file.c b/builtin/cat-file.c\n> index 5ebf13359e8..775b7dd1b01 100644\n> --- a/builtin/cat-file.c\n> +++ b/builtin/cat-file.c\n> @@ -360,6 +360,11 @@ static void batch_object_write(const char *obj_name,\n>                                struct batch_options *opt,\n>                                struct expand_data *data)\n>  {\n> +       int default_format = !strcmp(opt->format, \"%(objectname) %(objecttype) %(objectsize)\");\n> +       struct strbuf type_name = STRBUF_INIT;\n> +       if (default_format)\n> +               data->info.type_name = &type_name;\n> +\n>         if (!data->skip_object_info &&\n>             oid_object_info_extended(the_repository, &data->oid, &data->info,\n>                                      OBJECT_INFO_LOOKUP_REPLACE) < 0) {\n> @@ -369,14 +374,20 @@ static void batch_object_write(const char *obj_name,\n>                 return;\n>         }\n>\n> -       strbuf_reset(scratch);\n> -       strbuf_expand(scratch, opt->format, expand_format, data);\n> -       strbuf_addch(scratch, '\\n');\n> -       batch_write(opt, scratch->buf, scratch->len);\n> -\n> -       if (opt->print_contents) {\n> -               print_object_or_die(opt, data);\n> -               batch_write(opt, \"\\n\", 1);\n> +       if (default_format && !opt->print_contents) {\n> +               fprintf(stdout, \"%s %s %\"PRIuMAX\"\\n\", oid_to_hex(&data->oid),\n> +                       data->info.type_name->buf,\n> +                       (uintmax_t)*data->info.sizep);\n> +       } else {\n> +               strbuf_reset(scratch);\n> +               strbuf_expand(scratch, opt->format, expand_format, data);\n> +               strbuf_addch(scratch, '\\n');\n> +               batch_write(opt, scratch->buf, scratch->len);\n> +\n> +               if (opt->print_contents) {\n> +                       print_object_or_die(opt, data);\n> +                       batch_write(opt, \"\\n\", 1);\n> +               }\n>         }\n>  }\n>\n> diff --git a/t/perf/p1006-cat-file.sh b/t/perf/p1006-cat-file.sh\n> new file mode 100755\n> index 00000000000..a295d334715\n> --- /dev/null\n> +++ b/t/perf/p1006-cat-file.sh\n> @@ -0,0 +1,16 @@\n> +#!/bin/sh\n> +\n> +test_description='Basic sort performance tests'\n> +. ./perf-lib.sh\n> +\n> +test_perf_default_repo\n> +\n> +test_expect_success 'setup' '\n> +       git rev-list --all >rla\n> +'\n> +\n> +test_perf 'cat-file --batch-check' '\n> +       git cat-file --batch-check <rla\n> +'\n> +\n> +test_done\n\nThanks.\n--\nZheNing Hu\n"},{"id":"429185","messageId":"87lf6l6xfn.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"CAOLTT8SHAxtQFNYAhJcfpbv8P7k5xq2id8Bvwp8vwnLYm+2beg@mail.gmail.com","subject":"Re: [PATCH 08/15] [GSOC] ref-filter: add cat_file_mode in struct ref_format","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-05T07:17:27Z","receivedAt":"2021-07-05T07:18:26Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Sat, Jul 03 2021, ZheNing Hu wrote:\n\n> Christian Couder <christian.couder@gmail.com> 于2021年7月3日周六 上午6:11写道：\n>>\n>> On Fri, Jul 2, 2021 at 9:28 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>> >\n>> > Aside from the potential maintenance burden of the `atom_type >= ...\n>> > && atom_type <= ...` approach, another problem is that it increases\n>> > cognitive load. The reader has to go reference the enum to fully\n>> > understand the cases to which this code applies. On the other hand,\n>> > the way the patch mentions the enumeration items explicitly, it is\n>> > obvious at-a-glance to which cases the code applies. An additional\n>> > downside of the suggestion is that the range specified by `>=` and\n>> > `<=` may cause some readers to think that there is some sort of\n>> > implicit relationship between the items in the range, which doesn't\n>> > seem to be the case. So, I find the way it's done in the patch\n>> > presently easier to comprehend.\n>>\n>> I agree that it's less cognitive load, but maybe it could be improved\n>> using a separate function like:\n>>\n>> static int reject_atom(int cat_file_mode, enum atom_type atom_type)\n>> {\n>>     if (!cat_file_mode)\n>>         return atom_type == ATOM_REST;\n>>\n>>     /* cat_file_mode */\n>>     switch (atom_type) {\n>>     case ATOM_FLAG:\n>>     case ATOM_HEAD:\n>>     case ATOM_PUSH:\n>>     case ATOM_REFNAME:\n>>     case ATOM_SYMREF:\n>>     case ATOM_UPSTREAM:\n>>     case ATOM_WORKTREEPATH:\n>>         return 1;\n>>     default:\n>>         return 0;\n>>     }\n>> }\n>\n> I agree. It is clearer to use a function to wrap origin reject atoms step.\n>\n> Thanks.\n\nYeah I guess you're both right, I just found the dense giant if to be\nhard to read, but having a helper is even better.\n"},{"id":"429187","messageId":"87im1p6x34.fsf@evledraar.gmail.com","threadId":"56038","inReplyTo":"CAOLTT8TM8b2o535S5Xvd+=__xPH+oLOTeooXSwQJjj3O1SCVSA@mail.gmail.com","subject":"Re: [PATCH 12/15] [GSOC] cat-file: reuse ref-filter logic","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-05T07:18:43Z","receivedAt":"2021-07-05T07:25:56Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Sun, Jul 04 2021, ZheNing Hu wrote:\n\n> Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2021年7月3日周六 下午10:18写道：\n>\n>> Most of the problem, although this may not entirely fix the performance\n>> regression, is that you're either looking up everything twice now, or\n>> taking a much more expensive path.\n>>\n>\n> Yeah, In get_object(), oid_object_info_extended() is called twice when we\n> want print object's contents. The original intention is to reduce the call of\n> oid_object_info_extended() once when using --textconv. Should we make\n> --textconv and --filters have the same logic? In this way, without using\n> --textconv and --filters, we can call oid_object_info_extended() only once.\n>\n>> I think using gprof is probably much more handy here. See [1. I did a\n>> `git rev-list --all >rla` and ran that piped into 'git cat-file --batch'\n>> with/without your pathces. Results:\n>>\n>>     $ gprof ./git-master ./gmon-master.out | head -n 10\n>>     Flat profile:\n>>\n>>     Each sample counts as 0.01 seconds.\n>>       %   cumulative   self              self     total\n>>      time   seconds   seconds    calls  ms/call  ms/call  name\n>>      14.29      0.02     0.02   475186     0.00     0.00  nth_packed_object_offset\n>>      14.29      0.04     0.02   237835     0.00     0.00  hash_to_hex_algop_r\n>>       7.14      0.05     0.01  5220425     0.00     0.00  hashcmp_algop\n>>       7.14      0.06     0.01  4757120     0.00     0.00  hex2chr\n>>       7.14      0.07     0.01  1732023     0.00     0.00  find_entry_ptr\n>>\n>> And:\n>>\n>>     $ gprof ./git-new ./gmon-new.out |head -n 10\n>>     Flat profile:\n>>\n>>     Each sample counts as 0.01 seconds.\n>>       %   cumulative   self              self     total\n>>      time   seconds   seconds    calls  ms/call  ms/call  name\n>>       7.32      0.06     0.06   764570     0.00     0.00  lookup_object\n>>       7.32      0.12     0.06   237835     0.00     0.00  parse_commit_date\n>>       4.88      0.16     0.04   712779     0.00     0.00  nth_packed_object_offset\n>>       3.66      0.19     0.03   964574     0.00     0.00  bsearch_hash\n>>       3.66      0.22     0.03   237835     0.00     0.00  grab_sub_body_contents\n>>\n>\n> It seems that lookup_object() took a lot of time with the patch of my version .\n>\n>> If you e.g. make lookup_object() simply die when it's called you'll see\n>> that before we don't call it at all, after your patch it's our #1\n>> function.\n>>\n>> Before when we have the simplest case of writing out an object this is\n>> our callstack:\n>>\n>>     (gdb) bt\n>>     #0  batch_write (opt=0x7fffffffde50, data=0x555555ab9470, len=52) at builtin/cat-file.c:298\n>>     #1  0x000055555558b160 in batch_object_write (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0,\n>>         opt=0x7fffffffde50, data=0x7fffffffd7f0) at builtin/cat-file.c:375\n>>     #2  0x000055555558b36e in batch_one_object (obj_name=0x55555597cef0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd8c0, opt=0x7fffffffde50,\n>>         data=0x7fffffffd7f0) at builtin/cat-file.c:431\n>>     #3  0x000055555558b8ed in batch_objects (opt=0x7fffffffde50) at builtin/cat-file.c:588\n>>     #4  0x000055555558c0d3 in cmd_cat_file (argc=0, argv=0x7fffffffe1e0, prefix=0x0) at builtin/cat-file.c:716\n>>     #5  0x0000555555573adb in run_builtin (p=0x555555941870 <commands+240>, argc=2, argv=0x7fffffffe1e0) at git.c:461\n>>     #6  0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1e0) at git.c:714\n>>     #7  0x0000555555574182 in run_argv (argcp=0x7fffffffe08c, argv=0x7fffffffe080) at git.c:781\n>>     #8  0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1e0) at git.c:912\n>>     #9  0x000055555565b508 in main (argc=3, argv=0x7fffffffe1d8) at common-main.c:52\n>>\n>> After (well, here we're not even to writing it, just looking it up), the\n>> BUG() is my addition:\n>>\n>>     (gdb) bt\n>>     #0  BUG_fl (file=0x5555558ade71 \"object.c\", line=91, fmt=0x5555558ade6e \"yo\") at usage.c:290\n>>     #1  0x00005555557441ca in lookup_object (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at object.c:91\n>>     #2  0x000055555569dfc8 in lookup_commit (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>) at commit.c:62\n>>     #3  0x00005555557445f5 in parse_object_buffer (r=0x5555559755c0 <the_repo>, oid=0x555555975160 <oi>, type=OBJ_COMMIT, size=342, buffer=0x555555ab48e0,\n>>         eaten_p=0x7fffffffd36c) at object.c:215\n>>     #4  0x0000555555785094 in get_object (ref=0x7fffffffd6b0, deref=0, obj=0x7fffffffd520, oi=0x555555975160 <oi>, err=0x7fffffffd860) at ref-filter.c:1803\n>>     #5  0x0000555555785c99 in populate_value (ref=0x7fffffffd6b0, err=0x7fffffffd860) at ref-filter.c:2030\n>>     #6  0x0000555555785d7b in get_ref_atom_value (ref=0x7fffffffd6b0, atom=0, v=0x7fffffffd628, err=0x7fffffffd860) at ref-filter.c:2064\n>>     #7  0x000055555578742f in format_ref_array_item (info=0x7fffffffd6b0, format=0x7fffffffde30, final_buf=0x7fffffffd880, error_buf=0x7fffffffd860)\n>>         at ref-filter.c:2659\n>>     #8  0x000055555558ab1c in batch_object_write (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880,\n>>         err=0x7fffffffd860, opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:225\n>>     #9  0x000055555558ade5 in batch_one_object (obj_name=0x55555597e3f0 \"504fe6b39f7747be6427f28d9ca97decf5e6cecf\", scratch=0x7fffffffd880, err=0x7fffffffd860,\n>>         opt=0x7fffffffde10, data=0x7fffffffd800) at builtin/cat-file.c:298\n>>     #10 0x000055555558b394 in batch_objects (batch=0x7fffffffde10, options=0x7fffffffd900) at builtin/cat-file.c:458\n>>     #11 0x000055555558bbd5 in cmd_cat_file (argc=0, argv=0x7fffffffe1d0, prefix=0x0) at builtin/cat-file.c:585\n>>     #12 0x0000555555573adb in run_builtin (p=0x555555942850 <commands+240>, argc=2, argv=0x7fffffffe1d0) at git.c:461\n>>     #13 0x0000555555573f00 in handle_builtin (argc=2, argv=0x7fffffffe1d0) at git.c:714\n>>     #14 0x0000555555574182 in run_argv (argcp=0x7fffffffe07c, argv=0x7fffffffe070) at git.c:781\n>>     #15 0x000055555557460f in cmd_main (argc=2, argv=0x7fffffffe1d0) at git.c:912\n>>     #16 0x000055555565afc1 in main (argc=3, argv=0x7fffffffe1c8) at common-main.c:52\n>>\n>\n> It seems that the call stack is very deep after using my version.\n>\n>> I.e. before in batch_object_write() we could use a cheap path of doing\n>> oid_object_info_extended() and directly emitting the content. With your\n>> version we're all the way down to parse_object_buffer(). Meaning that\n>> we're going to be creating a \"struct commit\" or whatever if we're\n>> looking at a commit, just to print out the raw contents.\n>>\n>\n> I agree that the logic in ref-filter is too complicated in order to be\n> able to print\n> the structured object data.\n\nWell, maybe it isn't. I think the main problem with your performance is\njust looking things up again from the object store needlessly, and\npossibly redundantly copying things around. E.g. if you have a buffer\nwith a commit object and you need to xstrdup() that, and xstrdup() the\nanswer etc, it adds up.\n\nMaybe if all that's gone the formatting will still slow things down, but\nI bet it's going to be to a much smaller extent.\n\n>> I think the best next step here is to add a t/perf/t1006-cat-file.sh\n>> test to stress these various cases, i.e. a plain --batch without a\n>> format, with format, with --batch-all-objects etc. Try to then run that\n>> on each of your commits against the preceding one and see if/when you\n>> have regressions.\n>>\n>\n> Make sence.\n>\n>> Aside from any double-lookups etc, the problem is also that you're\n>> trying to handle a really general case (e.g. with textconv) in a\n>\n> Well, \"--textconv\" is a common situation? Here I may not be sure which\n> scenarios where the upper application calls \"git cat-file --batch\" are the\n> most common.\n\nI think --textconv is much rarer. I think it's used for API purposes\ne.g. for people who are doing \\n translation, or maybe for the likes of\ngit-lfs (or was that --filters)? I'm not very familiar with it.\n\nFor what it's worth 'git cat-file --batch' is very performance sensitive\nin backend use-cases like e.g. gitlab.com's gitaly (which executes all\ngit commands for you on that site). Anything that looks up .. any object\npretty much .. is prining that object line to a persistent \"cat-file\n--batch\" process.\n\nThere was a case where some very small (unrelated to cat-file per-se)\nslowdown in that codepath ended up slowing down overall API requests for\nsome calls by >100%, because it's executed in a tight loop. E.g. if\nyou're showing N commits per page that might be N cat-file --batch\ncalls.\n"}]}