Skip to content

Commit bb14cfd

Browse files
committed
Merge branch 'jc/merge-detached-head-name'
The default merge message prepared by "git merge" records the name of the current branch; the name can be overridden with a new option to allow users to pretend a merge is made on a different branch. * jc/merge-detached-head-name: merge: allow to pretend a merge is made into a different branch
2 parents a165484 + bd2bc94 commit bb14cfd

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

Documentation/git-fmt-merge-msg.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-fmt-merge-msg - Produce a merge commit message
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git fmt-merge-msg' [-m <message>] [--log[=<n>] | --no-log]
12+
'git fmt-merge-msg' [-m <message>] [--into-name <branch>] [--log[=<n>] | --no-log]
1313
'git fmt-merge-msg' [-m <message>] [--log[=<n>] | --no-log] -F <file>
1414

1515
DESCRIPTION
@@ -44,6 +44,10 @@ OPTIONS
4444
Use <message> instead of the branch names for the first line
4545
of the log message. For use with `--log`.
4646

47+
--into-name <branch>::
48+
Prepare the merge message as if merging to the branch `<branch>`,
49+
instead of the name of the real branch to which the merge is made.
50+
4751
-F <file>::
4852
--file <file>::
4953
Take the list of merged objects from <file> instead of

Documentation/git-merge.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ SYNOPSIS
1212
'git merge' [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
1313
[--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
1414
[--[no-]allow-unrelated-histories]
15-
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...]
15+
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>]
16+
[--into-name <branch>] [<commit>...]
1617
'git merge' (--continue | --abort | --quit)
1718

1819
DESCRIPTION
@@ -76,6 +77,11 @@ The 'git fmt-merge-msg' command can be
7677
used to give a good default for automated 'git merge'
7778
invocations. The automated message can include the branch description.
7879

