Skip to content

Commit 541d0d7

Browse files
committed
Merge branch 'la/trailer-cleanups' into maint-2.43
Code clean-up. * la/trailer-cleanups: trailer: use offsets for trailer_start/trailer_end trailer: find the end of the log message commit: ignore_non_trailer computes number of bytes to ignore
2 parents edf4c0d + de7c27a commit 541d0d7

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
900900
strbuf_stripspace(&sb, '\0');
901901

902902
if (signoff)
903-
append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
903+
append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
904904

905905
if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
906906
die_errno(_("could not write commit template"));

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
869869
_(no_scissors_editor_comment), comment_line_char);
870870
}
871871
if (signoff)
872-
append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
872+
append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0);
873873
write_merge_heads(remoteheads);
874874
write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
875875
if (run_commit_hook(0 < option_edit, get_index_file(), NULL,

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
17831783
* Returns the number of bytes from the tail to ignore, to be fed as
17841784
* the second parameter to append_signoff().
17851785
*/
1786-
size_t ignore_non_trailer(const char *buf, size_t len)
1786+
size_t ignored_log_message_bytes(const char *buf, size_t len)
17871787
{
17881788
size_t boc = 0;
17891789
size_t bol = 0;

commit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ const char *find_header_mem(const char *msg, size_t len,
294294
const char *find_commit_header(const char *msg, const char *key,
295295
size_t *out_len);
296296

297-
/* Find the end of the log message, the right place for a new trailer. */
298-
size_t ignore_non_trailer(const char *buf, size_t len);
297+
/* Find the number of bytes to ignore from the end of a log message. */
298+
size_t ignored_log_message_bytes(const char *buf, size_t len);
299299

300300
typedef int (*each_mergetag_fn)(struct commit *commit, struct commit_extra_header *extra,
301301
void *cb_data);

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
340340
if (ignore_footer)
341341
sb->buf[sb->len - ignore_footer] = saved_char;
342342

343-
if (info.trailer_start == info.trailer_end)
343+
if (info.trailer_block_start == info.trailer_block_end)
344344
return 0;
345345

346346
for (i = 0; i < info.trailer_nr; i++)

trailer.c

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -817,28 +817,56 @@ static ssize_t last_line(const char *buf, size_t len)
817817
}
818818

819819
/*
820-
* Return the position of the start of the patch or the length of str if there
821-
* is no patch in the message.
820+
* Find the end of the log message as an offset from the start of the input
821+
* (where callers of this function are interested in looking for a trailers
822+
* block in the same input). We have to consider two categories of content that
823+
* can come at the end of the input which we want to ignore (because they don't
824+
* belong in the log message):
825+
*
826+
* (1) the "patch part" which begins with a "---" divider and has patch
827+
* information (like the output of git-format-patch), and
828+
*
829+
* (2) any trailing comment lines, blank lines like in the output of "git
830+
* commit -v", or stuff below the "cut" (scissor) line.
831+
*
832+
* As a formula, the situation looks like this:
833+
*
834+
* INPUT = LOG MESSAGE + IGNORED
835+
*
836+
* where IGNORED can be either of the two categories described above. It may be
837+
* that there is nothing to ignore. Now it may be the case that the LOG MESSAGE
838+
* contains a trailer block, but that's not the concern of this function.
822839
*/
823-
static size_t find_patch_start(const char *str)
840+
static size_t find_end_of_log_message(const char *input, int no_divider)
824841
{
842+
size_t end;
825843
const char *s;
826844

827-
for (s = str; *s; s = next_line(s)) {
845+
/* Assume the naive end of the input is already what we want. */
846+
end = strlen(input);
847+
848+
if (no_divider)
849+
return end;
850+
851+
/* Optionally skip over any patch part ("---" line and below). */
852+
for (s = input; *s; s = next_line(s)) {
828853
const char *v;
829854

830-
if (skip_prefix(s, "---", &v) && isspace(*v))
831-
return s - str;
855+
if (skip_prefix(s, "---", &v) && isspace(*v)) {
856+
end = s - input;
857+
break;
858+
}
832859
}
833860

834-
return s - str;
861+
/* Skip over other ignorable bits. */
862+
return end - ignored_log_message_bytes(input, end);
835863
}
836864

837865
/*
838866
* Return the position of the first trailer line or len if there are no
839867
* trailers.
840868
*/
841-
static size_t find_trailer_start(const char *buf, size_t len)
869+
static size_t find_trailer_block_start(const char *buf, size_t len)
842870
{
843871
const char *s;
844872
ssize_t end_of_title, l;
@@ -933,12 +961,6 @@ static size_t find_trailer_start(const char *buf, size_t len)
933961
return len;
934962
}
935963

936-
/* Return the position of the end of the trailers. */
937-
static size_t find_trailer_end(const char *buf, size_t len)
938-
{
939-
return len - ignore_non_trailer(buf, len);
940-
}
941-
942964
static int ends_with_blank_line(const char *buf, size_t len)
943965
{
944966
ssize_t ll = last_line(buf, len);
@@ -1060,7 +1082,6 @@ void process_trailers(const char *file,
10601082
LIST_HEAD(head);
10611083
struct strbuf sb = STRBUF_INIT;
10621084
struct trailer_info info;
1063-
size_t trailer_end;
10641085
FILE *outfile = stdout;
10651086

10661087
ensure_configured();
@@ -1071,11 +1092,10 @@ void process_trailers(const char *file,
10711092
outfile = create_in_place_tempfile(file);
10721093

10731094
parse_trailers(&info, sb.buf, &head, opts);
1074-
trailer_end = info.trailer_end - sb.buf;
10751095

10761096
/* Print the lines before the trailers */
10771097
if (!opts->only_trailers)
1078-
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1098+
fwrite(sb.buf, 1, info.trailer_block_start, outfile);
10791099

10801100
if (!opts->only_trailers && !info.blank_line_before_trailer)
10811101
fprintf(outfile, "\n");
@@ -1097,7 +1117,7 @@ void process_trailers(const char *file,
10971117

10981118
/* Print the lines after the trailers as is */
10991119
if (!opts->only_trailers)
1100-
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1120+
fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile);
11011121

11021122
if (opts->in_place)
11031123
if (rename_tempfile(&trailers_tempfile, file))
@@ -1109,24 +1129,19 @@ void process_trailers(const char *file,
11091129
void trailer_info_get(struct trailer_info *info, const char *str,
11101130
const struct process_trailer_options *opts)
11111131
{
1112-
int patch_start, trailer_end, trailer_start;
1132+
size_t end_of_log_message = 0, trailer_block_start = 0;
11131133
struct strbuf **trailer_lines, **ptr;
11141134
char **trailer_strings = NULL;
11151135
size_t nr = 0, alloc = 0;
11161136
char **last = NULL;
11171137

11181138
ensure_configured();
11191139

1120-
if (opts->no_divider)
1121-
patch_start = strlen(str);
1122-
else
1123-
patch_start = find_patch_start(str);
1124-
1125-
trailer_end = find_trailer_end(str, patch_start);
1126-
trailer_start = find_trailer_start(str, trailer_end);
1140+
end_of_log_message = find_end_of_log_message(str, opts->no_divider);
1141+
trailer_block_start = find_trailer_block_start(str, end_of_log_message);
11271142

1128-
trailer_lines = strbuf_split_buf(str + trailer_start,
1129-
trailer_end - trailer_start,
1143+
trailer_lines = strbuf_split_buf(str + trailer_block_start,
1144+
end_of_log_message - trailer_block_start,
11301145
'\n',
11311146
0);
11321147
for (ptr = trailer_lines; *ptr; ptr++) {
@@ -1147,9 +1162,9 @@ void trailer_info_get(struct trailer_info *info, const char *str,
11471162
strbuf_list_free(trailer_lines);
11481163

11491164
info->blank_line_before_trailer = ends_with_blank_line(str,
1150-
trailer_start);
1151-
info->trailer_start = str + trailer_start;
1152-
info->trailer_end = str + trailer_end;
1165+
trailer_block_start);
1166+
info->trailer_block_start = trailer_block_start;
1167+
info->trailer_block_end = end_of_log_message;
11531168
info->trailers = trailer_strings;
11541169
info->trailer_nr = nr;
11551170
}
@@ -1164,6 +1179,7 @@ void trailer_info_release(struct trailer_info *info)
11641179

11651180
static void format_trailer_info(struct strbuf *out,
11661181
const struct trailer_info *info,
1182+
const char *msg,
11671183
const struct process_trailer_options *opts)
11681184
{
11691185
size_t origlen = out->len;
@@ -1173,8 +1189,8 @@ static void format_trailer_info(struct strbuf *out,
11731189
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
11741190
!opts->separator && !opts->key_only && !opts->value_only &&
11751191
!opts->key_value_separator) {
1176-
strbuf_add(out, info->trailer_start,
1177-
info->trailer_end - info->trailer_start);
1192+
strbuf_add(out, msg + info->trailer_block_start,
1193+
info->trailer_block_end - info->trailer_block_start);
11781194
return;
11791195
}
11801196

@@ -1228,7 +1244,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
12281244
struct trailer_info info;
12291245

12301246
trailer_info_get(&info, msg, opts);
1231-
format_trailer_info(out, &info, opts);
1247+
format_trailer_info(out, &info, msg, opts);
12321248
trailer_info_release(&info);
12331249
}
12341250

trailer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
3232
struct trailer_info {
3333
/*
3434
* True if there is a blank line before the location pointed to by
35-
* trailer_start.
35+
* trailer_block_start.
3636
*/
3737
int blank_line_before_trailer;
3838

3939
/*
40-
* Pointers to the start and end of the trailer block found. If there
41-
* is no trailer block found, these 2 pointers point to the end of the
42-
* input string.
40+
* Offsets to the trailer block start and end positions in the input
41+
* string. If no trailer block is found, these are both set to the
42+
* "true" end of the input (find_end_of_log_message()).
4343
*/
44-
const char *trailer_start, *trailer_end;
44+
size_t trailer_block_start, trailer_block_end;
4545

4646
/*
4747
* Array of trailers found.

0 commit comments

Comments
 (0)