Skip to content

Commit 6fe1b8c

Browse files
committed
Merge branch 'ng/rebase-merges-branch-name-as-label'
"git rebase --rebase-merges" now uses branch names as labels when able. * ng/rebase-merges-branch-name-as-label: rebase-merges: try and use branch names as labels rebase-update-refs: extract load_branch_decorations load_branch_decorations: fix memory leak with non-static filters
2 parents b967851 + 4368921 commit 6fe1b8c

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

log-tree.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ void load_ref_decorations(struct decoration_filter *filter, int flags)
232232
for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
233233
normalize_glob_ref(item, NULL, item->string);
234234
}
235+
236+
/* normalize_glob_ref duplicates the strings */
237+
filter->exclude_ref_pattern->strdup_strings = 1;
238+
filter->include_ref_pattern->strdup_strings = 1;
239+
filter->exclude_ref_config_pattern->strdup_strings = 1;
235240
}
236241
decoration_loaded = 1;
237242
decoration_flags = flags;
@@ -243,6 +248,27 @@ void load_ref_decorations(struct decoration_filter *filter, int flags)
243248
}
244249
}
245250

251+
void load_branch_decorations(void)
252+
{
253+
if (!decoration_loaded) {
254+
struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
255+
struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
256+
struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
257+
struct decoration_filter decoration_filter = {
258+
.include_ref_pattern = &decorate_refs_include,
259+
.exclude_ref_pattern = &decorate_refs_exclude,
260+
.exclude_ref_config_pattern = &decorate_refs_exclude_config,
261+
};
262+
263+
string_list_append(&decorate_refs_include, "refs/heads/");
264+
load_ref_decorations(&decoration_filter, 0);
265+
266+
string_list_clear(&decorate_refs_exclude, 0);
267+
string_list_clear(&decorate_refs_exclude_config, 0);
268+
string_list_clear(&decorate_refs_include, 0);
269+
}
270+
}
271+
246272
static void show_parents(struct commit *commit, int abbrev, FILE *file)
247273
{
248274
struct commit_list *p;

log-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
3333
int *need_8bit_cte_p,
3434
int maybe_multipart);
3535
void load_ref_decorations(struct decoration_filter *filter, int flags);
36+
void load_branch_decorations(void);
3637

3738
void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
3839
void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);

sequencer.c

Lines changed: 18 additions & 18 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

@@ -6403,14 +6412,6 @@ static int add_decorations_to_list(const struct commit *commit,
64036412
static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
64046413
{
64056414
int i, res;
6406-
static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
6407-
static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
6408-
static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
6409-
struct decoration_filter decoration_filter = {
6410-
.include_ref_pattern = &decorate_refs_include,
6411-
.exclude_ref_pattern = &decorate_refs_exclude,
6412-
.exclude_ref_config_pattern = &decorate_refs_exclude_config,
6413-
};
64146415
struct todo_add_branch_context ctx = {
64156416
.buf = &todo_list->buf,
64166417
.refs_to_oids = STRING_LIST_INIT_DUP,
@@ -6419,8 +6420,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
64196420
ctx.items_alloc = 2 * todo_list->nr + 1;
64206421
ALLOC_ARRAY(ctx.items, ctx.items_alloc);
64216422

6422-
string_list_append(&decorate_refs_include, "refs/heads/");
6423-
load_ref_decorations(&decoration_filter, 0);
6423+
load_branch_decorations();
64246424

64256425
for (i = 0; i < todo_list->nr; ) {
64266426
struct todo_item *item = &todo_list->items[i];

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)