trailer: use offsets for trailer_start/trailer_end

Previously these fields in the trailer_info struct were of type "const
char *" and pointed to positions in the input string directly (to the
start and end positions of the trailer block).

Use offsets to make the intended usage less ambiguous. We only need to
reference the input string in format_trailer_info(), so update that
function to take a pointer to the input.

Signed-off-by: Linus Arver <linusa@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Linus Arver
2023-09-09 06:16:17 +00:00
committed by Junio C Hamano
parent d2be104085
commit b5e75f87b5
2 changed files with 11 additions and 13 deletions

View File

@@ -1055,7 +1055,6 @@ void process_trailers(const char *file,
LIST_HEAD(head);
struct strbuf sb = STRBUF_INIT;
struct trailer_info info;
size_t trailer_end;
FILE *outfile = stdout;
ensure_configured();
@@ -1066,11 +1065,10 @@ void process_trailers(const char *file,
outfile = create_in_place_tempfile(file);
parse_trailers(&info, sb.buf, &head, opts);
trailer_end = info.trailer_end - sb.buf;
/* Print the lines before the trailers */
if (!opts->only_trailers)
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
fwrite(sb.buf, 1, info.trailer_start, outfile);
if (!opts->only_trailers && !info.blank_line_before_trailer)
fprintf(outfile, "\n");
@@ -1092,7 +1090,7 @@ void process_trailers(const char *file,
/* Print the lines after the trailers as is */
if (!opts->only_trailers)
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
fwrite(sb.buf + info.trailer_end, 1, sb.len - info.trailer_end, outfile);
if (opts->in_place)
if (rename_tempfile(&trailers_tempfile, file))
@@ -1104,7 +1102,7 @@ void process_trailers(const char *file,
void trailer_info_get(struct trailer_info *info, const char *str,
const struct process_trailer_options *opts)
{
int patch_start, trailer_end, trailer_start;
size_t patch_start, trailer_end = 0, trailer_start = 0;
struct strbuf **trailer_lines, **ptr;
char **trailer_strings = NULL;
size_t nr = 0, alloc = 0;
@@ -1139,8 +1137,8 @@ void trailer_info_get(struct trailer_info *info, const char *str,
info->blank_line_before_trailer = ends_with_blank_line(str,
trailer_start);
info->trailer_start = str + trailer_start;
info->trailer_end = str + trailer_end;
info->trailer_start = trailer_start;
info->trailer_end = trailer_end;
info->trailers = trailer_strings;
info->trailer_nr = nr;
}
@@ -1155,6 +1153,7 @@ void trailer_info_release(struct trailer_info *info)
static void format_trailer_info(struct strbuf *out,
const struct trailer_info *info,
const char *msg,
const struct process_trailer_options *opts)
{
size_t origlen = out->len;
@@ -1164,7 +1163,7 @@ static void format_trailer_info(struct strbuf *out,
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
!opts->separator && !opts->key_only && !opts->value_only &&
!opts->key_value_separator) {
strbuf_add(out, info->trailer_start,
strbuf_add(out, msg + info->trailer_start,
info->trailer_end - info->trailer_start);
return;
}
@@ -1219,7 +1218,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
struct trailer_info info;
trailer_info_get(&info, msg, opts);
format_trailer_info(out, &info, opts);
format_trailer_info(out, &info, msg, opts);
trailer_info_release(&info);
}

View File

@@ -37,11 +37,10 @@ struct trailer_info {
int blank_line_before_trailer;
/*
* Pointers to the start and end of the trailer block found. If there
* is no trailer block found, these 2 pointers point to the end of the
* input string.
* Offsets to the trailer block start and end positions in the input
* string. If no trailer block is found, these are set to 0.
*/
const char *trailer_start, *trailer_end;
size_t trailer_start, trailer_end;
/*
* Array of trailers found.