Merge branch 'jk/for-each-ref'

* jk/for-each-ref:
  for-each-ref: add split message parts to %(contents:*).
  for-each-ref: handle multiline subjects like --pretty
  for-each-ref: refactor subject and body placeholder parsing
  t6300: add more body-parsing tests
  t7004: factor out gpg setup
This commit is contained in:
Junio C Hamano 2011-10-05 12:36:22 -07:00
commit 4e20e36799
9 changed files with 206 additions and 58 deletions

View File

@ -101,9 +101,10 @@ Fields that have name-email-date tuple as its value (`author`,
`committer`, and `tagger`) can be suffixed with `name`, `email`,
and `date` to extract the named component.
The first line of the message in a commit and tag object is
`subject`, the remaining lines are `body`. The whole message
is `contents`.
The complete message in a commit and tag object is `contents`.
Its first line is `contents:subject`, the remaining lines
are `contents:body` and the optional GPG signature
is `contents:signature`.
For sorting purposes, fields with numeric values sort in numeric
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).

View File

@ -69,6 +69,9 @@ static struct {
{ "subject" },
{ "body" },
{ "contents" },
{ "contents:subject" },
{ "contents:body" },
{ "contents:signature" },
{ "upstream" },
{ "symref" },
{ "flag" },
@ -361,6 +364,18 @@ static const char *copy_email(const char *buf)
return xmemdupz(email, eoemail + 1 - email);
}
static char *copy_subject(const char *buf, unsigned long len)
{
char *r = xmemdupz(buf, len);
int i;
for (i = 0; i < len; i++)
if (r[i] == '\n')
r[i] = ' ';
return r;
}
static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
{
const char *eoemail = strstr(buf, "> ");
@ -458,38 +473,56 @@ 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,
unsigned long *nonsiglen,
const char **sig, unsigned long *siglen)
{
while (*buf) {
const char *eol = strchr(buf, '\n');
if (!eol)
return;
if (eol[1] == '\n') {
buf = eol + 1;
break; /* found end of header */
}
buf = eol + 1;
const char *eol;
/* skip past header until we hit empty line */
while (*buf && *buf != '\n') {
eol = strchrnul(buf, '\n');
if (*eol)
eol++;
buf = eol;
}
/* skip any empty lines */
while (*buf == '\n')
buf++;
if (!*buf)
return;
*sub = buf; /* first non-empty line */
buf = strchr(buf, '\n');
if (!buf) {
*body = "";
return; /* no body */
/* 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 */
*sub = buf;
/* subject goes to first empty line */
while (buf < *sig && *buf && *buf != '\n') {
eol = strchrnul(buf, '\n');
if (*eol)
eol++;
buf = eol;
}
*sublen = buf - *sub;
/* drop trailing newline, if present */
if (*sublen && (*sub)[*sublen - 1] == '\n')
*sublen -= 1;
/* skip any empty lines */
while (*buf == '\n')
buf++; /* skip blank between subject and body */
buf++;
*body = buf;
*bodylen = strlen(buf);
*nonsiglen = *sig - buf;
}
/* See grab_values */
static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
{
int i;
const char *subpos = NULL, *bodypos = NULL;
const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
for (i = 0; i < used_atom_cnt; i++) {
const char *name = used_atom[i];
@ -500,17 +533,27 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
name++;
if (strcmp(name, "subject") &&
strcmp(name, "body") &&
strcmp(name, "contents"))
strcmp(name, "contents") &&
strcmp(name, "contents:subject") &&
strcmp(name, "contents:body") &&
strcmp(name, "contents:signature"))
continue;
if (!subpos)
find_subpos(buf, sz, &subpos, &bodypos);
if (!subpos)
return;
find_subpos(buf, sz,
&subpos, &sublen,
&bodypos, &bodylen, &nonsiglen,
&sigpos, &siglen);
if (!strcmp(name, "subject"))
v->s = copy_line(subpos);
v->s = copy_subject(subpos, sublen);
else if (!strcmp(name, "contents:subject"))
v->s = copy_subject(subpos, sublen);
else if (!strcmp(name, "body"))
v->s = xstrdup(bodypos);
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"))
v->s = xstrdup(subpos);
}

34
t/lib-gpg.sh Executable file
View File

@ -0,0 +1,34 @@
#!/bin/sh
gpg_version=`gpg --version 2>&1`
if test $? = 127; then
say "You do not seem to have gpg installed"
else
# As said here: http://www.gnupg.org/documentation/faqs.html#q6.19
# the gpg version 1.0.6 didn't parse trust packets correctly, so for
# that version, creation of signed tags using the generated key fails.
case "$gpg_version" in
'gpg (GnuPG) 1.0.6'*)
say "Your version of gpg (1.0.6) is too buggy for testing"
;;
*)
# key generation info: gpg --homedir t/lib-gpg --gen-key
# Type DSA and Elgamal, size 2048 bits, no expiration date.
# Name and email: C O Mitter <committer@example.com>
# No password given, to enable non-interactive operation.
cp -R "$TEST_DIRECTORY"/lib-gpg ./gpghome
chmod 0700 gpghome
GNUPGHOME="$(pwd)/gpghome"
export GNUPGHOME
test_set_prereq GPG
;;
esac
fi
sanitize_pgp() {
perl -ne '
/^-----END PGP/ and $in_pgp = 0;
print unless $in_pgp;
/^-----BEGIN PGP/ and $in_pgp = 1;
'
}

