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
This commit is contained in:
Taylor Blau 2024-10-18 13:56:22 -04:00
commit 6fe1b8cee0
5 changed files with 53 additions and 26 deletions

View File

@ -232,6 +232,11 @@ void load_ref_decorations(struct decoration_filter *filter, int flags)
for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
normalize_glob_ref(item, NULL, item->string);
}
/* normalize_glob_ref duplicates the strings */
filter->exclude_ref_pattern->strdup_strings = 1;
filter->include_ref_pattern->strdup_strings = 1;
filter->exclude_ref_config_pattern->strdup_strings = 1;
}
decoration_loaded = 1;
decoration_flags = flags;
@ -243,6 +248,27 @@ void load_ref_decorations(struct decoration_filter *filter, int flags)
}
}
void load_branch_decorations(void)
{
if (!decoration_loaded) {
struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
struct decoration_filter decoration_filter = {
.include_ref_pattern = &decorate_refs_include,
.exclude_ref_pattern = &decorate_refs_exclude,
.exclude_ref_config_pattern = &decorate_refs_exclude_config,
};
string_list_append(&decorate_refs_include, "refs/heads/");
load_ref_decorations(&decoration_filter, 0);
string_list_clear(&decorate_refs_exclude, 0);
string_list_clear(&decorate_refs_exclude_config, 0);
string_list_clear(&decorate_refs_include, 0);
}
}
static void show_parents(struct commit *commit, int abbrev, FILE *file)
{
struct commit_list *p;

View File

@ -33,6 +33,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
int *need_8bit_cte_p,
int maybe_multipart);
void load_ref_decorations(struct decoration_filter *filter, int flags);
void load_branch_decorations(void);
void fmt_output_commit(struct strbuf *, struct commit *, struct rev_info *);
void fmt_output_subject(struct strbuf *, const char *subject, struct rev_info *);

View File

@ -5819,7 +5819,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
int root_with_onto = flags & TODO_LIST_ROOT_WITH_ONTO;
int skipped_commit = 0;
struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT;
struct strbuf label = STRBUF_INIT;
struct strbuf label_from_message = STRBUF_INIT;
struct commit_list *commits = NULL, **tail = &commits, *iter;
struct commit_list *tips = NULL, **tips_tail = &tips;
struct commit *commit;
@ -5842,6 +5842,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
oidmap_init(&state.commit2label, 0);
hashmap_init(&state.labels, labels_cmp, NULL, 0);
strbuf_init(&state.buf, 32);
load_branch_decorations();
if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) {
struct labels_entry *onto_label_entry;
@ -5902,18 +5903,18 @@ static int make_script_with_merges(struct pretty_print_context *pp,
continue;
}
/* Create a label */
strbuf_reset(&label);
/* Create a label from the commit message */
strbuf_reset(&label_from_message);
if (skip_prefix(oneline.buf, "Merge ", &p1) &&
(p1 = strchr(p1, '\'')) &&
(p2 = strchr(++p1, '\'')))
strbuf_add(&label, p1, p2 - p1);
strbuf_add(&label_from_message, p1, p2 - p1);
else if (skip_prefix(oneline.buf, "Merge pull request ",
&p1) &&
(p1 = strstr(p1, " from ")))
strbuf_addstr(&label, p1 + strlen(" from "));
strbuf_addstr(&label_from_message, p1 + strlen(" from "));
else
strbuf_addbuf(&label, &oneline);
strbuf_addbuf(&label_from_message, &oneline);
strbuf_reset(&buf);
strbuf_addf(&buf, "%s -C %s",
@ -5921,6 +5922,14 @@ static int make_script_with_merges(struct pretty_print_context *pp,
/* label the tips of merged branches */
for (; to_merge; to_merge = to_merge->next) {
const char *label = label_from_message.buf;
const struct name_decoration *decoration =
get_name_decoration(&to_merge->item->object);
if (decoration)
skip_prefix(decoration->name, "refs/heads/",
&label);
oid = &to_merge->item->object.oid;
strbuf_addch(&buf, ' ');
@ -5933,7 +5942,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
tips_tail = &commit_list_insert(to_merge->item,
tips_tail)->next;
strbuf_addstr(&buf, label_oid(oid, label.buf, &state));
strbuf_addstr(&buf, label_oid(oid, label, &state));
}
strbuf_addf(&buf, " # %s", oneline.buf);
@ -6041,7 +6050,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
free_commit_list(commits);
free_commit_list(tips);
strbuf_release(&label);
strbuf_release(&label_from_message);
strbuf_release(&oneline);
strbuf_release(&buf);
@ -6403,14 +6412,6 @@ static int add_decorations_to_list(const struct commit *commit,
static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
{
int i, res;
static struct string_list decorate_refs_exclude = STRING_LIST_INIT_NODUP;
static struct string_list decorate_refs_exclude_config = STRING_LIST_INIT_NODUP;
static struct string_list decorate_refs_include = STRING_LIST_INIT_NODUP;
struct decoration_filter decoration_filter = {
.include_ref_pattern = &decorate_refs_include,
.exclude_ref_pattern = &decorate_refs_exclude,
.exclude_ref_config_pattern = &decorate_refs_exclude_config,
};
struct todo_add_branch_context ctx = {
.buf = &todo_list->buf,
.refs_to_oids = STRING_LIST_INIT_DUP,
@ -6419,8 +6420,7 @@ static int todo_list_add_update_ref_commands(struct todo_list *todo_list)
ctx.items_alloc = 2 * todo_list->nr + 1;
ALLOC_ARRAY(ctx.items, ctx.items_alloc);
string_list_append(&decorate_refs_include, "refs/heads/");
load_ref_decorations(&decoration_filter, 0);
load_branch_decorations();
for (i = 0; i < todo_list->nr; ) {
struct todo_item *item = &todo_list->items[i];

View File

@ -1870,7 +1870,7 @@ test_expect_success '--update-refs adds commands with --rebase-merges' '
pick $(git log -1 --format=%h branch2~1) F
pick $(git log -1 --format=%h branch2) I
update-ref refs/heads/branch2
label merge
label branch2
reset onto
pick $(git log -1 --format=%h refs/heads/second) J
update-ref refs/heads/second
@ -1881,7 +1881,7 @@ test_expect_success '--update-refs adds commands with --rebase-merges' '
update-ref refs/heads/third
pick $(git log -1 --format=%h HEAD~2) M
update-ref refs/heads/no-conflict-branch
merge -C $(git log -1 --format=%h HEAD~1) merge # merge
merge -C $(git log -1 --format=%h HEAD~1) branch2 # merge
update-ref refs/heads/merge-branch
EOF

View File

@ -108,19 +108,19 @@ test_expect_success 'generate correct todo list' '
reset onto
pick $b B
label E
label first
reset onto
pick $c C
label branch-point
pick $f F
pick $g G
label H
label second
reset branch-point # C
pick $d D
merge -C $e E # E
merge -C $h H # H
merge -C $e first # E
merge -C $h second # H
EOF
@ -462,11 +462,11 @@ test_expect_success 'A root commit can be a cousin, treat it that way' '
'
test_expect_success 'labels that are object IDs are rewritten' '
git checkout -b third B &&
git checkout --detach B &&
test_commit I &&
third=$(git rev-parse HEAD) &&
git checkout -b labels main &&
git merge --no-commit third &&
git merge --no-commit $third &&
test_tick &&
git commit -m "Merge commit '\''$third'\'' into labels" &&
echo noop >script-from-scratch &&