Skip to content

Commit 7481d2b

Browse files
Mark Ruvald Pedersengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit 7481d2b

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ char *gitdirname(char *);
422422
#define PATH_MAX 4096
423423
#endif
424424

425+
#ifndef NAME_MAX
426+
#define NAME_MAX 255
427+
#endif
428+
425429
typedef uintmax_t timestamp_t;
426430
#define PRItime PRIuMAX
427431
#define parse_timestamp strtoumax

sequencer.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050

5151
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
5252

53+
/*
54+
* To accommodate common filesystem limitations, where the loose refs' file
55+
* names must not exceed `NAME_MAX`, the labels generated by `git rebase
56+
* --rebase-merges` need to be truncated if the corresponding commit subjects
57+
* are too long.
58+
* Add some margin to stay clear from reaching `NAME_MAX`.
59+
*/
60+
#define GIT_MAX_LABEL_LENGTH ((NAME_MAX) - (LOCK_SUFFIX_LEN) - 16)
61+
5362
static const char sign_off_header[] = "Signed-off-by: ";
5463
static const char cherry_picked_prefix[] = "(cherry picked from commit ";
5564

@@ -5386,6 +5395,8 @@ static const char *label_oid(struct object_id *oid, const char *label,
53865395
}
53875396
} else {
53885397
struct strbuf *buf = &state->buf;
5398+
int label_is_utf8 = 1; /* start with this assumption */
5399+
size_t max_len = buf->len + GIT_MAX_LABEL_LENGTH;
53895400

53905401
/*
53915402
* Sanitize labels by replacing non-alpha-numeric characters
@@ -5394,14 +5405,34 @@ static const char *label_oid(struct object_id *oid, const char *label,
53945405
*
53955406
* Note that we retain non-ASCII UTF-8 characters (identified
53965407
* via the most significant bit). They should be all acceptable
5397-
* in file names. We do not validate the UTF-8 here, that's not
5398-
* the job of this function.
5408+
* in file names.
5409+
*
5410+
* As we will use the labels as names of (loose) refs, it is
5411+
* vital that the name not be longer than the maximum component
5412+
* size of the file system (`NAME_MAX`). We are careful to
5413+
* truncate the label accordingly, allowing for the `.lock`
5414+
* suffix and for the label to be UTF-8 encoded (i.e. we avoid
5415+
* truncating in the middle of a character).
53995416
*/
5400-
for (; *label; label++)
5401-
if ((*label & 0x80) || isalnum(*label))
5417+
for (; *label && buf->len + 1 < max_len; label++)
5418+
if (isalnum(*label) ||
5419+
(!label_is_utf8 && (*label & 0x80)))
54025420
strbuf_addch(buf, *label);
5421+
else if (*label & 0x80) {
5422+
const char *p = label;
5423+
5424+
utf8_width(&p, NULL);
5425+
if (p) {
5426+
if (buf->len + (p - label) > max_len)
5427+
break;
5428+
strbuf_add(buf, label, p - label);
5429+
label = p - 1;
5430+
} else {
5431+
label_is_utf8 = 0;
5432+
strbuf_addch(buf, *label);
5433+
}
54035434
/* avoid leading dash and double-dashes */
5404-
else if (buf->len && buf->buf[buf->len - 1] != '-')
5435+
} else if (buf->len && buf->buf[buf->len - 1] != '-')
54055436
strbuf_addch(buf, '-');
54065437
if (!buf->len) {
54075438
strbuf_addstr(buf, "rev-");

0 commit comments

Comments
 (0)