Skip to content

Make git rebase -r's label generation more resilient #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4423,7 +4423,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
struct labels_entry *labels_entry;
struct string_entry *string_entry;
struct object_id dummy;
size_t len;
int i;

string_entry = oidmap_get(&state->commit2label, oid);
Expand All @@ -4443,10 +4442,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
* abbreviation for any uninteresting commit's names that does not
* clash with any other label.
*/
strbuf_reset(&state->buf);
if (!label) {
char *p;

strbuf_reset(&state->buf);
strbuf_grow(&state->buf, GIT_MAX_HEXSZ);
label = p = state->buf.buf;

Expand All @@ -4469,32 +4468,55 @@ static const char *label_oid(struct object_id *oid, const char *label,
p[i] = save;
}
}
} else if (((len = strlen(label)) == the_hash_algo->hexsz &&
!get_oid_hex(label, &dummy)) ||
(len == 1 && *label == '#') ||
hashmap_get_from_hash(&state->labels,
strihash(label), label)) {
} else {
struct strbuf *buf = &state->buf;

/*
* If the label already exists, or if the label is a valid full
* OID, or the label is a '#' (which we use as a separator
* between merge heads and oneline), we append a dash and a
* number to make it unique.
* Sanitize labels by replacing non-alpha-numeric characters
* (including white-space ones) by dashes, as they might be
* illegal in file names (and hence in ref names).
*
* Note that we retain non-ASCII UTF-8 characters (identified
* via the most significant bit). They should be all acceptable
* in file names. We do not validate the UTF-8 here, that's not
* the job of this function.
*/
struct strbuf *buf = &state->buf;
for (; *label; label++)
if ((*label & 0x80) || isalnum(*label))
strbuf_addch(buf, *label);
/* avoid leading dash and double-dashes */
else if (buf->len && buf->buf[buf->len - 1] != '-')
strbuf_addch(buf, '-');
if (!buf->len) {
strbuf_addstr(buf, "rev-");
strbuf_add_unique_abbrev(buf, oid, default_abbrev);
}
label = buf->buf;

strbuf_reset(buf);
strbuf_add(buf, label, len);
if ((buf->len == the_hash_algo->hexsz &&
!get_oid_hex(label, &dummy)) ||
(buf->len == 1 && *label == '#') ||
hashmap_get_from_hash(&state->labels,
strihash(label), label)) {
/*
* If the label already exists, or if the label is a
* valid full OID, or the label is a '#' (which we use
* as a separator between merge heads and oneline), we
* append a dash and a number to make it unique.
*/
size_t len = buf->len;

for (i = 2; ; i++) {
strbuf_setlen(buf, len);
strbuf_addf(buf, "-%d", i);
if (!hashmap_get_from_hash(&state->labels,
strihash(buf->buf),
buf->buf))
break;
}
for (i = 2; ; i++) {
strbuf_setlen(buf, len);
strbuf_addf(buf, "-%d", i);
if (!hashmap_get_from_hash(&state->labels,
strihash(buf->buf),
buf->buf))
break;
}

label = buf->buf;
label = buf->buf;
}
}

FLEX_ALLOC_STR(labels_entry, label, label);
Expand Down Expand Up @@ -4596,10 +4618,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
else
strbuf_addbuf(&label, &oneline);

for (p1 = label.buf; *p1; p1++)
if (isspace(*p1))
*(char *)p1 = '-';

strbuf_reset(&buf);
strbuf_addf(&buf, "%s -C %s",
cmd_merge, oid_to_hex(&commit->object.oid));
Expand Down
6 changes: 6 additions & 0 deletions t/t3430-rebase-merges.sh
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,10 @@ test_expect_success '--rebase-merges with strategies' '
test_cmp expect G.t
'

test_expect_success '--rebase-merges with commit that can generate bad characters for filename' '
git checkout -b colon-in-label E &&
git merge -m "colon: this should work" G &&
git rebase --rebase-merges --force-rebase E
'

test_done