Skip to content

Commit 4368921

Browse files
nicolas-guichardgitster
authored andcommitted
rebase-merges: try and use branch names as labels
When interactively rebasing merge commits, the commit message is parsed to extract a probably meaningful label name. For instance if the merge commit is “Merge branch 'feature0'”, then the rebase script will have thes lines: ``` label feature0 merge -C $sha feature0 # “Merge branch 'feature0' ``` This heuristic fails in the case of octopus merges or when the merge commit message is actually unrelated to the parent commits. An example that combines both is: ``` *---. 967bfa4 (HEAD -> integration) Integration |\ \ \ | | | * 2135be1 (feature2, feat2) Feature 2 | |_|/ |/| | | | * c88b01a Feature 1 | |/ |/| | * 75f3139 (feat0) Feature 0 |/ * 25c86d0 (main) Initial commit ``` yields the labels Integration, Integration-2 and Integration-3. Fix this by using a branch name for each merge commit's parent that is the tip of at least one branch, and falling back to a label derived from the merge commit message otherwise. In the example above, the labels become feat0, Integration and feature2. Signed-off-by: Nicolas Guichard <[email protected]> Acked-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68c9fcb commit 4368921

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

sequencer.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5819,7 +5819,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
58195819
int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO;
58205820
int skipped_commit = 0;
58215821
struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT;
5822-
struct strbuf label = STRBUF_INIT;
5822+
struct strbuf label_from_message = STRBUF_INIT;
58235823
struct commit_list *commits = NULL, **tail = &commits, *iter;
58245824
struct commit_list *tips = NULL, **tips_tail = &tips;
58255825
struct commit *commit;
@@ -5842,6 +5842,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
58425842
oidmap_init(&state.commit2label, 0);
58435843
hashmap_init(&state.labels, labels_cmp, NULL, 0);
58445844
strbuf_init(&state.buf, 32);
5845+
load_branch_decorations();
58455846

58465847
if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) {
58475848
struct labels_entry *onto_label_entry;
@@ -5902,25 +5903,33 @@ static int make_script_with_merges(struct pretty_print_context *pp,
59025903
continue;
59035904
}
59045905

5905-
/* Create a label */
5906-
strbuf_reset(&label);
5906+
/* Create a label from the commit message */
5907+
strbuf_reset(&label_from_message);
59075908
if (skip_prefix(oneline.buf, "Merge ", &p1) &&
59085909
(p1 = strchr(p1, '\'')) &&
59095910
(p2 = strchr(++p1, '\'')))
5910-
strbuf_add(&label, p1, p2 - p1);
5911+
strbuf_add(&label_from_message, p1, p2 - p1);
59115912
else if (skip_prefix(oneline.buf, "Merge pull request ",
59125913
&p1) &&
59135914
(p1 = strstr(p1, " from ")))
5914-
strbuf_addstr(&label, p1 + strlen(" from "));
5915+
strbuf_addstr(&label_from_message, p1 + strlen(" from "));
59155916
else
5916-
strbuf_addbuf(&label, &oneline);
5917+
strbuf_addbuf(&label_from_message, &oneline);
59175918

59185919
strbuf_reset(&buf);
59195920
strbuf_addf(&buf, "%s -C %s",
59205921
cmd_merge, oid_to_hex(&commit->object.oid));
59215922

59225923
/* label the tips of merged branches */
59235924
for (; to_merge; to_merge = to_merge->next) {
5925+
const char *label = label_from_message.buf;
5926+
const struct name_decoration *decoration =
5927+
get_name_decoration(&to_merge->item->object);
5928+
5929+
if (decoration)
5930+
skip_prefix(decoration->name, "refs/heads/",
5931+
&label);
5932+
59245933
oid = &to_merge->item->object.oid;
59255934
strbuf_addch(&buf, ' ');
59265935

@@ -5933,7 +5942,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
59335942
tips_tail = &commit_list_insert(to_merge->item,
59345943
tips_tail)->next;
59355944

5936-
strbuf_addstr(&buf, label_oid(oid, label.buf, &state));
5945+
strbuf_addstr(&buf, label_oid(oid, label, &state));
59375946
}
59385947
strbuf_addf(&buf, " # %s", oneline.buf);
59395948

@@ -6041,7 +6050,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
60416050
free_commit_list(commits);
60426051
free_commit_list(tips);
60436052

6044-
strbuf_release(&label);
6053+
strbuf_release(&label_from_message);
60456054
strbuf_release(&oneline);
60466055
strbuf_release(&buf);
60476056

t/t3404-rebase-interactive.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,7 @@ test_expect_success '--update-refs adds commands with --rebase-merges' '
18701870
pick $(git log -1 --format=%h branch2~1) F
18711871
pick $(git log -1 --format=%h branch2) I
18721872
update-ref refs/heads/branch2
1873-
label merge
1873+
label branch2
18741874
reset onto
18751875
pick $(git log -1 --format=%h refs/heads/second) J
18761876
update-ref refs/heads/second
@@ -1881,7 +1881,7 @@ test_expect_success '--update-refs adds commands with --rebase-merges' '
18811881
update-ref refs/heads/third
18821882
pick $(git log -1 --format=%h HEAD~2) M
18831883
update-ref refs/heads/no-conflict-branch
1884-
merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1884+
merge -C $(git log -1 --format=%h HEAD~1) branch2 # merge
18851885
update-ref refs/heads/merge-branch
18861886
EOF
18871887

t/t3430-rebase-merges.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ test_expect_success 'generate correct todo list' '
108108
109109
reset onto
110110
pick $b B
111-
label E
111+
label first
112112
113113
reset onto
114114
pick $c C
115115
label branch-point
116116
pick $f F
117117
pick $g G
118-
label H
118+
label second
119119
120120
reset branch-point # C
121121
pick $d D
122-
merge -C $e E # E
123-
merge -C $h H # H
122+
merge -C $e first # E
123+
merge -C $h second # H
124124
125125
EOF
126126
@@ -462,11 +462,11 @@ test_expect_success 'A root commit can be a cousin, treat it that way' '
462462
'
463463

464464
test_expect_success 'labels that are object IDs are rewritten' '
465-
git checkout -b third B &&
465+
git checkout --detach B &&
466466
test_commit I &&
467467
third=$(git rev-parse HEAD) &&
468468
git checkout -b labels main &&
469-
git merge --no-commit third &&
469+
git merge --no-commit $third &&
470470
test_tick &&
471471
git commit -m "Merge commit '\''$third'\'' into labels" &&
472472
echo noop >script-from-scratch &&

0 commit comments

Comments
 (0)