Skip to content

Commit 5f80060

Browse files
listxgitster
authored andcommitted
trailer: document parse_trailers() usage
Explain how to use parse_trailers(), because earlier we made the trailer_info struct opaque. That is, because clients can no longer peek inside it, we should give them guidance about how the (pointer to the) opaque struct can still be useful to them. Rename "head" struct to "trailer_objects" to make the wording of the new comments a bit easier to read (because "head" itself doesn't really have any domain-specific meaning here). Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf5c934 commit 5f80060

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

trailer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,12 +1026,12 @@ static struct trailer_info *trailer_info_get(const struct process_trailer_option
10261026
}
10271027

10281028
/*
1029-
* Parse trailers in "str", populating the trailer info and "head"
1029+
* Parse trailers in "str", populating the trailer info and "trailer_objects"
10301030
* linked list structure.
10311031
*/
10321032
struct trailer_info *parse_trailers(const struct process_trailer_options *opts,
10331033
const char *str,
1034-
struct list_head *head)
1034+
struct list_head *trailer_objects)
10351035
{
10361036
struct trailer_info *info;
10371037
struct strbuf tok = STRBUF_INIT;
@@ -1051,13 +1051,13 @@ struct trailer_info *parse_trailers(const struct process_trailer_options *opts,
10511051
separator_pos);
10521052
if (opts->unfold)
10531053
unfold_value(&val);
1054-
add_trailer_item(head,
1054+
add_trailer_item(trailer_objects,
10551055
strbuf_detach(&tok, NULL),
10561056
strbuf_detach(&val, NULL));
10571057
} else if (!opts->only_trailers) {
10581058
strbuf_addstr(&val, trailer);
10591059
strbuf_strip_suffix(&val, "\n");
1060-
add_trailer_item(head,
1060+
add_trailer_item(trailer_objects,
10611061
NULL,
10621062
strbuf_detach(&val, NULL));
10631063
}

trailer.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,63 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head,
7070
void process_trailers_lists(struct list_head *head,
7171
struct list_head *arg_head);
7272

73+
/*
74+
* Given some input string "str", return a pointer to an opaque trailer_info
75+
* structure. Also populate the trailer_objects list with parsed trailer
76+
* objects. Internally this calls trailer_info_get() to get the opaque pointer,
77+
* but does some extra work to populate the trailer_objects linked list.
78+
*
79+
* The opaque trailer_info pointer can be used to check the position of the
80+
* trailer block as offsets relative to the beginning of "str" in
81+
* trailer_block_start() and trailer_block_end().
82+
* blank_line_before_trailer_block() returns 1 if there is a blank line just
83+
* before the trailer block. All of these functions are useful for preserving
84+
* the input before and after the trailer block, if we were to write out the
85+
* original input (but with the trailer block itself modified); see
86+
* builtin/interpret-trailers.c for an example.
87+
*
88+
* For iterating through the parsed trailer block (if you don't care about the
89+
* position of the trailer block itself in the context of the larger string text
90+
* from which it was parsed), please see trailer_iterator_init() which uses the
91+
* trailer_info struct internally.
92+
*
93+
* Lastly, callers should call trailer_info_release() when they are done using
94+
* the opaque pointer.
95+
*
96+
* NOTE: Callers should treat both trailer_info and trailer_objects as
97+
* read-only items, because there is some overlap between the two (trailer_info
98+
* has "char **trailers" string array, and trailer_objects will have the same
99+
* data but as a linked list of trailer_item objects). This API does not perform
100+
* any synchronization between the two. In the future we should be able to
101+
* reduce the duplication and use just the linked list.
102+
*/
73103
struct trailer_info *parse_trailers(const struct process_trailer_options *,
74104
const char *str,
75-
struct list_head *head);
105+
struct list_head *trailer_objects);
76106

107+
/*
108+
* Return the offset of the start of the trailer block. That is, 0 is the start
109+
* of the input ("str" in parse_trailers()) and some other positive number
110+
* indicates how many bytes we have to skip over before we get to the beginning
111+
* of the trailer block.
112+
*/
77113
size_t trailer_block_start(struct trailer_info *);
114+
115+
/*
116+
* Return the end of the trailer block, again relative to the start of the
117+
* input.
118+
*/
78119
size_t trailer_block_end(struct trailer_info *);
120+
121+
/*
122+
* Return 1 if the trailer block had an extra newline (blank line) just before
123+
* it.
124+
*/
79125
int blank_line_before_trailer_block(struct trailer_info *);
80126

127+
/*
128+
* Free trailer_info struct.
129+
*/
81130
void trailer_info_release(struct trailer_info *info);
82131

83132
void trailer_config_init(void);

0 commit comments

Comments
 (0)