Skip to content

Commit ac79a02

Browse files
dschoGit for Windows Build Agent
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]>
1 parent 9f18c93 commit ac79a02

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
@@ -4425,7 +4425,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
44254425
struct labels_entry *labels_entry;
44264426
struct string_entry *string_entry;
44274427
struct object_id dummy;
4428-
size_t len;
44294428
int i;
44304429

44314430
string_entry = oidmap_get(&state->commit2label, oid);
@@ -4445,10 +4444,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
44454444
* abbreviation for any uninteresting commit's names that does not
44464445
* clash with any other label.
44474446
*/
4447+
strbuf_reset(&state->buf);
44484448
if (!label) {
44494449
char *p;
44504450

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

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

4487-
strbuf_reset(buf);
4488-
strbuf_add(buf, label, len);
4476+
for (; *label; label++)
4477+
strbuf_addch(buf, isspace(*label) ? '-' : *label);
4478+
label = buf->buf;
44894479

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

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

45024506
FLEX_ALLOC_STR(labels_entry, label, label);
@@ -4598,10 +4602,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
45984602
else
45994603
strbuf_addbuf(&label, &oneline);
46004604

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

0 commit comments

Comments
 (0)