Skip to content

Commit 867bc1d

Browse files
dschogitster
authored andcommitted
rebase-merges: move labels' whitespace mangling into label_oid()
One of the trickier aspects of the design of `git rebase --rebase-merges` is the way labels are generated for the initial todo list: those labels are supposed to be intuitive and first and foremost unique. To that end, `label_oid()` appends a unique suffix when necessary. Those labels not only need to be unique, but they also need to be valid refs. To make sure of that, `make_script_with_merges()` replaces whitespace by dashes. That would appear to be the wrong layer for that sanitizing step, though: all callers of `label_oid()` should get that same benefit. Even if it does not make a difference currently (the only called of `label_oid()` that passes a label that might need to be sanitized _is_ `make_script_with_merges()`), let's move the responsibility for sanitizing labels into the `label_oid()` function. This commit is best viewed with `-w` because it unfortunately needs to change the indentation of a large block of code in `label_oid()`. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9f6f3b commit 867bc1d

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

sequencer.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,7 +4423,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
44234423
struct labels_entry *labels_entry;
44244424
struct string_entry *string_entry;
44254425
struct object_id dummy;
4426-
size_t len;
44274426
int i;
44284427

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

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

@@ -4469,32 +4468,37 @@ static const char *label_oid(struct object_id *oid, const char *label,
44694468
p[i] = save;
44704469
}
44714470
}
4472-
} else if (((len = strlen(label)) == the_hash_algo->hexsz &&
4473-
!get_oid_hex(label, &dummy)) ||
4474-
(len == 1 && *label == '#') ||
4475-
hashmap_get_from_hash(&state->labels,
4476-
strihash(label), label)) {
4477-
/*
4478-
* If the label already exists, or if the label is a valid full
4479-
* OID, or the label is a '#' (which we use as a separator
4480-
* between merge heads and oneline), we append a dash and a
4481-
* number to make it unique.
4482-
*/
4471+
} else {
44834472
struct strbuf *buf = &state->buf;
44844473

4485-
strbuf_reset(buf);
4486-
strbuf_add(buf, label, len);
4474+
for (; *label; label++)
4475+
strbuf_addch(buf, isspace(*label) ? '-' : *label);
4476+
label = buf->buf;
44874477

4488-
for (i = 2; ; i++) {
4489-
strbuf_setlen(buf, len);
4490-
strbuf_addf(buf, "-%d", i);
4491-
if (!hashmap_get_from_hash(&state->labels,
4492-
strihash(buf->buf),
4493-
buf->buf))
4494-
break;
4495-
}
4478+
if ((buf->len == the_hash_algo->hexsz &&
4479+
!get_oid_hex(label, &dummy)) ||
4480+
(buf->len == 1 && *label == '#') ||
4481+
hashmap_get_from_hash(&state->labels,
4482+
strihash(label), label)) {
4483+
/*
4484+
* If the label already exists, or if the label is a
4485+
* valid full OID, or the label is a '#' (which we use
4486+
* as a separator between merge heads and oneline), we
4487+
* append a dash and a number to make it unique.
4488+
*/
4489+
size_t len = buf->len;
44964490

4497-
label = buf->buf;
4491+
for (i = 2; ; i++) {
4492+
strbuf_setlen(buf, len);
4493+
strbuf_addf(buf, "-%d", i);
4494+
if (!hashmap_get_from_hash(&state->labels,
4495+
strihash(buf->buf),
4496+
buf->buf))
4497+
break;
4498+
}
4499+
4500+
label = buf->buf;
4501+
}
44984502
}
44994503

45004504
FLEX_ALLOC_STR(labels_entry, label, label);
@@ -4596,10 +4600,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
45964600
else
45974601
strbuf_addbuf(&label, &oneline);
45984602

4599-
for (p1 = label.buf; *p1; p1++)
4600-
if (isspace(*p1))
4601-
*(char *)p1 = '-';
4602-
46034603
strbuf_reset(&buf);
46044604
strbuf_addf(&buf, "%s -C %s",
46054605
cmd_merge, oid_to_hex(&commit->object.oid));

0 commit comments

Comments
 (0)