View File

@ -6,6 +6,7 @@
test_description='for-each-ref test'
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-gpg.sh
# Mon Jul 3 15:18:43 2006 +0000
datestamp=1151939923
@ -37,11 +38,13 @@ test_atom() {
case "$1" in
head) ref=refs/heads/master ;;
tag) ref=refs/tags/testtag ;;
*) ref=$1 ;;
esac
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 &&
test_cmp expected actual
sanitize_pgp <actual >actual.clean &&
test_cmp expected actual.clean
"
}
@ -71,7 +74,10 @@ test_atom head taggerdate ''
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 subject 'Initial'
test_atom head contents:subject 'Initial'
test_atom head body ''
test_atom head contents:body ''
test_atom head contents:signature ''
test_atom head contents 'Initial
'
@ -101,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 creatordate 'Mon Jul 3 17:18:45 2006 +0200'
test_atom tag subject 'Tagging at 1151939927'
test_atom tag contents:subject 'Tagging at 1151939927'
test_atom tag body ''
test_atom tag contents:body ''
test_atom tag contents:signature ''
test_atom tag contents 'Tagging at 1151939927
'
@ -359,4 +368,92 @@ test_expect_success 'an unusual tag with an incomplete line' '
'
test_expect_success 'create tag with subject and body content' '
cat >>msg <<-\EOF &&
the subject line
first body line
second body line
EOF
git tag -F msg subject-body
'
test_atom refs/tags/subject-body subject 'the subject line'
test_atom refs/tags/subject-body body 'first body line
second body line
'
test_atom refs/tags/subject-body contents 'the subject line
first body line
second body line
'
test_expect_success 'create tag with multiline subject' '
cat >msg <<-\EOF &&
first subject line
second subject line
first body line
second body line
EOF
git tag -F msg multiline
'
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
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
second subject line
first 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

View File

@ -8,6 +8,7 @@ test_description='git tag
Tests for operations with tags.'
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-gpg.sh
# creating and listing lightweight tags:
@ -585,24 +586,6 @@ test_expect_success \
test_cmp expect actual
'
# subsequent tests require gpg; check if it is available
gpg --version >/dev/null 2>/dev/null
if [ $? -eq 127 ]; then
say "# gpg not found - skipping tag signing and verification tests"
else
# As said here: http://www.gnupg.org/documentation/faqs.html#q6.19
# the gpg version 1.0.6 didn't parse trust packets correctly, so for
# that version, creation of signed tags using the generated key fails.
case "$(gpg --version)" in
'gpg (GnuPG) 1.0.6'*)
say "Skipping signed tag tests, because a bug in 1.0.6 version"
;;
*)
test_set_prereq GPG
;;
esac
fi
# trying to verify annotated non-signed tags:
test_expect_success GPG \
@ -625,16 +608,6 @@ test_expect_success GPG \
# creating and verifying signed tags:
# key generation info: gpg --homedir t/t7004 --gen-key
# Type DSA and Elgamal, size 2048 bits, no expiration date.
# Name and email: C O Mitter <committer@example.com>
# No password given, to enable non-interactive operation.
cp -R "$TEST_DIRECTORY"/t7004 ./gpghome
chmod 0700 gpghome
GNUPGHOME="$(pwd)/gpghome"
export GNUPGHOME
get_tag_header signed-tag $commit commit $time >expect
echo 'A signed tag message' >>expect
echo '-----BEGIN PGP SIGNATURE-----' >>expect