rebase: allow overriding the maximal length of the generated labels

With this change, users can override the compiled-in default for the maximal length of the label names generated by `git rebase --rebase-merges`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Mark Ruvald Pedersen <mped@demant.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 10, 2023 at 16:35 UTC ac300bda10a137be06b7ac7c41452dbf8864f134
3 files changed +23 -2
Documentation/config/rebase.txt
+6
@@ -77,3 +77,9 @@ rebase.rebaseMerges::
77 equivalent to `--no-rebase-merges`. Passing `--rebase-merges` on the
78 command line, with or without an argument, overrides any
79 `rebase.rebaseMerges` configuration.
80 +
81 +rebase.maxLabelLength::
82 + When generating label names from commit subjects, truncate the names to
83 + this length. By default, the names are truncated to a little less than
84 + `NAME_MAX` (to allow e.g. `.lock` files to be written for the
85 + corresponding loose refs).
sequencer.c
+6 -2
@@ -5339,6 +5339,7 @@ struct label_state {
5339 struct oidmap commit2label;
5340 struct hashmap labels;
5341 struct strbuf buf;
5342 + int max_label_length;
5343 };
5344
5345 static const char *label_oid(struct object_id *oid, const char *label,
@@ -5396,7 +5397,7 @@ static const char *label_oid(struct object_id *oid, const char *label,
5397 } else {
5398 struct strbuf *buf = &state->buf;
5399 int label_is_utf8 = 1; /* start with this assumption */
5399 - size_t max_len = buf->len + GIT_MAX_LABEL_LENGTH;
5400 + size_t max_len = buf->len + state->max_label_length;
5401
5402 /*
5403 * Sanitize labels by replacing non-alpha-numeric characters
@@ -5494,7 +5495,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,
5495 struct string_entry *entry;
5496 struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT,
5497 shown = OIDSET_INIT;
5497 - struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT };
5498 + struct label_state state =
5499 + { OIDMAP_INIT, { NULL }, STRBUF_INIT, GIT_MAX_LABEL_LENGTH };
5500
5501 int abbr = flags & TODO_LIST_ABBREVIATE_CMDS;
5502 const char *cmd_pick = abbr ? "p" : "pick",
@@ -5502,6 +5504,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,
5504 *cmd_reset = abbr ? "t" : "reset",
5505 *cmd_merge = abbr ? "m" : "merge";
5506
5507 + git_config_get_int("rebase.maxlabellength", &state.max_label_length);
5508 +
5509 oidmap_init(&commit2todo, 0);
5510 oidmap_init(&state.commit2label, 0);
5511 hashmap_init(&state.labels, labels_cmp, NULL, 0);
t/t3430-rebase-merges.sh
+11
@@ -586,4 +586,15 @@ test_expect_success 'progress shows the correct total' '
586 test_line_count = 14 progress
587 '
588
589 +test_expect_success 'truncate label names' '
590 + commit=$(git commit-tree -p HEAD^ -p HEAD -m "0123456789 我 123" HEAD^{tree}) &&
591 + git merge --ff-only $commit &&
592 +
593 + done="$(git rev-parse --git-path rebase-merge/done)" &&
594 + git -c rebase.maxLabelLength=14 rebase --rebase-merges -x "cp \"$done\" out" --root &&
595 + grep "label 0123456789-我$" out &&
596 + git -c rebase.maxLabelLength=13 rebase --rebase-merges -x "cp \"$done\" out" --root &&
597 + grep "label 0123456789-$" out
598 +'
599 +
600 test_done