merge-tree: do not use strbuf_split*()

When reading merge instructions from the standard input, the program reads from the standard input, splits the line into tokens at whitespace, and trims each of them before using. We no longer need to use strbuf just for trimming, as string_list_split*() family can trim while splitting a string. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 31, 2025 at 15:54 UTC d33091220dadedfcb874d179fe164f507d5f09b2
1 file changed +16 -14
builtin/merge-tree.c
+16 -14
@@ -618,32 +618,34 @@ int cmd_merge_tree(int argc,
618 "--merge-base", "--stdin");
619 line_termination = '\0';
620 while (strbuf_getline_lf(&buf, stdin) != EOF) {
621 - struct strbuf **split;
621 + struct string_list split = STRING_LIST_INIT_NODUP;
622 const char *input_merge_base = NULL;
623
624 - split = strbuf_split(&buf, ' ');
625 - if (!split[0] || !split[1])
624 + string_list_split_in_place_f(&split, buf.buf, " ", -1,
625 + STRING_LIST_SPLIT_TRIM);
626 +
627 + if (split.nr < 2)
628 die(_("malformed input line: '%s'."), buf.buf);
627 - strbuf_rtrim(split[0]);
628 - strbuf_rtrim(split[1]);
629
630 /* parse the merge-base */
631 - if (!strcmp(split[1]->buf, "--")) {
632 - input_merge_base = split[0]->buf;
631 + if (!strcmp(split.items[1].string, "--")) {
632 + input_merge_base = split.items[0].string;
633 }
634
635 - if (input_merge_base && split[2] && split[3] && !split[4]) {
636 - strbuf_rtrim(split[2]);
637 - strbuf_rtrim(split[3]);
638 - real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
639 - } else if (!input_merge_base && !split[2]) {
640 - real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
635 + if (input_merge_base && split.nr == 4) {
636 + real_merge(&o, input_merge_base,
637 + split.items[2].string, split.items[3].string,
638 + prefix);
639 + } else if (!input_merge_base && split.nr == 2) {
640 + real_merge(&o, NULL,
641 + split.items[0].string, split.items[1].string,
642 + prefix);
643 } else {
644 die(_("malformed input line: '%s'."), buf.buf);
645 }
646 maybe_flush_or_die(stdout, "stdout");
647
646 - strbuf_list_free(split);
648 + string_list_clear(&split, 0);
649 }
650 strbuf_release(&buf);
651