sequencer: truncate labels to accommodate loose refs

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 <mped@demant.com>
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Mark Ruvald Pedersen
2023-08-10 16:34:59 +00:00
committed by Junio C Hamano
parent fb7d80edca
commit 7481d2bfca
2 changed files with 40 additions and 5 deletions

View File

@@ -422,6 +422,10 @@ char *gitdirname(char *);
#define PATH_MAX 4096 #define PATH_MAX 4096
#endif #endif
#ifndef NAME_MAX
#define NAME_MAX 255
#endif
typedef uintmax_t timestamp_t; typedef uintmax_t timestamp_t;
#define PRItime PRIuMAX #define PRItime PRIuMAX
#define parse_timestamp strtoumax #define parse_timestamp strtoumax

View File

@@ -50,6 +50,15 @@
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" #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 sign_off_header[] = "Signed-off-by: ";
static const char cherry_picked_prefix[] = "(cherry picked from commit "; static const char cherry_picked_prefix[] = "(cherry picked from commit ";
@@ -5386,6 +5395,8 @@ static const char *label_oid(struct object_id *oid, const char *label,
} }
} else { } else {
struct strbuf *buf = &state->buf; 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 * Sanitize labels by replacing non-alpha-numeric characters
@@ -5394,14 +5405,34 @@ static const char *label_oid(struct object_id *oid, const char *label,
* *
* Note that we retain non-ASCII UTF-8 characters (identified * Note that we retain non-ASCII UTF-8 characters (identified
* via the most significant bit). They should be all acceptable * via the most significant bit). They should be all acceptable
* in file names. We do not validate the UTF-8 here, that's not * in file names.
* the job of this function. *
* 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++) for (; *label && buf->len + 1 < max_len; label++)
if ((*label & 0x80) || isalnum(*label)) if (isalnum(*label) ||
(!label_is_utf8 && (*label & 0x80)))
strbuf_addch(buf, *label); 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 */ /* 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, '-'); strbuf_addch(buf, '-');
if (!buf->len) { if (!buf->len) {
strbuf_addstr(buf, "rev-"); strbuf_addstr(buf, "rev-");