@matheusbsbs / matheus-bagliano / commits / 867bc1d236
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 17, 2019 at 23:16 UTC 867bc1d236b8955414b3dbacf28c7f0c2e337cf4
1 file changed +28 -28
sequencer.c
+28 -28
@@ -4423,7 +4423,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
4423 struct labels_entry *labels_entry;
4424 struct string_entry *string_entry;
4425 struct object_id dummy;
4426 - size_t len;
4426 int i;
4427
4428 string_entry = oidmap_get(&state->commit2label, oid);
@@ -4443,10 +4442,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
4442 * abbreviation for any uninteresting commit's names that does not
4443 * clash with any other label.
4444 */
4445 + strbuf_reset(&state->buf);
4446 if (!label) {
4447 char *p;
4448
4449 - strbuf_reset(&state->buf);
4449 strbuf_grow(&state->buf, GIT_MAX_HEXSZ);
4450 label = p = state->buf.buf;
4451
@@ -4469,32 +4468,37 @@ static const char *label_oid(struct object_id *oid, const char *label,
4468 p[i] = save;
4469 }
4470 }
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 {
4472 struct strbuf *buf = &state->buf;
4473
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;
4477
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;
4490
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 + }
4502 }
4503
4504 FLEX_ALLOC_STR(labels_entry, label, label);
@@ -4596,10 +4600,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
4600 else
4601 strbuf_addbuf(&label, &oneline);
4602
4599 - for (p1 = label.buf; *p1; p1++)
4600 - if (isspace(*p1))
4601 - *(char *)p1 = '-';
4602 -
4603 strbuf_reset(&buf);
4604 strbuf_addf(&buf, "%s -C %s",
4605 cmd_merge, oid_to_hex(&commit->object.oid));