mirror of
https://github.com/git/git
synced 2024-08-25 02:35:46 +00:00
for-each-ref: add split message parts to %(contents:*).
The %(body) placeholder returns the whole body of a tag or commit, including the signature. However, callers may want to get just the body without signature, or just the signature. Rather than change the meaning of %(body), which might break some scripts, this patch introduces a new set of placeholders which break down the %(contents) placeholder into its constituent parts. [jk: initial patch by mg, rebased on top of my refactoring and with tests by me] Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
7f6e275bc0
commit
e2b239722a
|
@ -101,9 +101,10 @@ Fields that have name-email-date tuple as its value (`author`,
|
||||||
`committer`, and `tagger`) can be suffixed with `name`, `email`,
|
`committer`, and `tagger`) can be suffixed with `name`, `email`,
|
||||||
and `date` to extract the named component.
|
and `date` to extract the named component.
|
||||||
|
|
||||||
The first line of the message in a commit and tag object is
|
The complete message in a commit and tag object is `contents`.
|
||||||
`subject`, the remaining lines are `body`. The whole message
|
Its first line is `contents:subject`, the remaining lines
|
||||||
is `contents`.
|
are `contents:body` and the optional GPG signature
|
||||||
|
is `contents:signature`.
|
||||||
|
|
||||||
For sorting purposes, fields with numeric values sort in numeric
|
For sorting purposes, fields with numeric values sort in numeric
|
||||||
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
|
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
|
||||||
|
|
|
@ -69,6 +69,9 @@ static struct {
|
||||||
{ "subject" },
|
{ "subject" },
|
||||||
{ "body" },
|
{ "body" },
|
||||||
{ "contents" },
|
{ "contents" },
|
||||||
|
{ "contents:subject" },
|
||||||
|
{ "contents:body" },
|
||||||
|
{ "contents:signature" },
|
||||||
{ "upstream" },
|
{ "upstream" },
|
||||||
{ "symref" },
|
{ "symref" },
|
||||||
{ "flag" },
|
{ "flag" },
|
||||||
|
@ -472,7 +475,9 @@ static void grab_person(const char *who, struct atom_value *val, int deref, stru
|
||||||
|
|
||||||
static void find_subpos(const char *buf, unsigned long sz,
|
static void find_subpos(const char *buf, unsigned long sz,
|
||||||
const char **sub, unsigned long *sublen,
|
const char **sub, unsigned long *sublen,
|
||||||
const char **body, unsigned long *bodylen)
|
const char **body, unsigned long *bodylen,
|
||||||
|
unsigned long *nonsiglen,
|
||||||
|
const char **sig, unsigned long *siglen)
|
||||||
{
|
{
|
||||||
const char *eol;
|
const char *eol;
|
||||||
/* skip past header until we hit empty line */
|
/* skip past header until we hit empty line */
|
||||||
|
@ -486,10 +491,14 @@ static void find_subpos(const char *buf, unsigned long sz,
|
||||||
while (*buf == '\n')
|
while (*buf == '\n')
|
||||||
buf++;
|
buf++;
|
||||||
|
|
||||||
|
/* parse signature first; we might not even have a subject line */
|
||||||
|
*sig = buf + parse_signature(buf, strlen(buf));
|
||||||
|
*siglen = strlen(*sig);
|
||||||
|
|
||||||
/* subject is first non-empty line */
|
/* subject is first non-empty line */
|
||||||
*sub = buf;
|
*sub = buf;
|
||||||
/* subject goes to first empty line */
|
/* subject goes to first empty line */
|
||||||
while (*buf && *buf != '\n') {
|
while (buf < *sig && *buf && *buf != '\n') {
|
||||||
eol = strchrnul(buf, '\n');
|
eol = strchrnul(buf, '\n');
|
||||||
if (*eol)
|
if (*eol)
|
||||||
eol++;
|
eol++;
|
||||||
|
@ -505,14 +514,15 @@ static void find_subpos(const char *buf, unsigned long sz,
|
||||||
buf++;
|
buf++;
|
||||||
*body = buf;
|
*body = buf;
|
||||||
*bodylen = strlen(buf);
|
*bodylen = strlen(buf);
|
||||||
|
*nonsiglen = *sig - 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;
|
const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
|
||||||
unsigned long sublen, bodylen;
|
unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
|
||||||
|
|
||||||
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];
|
||||||
|
@ -523,17 +533,27 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
|
||||||
name++;
|
name++;
|
||||||
if (strcmp(name, "subject") &&
|
if (strcmp(name, "subject") &&
|
||||||
strcmp(name, "body") &&
|
strcmp(name, "body") &&
|
||||||
strcmp(name, "contents"))
|
strcmp(name, "contents") &&
|
||||||
|
strcmp(name, "contents:subject") &&
|
||||||
|
strcmp(name, "contents:body") &&
|
||||||
|
strcmp(name, "contents:signature"))
|
||||||
continue;
|
continue;
|
||||||
if (!subpos)
|
if (!subpos)
|
||||||
find_subpos(buf, sz,
|
find_subpos(buf, sz,
|
||||||
&subpos, &sublen,
|
&subpos, &sublen,
|
||||||
&bodypos, &bodylen);
|
&bodypos, &bodylen, &nonsiglen,
|
||||||
|
&sigpos, &siglen);
|
||||||
|
|
||||||
if (!strcmp(name, "subject"))
|
if (!strcmp(name, "subject"))
|
||||||
v->s = copy_subject(subpos, sublen);
|
v->s = copy_subject(subpos, sublen);
|
||||||
|
else if (!strcmp(name, "contents:subject"))
|
||||||
|
v->s = copy_subject(subpos, sublen);
|
||||||
else if (!strcmp(name, "body"))
|
else if (!strcmp(name, "body"))
|
||||||
v->s = xmemdupz(bodypos, bodylen);
|
v->s = xmemdupz(bodypos, bodylen);
|
||||||
|
else if (!strcmp(name, "contents:body"))
|
||||||
|
v->s = xmemdupz(bodypos, nonsiglen);
|
||||||
|
else if (!strcmp(name, "contents:signature"))
|
||||||
|
v->s = xmemdupz(sigpos, siglen);
|
||||||
else if (!strcmp(name, "contents"))
|
else if (!strcmp(name, "contents"))
|
||||||
v->s = xstrdup(subpos);
|
v->s = xstrdup(subpos);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,3 +24,11 @@ else
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
sanitize_pgp() {
|
||||||
|
perl -ne '
|
||||||
|
/^-----END PGP/ and $in_pgp = 0;
|
||||||
|
print unless $in_pgp;
|
||||||
|
/^-----BEGIN PGP/ and $in_pgp = 1;
|
||||||
|
'
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
test_description='for-each-ref test'
|
test_description='for-each-ref test'
|
||||||
|
|
||||||
. ./test-lib.sh
|
. ./test-lib.sh
|
||||||
|
. "$TEST_DIRECTORY"/lib-gpg.sh
|
||||||
|
|
||||||
# Mon Jul 3 15:18:43 2006 +0000
|
# Mon Jul 3 15:18:43 2006 +0000
|
||||||
datestamp=1151939923
|
datestamp=1151939923
|
||||||
|
@ -40,9 +41,10 @@ test_atom() {
|
||||||
*) ref=$1 ;;
|
*) ref=$1 ;;
|
||||||
esac
|
esac
|
||||||
printf '%s\n' "$3" >expected
|
printf '%s\n' "$3" >expected
|
||||||
test_expect_${4:-success} "basic atom: $1 $2" "
|
test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
|
||||||
git for-each-ref --format='%($2)' $ref >actual &&
|
git for-each-ref --format='%($2)' $ref >actual &&
|
||||||
test_cmp expected actual
|
sanitize_pgp <actual >actual.clean &&
|
||||||
|
test_cmp expected actual.clean
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +74,10 @@ test_atom head taggerdate ''
|
||||||
test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
|
test_atom head creator 'C O Mitter <committer@example.com> 1151939923 +0200'
|
||||||
test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
|
test_atom head creatordate 'Mon Jul 3 17:18:43 2006 +0200'
|
||||||
test_atom head subject 'Initial'
|
test_atom head subject 'Initial'
|
||||||
|
test_atom head contents:subject 'Initial'
|
||||||
test_atom head body ''
|
test_atom head body ''
|
||||||
|
test_atom head contents:body ''
|
||||||
|
test_atom head contents:signature ''
|
||||||
test_atom head contents 'Initial
|
test_atom head contents 'Initial
|
||||||
'
|
'
|
||||||
|
|
||||||
|
@ -102,7 +107,10 @@ test_atom tag taggerdate 'Mon Jul 3 17:18:45 2006 +0200'
|
||||||
test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
|
test_atom tag creator 'C O Mitter <committer@example.com> 1151939925 +0200'
|
||||||
test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
|
test_atom tag creatordate 'Mon Jul 3 17:18:45 2006 +0200'
|
||||||
test_atom tag subject 'Tagging at 1151939927'
|
test_atom tag subject 'Tagging at 1151939927'
|
||||||
|
test_atom tag contents:subject 'Tagging at 1151939927'
|
||||||
test_atom tag body ''
|
test_atom tag body ''
|
||||||
|
test_atom tag contents:body ''
|
||||||
|
test_atom tag contents:signature ''
|
||||||
test_atom tag contents 'Tagging at 1151939927
|
test_atom tag contents 'Tagging at 1151939927
|
||||||
'
|
'
|
||||||
|
|
||||||
|
@ -390,9 +398,14 @@ test_expect_success 'create tag with multiline subject' '
|
||||||
git tag -F msg multiline
|
git tag -F msg multiline
|
||||||
'
|
'
|
||||||
test_atom refs/tags/multiline subject 'first subject line second subject line'
|
test_atom refs/tags/multiline subject 'first subject line second subject line'
|
||||||
|
test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
|
||||||
test_atom refs/tags/multiline body 'first body line
|
test_atom refs/tags/multiline body 'first body line
|
||||||
second body line
|
second body line
|
||||||
'
|
'
|
||||||
|
test_atom refs/tags/multiline contents:body 'first body line
|
||||||
|
second body line
|
||||||
|
'
|
||||||
|
test_atom refs/tags/multiline contents:signature ''
|
||||||
test_atom refs/tags/multiline contents 'first subject line
|
test_atom refs/tags/multiline contents 'first subject line
|
||||||
second subject line
|
second subject line
|
||||||
|
|
||||||
|
@ -400,4 +413,47 @@ first body line
|
||||||
second body line
|
second body line
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success GPG 'create signed tags' '
|
||||||
|
git tag -s -m "" signed-empty &&
|
||||||
|
git tag -s -m "subject line" signed-short &&
|
||||||
|
cat >msg <<-\EOF &&
|
||||||
|
subject line
|
||||||
|
|
||||||
|
body contents
|
||||||
|
EOF
|
||||||
|
git tag -s -F msg signed-long
|
||||||
|
'
|
||||||
|
|
||||||
|
sig='-----BEGIN PGP SIGNATURE-----
|
||||||
|
-----END PGP SIGNATURE-----
|
||||||
|
'
|
||||||
|
|
||||||
|
PREREQ=GPG
|
||||||
|
test_atom refs/tags/signed-empty subject ''
|
||||||
|
test_atom refs/tags/signed-empty contents:subject ''
|
||||||
|
test_atom refs/tags/signed-empty body "$sig"
|
||||||
|
test_atom refs/tags/signed-empty contents:body ''
|
||||||
|
test_atom refs/tags/signed-empty contents:signature "$sig"
|
||||||
|
test_atom refs/tags/signed-empty contents "$sig"
|
||||||
|
|
||||||
|
test_atom refs/tags/signed-short subject 'subject line'
|
||||||
|
test_atom refs/tags/signed-short contents:subject 'subject line'
|
||||||
|
test_atom refs/tags/signed-short body "$sig"
|
||||||
|
test_atom refs/tags/signed-short contents:body ''
|
||||||
|
test_atom refs/tags/signed-short contents:signature "$sig"
|
||||||
|
test_atom refs/tags/signed-short contents "subject line
|
||||||
|
$sig"
|
||||||
|
|
||||||
|
test_atom refs/tags/signed-long subject 'subject line'
|
||||||
|
test_atom refs/tags/signed-long contents:subject 'subject line'
|
||||||
|
test_atom refs/tags/signed-long body "body contents
|
||||||
|
$sig"
|
||||||
|
test_atom refs/tags/signed-long contents:body 'body contents
|
||||||
|
'
|
||||||
|
test_atom refs/tags/signed-long contents:signature "$sig"
|
||||||
|
test_atom refs/tags/signed-long contents "subject line
|
||||||
|
|
||||||
|
body contents
|
||||||
|
$sig"
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
|
Loading…
Reference in a new issue