diff --git a/builtin/merge.c b/builtin/merge.c index 58d1b7bb07..5b4eb23a83 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -1373,7 +1373,7 @@ int cmd_merge(int argc, struct commit_list *common = NULL; const char *best_strategy = NULL, *wt_strategy = NULL; struct commit_list *remoteheads = NULL, *p; - void *branch_to_free; + void *branch_to_free, *argv_to_free = NULL; int orig_argc = argc; int merge_log_config = -1; @@ -1517,8 +1517,10 @@ int cmd_merge(int argc, option_commit = 1; if (!argc) { - if (default_to_upstream) + if (default_to_upstream) { argc = setup_with_upstream(&argv); + argv_to_free = argv; + } else die(_("No commit specified and merge.defaultToUpstream not set.")); } else if (argc == 1 && !strcmp(argv[0], "-")) { @@ -1880,6 +1882,7 @@ done: } strbuf_release(&buf); free(branch_to_free); + free(argv_to_free); free(pull_twohead); free(pull_octopus); discard_index(the_repository->index); diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh index 7f2a1db16d..e31d261f9d 100755 --- a/t/t7600-merge.sh +++ b/t/t7600-merge.sh @@ -1166,4 +1166,21 @@ test_expect_success 'suggested names are not ambiguous' ' test_grep remotes/origin/not-local stderr ' +test_expect_success 'merge with no argument defaults to upstream' ' + test_when_finished "rm -rf upstream downstream" && + git init upstream && + ( + cd upstream && + test_commit one && + test_commit two + ) && + git clone upstream downstream && + ( + cd downstream && + git reset --hard HEAD^ && + git merge && + test_cmp_rev origin/main HEAD + ) +' + test_done