{"thread":{"id":"63437","subject":"[PATCH 0/2] merge-tree: add new --mergeability-only option","startedAt":"2025-05-10T22:02:45Z","lastAt":"2025-05-19T15:59:07Z","messageCount":29,"participants":["Elijah Newren via GitGitGadget","Junio C Hamano","Elijah Newren","Kristoffer Haugsbakk","Phillip Wood"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"517764","messageId":"pull.1920.git.1746914561.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":null,"subject":"[PATCH 0/2] merge-tree: add new --mergeability-only option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-10T22:02:39Z","receivedAt":"2025-05-10T22:02:45Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"This adds a new flag, --mergeability-only, to git merge-tree, which\nsuppresses all output and leaves only the exit status (reflecting successful\nmerge or conflict). This is useful for Git Forges in cases where they are\nonly interested in whether two branches can be merged, without needing the\nactual merge result or conflict details.\n\nThe advantage of the flag is two fold:\n\n * The merge machinery can exit once it detects the first conflict, instead\n   of continuing to compute merge result information\n * The merge machinery can avoid writing merged blobs and trees to the\n   object store when in the outer layer of the merging process (more details\n   in the first commit message).\n\nElijah Newren (2):\n  merge-ort: add a new mergeability_only option\n  merge-tree: add a new --mergeability-only flag\n\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n merge-ort.c                       | 38 +++++++++++++++++++++++++------\n merge-ort.h                       |  1 +\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 5 files changed, 98 insertions(+), 7 deletions(-)\n\n\nbase-commit: 6c0bd1fc70efaf053abe4e57c976afdc72d15377\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1920%2Fnewren%2Fmergeability-only-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1920/newren/mergeability-only-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1920\n-- \ngitgitgadget\n"},{"id":"517765","messageId":"09292804cffc41d15f1156e0b4bba4cba7ec7ca4.1746914561.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.git.1746914561.gitgitgadget@gmail.com","subject":"[PATCH 1/2] merge-ort: add a new mergeability_only option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-10T22:02:40Z","receivedAt":"2025-05-10T22:02:45Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new mergeability_only option.  This\noption allows the merge machinery to, in the outer layer of the merge:\n  * exit upon first conflict\n  * avoid writing merged blobs/trees to the object store\n\nNote that since the recursive merge of merge bases (corresponding to\ncall_depth > 0) can conflict without the outer final merge (corresponding\nto call_depth == 0) conflicting, we can't short-circuit nor avoid\nwriting merges blobs/trees to the object store during those inner\nmerges.\n\nThere is a further potential micro-optimization here.  rename/rename\nconflicts have the potential for nested conflicts even without recursive\nmerges; because of that, handle_content_merge() can be called multiple\ntimes and is done via different paths.  Currently, we only exit early in\nprocess_entries(), which is where the final handle_content_merge() is\ninvoked.  Since rename/rename conflicts have an additional earlier\nhandle_content_merge() call that can be invoked from\ndetect_and_process_renames() (via process_renames()), we could\npotentially exit earlier at that call point.  However, rename/rename\nconflicts are exceptionally rare, and feeding the extra logic through\ndidn't seem worth it.  (And, if we don't exit early at that point, then\nany resulting blobs need to be written to the store so that subsequent\nhandle_content_merge() calls trying to use the blob don't throw\nexceptions.)\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n merge-ort.c | 38 +++++++++++++++++++++++++++++++-------\n merge-ort.h |  1 +\n 2 files changed, 32 insertions(+), 7 deletions(-)\n\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 77310a4a52c9..47b3d1730ece 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -2127,6 +2127,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\t\tconst struct version_info *b,\n \t\t\t\tconst char *pathnames[3],\n \t\t\t\tconst int extra_marker_size,\n+\t\t\t\tconst int record_object,\n \t\t\t\tstruct version_info *result)\n {\n \t/*\n@@ -2214,7 +2215,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\tret = -1;\n \t\t}\n \n-\t\tif (!ret &&\n+\t\tif (!ret && record_object &&\n \t\t    write_object_file(result_buf.ptr, result_buf.size,\n \t\t\t\t      OBJ_BLOB, &result->oid)) {\n \t\t\tpath_msg(opt, ERROR_OBJECT_WRITE_FAILED, 0,\n@@ -2897,6 +2898,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\tstruct version_info merged;\n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tunsigned was_binary_blob = 0;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[1] = newpath;\n@@ -2947,6 +2949,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t\t   &side2->stages[2],\n \t\t\t\t\t\t\t   pathnames,\n \t\t\t\t\t\t\t   1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t\t   &merged);\n \t\t\tif (clean_merge < 0)\n \t\t\t\treturn -1;\n@@ -3061,6 +3064,7 @@ static int process_renames(struct merge_options *opt,\n \n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tint clean;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[other_source_index] = oldpath;\n@@ -3080,6 +3084,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t     &side2->stages[2],\n \t\t\t\t\t\t     pathnames,\n \t\t\t\t\t\t     1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t     record_object,\n \t\t\t\t\t\t     &merged);\n \t\t\tif (clean < 0)\n \t\t\t\treturn -1;\n@@ -3931,9 +3936,12 @@ static int write_completed_directory(struct merge_options *opt,\n \t\t * Write out the tree to the git object directory, and also\n \t\t * record the mode and oid in dir_info->result.\n \t\t */\n+\t\tint record_tree = (!opt->mergeability_only ||\n+\t\t\t\t   opt->priv->call_depth);\n \t\tdir_info->is_null = 0;\n \t\tdir_info->result.mode = S_IFDIR;\n-\t\tif (write_tree(&dir_info->result.oid, &info->versions, offset,\n+\t\tif (record_tree &&\n+\t\t    write_tree(&dir_info->result.oid, &info->versions, offset,\n \t\t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\t\tret = -1;\n \t}\n@@ -4231,10 +4239,13 @@ static int process_entry(struct merge_options *opt,\n \t\tstruct version_info *o = &ci->stages[0];\n \t\tstruct version_info *a = &ci->stages[1];\n \t\tstruct version_info *b = &ci->stages[2];\n+\t\tint record_object = (!opt->mergeability_only ||\n+\t\t\t\t     opt->priv->call_depth);\n \n \t\tclean_merge = handle_content_merge(opt, path, o, a, b,\n \t\t\t\t\t\t   ci->pathnames,\n \t\t\t\t\t\t   opt->priv->call_depth * 2,\n+\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t   &merged_file);\n \t\tif (clean_merge < 0)\n \t\t\treturn -1;\n@@ -4395,6 +4406,8 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\t\t\t   STRING_LIST_INIT_NODUP,\n \t\t\t\t\t\t   NULL, 0 };\n \tint ret = 0;\n+\tconst int record_tree = (!opt->mergeability_only ||\n+\t\t\t\t opt->priv->call_depth);\n \n \ttrace2_region_enter(\"merge\", \"process_entries setup\", opt->repo);\n \tif (strmap_empty(&opt->priv->paths)) {\n@@ -4454,6 +4467,12 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\tret = -1;\n \t\t\t\tgoto cleanup;\n \t\t\t};\n+\t\t\tif (!ci->merged.clean && opt->mergeability_only &&\n+\t\t\t    !opt->priv->call_depth) {\n+\t\t\t\tret = 0;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\n \t\t}\n \t}\n \ttrace2_region_leave(\"merge\", \"processing\", opt->repo);\n@@ -4468,7 +4487,8 @@ static int process_entries(struct merge_options *opt,\n \t\tfflush(stdout);\n \t\tBUG(\"dir_metadata accounting completely off; shouldn't happen\");\n \t}\n-\tif (write_tree(result_oid, &dir_metadata.versions, 0,\n+\tif (record_tree &&\n+\t    write_tree(result_oid, &dir_metadata.versions, 0,\n \t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\tret = -1;\n cleanup:\n@@ -4715,6 +4735,8 @@ void merge_display_update_messages(struct merge_options *opt,\n \n \tif (opt->record_conflict_msgs_as_headers)\n \t\tBUG(\"Either display conflict messages or record them as headers, not both\");\n+\tif (opt->mergeability_only)\n+\t\tBUG(\"Displaying conflict messages incompatible with mergeability-only checks\");\n \n \ttrace2_region_enter(\"merge\", \"display messages\", opt->repo);\n \n@@ -5171,10 +5193,12 @@ redo:\n \tresult->path_messages = &opt->priv->conflicts;\n \n \tif (result->clean >= 0) {\n-\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n-\t\tif (!result->tree)\n-\t\t\tdie(_(\"unable to read tree (%s)\"),\n-\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\tif (!opt->mergeability_only) {\n+\t\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n+\t\t\tif (!result->tree)\n+\t\t\t\tdie(_(\"unable to read tree (%s)\"),\n+\t\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\t}\n \t\t/* existence of conflicted entries implies unclean */\n \t\tresult->clean &= strmap_empty(&opt->priv->conflicted);\n \t}\ndiff --git a/merge-ort.h b/merge-ort.h\nindex 30750c03962f..6045579825da 100644\n--- a/merge-ort.h\n+++ b/merge-ort.h\n@@ -83,6 +83,7 @@ struct merge_options {\n \t/* miscellaneous control options */\n \tconst char *subtree_shift;\n \tunsigned renormalize : 1;\n+\tunsigned mergeability_only : 1; /* exit early, write fewer objects */\n \tunsigned record_conflict_msgs_as_headers : 1;\n \tconst char *msg_header_prefix;\n \n-- \ngitgitgadget\n\n"},{"id":"517766","messageId":"0b32c80d2867492b7b63f346bdf1e9401d4644ae.1746914561.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.git.1746914561.gitgitgadget@gmail.com","subject":"[PATCH 2/2] merge-tree: add a new --mergeability-only flag","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-10T22:02:41Z","receivedAt":"2025-05-10T22:02:46Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new --mergeability-only flag which\nwill make use of the new mergeability_only flag added to merge-ort in\nthe previous commit.  This option allows the merge machinery to, in the\nouter layer of the merge:\n      * exit upon first conflict\n      * avoid writing merged blobs/trees to the object store\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 3 files changed, 66 insertions(+)\n\ndiff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\nindex cf0578f9b5e8..7dcc17806191 100644\n--- a/Documentation/git-merge-tree.adoc\n+++ b/Documentation/git-merge-tree.adoc\n@@ -65,6 +65,12 @@ OPTIONS\n \tdefault is to include these messages if there are merge\n \tconflicts, and to omit them otherwise.\n \n+--mergeability-only::\n+\tDisable all output from the program.  Useful when you are only\n+\tinterested in the exit status.  Allows merge-tree to exit\n+\tearly on the first conflict it finds, and allows it to avoid\n+\twriting most objects created by merges.\n+\n --allow-unrelated-histories::\n \tmerge-tree will by default error out if the two branches specified\n \tshare no common history.  This flag can be given to override that\ndiff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\nindex 4aafa73c6155..48179d83596c 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -490,6 +490,9 @@ static int real_merge(struct merge_tree_options *o,\n \tif (result.clean < 0)\n \t\tdie(_(\"failure to merge\"));\n \n+\tif (o->merge_options.mergeability_only)\n+\t\tgoto cleanup;\n+\n \tif (show_messages == -1)\n \t\tshow_messages = !result.clean;\n \n@@ -522,6 +525,8 @@ static int real_merge(struct merge_tree_options *o,\n \t}\n \tif (o->use_stdin)\n \t\tputchar(line_termination);\n+\n+cleanup:\n \tmerge_finalize(&opt, &result);\n \tclear_merge_options(&opt);\n \treturn !result.clean; /* result.clean < 0 handled above */\n@@ -538,6 +543,7 @@ int cmd_merge_tree(int argc,\n \tint original_argc;\n \tconst char *merge_base = NULL;\n \tint ret;\n+\tint mergeability_only = 0;\n \n \tconst char * const merge_tree_usage[] = {\n \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n@@ -552,6 +558,10 @@ int cmd_merge_tree(int argc,\n \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n \t\t\t N_(\"also show informational/conflict messages\")),\n+\t\tOPT_BOOL_F(0, \"mergeability-only\",\n+\t\t\t   &mergeability_only,\n+\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n+\t\t\t   PARSE_OPT_NONEG),\n \t\tOPT_SET_INT('z', NULL, &line_termination,\n \t\t\t    N_(\"separate paths with the NUL character\"), '\\0'),\n \t\tOPT_BOOL_F(0, \"name-only\",\n@@ -583,6 +593,18 @@ int cmd_merge_tree(int argc,\n \targc = parse_options(argc, argv, prefix, mt_options,\n \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n \n+\tif (mergeability_only && o.show_messages == -1)\n+\t\to.show_messages = 0;\n+\to.merge_options.mergeability_only = mergeability_only;\n+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n+\t\t\t\t  o.show_messages, \"--messages\");\n+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n+\t\t\t\t  o.name_only, \"--name-only\");\n+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n+\t\t\t\t  o.use_stdin, \"--stdin\");\n+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n+\t\t\t\t  !line_termination, \"-z\");\n+\n \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n \tfor (size_t x = 0; x < xopts.nr; x++)\ndiff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh\nindex f9c5883a7f7c..3170830dd79d 100755\n--- a/t/t4301-merge-tree-write-tree.sh\n+++ b/t/t4301-merge-tree-write-tree.sh\n@@ -54,6 +54,25 @@ test_expect_success setup '\n \tgit commit -m first-commit\n '\n \n+test_expect_success '--mergeability-only on clean merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge is successful (exit code of 0)\n+\tgit merge-tree --write-tree --mergeability-only side1 side3 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Clean merge' '\n \tTREE_OID=$(git merge-tree --write-tree side1 side3) &&\n \tq_to_tab <<-EOF >expect &&\n@@ -72,6 +91,25 @@ test_expect_success 'Failed merge without rename detection' '\n \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n '\n \n+test_expect_success  '--mergeability-only on conflicted merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge has conflict\n+\ttest_expect_code 1 git merge-tree --write-tree --mergeability-only side1 side2 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Content merge and a few conflicts' '\n \tgit checkout side1^0 &&\n \ttest_must_fail git merge side2 &&\n-- \ngitgitgadget\n"},{"id":"517875","messageId":"xmqqplgdu5ex.fsf@gitster.g","threadId":"63437","inReplyTo":"pull.1920.git.1746914561.gitgitgadget@gmail.com","subject":"Re: [PATCH 0/2] merge-tree: add new --mergeability-only option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-12T17:04:22Z","receivedAt":"2025-05-12T17:04:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Elijah Newren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> This adds a new flag, --mergeability-only, to git merge-tree, which\n> suppresses all output and leaves only the exit status (reflecting successful\n> merge or conflict). This is useful for Git Forges in cases where they are\n> only interested in whether two branches can be merged, without needing the\n> actual merge result or conflict details.\n\nSounds useful, but wouldn't that usually called --dry-run?\n\n"},{"id":"517886","messageId":"CABPp-BEJrkq4A715pV6WY-t-M205oheNABdGHy6HLmrxhNrk+g@mail.gmail.com","threadId":"63437","inReplyTo":"xmqqplgdu5ex.fsf@gitster.g","subject":"Re: [PATCH 0/2] merge-tree: add new --mergeability-only option","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2025-05-12T17:41:01Z","receivedAt":"2025-05-12T17:41:12Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, May 12, 2025 at 10:04 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Elijah Newren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > This adds a new flag, --mergeability-only, to git merge-tree, which\n> > suppresses all output and leaves only the exit status (reflecting successful\n> > merge or conflict). This is useful for Git Forges in cases where they are\n> > only interested in whether two branches can be merged, without needing the\n> > actual merge result or conflict details.\n>\n> Sounds useful, but wouldn't that usually called --dry-run?\n\nI thought about that, but I was worried that folks would expect\n\"--dry-run\" to not make any changes.  This mode does not prevent\nwriting objects to the object store, it merely avoids it in the \"outer\nlayer\" of the merge.  More precisely, objects will still be written to\nthe object store for the merging of merge bases, and also be written\nto the object store in the case of rename/rename conflicts if the\ncontents of the files involved in the conflicting renames were also\nmodified by both sides.\n"},{"id":"517891","messageId":"xmqqldr1sn0j.fsf@gitster.g","threadId":"63437","inReplyTo":"CABPp-BEJrkq4A715pV6WY-t-M205oheNABdGHy6HLmrxhNrk+g@mail.gmail.com","subject":"Re: [PATCH 0/2] merge-tree: add new --mergeability-only option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-12T18:27:08Z","receivedAt":"2025-05-12T18:27:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Elijah Newren <newren@gmail.com> writes:\n\n> I thought about that, but I was worried that folks would expect\n> \"--dry-run\" to not make any changes.  This mode does not prevent\n> writing objects to the object store, it merely avoids it in the \"outer\n> layer\" of the merge.\n\nI think we have already precedence to call something that creates\nnew objects in the object database, as long as the resulting objects\nare not made reachable (\"git fetch --dry-run\" probably falls into\nthat category).  The idea is that it does not make a change that is\n\"observable\" by end-users (and what \"gc\" sees is not part of what\nthe users would be observaing).\n\nWe have \"--check\" (in \"git apply\"), which is an exact counterpart in\nthe patch based workflow to this thing.  It reads\n\n\tInstead of applying the patch, see if the patch is\n\tapplicable to the current working tree and/or the index\n\tfile and detects errors.  Turns off \"apply\".\n\nI feel that `apply --check` should have been `apply --dry-run`, so I\nwould not recommend calling it `--check` for `merge-tree`, though.\n\nThanks.\n"},{"id":"517895","messageId":"CABPp-BEP9Uj4o5Hd6w2N8zH3Te7tBkVWE_9ytd6y1vkKGiHZNw@mail.gmail.com","threadId":"63437","inReplyTo":"xmqqldr1sn0j.fsf@gitster.g","subject":"Re: [PATCH 0/2] merge-tree: add new --mergeability-only option","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2025-05-12T18:37:44Z","receivedAt":"2025-05-12T18:37:56Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Mon, May 12, 2025 at 11:27 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Elijah Newren <newren@gmail.com> writes:\n>\n> > I thought about that, but I was worried that folks would expect\n> > \"--dry-run\" to not make any changes.  This mode does not prevent\n> > writing objects to the object store, it merely avoids it in the \"outer\n> > layer\" of the merge.\n>\n> I think we have already precedence to call something that creates\n> new objects in the object database, as long as the resulting objects\n> are not made reachable (\"git fetch --dry-run\" probably falls into\n> that category).  The idea is that it does not make a change that is\n> \"observable\" by end-users (and what \"gc\" sees is not part of what\n> the users would be observaing).\n\nOh, I was unaware of `git fetch --dry-run` for some reason.  And its\ndocumentation even states \"without making any changes\" despite the\nfact that it downloads more objects to the object store, so it indeed\nsounds like a good precedent.\n\nI'll switch the flag name to --dry-run.  (I have a suspicion, however,\nthat the primary users of this new merge-tree flag will care about\nwhether objects are created, so I still want the documentation to call\nit out, unlike git fetch's --dry-run option.)\n\n> We have \"--check\" (in \"git apply\"), which is an exact counterpart in\n> the patch based workflow to this thing.  It reads\n>\n>         Instead of applying the patch, see if the patch is\n>         applicable to the current working tree and/or the index\n>         file and detects errors.  Turns off \"apply\".\n>\n> I feel that `apply --check` should have been `apply --dry-run`, so I\n> would not recommend calling it `--check` for `merge-tree`, though.\n\nMakes sense; thanks for the pointers.\n"},{"id":"517916","messageId":"pull.1920.v2.git.1747093322.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.git.1746914561.gitgitgadget@gmail.com","subject":"[PATCH v2 0/2] merge-tree: add new --dry-run option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-12T23:42:00Z","receivedAt":"2025-05-12T23:42:06Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"Changes since v1:\n\n * Renamed --mergeability-only flag to --dry-run, as per suggestion from\n   Junio\n * added some commit message clarifications\n\nThis adds a new flag, --dry-run, to git merge-tree, which suppresses all\noutput and leaves only the exit status (reflecting successful merge or\nconflict). This is useful for Git Forges in cases where they are only\ninterested in whether two branches can be merged, without needing the actual\nmerge result or conflict details.\n\nThe advantage of the flag is two fold:\n\n * The merge machinery can exit once it detects a conflict, instead of\n   continuing to compute merge result information\n * The merge machinery can avoid writing merged blobs and trees to the\n   object store when in the outer layer of the merging process (more details\n   in the first commit message).\n\nElijah Newren (2):\n  merge-ort: add a new mergeability_only option\n  merge-tree: add a new --dry-run flag\n\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n merge-ort.c                       | 38 +++++++++++++++++++++++++------\n merge-ort.h                       |  1 +\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 5 files changed, 98 insertions(+), 7 deletions(-)\n\n\nbase-commit: 6c0bd1fc70efaf053abe4e57c976afdc72d15377\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1920%2Fnewren%2Fmergeability-only-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1920/newren/mergeability-only-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1920\n\nRange-diff vs v1:\n\n 1:  09292804cff ! 1:  4757c4810d3 merge-ort: add a new mergeability_only option\n     @@ Commit message\n          Git Forges may be interested in whether two branches can be merged while\n          not being interested in what the resulting merge tree is nor which files\n          conflicted.  For such cases, add a new mergeability_only option.  This\n     -    option allows the merge machinery to, in the outer layer of the merge:\n     -      * exit upon first conflict\n     -      * avoid writing merged blobs/trees to the object store\n     +    option allows the merge machinery to, in the \"outer layer\" of the merge:\n     +      * exit upon first[-ish] conflict\n     +      * avoid (not prevent) writing merged blobs/trees to the object store\n     +\n     +    I have a number of qualifiers there, so let me explain each:\n     +\n     +    \"outer layer\":\n      \n          Note that since the recursive merge of merge bases (corresponding to\n     -    call_depth > 0) can conflict without the outer final merge (corresponding\n     -    to call_depth == 0) conflicting, we can't short-circuit nor avoid\n     -    writing merges blobs/trees to the object store during those inner\n     -    merges.\n     +    call_depth > 0) can conflict without the outer final merge\n     +    (corresponding to call_depth == 0) conflicting, we can't short-circuit\n     +    nor avoid writing merged blobs/trees to the object store during those\n     +    inner merges.\n     +\n     +    \"first-ish conflict\":\n     +\n     +    The current patch only exits early from process_entries() on the first\n     +    conflict it detects, but conflicts could have been detected in a\n     +    previous function call, namely detect_and_process_renames().  However:\n     +      * conflicts detected by detect_and_process_renames() are quite rare\n     +        conflict types\n     +      * the detection would still come after regular rename detection\n     +        (which is the expensive part of detect_and_process_renames()), so\n     +        it is not saving us much in computation time given that\n     +        process_entries() directly follows detect_and_process_renames()\n     +      * [this overlaps with the next bullet point] process_entries() is the\n     +        place where virtually all object writing occurs (object writing is\n     +        sometimes more of a concern for Forges than computation time), so\n     +        exiting early here isn't saving us much in object writes either\n     +      * the code changes needed to handle an earlier exit are slightly\n     +        more invasive in detect_and_process_renames() than for\n     +        process_entries().\n     +    Given the rareness of the even earlier conflicts, the limited savings\n     +    we'd get from exiting even earlier, and in an attempt to keep this\n     +    patch simpler, we don't guarantee that we actually exit on the first\n     +    conflict detected.  We can always revisit this decision later if we\n     +    decide that a further micro-optimization to exit slightly earlier in\n     +    rare cases is worthwhile.\n     +\n     +    \"avoid (not prevent) writing objects\":\n      \n     -    There is a further potential micro-optimization here.  rename/rename\n     -    conflicts have the potential for nested conflicts even without recursive\n     -    merges; because of that, handle_content_merge() can be called multiple\n     -    times and is done via different paths.  Currently, we only exit early in\n     -    process_entries(), which is where the final handle_content_merge() is\n     -    invoked.  Since rename/rename conflicts have an additional earlier\n     -    handle_content_merge() call that can be invoked from\n     -    detect_and_process_renames() (via process_renames()), we could\n     -    potentially exit earlier at that call point.  However, rename/rename\n     -    conflicts are exceptionally rare, and feeding the extra logic through\n     -    didn't seem worth it.  (And, if we don't exit early at that point, then\n     -    any resulting blobs need to be written to the store so that subsequent\n     -    handle_content_merge() calls trying to use the blob don't throw\n     -    exceptions.)\n     +    The detect_and_process_renames() call can also write objects to the\n     +    object store, when rename/rename conflicts involve one (or more) files\n     +    that have also been modified on both sides.  Because of this alternate\n     +    call path leading to handle_content_merges(), our \"early exit\" does not\n     +    prevent writing objects entirely, even within the \"outer layer\"\n     +    (i.e. even within call_depth == 0).  I figure that's fine though, since\n     +    we're already writing objects for the inner merges (i.e. for call_depth\n     +    > 0), which are likely going to represent vastly more objects than files\n     +    involved in rename/rename+modify/modify cases in the outer merge, on\n     +    average.\n      \n          Signed-off-by: Elijah Newren <newren@gmail.com>\n      \n 2:  0b32c80d286 ! 2:  1d18ab7feb8 merge-tree: add a new --mergeability-only flag\n     @@ Metadata\n      Author: Elijah Newren <newren@gmail.com>\n      \n       ## Commit message ##\n     -    merge-tree: add a new --mergeability-only flag\n     +    merge-tree: add a new --dry-run flag\n      \n          Git Forges may be interested in whether two branches can be merged while\n          not being interested in what the resulting merge tree is nor which files\n     -    conflicted.  For such cases, add a new --mergeability-only flag which\n     +    conflicted.  For such cases, add a new --dry-run flag which\n          will make use of the new mergeability_only flag added to merge-ort in\n          the previous commit.  This option allows the merge machinery to, in the\n          outer layer of the merge:\n     -          * exit upon first conflict\n     -          * avoid writing merged blobs/trees to the object store\n     +        * exit early when a conflict is detected\n     +        * avoid writing (most) merged blobs/trees to the object store\n      \n          Signed-off-by: Elijah Newren <newren@gmail.com>\n      \n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \tint original_argc;\n       \tconst char *merge_base = NULL;\n       \tint ret;\n     -+\tint mergeability_only = 0;\n     ++\tint dry_run = 0;\n       \n       \tconst char * const merge_tree_usage[] = {\n       \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n       \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n       \t\t\t N_(\"also show informational/conflict messages\")),\n     -+\t\tOPT_BOOL_F(0, \"mergeability-only\",\n     -+\t\t\t   &mergeability_only,\n     ++\t\tOPT_BOOL_F(0, \"dry-run\",\n     ++\t\t\t   &dry_run,\n      +\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n      +\t\t\t   PARSE_OPT_NONEG),\n       \t\tOPT_SET_INT('z', NULL, &line_termination,\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \targc = parse_options(argc, argv, prefix, mt_options,\n       \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n       \n     -+\tif (mergeability_only && o.show_messages == -1)\n     ++\tif (dry_run && o.show_messages == -1)\n      +\t\to.show_messages = 0;\n     -+\to.merge_options.mergeability_only = mergeability_only;\n     -+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n     ++\to.merge_options.mergeability_only = dry_run;\n     ++\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n      +\t\t\t\t  o.show_messages, \"--messages\");\n     -+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n      +\t\t\t\t  o.name_only, \"--name-only\");\n     -+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n      +\t\t\t\t  o.use_stdin, \"--stdin\");\n     -+\tdie_for_incompatible_opt2(mergeability_only, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n      +\t\t\t\t  !line_termination, \"-z\");\n      +\n       \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n       \tgit commit -m first-commit\n       '\n       \n     -+test_expect_success '--mergeability-only on clean merge' '\n     ++test_expect_success '--dry-run on clean merge' '\n      +\t# Get rid of loose objects to start with\n      +\tgit gc &&\n      +\techo \"0 objects, 0 kilobytes\" >expect &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n      +\ttest_cmp expect actual &&\n      +\n      +\t# Ensure merge is successful (exit code of 0)\n     -+\tgit merge-tree --write-tree --mergeability-only side1 side3 >output &&\n     ++\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n      +\n      +\t# Ensure there is no output\n      +\ttest_must_be_empty output &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n       \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n       '\n       \n     -+test_expect_success  '--mergeability-only on conflicted merge' '\n     ++test_expect_success  '--dry-run on conflicted merge' '\n      +\t# Get rid of loose objects to start with\n      +\tgit gc &&\n      +\techo \"0 objects, 0 kilobytes\" >expect &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n      +\ttest_cmp expect actual &&\n      +\n      +\t# Ensure merge has conflict\n     -+\ttest_expect_code 1 git merge-tree --write-tree --mergeability-only side1 side2 >output &&\n     ++\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n      +\n      +\t# Ensure there is no output\n      +\ttest_must_be_empty output &&\n\n-- \ngitgitgadget\n"},{"id":"517917","messageId":"4757c4810d3421b6c41723da837cab14824239e2.1747093322.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v2.git.1747093322.gitgitgadget@gmail.com","subject":"[PATCH v2 1/2] merge-ort: add a new mergeability_only option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-12T23:42:01Z","receivedAt":"2025-05-12T23:42:07Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new mergeability_only option.  This\noption allows the merge machinery to, in the \"outer layer\" of the merge:\n  * exit upon first[-ish] conflict\n  * avoid (not prevent) writing merged blobs/trees to the object store\n\nI have a number of qualifiers there, so let me explain each:\n\n\"outer layer\":\n\nNote that since the recursive merge of merge bases (corresponding to\ncall_depth > 0) can conflict without the outer final merge\n(corresponding to call_depth == 0) conflicting, we can't short-circuit\nnor avoid writing merged blobs/trees to the object store during those\ninner merges.\n\n\"first-ish conflict\":\n\nThe current patch only exits early from process_entries() on the first\nconflict it detects, but conflicts could have been detected in a\nprevious function call, namely detect_and_process_renames().  However:\n  * conflicts detected by detect_and_process_renames() are quite rare\n    conflict types\n  * the detection would still come after regular rename detection\n    (which is the expensive part of detect_and_process_renames()), so\n    it is not saving us much in computation time given that\n    process_entries() directly follows detect_and_process_renames()\n  * [this overlaps with the next bullet point] process_entries() is the\n    place where virtually all object writing occurs (object writing is\n    sometimes more of a concern for Forges than computation time), so\n    exiting early here isn't saving us much in object writes either\n  * the code changes needed to handle an earlier exit are slightly\n    more invasive in detect_and_process_renames() than for\n    process_entries().\nGiven the rareness of the even earlier conflicts, the limited savings\nwe'd get from exiting even earlier, and in an attempt to keep this\npatch simpler, we don't guarantee that we actually exit on the first\nconflict detected.  We can always revisit this decision later if we\ndecide that a further micro-optimization to exit slightly earlier in\nrare cases is worthwhile.\n\n\"avoid (not prevent) writing objects\":\n\nThe detect_and_process_renames() call can also write objects to the\nobject store, when rename/rename conflicts involve one (or more) files\nthat have also been modified on both sides.  Because of this alternate\ncall path leading to handle_content_merges(), our \"early exit\" does not\nprevent writing objects entirely, even within the \"outer layer\"\n(i.e. even within call_depth == 0).  I figure that's fine though, since\nwe're already writing objects for the inner merges (i.e. for call_depth\n> 0), which are likely going to represent vastly more objects than files\ninvolved in rename/rename+modify/modify cases in the outer merge, on\naverage.\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n merge-ort.c | 38 +++++++++++++++++++++++++++++++-------\n merge-ort.h |  1 +\n 2 files changed, 32 insertions(+), 7 deletions(-)\n\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 77310a4a52c9..47b3d1730ece 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -2127,6 +2127,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\t\tconst struct version_info *b,\n \t\t\t\tconst char *pathnames[3],\n \t\t\t\tconst int extra_marker_size,\n+\t\t\t\tconst int record_object,\n \t\t\t\tstruct version_info *result)\n {\n \t/*\n@@ -2214,7 +2215,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\tret = -1;\n \t\t}\n \n-\t\tif (!ret &&\n+\t\tif (!ret && record_object &&\n \t\t    write_object_file(result_buf.ptr, result_buf.size,\n \t\t\t\t      OBJ_BLOB, &result->oid)) {\n \t\t\tpath_msg(opt, ERROR_OBJECT_WRITE_FAILED, 0,\n@@ -2897,6 +2898,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\tstruct version_info merged;\n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tunsigned was_binary_blob = 0;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[1] = newpath;\n@@ -2947,6 +2949,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t\t   &side2->stages[2],\n \t\t\t\t\t\t\t   pathnames,\n \t\t\t\t\t\t\t   1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t\t   &merged);\n \t\t\tif (clean_merge < 0)\n \t\t\t\treturn -1;\n@@ -3061,6 +3064,7 @@ static int process_renames(struct merge_options *opt,\n \n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tint clean;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[other_source_index] = oldpath;\n@@ -3080,6 +3084,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t     &side2->stages[2],\n \t\t\t\t\t\t     pathnames,\n \t\t\t\t\t\t     1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t     record_object,\n \t\t\t\t\t\t     &merged);\n \t\t\tif (clean < 0)\n \t\t\t\treturn -1;\n@@ -3931,9 +3936,12 @@ static int write_completed_directory(struct merge_options *opt,\n \t\t * Write out the tree to the git object directory, and also\n \t\t * record the mode and oid in dir_info->result.\n \t\t */\n+\t\tint record_tree = (!opt->mergeability_only ||\n+\t\t\t\t   opt->priv->call_depth);\n \t\tdir_info->is_null = 0;\n \t\tdir_info->result.mode = S_IFDIR;\n-\t\tif (write_tree(&dir_info->result.oid, &info->versions, offset,\n+\t\tif (record_tree &&\n+\t\t    write_tree(&dir_info->result.oid, &info->versions, offset,\n \t\t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\t\tret = -1;\n \t}\n@@ -4231,10 +4239,13 @@ static int process_entry(struct merge_options *opt,\n \t\tstruct version_info *o = &ci->stages[0];\n \t\tstruct version_info *a = &ci->stages[1];\n \t\tstruct version_info *b = &ci->stages[2];\n+\t\tint record_object = (!opt->mergeability_only ||\n+\t\t\t\t     opt->priv->call_depth);\n \n \t\tclean_merge = handle_content_merge(opt, path, o, a, b,\n \t\t\t\t\t\t   ci->pathnames,\n \t\t\t\t\t\t   opt->priv->call_depth * 2,\n+\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t   &merged_file);\n \t\tif (clean_merge < 0)\n \t\t\treturn -1;\n@@ -4395,6 +4406,8 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\t\t\t   STRING_LIST_INIT_NODUP,\n \t\t\t\t\t\t   NULL, 0 };\n \tint ret = 0;\n+\tconst int record_tree = (!opt->mergeability_only ||\n+\t\t\t\t opt->priv->call_depth);\n \n \ttrace2_region_enter(\"merge\", \"process_entries setup\", opt->repo);\n \tif (strmap_empty(&opt->priv->paths)) {\n@@ -4454,6 +4467,12 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\tret = -1;\n \t\t\t\tgoto cleanup;\n \t\t\t};\n+\t\t\tif (!ci->merged.clean && opt->mergeability_only &&\n+\t\t\t    !opt->priv->call_depth) {\n+\t\t\t\tret = 0;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\n \t\t}\n \t}\n \ttrace2_region_leave(\"merge\", \"processing\", opt->repo);\n@@ -4468,7 +4487,8 @@ static int process_entries(struct merge_options *opt,\n \t\tfflush(stdout);\n \t\tBUG(\"dir_metadata accounting completely off; shouldn't happen\");\n \t}\n-\tif (write_tree(result_oid, &dir_metadata.versions, 0,\n+\tif (record_tree &&\n+\t    write_tree(result_oid, &dir_metadata.versions, 0,\n \t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\tret = -1;\n cleanup:\n@@ -4715,6 +4735,8 @@ void merge_display_update_messages(struct merge_options *opt,\n \n \tif (opt->record_conflict_msgs_as_headers)\n \t\tBUG(\"Either display conflict messages or record them as headers, not both\");\n+\tif (opt->mergeability_only)\n+\t\tBUG(\"Displaying conflict messages incompatible with mergeability-only checks\");\n \n \ttrace2_region_enter(\"merge\", \"display messages\", opt->repo);\n \n@@ -5171,10 +5193,12 @@ redo:\n \tresult->path_messages = &opt->priv->conflicts;\n \n \tif (result->clean >= 0) {\n-\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n-\t\tif (!result->tree)\n-\t\t\tdie(_(\"unable to read tree (%s)\"),\n-\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\tif (!opt->mergeability_only) {\n+\t\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n+\t\t\tif (!result->tree)\n+\t\t\t\tdie(_(\"unable to read tree (%s)\"),\n+\t\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\t}\n \t\t/* existence of conflicted entries implies unclean */\n \t\tresult->clean &= strmap_empty(&opt->priv->conflicted);\n \t}\ndiff --git a/merge-ort.h b/merge-ort.h\nindex 30750c03962f..6045579825da 100644\n--- a/merge-ort.h\n+++ b/merge-ort.h\n@@ -83,6 +83,7 @@ struct merge_options {\n \t/* miscellaneous control options */\n \tconst char *subtree_shift;\n \tunsigned renormalize : 1;\n+\tunsigned mergeability_only : 1; /* exit early, write fewer objects */\n \tunsigned record_conflict_msgs_as_headers : 1;\n \tconst char *msg_header_prefix;\n \n-- \ngitgitgadget\n\n"},{"id":"517918","messageId":"1d18ab7feb877a4173ac55814177aefe88cee658.1747093322.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v2.git.1747093322.gitgitgadget@gmail.com","subject":"[PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-12T23:42:02Z","receivedAt":"2025-05-12T23:42:08Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new --dry-run flag which\nwill make use of the new mergeability_only flag added to merge-ort in\nthe previous commit.  This option allows the merge machinery to, in the\nouter layer of the merge:\n    * exit early when a conflict is detected\n    * avoid writing (most) merged blobs/trees to the object store\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 3 files changed, 66 insertions(+)\n\ndiff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\nindex cf0578f9b5e8..7dcc17806191 100644\n--- a/Documentation/git-merge-tree.adoc\n+++ b/Documentation/git-merge-tree.adoc\n@@ -65,6 +65,12 @@ OPTIONS\n \tdefault is to include these messages if there are merge\n \tconflicts, and to omit them otherwise.\n \n+--mergeability-only::\n+\tDisable all output from the program.  Useful when you are only\n+\tinterested in the exit status.  Allows merge-tree to exit\n+\tearly on the first conflict it finds, and allows it to avoid\n+\twriting most objects created by merges.\n+\n --allow-unrelated-histories::\n \tmerge-tree will by default error out if the two branches specified\n \tshare no common history.  This flag can be given to override that\ndiff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\nindex 4aafa73c6155..579e81d51844 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -490,6 +490,9 @@ static int real_merge(struct merge_tree_options *o,\n \tif (result.clean < 0)\n \t\tdie(_(\"failure to merge\"));\n \n+\tif (o->merge_options.mergeability_only)\n+\t\tgoto cleanup;\n+\n \tif (show_messages == -1)\n \t\tshow_messages = !result.clean;\n \n@@ -522,6 +525,8 @@ static int real_merge(struct merge_tree_options *o,\n \t}\n \tif (o->use_stdin)\n \t\tputchar(line_termination);\n+\n+cleanup:\n \tmerge_finalize(&opt, &result);\n \tclear_merge_options(&opt);\n \treturn !result.clean; /* result.clean < 0 handled above */\n@@ -538,6 +543,7 @@ int cmd_merge_tree(int argc,\n \tint original_argc;\n \tconst char *merge_base = NULL;\n \tint ret;\n+\tint dry_run = 0;\n \n \tconst char * const merge_tree_usage[] = {\n \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n@@ -552,6 +558,10 @@ int cmd_merge_tree(int argc,\n \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n \t\t\t N_(\"also show informational/conflict messages\")),\n+\t\tOPT_BOOL_F(0, \"dry-run\",\n+\t\t\t   &dry_run,\n+\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n+\t\t\t   PARSE_OPT_NONEG),\n \t\tOPT_SET_INT('z', NULL, &line_termination,\n \t\t\t    N_(\"separate paths with the NUL character\"), '\\0'),\n \t\tOPT_BOOL_F(0, \"name-only\",\n@@ -583,6 +593,18 @@ int cmd_merge_tree(int argc,\n \targc = parse_options(argc, argv, prefix, mt_options,\n \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n \n+\tif (dry_run && o.show_messages == -1)\n+\t\to.show_messages = 0;\n+\to.merge_options.mergeability_only = dry_run;\n+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\t\t\t\t  o.show_messages, \"--messages\");\n+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\t\t\t\t  o.name_only, \"--name-only\");\n+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\t\t\t\t  o.use_stdin, \"--stdin\");\n+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\t\t\t\t  !line_termination, \"-z\");\n+\n \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n \tfor (size_t x = 0; x < xopts.nr; x++)\ndiff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh\nindex f9c5883a7f7c..566a2b4ec737 100755\n--- a/t/t4301-merge-tree-write-tree.sh\n+++ b/t/t4301-merge-tree-write-tree.sh\n@@ -54,6 +54,25 @@ test_expect_success setup '\n \tgit commit -m first-commit\n '\n \n+test_expect_success '--dry-run on clean merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge is successful (exit code of 0)\n+\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Clean merge' '\n \tTREE_OID=$(git merge-tree --write-tree side1 side3) &&\n \tq_to_tab <<-EOF >expect &&\n@@ -72,6 +91,25 @@ test_expect_success 'Failed merge without rename detection' '\n \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n '\n \n+test_expect_success  '--dry-run on conflicted merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge has conflict\n+\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Content merge and a few conflicts' '\n \tgit checkout side1^0 &&\n \ttest_must_fail git merge side2 &&\n-- \ngitgitgadget\n"},{"id":"517930","messageId":"fb1e64eb-3e38-4929-a7bd-09051490760d@app.fastmail.com","threadId":"63437","inReplyTo":"1d18ab7feb877a4173ac55814177aefe88cee658.1747093322.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2025-05-13T07:15:17Z","receivedAt":"2025-05-13T07:15:38Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Tue, May 13, 2025, at 01:42, Elijah Newren via GitGitGadget wrote:\n> From: Elijah Newren <newren@gmail.com>\n> +--mergeability-only::\n> +\tDisable all output from the program.  Useful when you are only\n> +\tinterested in the exit status.  Allows merge-tree to exit\n> +\tearly on the first conflict it finds, and allows it to avoid\n> +\twriting most objects created by merges.\n\nThe previous name is being used here.\n\n-- \nKristoffer Haugsbakk\n"},{"id":"517944","messageId":"xmqqa57giqyi.fsf@gitster.g","threadId":"63437","inReplyTo":"1d18ab7feb877a4173ac55814177aefe88cee658.1747093322.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-13T13:24:21Z","receivedAt":"2025-05-13T13:24:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Elijah Newren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Elijah Newren <newren@gmail.com>\n>\n> Git Forges may be interested in whether two branches can be merged while\n> not being interested in what the resulting merge tree is nor which files\n> conflicted.  For such cases, add a new --dry-run flag which\n> will make use of the new mergeability_only flag added to merge-ort in\n> the previous commit.  This option allows the merge machinery to, in the\n\nThe first three lines are almost identical to [1/2] here, modulo the\ninternal name still being mergeability-only while the external name\nis now dry-run, which is perfectly fine as long as that is done\nconsistently.\n\n> diff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\n> index cf0578f9b5e8..7dcc17806191 100644\n> --- a/Documentation/git-merge-tree.adoc\n> +++ b/Documentation/git-merge-tree.adoc\n> @@ -65,6 +65,12 @@ OPTIONS\n>  \tdefault is to include these messages if there are merge\n>  \tconflicts, and to omit them otherwise.\n>  \n> +--mergeability-only::\n\nBut is this internal or external name?\n\n> +\tDisable all output from the program.  Useful when you are only\n> +\tinterested in the exit status.  Allows merge-tree to exit\n> +\tearly on the first conflict it finds, and allows it to avoid\n> +\twriting most objects created by merges.\n\nOK.\n"},{"id":"517951","messageId":"CABPp-BGtNMTaZH3stP3EjNUFssyk20t8Q1+dCipA_L7zeQGbgQ@mail.gmail.com","threadId":"63437","inReplyTo":"fb1e64eb-3e38-4929-a7bd-09051490760d@app.fastmail.com","subject":"Re: [PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2025-05-13T15:28:55Z","receivedAt":"2025-05-13T15:29:18Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Tue, May 13, 2025 at 12:15 AM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Tue, May 13, 2025, at 01:42, Elijah Newren via GitGitGadget wrote:\n> > From: Elijah Newren <newren@gmail.com>\n> > +--mergeability-only::\n> > +     Disable all output from the program.  Useful when you are only\n> > +     interested in the exit status.  Allows merge-tree to exit\n> > +     early on the first conflict it finds, and allows it to avoid\n> > +     writing most objects created by merges.\n>\n> The previous name is being used here.\n\nOops; thanks for catching that.  I'll fix it up.\n"},{"id":"517952","messageId":"CABPp-BHp7RXHFy18=fr1dqQgKiO3PNjXQkL2VX-cjSGsHp48aw@mail.gmail.com","threadId":"63437","inReplyTo":"xmqqa57giqyi.fsf@gitster.g","subject":"Re: [PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2025-05-13T15:30:51Z","receivedAt":"2025-05-13T15:31:02Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Tue, May 13, 2025 at 6:24 AM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Elijah Newren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > From: Elijah Newren <newren@gmail.com>\n> >\n> > Git Forges may be interested in whether two branches can be merged while\n> > not being interested in what the resulting merge tree is nor which files\n> > conflicted.  For such cases, add a new --dry-run flag which\n> > will make use of the new mergeability_only flag added to merge-ort in\n> > the previous commit.  This option allows the merge machinery to, in the\n>\n> The first three lines are almost identical to [1/2] here, modulo the\n> internal name still being mergeability-only while the external name\n> is now dry-run, which is perfectly fine as long as that is done\n> consistently.\n>\n> > diff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\n> > index cf0578f9b5e8..7dcc17806191 100644\n> > --- a/Documentation/git-merge-tree.adoc\n> > +++ b/Documentation/git-merge-tree.adoc\n> > @@ -65,6 +65,12 @@ OPTIONS\n> >       default is to include these messages if there are merge\n> >       conflicts, and to omit them otherwise.\n> >\n> > +--mergeability-only::\n>\n> But is this internal or external name?\n\nexternal; Kristoffer caught this oversight too.  I'll send a re-roll\nwith this fixed.  (I apparently also missed it in the\ndie-if-incompatible-options too, not sure how I missed all of these,\nbut I'll ensure they're all fixed up).\n"},{"id":"517988","messageId":"pull.1920.v3.git.1747182287.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v2.git.1747093322.gitgitgadget@gmail.com","subject":"[PATCH v3 0/2] merge-tree: add new --dry-run option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-14T00:24:45Z","receivedAt":"2025-05-14T00:24:51Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"Changes since v2:\n\n * Converted locations missed in v1 in changing --mergeability-only ->\n   --dry-run\n\nChanges since v1:\n\n * Renamed --mergeability-only flag to --dry-run, as per suggestion from\n   Junio\n * added some commit message clarifications\n\nThis adds a new flag, --dry-run, to git merge-tree, which suppresses all\noutput and leaves only the exit status (reflecting successful merge or\nconflict). This is useful for Git Forges in cases where they are only\ninterested in whether two branches can be merged, without needing the actual\nmerge result or conflict details.\n\nThe advantage of the flag is two fold:\n\n * The merge machinery can exit once it detects a conflict, instead of\n   continuing to compute merge result information\n * The merge machinery can avoid writing merged blobs and trees to the\n   object store when in the outer layer of the merging process (more details\n   in the first commit message).\n\nElijah Newren (2):\n  merge-ort: add a new mergeability_only option\n  merge-tree: add a new --dry-run flag\n\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n merge-ort.c                       | 38 +++++++++++++++++++++++++------\n merge-ort.h                       |  1 +\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 5 files changed, 98 insertions(+), 7 deletions(-)\n\n\nbase-commit: 6c0bd1fc70efaf053abe4e57c976afdc72d15377\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1920%2Fnewren%2Fmergeability-only-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1920/newren/mergeability-only-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/1920\n\nRange-diff vs v2:\n\n 1:  4757c4810d3 = 1:  4757c4810d3 merge-ort: add a new mergeability_only option\n 2:  1d18ab7feb8 ! 2:  f11824317a8 merge-tree: add a new --dry-run flag\n     @@ Documentation/git-merge-tree.adoc: OPTIONS\n       \tdefault is to include these messages if there are merge\n       \tconflicts, and to omit them otherwise.\n       \n     -+--mergeability-only::\n     ++--dry-run::\n      +\tDisable all output from the program.  Useful when you are only\n      +\tinterested in the exit status.  Allows merge-tree to exit\n     -+\tearly on the first conflict it finds, and allows it to avoid\n     -+\twriting most objects created by merges.\n     ++\tearly when it finds a conflict, and allows it to avoid writing\n     ++\tmost objects created by merges.\n      +\n       --allow-unrelated-histories::\n       \tmerge-tree will by default error out if the two branches specified\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n      +\tif (dry_run && o.show_messages == -1)\n      +\t\to.show_messages = 0;\n      +\to.merge_options.mergeability_only = dry_run;\n     -+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n      +\t\t\t\t  o.show_messages, \"--messages\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n      +\t\t\t\t  o.name_only, \"--name-only\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n      +\t\t\t\t  o.use_stdin, \"--stdin\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n     ++\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n      +\t\t\t\t  !line_termination, \"-z\");\n      +\n       \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n\n-- \ngitgitgadget\n"},{"id":"517989","messageId":"4757c4810d3421b6c41723da837cab14824239e2.1747182288.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v3.git.1747182287.gitgitgadget@gmail.com","subject":"[PATCH v3 1/2] merge-ort: add a new mergeability_only option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-14T00:24:46Z","receivedAt":"2025-05-14T00:24:53Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new mergeability_only option.  This\noption allows the merge machinery to, in the \"outer layer\" of the merge:\n  * exit upon first[-ish] conflict\n  * avoid (not prevent) writing merged blobs/trees to the object store\n\nI have a number of qualifiers there, so let me explain each:\n\n\"outer layer\":\n\nNote that since the recursive merge of merge bases (corresponding to\ncall_depth > 0) can conflict without the outer final merge\n(corresponding to call_depth == 0) conflicting, we can't short-circuit\nnor avoid writing merged blobs/trees to the object store during those\ninner merges.\n\n\"first-ish conflict\":\n\nThe current patch only exits early from process_entries() on the first\nconflict it detects, but conflicts could have been detected in a\nprevious function call, namely detect_and_process_renames().  However:\n  * conflicts detected by detect_and_process_renames() are quite rare\n    conflict types\n  * the detection would still come after regular rename detection\n    (which is the expensive part of detect_and_process_renames()), so\n    it is not saving us much in computation time given that\n    process_entries() directly follows detect_and_process_renames()\n  * [this overlaps with the next bullet point] process_entries() is the\n    place where virtually all object writing occurs (object writing is\n    sometimes more of a concern for Forges than computation time), so\n    exiting early here isn't saving us much in object writes either\n  * the code changes needed to handle an earlier exit are slightly\n    more invasive in detect_and_process_renames() than for\n    process_entries().\nGiven the rareness of the even earlier conflicts, the limited savings\nwe'd get from exiting even earlier, and in an attempt to keep this\npatch simpler, we don't guarantee that we actually exit on the first\nconflict detected.  We can always revisit this decision later if we\ndecide that a further micro-optimization to exit slightly earlier in\nrare cases is worthwhile.\n\n\"avoid (not prevent) writing objects\":\n\nThe detect_and_process_renames() call can also write objects to the\nobject store, when rename/rename conflicts involve one (or more) files\nthat have also been modified on both sides.  Because of this alternate\ncall path leading to handle_content_merges(), our \"early exit\" does not\nprevent writing objects entirely, even within the \"outer layer\"\n(i.e. even within call_depth == 0).  I figure that's fine though, since\nwe're already writing objects for the inner merges (i.e. for call_depth\n> 0), which are likely going to represent vastly more objects than files\ninvolved in rename/rename+modify/modify cases in the outer merge, on\naverage.\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n merge-ort.c | 38 +++++++++++++++++++++++++++++++-------\n merge-ort.h |  1 +\n 2 files changed, 32 insertions(+), 7 deletions(-)\n\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 77310a4a52c9..47b3d1730ece 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -2127,6 +2127,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\t\tconst struct version_info *b,\n \t\t\t\tconst char *pathnames[3],\n \t\t\t\tconst int extra_marker_size,\n+\t\t\t\tconst int record_object,\n \t\t\t\tstruct version_info *result)\n {\n \t/*\n@@ -2214,7 +2215,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\tret = -1;\n \t\t}\n \n-\t\tif (!ret &&\n+\t\tif (!ret && record_object &&\n \t\t    write_object_file(result_buf.ptr, result_buf.size,\n \t\t\t\t      OBJ_BLOB, &result->oid)) {\n \t\t\tpath_msg(opt, ERROR_OBJECT_WRITE_FAILED, 0,\n@@ -2897,6 +2898,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\tstruct version_info merged;\n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tunsigned was_binary_blob = 0;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[1] = newpath;\n@@ -2947,6 +2949,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t\t   &side2->stages[2],\n \t\t\t\t\t\t\t   pathnames,\n \t\t\t\t\t\t\t   1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t\t   &merged);\n \t\t\tif (clean_merge < 0)\n \t\t\t\treturn -1;\n@@ -3061,6 +3064,7 @@ static int process_renames(struct merge_options *opt,\n \n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tint clean;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[other_source_index] = oldpath;\n@@ -3080,6 +3084,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t     &side2->stages[2],\n \t\t\t\t\t\t     pathnames,\n \t\t\t\t\t\t     1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t     record_object,\n \t\t\t\t\t\t     &merged);\n \t\t\tif (clean < 0)\n \t\t\t\treturn -1;\n@@ -3931,9 +3936,12 @@ static int write_completed_directory(struct merge_options *opt,\n \t\t * Write out the tree to the git object directory, and also\n \t\t * record the mode and oid in dir_info->result.\n \t\t */\n+\t\tint record_tree = (!opt->mergeability_only ||\n+\t\t\t\t   opt->priv->call_depth);\n \t\tdir_info->is_null = 0;\n \t\tdir_info->result.mode = S_IFDIR;\n-\t\tif (write_tree(&dir_info->result.oid, &info->versions, offset,\n+\t\tif (record_tree &&\n+\t\t    write_tree(&dir_info->result.oid, &info->versions, offset,\n \t\t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\t\tret = -1;\n \t}\n@@ -4231,10 +4239,13 @@ static int process_entry(struct merge_options *opt,\n \t\tstruct version_info *o = &ci->stages[0];\n \t\tstruct version_info *a = &ci->stages[1];\n \t\tstruct version_info *b = &ci->stages[2];\n+\t\tint record_object = (!opt->mergeability_only ||\n+\t\t\t\t     opt->priv->call_depth);\n \n \t\tclean_merge = handle_content_merge(opt, path, o, a, b,\n \t\t\t\t\t\t   ci->pathnames,\n \t\t\t\t\t\t   opt->priv->call_depth * 2,\n+\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t   &merged_file);\n \t\tif (clean_merge < 0)\n \t\t\treturn -1;\n@@ -4395,6 +4406,8 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\t\t\t   STRING_LIST_INIT_NODUP,\n \t\t\t\t\t\t   NULL, 0 };\n \tint ret = 0;\n+\tconst int record_tree = (!opt->mergeability_only ||\n+\t\t\t\t opt->priv->call_depth);\n \n \ttrace2_region_enter(\"merge\", \"process_entries setup\", opt->repo);\n \tif (strmap_empty(&opt->priv->paths)) {\n@@ -4454,6 +4467,12 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\tret = -1;\n \t\t\t\tgoto cleanup;\n \t\t\t};\n+\t\t\tif (!ci->merged.clean && opt->mergeability_only &&\n+\t\t\t    !opt->priv->call_depth) {\n+\t\t\t\tret = 0;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\n \t\t}\n \t}\n \ttrace2_region_leave(\"merge\", \"processing\", opt->repo);\n@@ -4468,7 +4487,8 @@ static int process_entries(struct merge_options *opt,\n \t\tfflush(stdout);\n \t\tBUG(\"dir_metadata accounting completely off; shouldn't happen\");\n \t}\n-\tif (write_tree(result_oid, &dir_metadata.versions, 0,\n+\tif (record_tree &&\n+\t    write_tree(result_oid, &dir_metadata.versions, 0,\n \t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\tret = -1;\n cleanup:\n@@ -4715,6 +4735,8 @@ void merge_display_update_messages(struct merge_options *opt,\n \n \tif (opt->record_conflict_msgs_as_headers)\n \t\tBUG(\"Either display conflict messages or record them as headers, not both\");\n+\tif (opt->mergeability_only)\n+\t\tBUG(\"Displaying conflict messages incompatible with mergeability-only checks\");\n \n \ttrace2_region_enter(\"merge\", \"display messages\", opt->repo);\n \n@@ -5171,10 +5193,12 @@ redo:\n \tresult->path_messages = &opt->priv->conflicts;\n \n \tif (result->clean >= 0) {\n-\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n-\t\tif (!result->tree)\n-\t\t\tdie(_(\"unable to read tree (%s)\"),\n-\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\tif (!opt->mergeability_only) {\n+\t\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n+\t\t\tif (!result->tree)\n+\t\t\t\tdie(_(\"unable to read tree (%s)\"),\n+\t\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\t}\n \t\t/* existence of conflicted entries implies unclean */\n \t\tresult->clean &= strmap_empty(&opt->priv->conflicted);\n \t}\ndiff --git a/merge-ort.h b/merge-ort.h\nindex 30750c03962f..6045579825da 100644\n--- a/merge-ort.h\n+++ b/merge-ort.h\n@@ -83,6 +83,7 @@ struct merge_options {\n \t/* miscellaneous control options */\n \tconst char *subtree_shift;\n \tunsigned renormalize : 1;\n+\tunsigned mergeability_only : 1; /* exit early, write fewer objects */\n \tunsigned record_conflict_msgs_as_headers : 1;\n \tconst char *msg_header_prefix;\n \n-- \ngitgitgadget\n\n"},{"id":"517990","messageId":"f11824317a8050764fe35698039f863dd842b0c2.1747182288.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v3.git.1747182287.gitgitgadget@gmail.com","subject":"[PATCH v3 2/2] merge-tree: add a new --dry-run flag","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-14T00:24:47Z","receivedAt":"2025-05-14T00:24:53Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new --dry-run flag which\nwill make use of the new mergeability_only flag added to merge-ort in\nthe previous commit.  This option allows the merge machinery to, in the\nouter layer of the merge:\n    * exit early when a conflict is detected\n    * avoid writing (most) merged blobs/trees to the object store\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 22 ++++++++++++++++++\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 3 files changed, 66 insertions(+)\n\ndiff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\nindex cf0578f9b5e8..74716b910192 100644\n--- a/Documentation/git-merge-tree.adoc\n+++ b/Documentation/git-merge-tree.adoc\n@@ -65,6 +65,12 @@ OPTIONS\n \tdefault is to include these messages if there are merge\n \tconflicts, and to omit them otherwise.\n \n+--dry-run::\n+\tDisable all output from the program.  Useful when you are only\n+\tinterested in the exit status.  Allows merge-tree to exit\n+\tearly when it finds a conflict, and allows it to avoid writing\n+\tmost objects created by merges.\n+\n --allow-unrelated-histories::\n \tmerge-tree will by default error out if the two branches specified\n \tshare no common history.  This flag can be given to override that\ndiff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\nindex 4aafa73c6155..273ec171e988 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -490,6 +490,9 @@ static int real_merge(struct merge_tree_options *o,\n \tif (result.clean < 0)\n \t\tdie(_(\"failure to merge\"));\n \n+\tif (o->merge_options.mergeability_only)\n+\t\tgoto cleanup;\n+\n \tif (show_messages == -1)\n \t\tshow_messages = !result.clean;\n \n@@ -522,6 +525,8 @@ static int real_merge(struct merge_tree_options *o,\n \t}\n \tif (o->use_stdin)\n \t\tputchar(line_termination);\n+\n+cleanup:\n \tmerge_finalize(&opt, &result);\n \tclear_merge_options(&opt);\n \treturn !result.clean; /* result.clean < 0 handled above */\n@@ -538,6 +543,7 @@ int cmd_merge_tree(int argc,\n \tint original_argc;\n \tconst char *merge_base = NULL;\n \tint ret;\n+\tint dry_run = 0;\n \n \tconst char * const merge_tree_usage[] = {\n \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n@@ -552,6 +558,10 @@ int cmd_merge_tree(int argc,\n \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n \t\t\t N_(\"also show informational/conflict messages\")),\n+\t\tOPT_BOOL_F(0, \"dry-run\",\n+\t\t\t   &dry_run,\n+\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n+\t\t\t   PARSE_OPT_NONEG),\n \t\tOPT_SET_INT('z', NULL, &line_termination,\n \t\t\t    N_(\"separate paths with the NUL character\"), '\\0'),\n \t\tOPT_BOOL_F(0, \"name-only\",\n@@ -583,6 +593,18 @@ int cmd_merge_tree(int argc,\n \targc = parse_options(argc, argv, prefix, mt_options,\n \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n \n+\tif (dry_run && o.show_messages == -1)\n+\t\to.show_messages = 0;\n+\to.merge_options.mergeability_only = dry_run;\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n+\t\t\t\t  o.show_messages, \"--messages\");\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n+\t\t\t\t  o.name_only, \"--name-only\");\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n+\t\t\t\t  o.use_stdin, \"--stdin\");\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n+\t\t\t\t  !line_termination, \"-z\");\n+\n \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n \tfor (size_t x = 0; x < xopts.nr; x++)\ndiff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh\nindex f9c5883a7f7c..566a2b4ec737 100755\n--- a/t/t4301-merge-tree-write-tree.sh\n+++ b/t/t4301-merge-tree-write-tree.sh\n@@ -54,6 +54,25 @@ test_expect_success setup '\n \tgit commit -m first-commit\n '\n \n+test_expect_success '--dry-run on clean merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge is successful (exit code of 0)\n+\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Clean merge' '\n \tTREE_OID=$(git merge-tree --write-tree side1 side3) &&\n \tq_to_tab <<-EOF >expect &&\n@@ -72,6 +91,25 @@ test_expect_success 'Failed merge without rename detection' '\n \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n '\n \n+test_expect_success  '--dry-run on conflicted merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge has conflict\n+\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Content merge and a few conflicts' '\n \tgit checkout side1^0 &&\n \ttest_must_fail git merge side2 &&\n-- \ngitgitgadget\n"},{"id":"518040","messageId":"xmqqv7q3b7yf.fsf@gitster.g","threadId":"63437","inReplyTo":"CABPp-BHp7RXHFy18=fr1dqQgKiO3PNjXQkL2VX-cjSGsHp48aw@mail.gmail.com","subject":"Re: [PATCH v2 2/2] merge-tree: add a new --dry-run flag","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-14T14:08:56Z","receivedAt":"2025-05-14T14:08:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Elijah Newren <newren@gmail.com> writes:\n\n> external; Kristoffer caught this oversight too.  I'll send a re-roll\n> with this fixed.  (I apparently also missed it in the\n> die-if-incompatible-options too, not sure how I missed all of these,\n> but I'll ensure they're all fixed up).\n\nThanks.  Will hold.\n"},{"id":"518050","messageId":"be8de818-0e04-442d-9639-d34888df10cf@app.fastmail.com","threadId":"63437","inReplyTo":"pull.1920.v3.git.1747182287.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 0/2] merge-tree: add new --dry-run option","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2025-05-14T15:34:02Z","receivedAt":"2025-05-14T15:34:24Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Wed, May 14, 2025, at 02:24, Elijah Newren via GitGitGadget wrote:\n> Changes since v2:\n>\n>  * Converted locations missed in v1 in changing --mergeability-only ->\n>    --dry-run\n>\n> Changes since v1:\n>\n>  * Renamed --mergeability-only flag to --dry-run, as per suggestion from\n>    Junio\n>  * added some commit message clarifications\n>\n> This adds a new flag, --dry-run, to git merge-tree, which suppresses all\n> output and leaves only the exit status (reflecting successful merge or\n> conflict). This is useful for Git Forges in cases where they are only\n> interested in whether two branches can be merged, without needing the actual\n> merge result or conflict details.\n>\n> The advantage of the flag is two fold:\n>\n>  * The merge machinery can exit once it detects a conflict, instead of\n>    continuing to compute merge result information\n>  * The merge machinery can avoid writing merged blobs and trees to the\n>    object store when in the outer layer of the merging process (more details\n>    in the first commit message).\n>\n> Elijah Newren (2):\n>   merge-ort: add a new mergeability_only option\n>   merge-tree: add a new --dry-run flag\n\nAll I can say is that this looks good considering the comments on v2.\n\nInterdiff:\n\n```\ndiff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\nindex 7dcc1780619..74716b91019 100644\n--- a/Documentation/git-merge-tree.adoc\n+++ b/Documentation/git-merge-tree.adoc\n@@ -65,11 +65,11 @@ OPTIONS\n \tdefault is to include these messages if there are merge\n \tconflicts, and to omit them otherwise.\n\n---mergeability-only::\n+--dry-run::\n \tDisable all output from the program.  Useful when you are only\n \tinterested in the exit status.  Allows merge-tree to exit\n-\tearly on the first conflict it finds, and allows it to avoid\n-\twriting most objects created by merges.\n+\tearly when it finds a conflict, and allows it to avoid writing\n+\tmost objects created by merges.\n\n --allow-unrelated-histories::\n \tmerge-tree will by default error out if the two branches specified\ndiff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\nindex 579e81d5184..273ec171e98 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -596,13 +596,13 @@ int cmd_merge_tree(int argc,\n \tif (dry_run && o.show_messages == -1)\n \t\to.show_messages = 0;\n \to.merge_options.mergeability_only = dry_run;\n-\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n \t\t\t\t  o.show_messages, \"--messages\");\n-\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n \t\t\t\t  o.name_only, \"--name-only\");\n-\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n \t\t\t\t  o.use_stdin, \"--stdin\");\n-\tdie_for_incompatible_opt2(dry_run, \"--mergeability-only\",\n+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n \t\t\t\t  !line_termination, \"-z\");\n\n \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n```\n"},{"id":"518122","messageId":"xmqqbjru3tu8.fsf@gitster.g","threadId":"63437","inReplyTo":"f11824317a8050764fe35698039f863dd842b0c2.1747182288.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 2/2] merge-tree: add a new --dry-run flag","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-15T13:07:59Z","receivedAt":"2025-05-15T13:08:02Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Elijah Newren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> +--dry-run::\n> +\tDisable all output from the program.  Useful when you are only\n> +\tinterested in the exit status.  Allows merge-tree to exit\n> +\tearly when it finds a conflict, and allows it to avoid writing\n> +\tmost objects created by merges.\n> +\n\nOK.\n\n> @@ -583,6 +593,18 @@ int cmd_merge_tree(int argc,\n>  \targc = parse_options(argc, argv, prefix, mt_options,\n>  \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n>  \n> +\tif (dry_run && o.show_messages == -1)\n> +\t\to.show_messages = 0;\n> +\to.merge_options.mergeability_only = dry_run;\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.show_messages, \"--messages\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.name_only, \"--name-only\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.use_stdin, \"--stdin\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  !line_termination, \"-z\");\n\nAh, I missed this one.  Would have been caught in the previous round\nif we had test coverage for incompatible options.\n\nThis round looks quite sensible to me.  Queued.  Thanks.\n"},{"id":"518236","messageId":"23e4267a-34fb-414d-bae3-7f607d3bdbec@gmail.com","threadId":"63437","inReplyTo":"f11824317a8050764fe35698039f863dd842b0c2.1747182288.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 2/2] merge-tree: add a new --dry-run flag","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2025-05-16T13:18:19Z","receivedAt":"2025-05-16T13:18:24Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Elijah\n\nOn 14/05/2025 01:24, Elijah Newren via GitGitGadget wrote:\n> From: Elijah Newren <newren@gmail.com>\n> \n> +--dry-run::\n> +\tDisable all output from the program.  Useful when you are only\n> +\tinterested in the exit status.  Allows merge-tree to exit\n> +\tearly when it finds a conflict, and allows it to avoid writing\n> +\tmost objects created by merges.\n\nI don't want to bike-shed but to me this feels more like \"git diff \n--quiet\" than \"git push --dry-run\"  or \"git send-email --dry-run\" which \nstill print diagnostic messages.\n\nBest Wishes\n\nPhillip\n\n>   --allow-unrelated-histories::\n>   \tmerge-tree will by default error out if the two branches specified\n>   \tshare no common history.  This flag can be given to override that\n> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\n> index 4aafa73c6155..273ec171e988 100644\n> --- a/builtin/merge-tree.c\n> +++ b/builtin/merge-tree.c\n> @@ -490,6 +490,9 @@ static int real_merge(struct merge_tree_options *o,\n>   \tif (result.clean < 0)\n>   \t\tdie(_(\"failure to merge\"));\n>   \n> +\tif (o->merge_options.mergeability_only)\n> +\t\tgoto cleanup;\n> +\n>   \tif (show_messages == -1)\n>   \t\tshow_messages = !result.clean;\n>   \n> @@ -522,6 +525,8 @@ static int real_merge(struct merge_tree_options *o,\n>   \t}\n>   \tif (o->use_stdin)\n>   \t\tputchar(line_termination);\n> +\n> +cleanup:\n>   \tmerge_finalize(&opt, &result);\n>   \tclear_merge_options(&opt);\n>   \treturn !result.clean; /* result.clean < 0 handled above */\n> @@ -538,6 +543,7 @@ int cmd_merge_tree(int argc,\n>   \tint original_argc;\n>   \tconst char *merge_base = NULL;\n>   \tint ret;\n> +\tint dry_run = 0;\n>   \n>   \tconst char * const merge_tree_usage[] = {\n>   \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n> @@ -552,6 +558,10 @@ int cmd_merge_tree(int argc,\n>   \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n>   \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n>   \t\t\t N_(\"also show informational/conflict messages\")),\n> +\t\tOPT_BOOL_F(0, \"dry-run\",\n> +\t\t\t   &dry_run,\n> +\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n> +\t\t\t   PARSE_OPT_NONEG),\n>   \t\tOPT_SET_INT('z', NULL, &line_termination,\n>   \t\t\t    N_(\"separate paths with the NUL character\"), '\\0'),\n>   \t\tOPT_BOOL_F(0, \"name-only\",\n> @@ -583,6 +593,18 @@ int cmd_merge_tree(int argc,\n>   \targc = parse_options(argc, argv, prefix, mt_options,\n>   \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n>   \n> +\tif (dry_run && o.show_messages == -1)\n> +\t\to.show_messages = 0;\n> +\to.merge_options.mergeability_only = dry_run;\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.show_messages, \"--messages\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.name_only, \"--name-only\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  o.use_stdin, \"--stdin\");\n> +\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n> +\t\t\t\t  !line_termination, \"-z\");\n> +\n>   \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n>   \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n>   \tfor (size_t x = 0; x < xopts.nr; x++)\n> diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh\n> index f9c5883a7f7c..566a2b4ec737 100755\n> --- a/t/t4301-merge-tree-write-tree.sh\n> +++ b/t/t4301-merge-tree-write-tree.sh\n> @@ -54,6 +54,25 @@ test_expect_success setup '\n>   \tgit commit -m first-commit\n>   '\n>   \n> +test_expect_success '--dry-run on clean merge' '\n> +\t# Get rid of loose objects to start with\n> +\tgit gc &&\n> +\techo \"0 objects, 0 kilobytes\" >expect &&\n> +\tgit count-objects >actual &&\n> +\ttest_cmp expect actual &&\n> +\n> +\t# Ensure merge is successful (exit code of 0)\n> +\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n> +\n> +\t# Ensure there is no output\n> +\ttest_must_be_empty output &&\n> +\n> +\t# Ensure no loose objects written (all new objects written would have\n> +\t# been in \"outer layer\" of the merge)\n> +\tgit count-objects >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>   test_expect_success 'Clean merge' '\n>   \tTREE_OID=$(git merge-tree --write-tree side1 side3) &&\n>   \tq_to_tab <<-EOF >expect &&\n> @@ -72,6 +91,25 @@ test_expect_success 'Failed merge without rename detection' '\n>   \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n>   '\n>   \n> +test_expect_success  '--dry-run on conflicted merge' '\n> +\t# Get rid of loose objects to start with\n> +\tgit gc &&\n> +\techo \"0 objects, 0 kilobytes\" >expect &&\n> +\tgit count-objects >actual &&\n> +\ttest_cmp expect actual &&\n> +\n> +\t# Ensure merge has conflict\n> +\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n> +\n> +\t# Ensure there is no output\n> +\ttest_must_be_empty output &&\n> +\n> +\t# Ensure no loose objects written (all new objects written would have\n> +\t# been in \"outer layer\" of the merge)\n> +\tgit count-objects >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>   test_expect_success 'Content merge and a few conflicts' '\n>   \tgit checkout side1^0 &&\n>   \ttest_must_fail git merge side2 &&\n\n"},{"id":"518258","messageId":"CABPp-BHpuBnZacf39wVwYYuBjau_joW2ztkLJSAW_nNNTEwnpQ@mail.gmail.com","threadId":"63437","inReplyTo":"23e4267a-34fb-414d-bae3-7f607d3bdbec@gmail.com","subject":"Re: [PATCH v3 2/2] merge-tree: add a new --dry-run flag","fromName":"Elijah Newren","fromEmail":"newren@gmail.com","sentAt":"2025-05-16T16:03:57Z","receivedAt":"2025-05-16T16:04:09Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"On Fri, May 16, 2025 at 6:18 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Elijah\n>\n> On 14/05/2025 01:24, Elijah Newren via GitGitGadget wrote:\n> > From: Elijah Newren <newren@gmail.com>\n> >\n> > +--dry-run::\n> > +     Disable all output from the program.  Useful when you are only\n> > +     interested in the exit status.  Allows merge-tree to exit\n> > +     early when it finds a conflict, and allows it to avoid writing\n> > +     most objects created by merges.\n>\n> I don't want to bike-shed but to me this feels more like \"git diff\n> --quiet\" than \"git push --dry-run\"  or \"git send-email --dry-run\" which\n> still print diagnostic messages.\n>\n> Best Wishes\n>\n> Phillip\n\n--quiet seems fine to me.  I'll make the change and then expect\nsomeone to comment on it with an even better name...  ;-)\n"},{"id":"518306","messageId":"pull.1920.v4.git.1747425858.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v3.git.1747182287.gitgitgadget@gmail.com","subject":"[PATCH v4 0/2] merge-tree: add new --quiet option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-16T20:04:16Z","receivedAt":"2025-05-16T20:04:22Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"Changes since v3:\n\n * Renamed --dry-run -> --quiet . Any further naming suggestions?\n\nChanges since v2:\n\n * Converted locations missed in v1 in changing --mergeability-only ->\n   --dry-run\n\nChanges since v1:\n\n * Renamed --mergeability-only flag to --dry-run, as per suggestion from\n   Junio\n * added some commit message clarifications\n\nThis adds a new flag, --dry-run, to git merge-tree, which suppresses all\noutput and leaves only the exit status (reflecting successful merge or\nconflict). This is useful for Git Forges in cases where they are only\ninterested in whether two branches can be merged, without needing the actual\nmerge result or conflict details.\n\nThe advantage of the flag is two fold:\n\n * The merge machinery can exit once it detects a conflict, instead of\n   continuing to compute merge result information\n * The merge machinery can avoid writing merged blobs and trees to the\n   object store when in the outer layer of the merging process (more details\n   in the first commit message).\n\nElijah Newren (2):\n  merge-ort: add a new mergeability_only option\n  merge-tree: add a new --quiet flag\n\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 18 +++++++++++++++\n merge-ort.c                       | 38 +++++++++++++++++++++++++------\n merge-ort.h                       |  1 +\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 5 files changed, 94 insertions(+), 7 deletions(-)\n\n\nbase-commit: 6c0bd1fc70efaf053abe4e57c976afdc72d15377\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1920%2Fnewren%2Fmergeability-only-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1920/newren/mergeability-only-v4\nPull-Request: https://github.com/gitgitgadget/git/pull/1920\n\nRange-diff vs v3:\n\n 1:  4757c4810d3 = 1:  4757c4810d3 merge-ort: add a new mergeability_only option\n 2:  f11824317a8 ! 2:  7c40d3c9216 merge-tree: add a new --dry-run flag\n     @@ Metadata\n      Author: Elijah Newren <newren@gmail.com>\n      \n       ## Commit message ##\n     -    merge-tree: add a new --dry-run flag\n     +    merge-tree: add a new --quiet flag\n      \n          Git Forges may be interested in whether two branches can be merged while\n          not being interested in what the resulting merge tree is nor which files\n     -    conflicted.  For such cases, add a new --dry-run flag which\n     +    conflicted.  For such cases, add a new --quiet flag which\n          will make use of the new mergeability_only flag added to merge-ort in\n          the previous commit.  This option allows the merge machinery to, in the\n          outer layer of the merge:\n     @@ Documentation/git-merge-tree.adoc: OPTIONS\n       \tdefault is to include these messages if there are merge\n       \tconflicts, and to omit them otherwise.\n       \n     -+--dry-run::\n     ++--quiet::\n      +\tDisable all output from the program.  Useful when you are only\n      +\tinterested in the exit status.  Allows merge-tree to exit\n      +\tearly when it finds a conflict, and allows it to avoid writing\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \tint original_argc;\n       \tconst char *merge_base = NULL;\n       \tint ret;\n     -+\tint dry_run = 0;\n     ++\tint quiet = 0;\n       \n       \tconst char * const merge_tree_usage[] = {\n       \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n       \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n       \t\t\t N_(\"also show informational/conflict messages\")),\n     -+\t\tOPT_BOOL_F(0, \"dry-run\",\n     -+\t\t\t   &dry_run,\n     ++\t\tOPT_BOOL_F(0, \"quiet\",\n     ++\t\t\t   &quiet,\n      +\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n      +\t\t\t   PARSE_OPT_NONEG),\n       \t\tOPT_SET_INT('z', NULL, &line_termination,\n     @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n       \targc = parse_options(argc, argv, prefix, mt_options,\n       \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n       \n     -+\tif (dry_run && o.show_messages == -1)\n     ++\tif (quiet && o.show_messages == -1)\n      +\t\to.show_messages = 0;\n     -+\to.merge_options.mergeability_only = dry_run;\n     -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n     -+\t\t\t\t  o.show_messages, \"--messages\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n     -+\t\t\t\t  o.name_only, \"--name-only\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n     -+\t\t\t\t  o.use_stdin, \"--stdin\");\n     -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n     -+\t\t\t\t  !line_termination, \"-z\");\n     ++\to.merge_options.mergeability_only = quiet;\n     ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.show_messages, \"--messages\");\n     ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.name_only, \"--name-only\");\n     ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.use_stdin, \"--stdin\");\n     ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", !line_termination, \"-z\");\n      +\n       \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n       \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n       \tgit commit -m first-commit\n       '\n       \n     -+test_expect_success '--dry-run on clean merge' '\n     ++test_expect_success '--quiet on clean merge' '\n      +\t# Get rid of loose objects to start with\n      +\tgit gc &&\n      +\techo \"0 objects, 0 kilobytes\" >expect &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n      +\ttest_cmp expect actual &&\n      +\n      +\t# Ensure merge is successful (exit code of 0)\n     -+\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n     ++\tgit merge-tree --write-tree --quiet side1 side3 >output &&\n      +\n      +\t# Ensure there is no output\n      +\ttest_must_be_empty output &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n       \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n       '\n       \n     -+test_expect_success  '--dry-run on conflicted merge' '\n     ++test_expect_success  '--quiet on conflicted merge' '\n      +\t# Get rid of loose objects to start with\n      +\tgit gc &&\n      +\techo \"0 objects, 0 kilobytes\" >expect &&\n     @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n      +\ttest_cmp expect actual &&\n      +\n      +\t# Ensure merge has conflict\n     -+\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n     ++\ttest_expect_code 1 git merge-tree --write-tree --quiet side1 side2 >output &&\n      +\n      +\t# Ensure there is no output\n      +\ttest_must_be_empty output &&\n\n-- \ngitgitgadget\n"},{"id":"518307","messageId":"4757c4810d3421b6c41723da837cab14824239e2.1747425858.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v4.git.1747425858.gitgitgadget@gmail.com","subject":"[PATCH v4 1/2] merge-ort: add a new mergeability_only option","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-16T20:04:17Z","receivedAt":"2025-05-16T20:04:23Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new mergeability_only option.  This\noption allows the merge machinery to, in the \"outer layer\" of the merge:\n  * exit upon first[-ish] conflict\n  * avoid (not prevent) writing merged blobs/trees to the object store\n\nI have a number of qualifiers there, so let me explain each:\n\n\"outer layer\":\n\nNote that since the recursive merge of merge bases (corresponding to\ncall_depth > 0) can conflict without the outer final merge\n(corresponding to call_depth == 0) conflicting, we can't short-circuit\nnor avoid writing merged blobs/trees to the object store during those\ninner merges.\n\n\"first-ish conflict\":\n\nThe current patch only exits early from process_entries() on the first\nconflict it detects, but conflicts could have been detected in a\nprevious function call, namely detect_and_process_renames().  However:\n  * conflicts detected by detect_and_process_renames() are quite rare\n    conflict types\n  * the detection would still come after regular rename detection\n    (which is the expensive part of detect_and_process_renames()), so\n    it is not saving us much in computation time given that\n    process_entries() directly follows detect_and_process_renames()\n  * [this overlaps with the next bullet point] process_entries() is the\n    place where virtually all object writing occurs (object writing is\n    sometimes more of a concern for Forges than computation time), so\n    exiting early here isn't saving us much in object writes either\n  * the code changes needed to handle an earlier exit are slightly\n    more invasive in detect_and_process_renames() than for\n    process_entries().\nGiven the rareness of the even earlier conflicts, the limited savings\nwe'd get from exiting even earlier, and in an attempt to keep this\npatch simpler, we don't guarantee that we actually exit on the first\nconflict detected.  We can always revisit this decision later if we\ndecide that a further micro-optimization to exit slightly earlier in\nrare cases is worthwhile.\n\n\"avoid (not prevent) writing objects\":\n\nThe detect_and_process_renames() call can also write objects to the\nobject store, when rename/rename conflicts involve one (or more) files\nthat have also been modified on both sides.  Because of this alternate\ncall path leading to handle_content_merges(), our \"early exit\" does not\nprevent writing objects entirely, even within the \"outer layer\"\n(i.e. even within call_depth == 0).  I figure that's fine though, since\nwe're already writing objects for the inner merges (i.e. for call_depth\n> 0), which are likely going to represent vastly more objects than files\ninvolved in rename/rename+modify/modify cases in the outer merge, on\naverage.\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n merge-ort.c | 38 +++++++++++++++++++++++++++++++-------\n merge-ort.h |  1 +\n 2 files changed, 32 insertions(+), 7 deletions(-)\n\ndiff --git a/merge-ort.c b/merge-ort.c\nindex 77310a4a52c9..47b3d1730ece 100644\n--- a/merge-ort.c\n+++ b/merge-ort.c\n@@ -2127,6 +2127,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\t\tconst struct version_info *b,\n \t\t\t\tconst char *pathnames[3],\n \t\t\t\tconst int extra_marker_size,\n+\t\t\t\tconst int record_object,\n \t\t\t\tstruct version_info *result)\n {\n \t/*\n@@ -2214,7 +2215,7 @@ static int handle_content_merge(struct merge_options *opt,\n \t\t\tret = -1;\n \t\t}\n \n-\t\tif (!ret &&\n+\t\tif (!ret && record_object &&\n \t\t    write_object_file(result_buf.ptr, result_buf.size,\n \t\t\t\t      OBJ_BLOB, &result->oid)) {\n \t\t\tpath_msg(opt, ERROR_OBJECT_WRITE_FAILED, 0,\n@@ -2897,6 +2898,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\tstruct version_info merged;\n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tunsigned was_binary_blob = 0;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[1] = newpath;\n@@ -2947,6 +2949,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t\t   &side2->stages[2],\n \t\t\t\t\t\t\t   pathnames,\n \t\t\t\t\t\t\t   1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t\t   &merged);\n \t\t\tif (clean_merge < 0)\n \t\t\t\treturn -1;\n@@ -3061,6 +3064,7 @@ static int process_renames(struct merge_options *opt,\n \n \t\t\tstruct conflict_info *base, *side1, *side2;\n \t\t\tint clean;\n+\t\t\tconst int record_object = true;\n \n \t\t\tpathnames[0] = oldpath;\n \t\t\tpathnames[other_source_index] = oldpath;\n@@ -3080,6 +3084,7 @@ static int process_renames(struct merge_options *opt,\n \t\t\t\t\t\t     &side2->stages[2],\n \t\t\t\t\t\t     pathnames,\n \t\t\t\t\t\t     1 + 2 * opt->priv->call_depth,\n+\t\t\t\t\t\t     record_object,\n \t\t\t\t\t\t     &merged);\n \t\t\tif (clean < 0)\n \t\t\t\treturn -1;\n@@ -3931,9 +3936,12 @@ static int write_completed_directory(struct merge_options *opt,\n \t\t * Write out the tree to the git object directory, and also\n \t\t * record the mode and oid in dir_info->result.\n \t\t */\n+\t\tint record_tree = (!opt->mergeability_only ||\n+\t\t\t\t   opt->priv->call_depth);\n \t\tdir_info->is_null = 0;\n \t\tdir_info->result.mode = S_IFDIR;\n-\t\tif (write_tree(&dir_info->result.oid, &info->versions, offset,\n+\t\tif (record_tree &&\n+\t\t    write_tree(&dir_info->result.oid, &info->versions, offset,\n \t\t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\t\tret = -1;\n \t}\n@@ -4231,10 +4239,13 @@ static int process_entry(struct merge_options *opt,\n \t\tstruct version_info *o = &ci->stages[0];\n \t\tstruct version_info *a = &ci->stages[1];\n \t\tstruct version_info *b = &ci->stages[2];\n+\t\tint record_object = (!opt->mergeability_only ||\n+\t\t\t\t     opt->priv->call_depth);\n \n \t\tclean_merge = handle_content_merge(opt, path, o, a, b,\n \t\t\t\t\t\t   ci->pathnames,\n \t\t\t\t\t\t   opt->priv->call_depth * 2,\n+\t\t\t\t\t\t   record_object,\n \t\t\t\t\t\t   &merged_file);\n \t\tif (clean_merge < 0)\n \t\t\treturn -1;\n@@ -4395,6 +4406,8 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\t\t\t   STRING_LIST_INIT_NODUP,\n \t\t\t\t\t\t   NULL, 0 };\n \tint ret = 0;\n+\tconst int record_tree = (!opt->mergeability_only ||\n+\t\t\t\t opt->priv->call_depth);\n \n \ttrace2_region_enter(\"merge\", \"process_entries setup\", opt->repo);\n \tif (strmap_empty(&opt->priv->paths)) {\n@@ -4454,6 +4467,12 @@ static int process_entries(struct merge_options *opt,\n \t\t\t\tret = -1;\n \t\t\t\tgoto cleanup;\n \t\t\t};\n+\t\t\tif (!ci->merged.clean && opt->mergeability_only &&\n+\t\t\t    !opt->priv->call_depth) {\n+\t\t\t\tret = 0;\n+\t\t\t\tgoto cleanup;\n+\t\t\t}\n+\n \t\t}\n \t}\n \ttrace2_region_leave(\"merge\", \"processing\", opt->repo);\n@@ -4468,7 +4487,8 @@ static int process_entries(struct merge_options *opt,\n \t\tfflush(stdout);\n \t\tBUG(\"dir_metadata accounting completely off; shouldn't happen\");\n \t}\n-\tif (write_tree(result_oid, &dir_metadata.versions, 0,\n+\tif (record_tree &&\n+\t    write_tree(result_oid, &dir_metadata.versions, 0,\n \t\t       opt->repo->hash_algo->rawsz) < 0)\n \t\tret = -1;\n cleanup:\n@@ -4715,6 +4735,8 @@ void merge_display_update_messages(struct merge_options *opt,\n \n \tif (opt->record_conflict_msgs_as_headers)\n \t\tBUG(\"Either display conflict messages or record them as headers, not both\");\n+\tif (opt->mergeability_only)\n+\t\tBUG(\"Displaying conflict messages incompatible with mergeability-only checks\");\n \n \ttrace2_region_enter(\"merge\", \"display messages\", opt->repo);\n \n@@ -5171,10 +5193,12 @@ redo:\n \tresult->path_messages = &opt->priv->conflicts;\n \n \tif (result->clean >= 0) {\n-\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n-\t\tif (!result->tree)\n-\t\t\tdie(_(\"unable to read tree (%s)\"),\n-\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\tif (!opt->mergeability_only) {\n+\t\t\tresult->tree = parse_tree_indirect(&working_tree_oid);\n+\t\t\tif (!result->tree)\n+\t\t\t\tdie(_(\"unable to read tree (%s)\"),\n+\t\t\t\t    oid_to_hex(&working_tree_oid));\n+\t\t}\n \t\t/* existence of conflicted entries implies unclean */\n \t\tresult->clean &= strmap_empty(&opt->priv->conflicted);\n \t}\ndiff --git a/merge-ort.h b/merge-ort.h\nindex 30750c03962f..6045579825da 100644\n--- a/merge-ort.h\n+++ b/merge-ort.h\n@@ -83,6 +83,7 @@ struct merge_options {\n \t/* miscellaneous control options */\n \tconst char *subtree_shift;\n \tunsigned renormalize : 1;\n+\tunsigned mergeability_only : 1; /* exit early, write fewer objects */\n \tunsigned record_conflict_msgs_as_headers : 1;\n \tconst char *msg_header_prefix;\n \n-- \ngitgitgadget\n\n"},{"id":"518308","messageId":"7c40d3c921610576dc7947bc4c517055717dd94e.1747425858.git.gitgitgadget@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v4.git.1747425858.gitgitgadget@gmail.com","subject":"[PATCH v4 2/2] merge-tree: add a new --quiet flag","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-05-16T20:04:18Z","receivedAt":"2025-05-16T20:04:24Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nGit Forges may be interested in whether two branches can be merged while\nnot being interested in what the resulting merge tree is nor which files\nconflicted.  For such cases, add a new --quiet flag which\nwill make use of the new mergeability_only flag added to merge-ort in\nthe previous commit.  This option allows the merge machinery to, in the\nouter layer of the merge:\n    * exit early when a conflict is detected\n    * avoid writing (most) merged blobs/trees to the object store\n\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n Documentation/git-merge-tree.adoc |  6 +++++\n builtin/merge-tree.c              | 18 +++++++++++++++\n t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n 3 files changed, 62 insertions(+)\n\ndiff --git a/Documentation/git-merge-tree.adoc b/Documentation/git-merge-tree.adoc\nindex cf0578f9b5e8..f824eea61f1e 100644\n--- a/Documentation/git-merge-tree.adoc\n+++ b/Documentation/git-merge-tree.adoc\n@@ -65,6 +65,12 @@ OPTIONS\n \tdefault is to include these messages if there are merge\n \tconflicts, and to omit them otherwise.\n \n+--quiet::\n+\tDisable all output from the program.  Useful when you are only\n+\tinterested in the exit status.  Allows merge-tree to exit\n+\tearly when it finds a conflict, and allows it to avoid writing\n+\tmost objects created by merges.\n+\n --allow-unrelated-histories::\n \tmerge-tree will by default error out if the two branches specified\n \tshare no common history.  This flag can be given to override that\ndiff --git a/builtin/merge-tree.c b/builtin/merge-tree.c\nindex 4aafa73c6155..7f41665dfd7e 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -490,6 +490,9 @@ static int real_merge(struct merge_tree_options *o,\n \tif (result.clean < 0)\n \t\tdie(_(\"failure to merge\"));\n \n+\tif (o->merge_options.mergeability_only)\n+\t\tgoto cleanup;\n+\n \tif (show_messages == -1)\n \t\tshow_messages = !result.clean;\n \n@@ -522,6 +525,8 @@ static int real_merge(struct merge_tree_options *o,\n \t}\n \tif (o->use_stdin)\n \t\tputchar(line_termination);\n+\n+cleanup:\n \tmerge_finalize(&opt, &result);\n \tclear_merge_options(&opt);\n \treturn !result.clean; /* result.clean < 0 handled above */\n@@ -538,6 +543,7 @@ int cmd_merge_tree(int argc,\n \tint original_argc;\n \tconst char *merge_base = NULL;\n \tint ret;\n+\tint quiet = 0;\n \n \tconst char * const merge_tree_usage[] = {\n \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n@@ -552,6 +558,10 @@ int cmd_merge_tree(int argc,\n \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n \t\t\t N_(\"also show informational/conflict messages\")),\n+\t\tOPT_BOOL_F(0, \"quiet\",\n+\t\t\t   &quiet,\n+\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n+\t\t\t   PARSE_OPT_NONEG),\n \t\tOPT_SET_INT('z', NULL, &line_termination,\n \t\t\t    N_(\"separate paths with the NUL character\"), '\\0'),\n \t\tOPT_BOOL_F(0, \"name-only\",\n@@ -583,6 +593,14 @@ int cmd_merge_tree(int argc,\n \targc = parse_options(argc, argv, prefix, mt_options,\n \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n \n+\tif (quiet && o.show_messages == -1)\n+\t\to.show_messages = 0;\n+\to.merge_options.mergeability_only = quiet;\n+\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.show_messages, \"--messages\");\n+\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.name_only, \"--name-only\");\n+\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.use_stdin, \"--stdin\");\n+\tdie_for_incompatible_opt2(quiet, \"--quiet\", !line_termination, \"-z\");\n+\n \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n \tfor (size_t x = 0; x < xopts.nr; x++)\ndiff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh\nindex f9c5883a7f7c..6e117ee93c8b 100755\n--- a/t/t4301-merge-tree-write-tree.sh\n+++ b/t/t4301-merge-tree-write-tree.sh\n@@ -54,6 +54,25 @@ test_expect_success setup '\n \tgit commit -m first-commit\n '\n \n+test_expect_success '--quiet on clean merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge is successful (exit code of 0)\n+\tgit merge-tree --write-tree --quiet side1 side3 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Clean merge' '\n \tTREE_OID=$(git merge-tree --write-tree side1 side3) &&\n \tq_to_tab <<-EOF >expect &&\n@@ -72,6 +91,25 @@ test_expect_success 'Failed merge without rename detection' '\n \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n '\n \n+test_expect_success  '--quiet on conflicted merge' '\n+\t# Get rid of loose objects to start with\n+\tgit gc &&\n+\techo \"0 objects, 0 kilobytes\" >expect &&\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual &&\n+\n+\t# Ensure merge has conflict\n+\ttest_expect_code 1 git merge-tree --write-tree --quiet side1 side2 >output &&\n+\n+\t# Ensure there is no output\n+\ttest_must_be_empty output &&\n+\n+\t# Ensure no loose objects written (all new objects written would have\n+\t# been in \"outer layer\" of the merge)\n+\tgit count-objects >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Content merge and a few conflicts' '\n \tgit checkout side1^0 &&\n \ttest_must_fail git merge side2 &&\n-- \ngitgitgadget\n"},{"id":"518342","messageId":"6cc85081-bbdd-43f6-b17d-8baffedec8c9@app.fastmail.com","threadId":"63437","inReplyTo":"7c40d3c921610576dc7947bc4c517055717dd94e.1747425858.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 2/2] merge-tree: add a new --quiet flag","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2025-05-17T19:52:50Z","receivedAt":"2025-05-17T19:53:25Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Fri, May 16, 2025, at 22:04, Elijah Newren via GitGitGadget wrote:\n> From: Elijah Newren <newren@gmail.com>\n> +\tif (quiet && o.show_messages == -1)\n> +\t\to.show_messages = 0;\n> +\to.merge_options.mergeability_only = quiet;\n> +\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.show_messages, \"--messages\");\n> +\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.name_only, \"--name-only\");\n> +\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.use_stdin, \"--stdin\");\n> +\tdie_for_incompatible_opt2(quiet, \"--quiet\", !line_termination, \"-z\");\n\nI’ve been using git-merge-tree(1) for some scripting but only today\ntried out `--stdin` for printing refs that conflict.\n\n```\n# Pipe in pairs\nmerge_pairs=$(mktemp)\ntee $merge_pairs \\\n    | git merge-tree --stdin --no-messages \\\n    | tr '\\0' '\\n' \\\n    | grep --extended-regexp '^(1|0)$' \\\n    | paste -d' ' - $merge_pairs \\\n    | grep '^0' \\\n    | cut -d' ' -f2-\n```\n\n(Previously I called the command in a loop)\n\nI could imagine a `--format` option to just keep one of the arguments,\nwhich means the tee(1) (for cross-referencing the ref) and all the other\nthings are gone:\n\n```\ngit merge-tree --format='%(if)%(conflicted)%(then)oid2%(end)' --stdin\n```\n\n(But imagined options aside)\n\n`--stdin` is presumably for efficiency and `--quiet`/`--dry-run`\ndefinitely is.  But `--quiet` can only be used in the mode where you can\nonly do a single merge, not in the `--stdin` batch mode.\n\n`--quiet`/`--dry-run` with informational output (c.f. the above\ndie-for-incompatible) would “break” the documented output format since\nconflicts haven’t been computed all the way and there are no OIDs for\nsuccessful merges.  But the user is opting into a new mode here, never\nseen before.  Can’t they opt into a new informational mode where\n`--stdin --quiet` can co-exist?  Then you can have dry-run batch mode.\n\n-- \nKristoffer Haugsbakk\n"},{"id":"518343","messageId":"57dcdc06-d7f2-4661-a2bd-de48690eae6e@app.fastmail.com","threadId":"63437","inReplyTo":"6cc85081-bbdd-43f6-b17d-8baffedec8c9@app.fastmail.com","subject":"Re: [PATCH v4 2/2] merge-tree: add a new --quiet flag","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2025-05-17T19:57:13Z","receivedAt":"2025-05-17T19:57:36Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Sat, May 17, 2025, at 21:52, Kristoffer Haugsbakk wrote:\n> Can’t they opt into a new informational mode where\n> `--stdin --quiet` can co-exist?\n\nYes, I now immediately see the contradiction in the\nliteral text: “informational mode” and `--quiet`. \nBut I shall not weigh in on the naming matter.\n\n-- \nKristoffer Haugsbakk\n\n"},{"id":"518398","messageId":"74bc8741-0a52-45d7-be2f-cc10b641c704@gmail.com","threadId":"63437","inReplyTo":"pull.1920.v4.git.1747425858.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 0/2] merge-tree: add new --quiet option","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2025-05-19T09:05:09Z","receivedAt":"2025-05-19T09:05:12Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Elijah\n\nOn 16/05/2025 21:04, Elijah Newren via GitGitGadget wrote:\n> Changes since v3:\n> \n>   * Renamed --dry-run -> --quiet . Any further naming suggestions?\n\nThanks for re-rolling this version looks good to me.\n\nThanks\n\nPhillip\n\n> Changes since v2:\n> \n>   * Converted locations missed in v1 in changing --mergeability-only ->\n>     --dry-run\n> \n> Changes since v1:\n> \n>   * Renamed --mergeability-only flag to --dry-run, as per suggestion from\n>     Junio\n>   * added some commit message clarifications\n> \n> This adds a new flag, --dry-run, to git merge-tree, which suppresses all\n> output and leaves only the exit status (reflecting successful merge or\n> conflict). This is useful for Git Forges in cases where they are only\n> interested in whether two branches can be merged, without needing the actual\n> merge result or conflict details.\n> \n> The advantage of the flag is two fold:\n> \n>   * The merge machinery can exit once it detects a conflict, instead of\n>     continuing to compute merge result information\n>   * The merge machinery can avoid writing merged blobs and trees to the\n>     object store when in the outer layer of the merging process (more details\n>     in the first commit message).\n> \n> Elijah Newren (2):\n>    merge-ort: add a new mergeability_only option\n>    merge-tree: add a new --quiet flag\n> \n>   Documentation/git-merge-tree.adoc |  6 +++++\n>   builtin/merge-tree.c              | 18 +++++++++++++++\n>   merge-ort.c                       | 38 +++++++++++++++++++++++++------\n>   merge-ort.h                       |  1 +\n>   t/t4301-merge-tree-write-tree.sh  | 38 +++++++++++++++++++++++++++++++\n>   5 files changed, 94 insertions(+), 7 deletions(-)\n> \n> \n> base-commit: 6c0bd1fc70efaf053abe4e57c976afdc72d15377\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1920%2Fnewren%2Fmergeability-only-v4\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1920/newren/mergeability-only-v4\n> Pull-Request: https://github.com/gitgitgadget/git/pull/1920\n> \n> Range-diff vs v3:\n> \n>   1:  4757c4810d3 = 1:  4757c4810d3 merge-ort: add a new mergeability_only option\n>   2:  f11824317a8 ! 2:  7c40d3c9216 merge-tree: add a new --dry-run flag\n>       @@ Metadata\n>        Author: Elijah Newren <newren@gmail.com>\n>        \n>         ## Commit message ##\n>       -    merge-tree: add a new --dry-run flag\n>       +    merge-tree: add a new --quiet flag\n>        \n>            Git Forges may be interested in whether two branches can be merged while\n>            not being interested in what the resulting merge tree is nor which files\n>       -    conflicted.  For such cases, add a new --dry-run flag which\n>       +    conflicted.  For such cases, add a new --quiet flag which\n>            will make use of the new mergeability_only flag added to merge-ort in\n>            the previous commit.  This option allows the merge machinery to, in the\n>            outer layer of the merge:\n>       @@ Documentation/git-merge-tree.adoc: OPTIONS\n>         \tdefault is to include these messages if there are merge\n>         \tconflicts, and to omit them otherwise.\n>         \n>       -+--dry-run::\n>       ++--quiet::\n>        +\tDisable all output from the program.  Useful when you are only\n>        +\tinterested in the exit status.  Allows merge-tree to exit\n>        +\tearly when it finds a conflict, and allows it to avoid writing\n>       @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n>         \tint original_argc;\n>         \tconst char *merge_base = NULL;\n>         \tint ret;\n>       -+\tint dry_run = 0;\n>       ++\tint quiet = 0;\n>         \n>         \tconst char * const merge_tree_usage[] = {\n>         \t\tN_(\"git merge-tree [--write-tree] [<options>] <branch1> <branch2>\"),\n>       @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n>         \t\t\t    N_(\"do a trivial merge only\"), MODE_TRIVIAL),\n>         \t\tOPT_BOOL(0, \"messages\", &o.show_messages,\n>         \t\t\t N_(\"also show informational/conflict messages\")),\n>       -+\t\tOPT_BOOL_F(0, \"dry-run\",\n>       -+\t\t\t   &dry_run,\n>       ++\t\tOPT_BOOL_F(0, \"quiet\",\n>       ++\t\t\t   &quiet,\n>        +\t\t\t   N_(\"suppress all output; only exit status wanted\"),\n>        +\t\t\t   PARSE_OPT_NONEG),\n>         \t\tOPT_SET_INT('z', NULL, &line_termination,\n>       @@ builtin/merge-tree.c: int cmd_merge_tree(int argc,\n>         \targc = parse_options(argc, argv, prefix, mt_options,\n>         \t\t\t     merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);\n>         \n>       -+\tif (dry_run && o.show_messages == -1)\n>       ++\tif (quiet && o.show_messages == -1)\n>        +\t\to.show_messages = 0;\n>       -+\to.merge_options.mergeability_only = dry_run;\n>       -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n>       -+\t\t\t\t  o.show_messages, \"--messages\");\n>       -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n>       -+\t\t\t\t  o.name_only, \"--name-only\");\n>       -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n>       -+\t\t\t\t  o.use_stdin, \"--stdin\");\n>       -+\tdie_for_incompatible_opt2(dry_run, \"--dry-run\",\n>       -+\t\t\t\t  !line_termination, \"-z\");\n>       ++\to.merge_options.mergeability_only = quiet;\n>       ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.show_messages, \"--messages\");\n>       ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.name_only, \"--name-only\");\n>       ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", o.use_stdin, \"--stdin\");\n>       ++\tdie_for_incompatible_opt2(quiet, \"--quiet\", !line_termination, \"-z\");\n>        +\n>         \tif (xopts.nr && o.mode == MODE_TRIVIAL)\n>         \t\tdie(_(\"--trivial-merge is incompatible with all other options\"));\n>       @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n>         \tgit commit -m first-commit\n>         '\n>         \n>       -+test_expect_success '--dry-run on clean merge' '\n>       ++test_expect_success '--quiet on clean merge' '\n>        +\t# Get rid of loose objects to start with\n>        +\tgit gc &&\n>        +\techo \"0 objects, 0 kilobytes\" >expect &&\n>       @@ t/t4301-merge-tree-write-tree.sh: test_expect_success setup '\n>        +\ttest_cmp expect actual &&\n>        +\n>        +\t# Ensure merge is successful (exit code of 0)\n>       -+\tgit merge-tree --write-tree --dry-run side1 side3 >output &&\n>       ++\tgit merge-tree --write-tree --quiet side1 side3 >output &&\n>        +\n>        +\t# Ensure there is no output\n>        +\ttest_must_be_empty output &&\n>       @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n>         \tgrep \"CONFLICT (modify/delete): numbers deleted\" out\n>         '\n>         \n>       -+test_expect_success  '--dry-run on conflicted merge' '\n>       ++test_expect_success  '--quiet on conflicted merge' '\n>        +\t# Get rid of loose objects to start with\n>        +\tgit gc &&\n>        +\techo \"0 objects, 0 kilobytes\" >expect &&\n>       @@ t/t4301-merge-tree-write-tree.sh: test_expect_success 'Failed merge without rena\n>        +\ttest_cmp expect actual &&\n>        +\n>        +\t# Ensure merge has conflict\n>       -+\ttest_expect_code 1 git merge-tree --write-tree --dry-run side1 side2 >output &&\n>       ++\ttest_expect_code 1 git merge-tree --write-tree --quiet side1 side2 >output &&\n>        +\n>        +\t# Ensure there is no output\n>        +\ttest_must_be_empty output &&\n> \n\n"},{"id":"518430","messageId":"xmqqecwkoam0.fsf@gitster.g","threadId":"63437","inReplyTo":"74bc8741-0a52-45d7-be2f-cc10b641c704@gmail.com","subject":"Re: [PATCH v4 0/2] merge-tree: add new --quiet option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-05-19T15:59:03Z","receivedAt":"2025-05-19T15:59:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Hi Elijah\n>\n> On 16/05/2025 21:04, Elijah Newren via GitGitGadget wrote:\n>> Changes since v3:\n>>   * Renamed --dry-run -> --quiet . Any further naming suggestions?\n>\n> Thanks for re-rolling this version looks good to me.\n>\n> Thanks\n>\n> Phillip\n\nThanks, all.  Let's mark it for 'next' and merge it down.\n"}]}