From patchwork Thu Aug 10 16:34:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Ruvald Pedersen X-Patchwork-Id: 13349727 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5557AC04A6A for ; Thu, 10 Aug 2023 16:35:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235109AbjHJQfN (ORCPT ); Thu, 10 Aug 2023 12:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234018AbjHJQfG (ORCPT ); Thu, 10 Aug 2023 12:35:06 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E474190 for ; Thu, 10 Aug 2023 09:35:03 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3fe2d218eedso10389455e9.0 for ; Thu, 10 Aug 2023 09:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691685302; x=1692290102; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=LfaR1DsJbmzySd8MAZZ0uJQWuPOxdrx72NBoPEpVV2w=; b=gCpevvaQdD+60MQRN1Mwx7Lzq6doOInerulWeXQFdOkpUVGtKTxsEQR8Imv3i1X3ro PfAbwx6lp/EsCNR+Iw0SuEDPB8Urw5leMHGsZeSCR720jaCbQ9Ix5xtY+wu2qEn7HPVQ 2Hoi0ii/Y/MYTzrfqB0Se5tAEJKvaf6GNSK4ntLmHtjxLnWxuJoBSD9V7gnmA3KEbzg3 wa/l9vUj4k0EFRbRbqJOOoy0kEaNuu8IQ2x2M113FHwu/6gugy5qmddvcH8oC87TLoU9 N8Qd89uSNpgSjWySRuQ24x0wC+YTw1rifY+wgE4xI/9XjxU4TsmGWwLB3mGyQ7Cf/0TI vF+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691685302; x=1692290102; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LfaR1DsJbmzySd8MAZZ0uJQWuPOxdrx72NBoPEpVV2w=; b=MkGgQjJY1WgwYtzODAb6Luf/gQDCep8aGMRn0CSvAO5hEjKZbGmI2IH/EjQOBQ1YoQ s53yoz7cbj2dM0HC72lqoIrYXe6KzGQ+BEvESzuLE8OPmjXPNO0jwETSGDfvYJmUTUl2 /2K1NWq8k3tdaJCZWqbtmjt5dwKmV87nwSVeMI8Qi1HkwJKYw9Q8MiSVw0FU53wnGhlX 0cErM6w9t74chdZGfDKhYpug6E+0YBuHtgr53SvY25nBa7FJLP3qcjyJHBzOTU7sgQ8s YHUa8OLVmzTDHGHXb92x15IOedzPj60C5ah8aOo2JzhRGBYCqxnLjjfSkdEzlBoj59eO sXUA== X-Gm-Message-State: AOJu0YybonGxIWXcsG/v2C7ragHjRhm0EoOY7/Pt0R1KyTrD2Rn9AB5d 4PfUAh9NzXpzAZuOywgYABN3tMkTTmA= X-Google-Smtp-Source: AGHT+IEBn954YO14LQrYD5QHNdtzZBSPq7YcwzuJk6MXks4Zn7j0XSqB2IyL1LRXBxpySoKOtWJpFA== X-Received: by 2002:a05:600c:2196:b0:3fe:108d:7f88 with SMTP id e22-20020a05600c219600b003fe108d7f88mr2285238wme.36.1691685302009; Thu, 10 Aug 2023 09:35:02 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k1-20020a05600c0b4100b003fa95f328afsm5536078wmr.29.2023.08.10.09.35.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:35:01 -0700 (PDT) Message-ID: <4971e3c52504bf965aa754c9a5d31abddbcc1466.1691685300.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 10 Aug 2023 16:34:59 +0000 Subject: [PATCH 1/2] sequencer: truncate labels to accommodate loose refs Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Mark Ruvald Pedersen , Mark Ruvald Pedersen Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Mark Ruvald Pedersen From: Mark Ruvald Pedersen Some commits may have unusually long subject lines. When those subject lines are used as labels in the `--rebase-merges` mode of `git rebase`, they can cause errors when writing the corresponding loose refs because most file systems have a maximal file name length of 255 (`NAME_MAX`). The symptom looks like this: $ git rebase --continue error: cannot lock ref 'refs/rewritten/SANITIZED-SUBJECT': Unable to create '.git/refs/rewritten/SANITIZED-SUBJECT.lock': File name too long - where SANITIZED-SUBJECT is very long Let's accommodate this situation by truncating the labels. Care must be taken in case the subject line contains multi-byte characters so as not to truncate in the middle of a character. Signed-off-by: Mark Ruvald Pedersen Signed-off-by: Johannes Schindelin --- git-compat-util.h | 4 ++++ sequencer.c | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index d32aa754ae1..3e7a59b5ff1 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -422,6 +422,10 @@ char *gitdirname(char *); #define PATH_MAX 4096 #endif +#ifndef NAME_MAX +#define NAME_MAX 255 +#endif + typedef uintmax_t timestamp_t; #define PRItime PRIuMAX #define parse_timestamp strtoumax diff --git a/sequencer.c b/sequencer.c index adc9cfb4df3..be837bd2948 100644 --- a/sequencer.c +++ b/sequencer.c @@ -51,6 +51,15 @@ #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" +/* + * To accommodate common filesystem limitations, where the loose refs' file + * names must not exceed `NAME_MAX`, the labels generated by `git rebase + * --rebase-merges` need to be truncated if the corresponding commit subjects + * are too long. + * Add some margin to stay clear from reaching `NAME_MAX`. + */ +#define GIT_MAX_LABEL_LENGTH ((NAME_MAX) - (LOCK_SUFFIX_LEN) - 16) + static const char sign_off_header[] = "Signed-off-by: "; static const char cherry_picked_prefix[] = "(cherry picked from commit "; @@ -5396,6 +5405,8 @@ static const char *label_oid(struct object_id *oid, const char *label, } } else { struct strbuf *buf = &state->buf; + int label_is_utf8 = 1; /* start with this assumption */ + size_t max_len = buf->len + GIT_MAX_LABEL_LENGTH; /* * Sanitize labels by replacing non-alpha-numeric characters @@ -5404,14 +5415,34 @@ static const char *label_oid(struct object_id *oid, const char *label, * * Note that we retain non-ASCII UTF-8 characters (identified * via the most significant bit). They should be all acceptable - * in file names. We do not validate the UTF-8 here, that's not - * the job of this function. + * in file names. + * + * As we will use the labels as names of (loose) refs, it is + * vital that the name not be longer than the maximum component + * size of the file system (`NAME_MAX`). We are careful to + * truncate the label accordingly, allowing for the `.lock` + * suffix and for the label to be UTF-8 encoded (i.e. we avoid + * truncating in the middle of a character). */ - for (; *label; label++) - if ((*label & 0x80) || isalnum(*label)) + for (; *label && buf->len + 1 < max_len; label++) + if (isalnum(*label) || + (!label_is_utf8 && (*label & 0x80))) strbuf_addch(buf, *label); + else if (*label & 0x80) { + const char *p = label; + + utf8_width(&p, NULL); + if (p) { + if (buf->len + (p - label) > max_len) + break; + strbuf_add(buf, label, p - label); + label = p - 1; + } else { + label_is_utf8 = 0; + strbuf_addch(buf, *label); + } /* avoid leading dash and double-dashes */ - else if (buf->len && buf->buf[buf->len - 1] != '-') + } else if (buf->len && buf->buf[buf->len - 1] != '-') strbuf_addch(buf, '-'); if (!buf->len) { strbuf_addstr(buf, "rev-"); From patchwork Thu Aug 10 16:35:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Johannes Schindelin X-Patchwork-Id: 13349726 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6D6BC001E0 for ; Thu, 10 Aug 2023 16:35:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234052AbjHJQfH (ORCPT ); Thu, 10 Aug 2023 12:35:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230486AbjHJQfF (ORCPT ); Thu, 10 Aug 2023 12:35:05 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63CF1A8 for ; Thu, 10 Aug 2023 09:35:04 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3fe4ad22eb0so9820825e9.3 for ; Thu, 10 Aug 2023 09:35:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691685303; x=1692290103; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=iuFKXCQklQYyiL5v9bTDXwQ/6Db/+sCbn/ePu7oLcH4=; b=agE2SP4HeILm+pqDZsJlCVNSJti/XX6fqY6w8T0pULJxIe7TxNwtv/tUaO32ze18wz Ak15qB3GrOjSQLghNSBdNviHbbhDqXgjeEiDJZI50HJnNlM0k2gytqmFccMB2IxJgSxA 6D/74XbE7oisEij01xVPhXEhCehwxtdMLCQpubDEDcwLOWEAyIsd7MztfEnRaThbh+6o qH4SvA76zDl69tOJdWOv6T0LzOtyD0wi27binEFUJ4ydICYRZYEERgm+O2efwNPez5KZ i/03rGtiCqD/Lya392BmaKobLtOsXdbmcrCi8ga5sHojHmz8p47nSvovoniDvbAhOQ++ QamQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691685303; x=1692290103; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iuFKXCQklQYyiL5v9bTDXwQ/6Db/+sCbn/ePu7oLcH4=; b=Sa98ORtJhf+hvXulVuRCUt7ab1KNFkclpM6ZFo6xn307z0YACN0aBCgzFUDkDX7Zzv Ee67IonwoI2B/WuKeTFCRSfdTIjnqnJRot/VNwlikxtsJs9mFQVR9wqVQOqOMzMAdU1z rCqh5knPNOQqeHICBjKcPxAIlKT0deCBKvv7zKORlW44/PQCaY1B++2GSQ5LliIOCi4G HRif1amlb4pZC5F+I4khn1exbdOQ/GNpwt2Roib3x47QwexP61RnWeyji35Wwtvx6sRK Ehk7DylrR3ZsCYAvA8UAICaOsaXJ/qx/jhVMFkLlPpJrSHT4m3pU2gIRLhFlsEiVGTjV ovLg== X-Gm-Message-State: AOJu0YyBU2TxyIqfMgUfWG8uIf3DcfxvPY3Q5XECpViHYtgDnRWdy42g 71msB4RQ6X+yRvP3TaLe+g0nGM3XqKY= X-Google-Smtp-Source: AGHT+IHdk6v9JAiPpp50h0wKxOYIYLMi41cQXNV2uMmaCb3XYWdpan8Z6SNiFNYJ9XBzt9w9+9a/nA== X-Received: by 2002:a1c:7901:0:b0:3fb:b56b:470f with SMTP id l1-20020a1c7901000000b003fbb56b470fmr2444800wme.14.1691685302592; Thu, 10 Aug 2023 09:35:02 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 20-20020a05600c025400b003fa98908014sm5626371wmj.8.2023.08.10.09.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:35:02 -0700 (PDT) Message-ID: In-Reply-To: References: Date: Thu, 10 Aug 2023 16:35:00 +0000 Subject: [PATCH 2/2] rebase: allow overriding the maximal length of the generated labels Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Mark Ruvald Pedersen , Johannes Schindelin Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Johannes Schindelin From: Johannes Schindelin 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 Signed-off-by: Mark Ruvald Pedersen --- Documentation/config/rebase.txt | 6 ++++++ sequencer.c | 8 ++++++-- t/t3430-rebase-merges.sh | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Documentation/config/rebase.txt b/Documentation/config/rebase.txt index afaf6dad99b..9c248accec2 100644 --- a/Documentation/config/rebase.txt +++ b/Documentation/config/rebase.txt @@ -77,3 +77,9 @@ rebase.rebaseMerges:: equivalent to `--no-rebase-merges`. Passing `--rebase-merges` on the command line, with or without an argument, overrides any `rebase.rebaseMerges` configuration. + +rebase.maxLabelLength:: + When generating label names from commit subjects, truncate the names to + this length. By default, the names are truncated to a little less than + `NAME_MAX` (to allow e.g. `.lock` files to be written for the + corresponding loose refs). diff --git a/sequencer.c b/sequencer.c index be837bd2948..b1fc44f0321 100644 --- a/sequencer.c +++ b/sequencer.c @@ -5349,6 +5349,7 @@ struct label_state { struct oidmap commit2label; struct hashmap labels; struct strbuf buf; + int max_label_length; }; static const char *label_oid(struct object_id *oid, const char *label, @@ -5406,7 +5407,7 @@ static const char *label_oid(struct object_id *oid, const char *label, } else { struct strbuf *buf = &state->buf; int label_is_utf8 = 1; /* start with this assumption */ - size_t max_len = buf->len + GIT_MAX_LABEL_LENGTH; + size_t max_len = buf->len + state->max_label_length; /* * Sanitize labels by replacing non-alpha-numeric characters @@ -5504,7 +5505,8 @@ static int make_script_with_merges(struct pretty_print_context *pp, struct string_entry *entry; struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, shown = OIDSET_INIT; - struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT }; + struct label_state state = + { OIDMAP_INIT, { NULL }, STRBUF_INIT, GIT_MAX_LABEL_LENGTH }; int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; const char *cmd_pick = abbr ? "p" : "pick", @@ -5512,6 +5514,8 @@ static int make_script_with_merges(struct pretty_print_context *pp, *cmd_reset = abbr ? "t" : "reset", *cmd_merge = abbr ? "m" : "merge"; + git_config_get_int("rebase.maxlabellength", &state.max_label_length); + oidmap_init(&commit2todo, 0); oidmap_init(&state.commit2label, 0); hashmap_init(&state.labels, labels_cmp, NULL, 0); diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh index 96ae0edf1e1..ac5c390652f 100755 --- a/t/t3430-rebase-merges.sh +++ b/t/t3430-rebase-merges.sh @@ -586,4 +586,15 @@ test_expect_success 'progress shows the correct total' ' test_line_count = 14 progress ' +test_expect_success 'truncate label names' ' + commit=$(git commit-tree -p HEAD^ -p HEAD -m "0123456789 我 123" HEAD^{tree}) && + git merge --ff-only $commit && + + done="$(git rev-parse --git-path rebase-merge/done)" && + git -c rebase.maxLabelLength=14 rebase --rebase-merges -x "cp \"$done\" out" --root && + grep "label 0123456789-我$" out && + git -c rebase.maxLabelLength=13 rebase --rebase-merges -x "cp \"$done\" out" --root && + grep "label 0123456789-$" out +' + test_done