for-each-ref: refactor subject and body placeholder parsing
The find_subpos function was a little hard to use, as well as to read. It would sometimes write into the subject and body pointers, and sometimes not. The body pointer sometimes could be compared to subject, and sometimes not. When actually duplicating the subject, the caller was forced to figure out again how long the subject is (which is not too big a deal when the subject is a single line, but hard to extend). The refactoring makes the function more straightforward, both to read and to use. We will always put something into the subject and body pointers, and we return explicit lengths for them, too. This lays the groundwork both for more complex subject parsing (e.g., multiline), as well as splitting the body into subparts (like the text versus the signature). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
7140c22c8e
commit
7ec0f31eec
@ -458,38 +458,42 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void find_subpos(const char *buf, unsigned long sz, const char **sub, const char **body)
|
static void find_subpos(const char *buf, unsigned long sz,
|
||||||
|
const char **sub, unsigned long *sublen,
|
||||||
|
const char **body, unsigned long *bodylen)
|
||||||
{
|
{
|
||||||
while (*buf) {
|
const char *eol;
|
||||||
const char *eol = strchr(buf, '\n');
|
/* skip past header until we hit empty line */
|
||||||
if (!eol)
|
while (*buf && *buf != '\n') {
|
||||||
return;
|
eol = strchrnul(buf, '\n');
|
||||||
if (eol[1] == '\n') {
|
if (*eol)
|
||||||
buf = eol + 1;
|
eol++;
|
||||||
break; /* found end of header */
|
buf = eol;
|
||||||
}
|
|
||||||
buf = eol + 1;
|
|
||||||
}
|
}
|
||||||
|
/* skip any empty lines */
|
||||||
while (*buf == '\n')
|
while (*buf == '\n')
|
||||||
buf++;
|
buf++;
|
||||||
if (!*buf)
|
|
||||||
return;
|
/* subject is first non-empty line */
|
||||||
*sub = buf; /* first non-empty line */
|
*sub = buf;
|
||||||
buf = strchr(buf, '\n');
|
/* subject goes to end of line */
|
||||||
if (!buf) {
|
eol = strchrnul(buf, '\n');
|
||||||
*body = "";
|
*sublen = eol - buf;
|
||||||
return; /* no body */
|
buf = eol;
|
||||||
}
|
|
||||||
|
/* skip any empty lines */
|
||||||
while (*buf == '\n')
|
while (*buf == '\n')
|
||||||
buf++; /* skip blank between subject and body */
|
buf++;
|
||||||
*body = buf;
|
*body = buf;
|
||||||
|
*bodylen = strlen(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See grab_values */
|
/* See grab_values */
|
||||||
static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
|
static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
const char *subpos = NULL, *bodypos = NULL;
|
const char *subpos = NULL, *bodypos;
|
||||||
|
unsigned long sublen, bodylen;
|
||||||
|
|
||||||
for (i = 0; i < used_atom_cnt; i++) {
|
for (i = 0; i < used_atom_cnt; i++) {
|
||||||
const char *name = used_atom[i];
|
const char *name = used_atom[i];
|
||||||
@ -503,14 +507,14 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
|
|||||||
strcmp(name, "contents"))
|
strcmp(name, "contents"))
|
||||||
continue;
|
continue;
|
||||||
if (!subpos)
|
if (!subpos)
|
||||||
find_subpos(buf, sz, &subpos, &bodypos);
|
find_subpos(buf, sz,
|
||||||
if (!subpos)
|
&subpos, &sublen,
|
||||||
return;
|
&bodypos, &bodylen);
|
||||||
|
|
||||||
if (!strcmp(name, "subject"))
|
if (!strcmp(name, "subject"))
|
||||||
v->s = copy_line(subpos);
|
v->s = xmemdupz(subpos, sublen);
|
||||||
else if (!strcmp(name, "body"))
|
else if (!strcmp(name, "body"))
|
||||||
v->s = xstrdup(bodypos);
|
v->s = xmemdupz(bodypos, bodylen);
|
||||||
else if (!strcmp(name, "contents"))
|
else if (!strcmp(name, "contents"))
|
||||||
v->s = xstrdup(subpos);
|
v->s = xstrdup(subpos);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user