80+
--into-name <branch>::
81+
Prepare the default merge message as if merging to the branch
82+
`<branch>`, instead of the name of the real branch to which
83+
the merge is made.
84+
7985
-F <file>::
8086
--file=<file>::
8187
Read the commit message to be used for the merge commit (in

builtin/fmt-merge-msg.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
1212
{
1313
const char *inpath = NULL;
1414
const char *message = NULL;
15+
char *into_name = NULL;
1516
int shortlog_len = -1;
1617
struct option options[] = {
1718
{ OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
@@ -23,6 +24,8 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
2324
DEFAULT_MERGE_LOG_LEN },
2425
OPT_STRING('m', "message", &message, N_("text"),
2526
N_("use <text> as start of message")),
27+
OPT_STRING(0, "into-name", &into_name, N_("name"),
28+
N_("use <name> instead of the real target branch")),
2629
OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
2730
OPT_END()
2831
};
@@ -56,6 +59,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
5659
opts.add_title = !message;
5760
opts.credit_people = 1;
5861
opts.shortlog_len = shortlog_len;
62+
opts.into_name = into_name;
5963

6064
ret = fmt_merge_msg(&input, &output, &opts);
6165
if (ret)

builtin/merge.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static int signoff;
8787
static const char *sign_commit;
8888
static int autostash;
8989
static int no_verify;
90+
static char *into_name;
9091

9192
static struct strategy all_strategy[] = {
9293
{ "recursive", NO_TRIVIAL },
@@ -286,6 +287,8 @@ static struct option builtin_merge_options[] = {
286287
{ OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
287288
N_("read message from file"), PARSE_OPT_NONEG,
288289
NULL, 0, option_read_message },
290+
OPT_STRING(0, "into-name", &into_name, N_("name"),
291+
N_("use <name> instead of the real target")),
289292
OPT__VERBOSITY(&verbosity),
290293
OPT_BOOL(0, "abort", &abort_current_merge,
291294
N_("abort the current in-progress merge")),
@@ -1121,6 +1124,7 @@ static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *mer
11211124
opts.add_title = !have_message;
11221125
opts.shortlog_len = shortlog_len;
11231126
opts.credit_people = (0 < option_edit);
1127+
opts.into_name = into_name;
11241128

11251129
fmt_merge_msg(merge_names, merge_msg, &opts);
11261130
if (merge_msg->len)

fmt-merge-msg.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,15 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
650650

651651
memset(&merge_parents, 0, sizeof(merge_parents));
652652

653-
/* get current branch */
653+
/* learn the commit that we merge into and the current branch name */
654654
current_branch = current_branch_to_free =
655655
resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL);
656656
if (!current_branch)
657657
die("No current branch");
658-
if (starts_with(current_branch, "refs/heads/"))
658+
659+
if (opts->into_name)
660+
current_branch = opts->into_name;
661+
else if (starts_with(current_branch, "refs/heads/"))
659662
current_branch += 11;
660663

661664
find_merge_parents(&merge_parents, in, &head_oid);

fmt-merge-msg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct fmt_merge_msg_opts {
99
unsigned add_title:1,
1010
credit_people:1;
1111
int shortlog_len;
12+
const char *into_name;
1213
};
1314

1415
extern int merge_log_config;

t/t6200-fmt-merge-msg.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,35 @@ test_expect_success 'merge-msg with "merging" an annotated tag' '
633633
test_cmp expected .git/MERGE_MSG
634634
'
635635

636+
test_expect_success 'merge --into-name=<name>' '
637+
test_when_finished "git checkout main" &&
638+
git checkout -B side main &&
639+
git commit --allow-empty -m "One step ahead" &&
640+
641+
git checkout --detach main &&
642+
git merge --no-ff side &&
643+
git show -s --format="%s" >full.0 &&
644+
head -n1 full.0 >actual &&
645+
# expect that HEAD is shown as-is
646+
grep -e "Merge branch .side. into HEAD$" actual &&
647+
648+
git reset --hard main &&
649+
git merge --no-ff --into-name=main side &&
650+
git show -s --format="%s" >full.1 &&
651+
head -n1 full.1 >actual &&
652+
# expect that we pretend to be merging to main, that is suppressed
653+
grep -e "Merge branch .side.$" actual &&
654+
655+
git checkout -b throwaway main &&
656+
git merge --no-ff --into-name=main side &&
657+
git show -s --format="%s" >full.2 &&
658+
head -n1 full.2 >actual &&
659+
# expect that we pretend to be merging to main, that is suppressed
660+
grep -e "Merge branch .side.$" actual
661+
'
662+
636663
test_expect_success 'merge.suppressDest configuration' '
664+
test_when_finished "git checkout main" &&
637665
git checkout -B side main &&
638666
git commit --allow-empty -m "One step ahead" &&
639667
git checkout main &&
@@ -650,7 +678,19 @@ test_expect_success 'merge.suppressDest configuration' '
650678
git -c merge.suppressDest="ma?*[rn]" fmt-merge-msg <.git/FETCH_HEAD >full.3 &&
651679
head -n1 full.3 >actual &&
652680
grep -e "Merge branch .side." actual &&
653-
! grep -e " into main$" actual
681+
! grep -e " into main$" actual &&
682+
683+
git checkout --detach HEAD &&
684+
git -c merge.suppressDest="main" fmt-merge-msg <.git/FETCH_HEAD >full.4 &&
685+
head -n1 full.4 >actual &&
686+
grep -e "Merge branch .side. into HEAD$" actual &&
687+
688+
git -c merge.suppressDest="main" fmt-merge-msg \
689+
--into-name=main <.git/FETCH_HEAD >full.5 &&
690+
head -n1 full.5 >actual &&
691+
grep -e "Merge branch .side." actual &&
692+
! grep -e " into main$" actual &&
693+
! grep -e " into HEAD$" actual
654694
'
655695

656696
test_done

0 commit comments

Comments
 (0)