Initial commit - full huishou project

This commit is contained in:
jiapengyu
2026-07-27 14:07:26 +08:00
commit 60790ad1b3
39127 changed files with 5989265 additions and 0 deletions
+186
View File
@@ -0,0 +1,186 @@
#include "clar_libgit2.h"
#include "commit.h"
#include "git2/commit.h"
static git_repository *_repo;
void test_commit_commit__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
}
void test_commit_commit__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
cl_fixture_cleanup("testrepo.git");
}
void test_commit_commit__create_unexisting_update_ref(void)
{
git_oid oid;
git_tree *tree;
git_commit *commit;
git_signature *s;
git_reference *ref;
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
NULL, "some msg", tree, 1, (const git_commit **) &commit));
/* fail because the parent isn't the tip of the branch anymore */
cl_git_fail(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
NULL, "some msg", tree, 1, (const git_commit **) &commit));
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_assert_equal_oid(&oid, git_reference_target(ref));
git_tree_free(tree);
git_commit_free(commit);
git_signature_free(s);
git_reference_free(ref);
}
void test_commit_commit__create_initial_commit(void)
{
git_oid oid;
git_tree *tree;
git_commit *commit;
git_signature *s;
git_reference *ref;
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_fail(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_git_pass(git_commit_create(&oid, _repo, "refs/heads/foo/bar", s, s,
NULL, "initial commit", tree, 0, NULL));
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/foo/bar"));
cl_assert_equal_oid(&oid, git_reference_target(ref));
git_tree_free(tree);
git_commit_free(commit);
git_signature_free(s);
git_reference_free(ref);
}
void test_commit_commit__create_initial_commit_parent_not_current(void)
{
git_oid oid;
git_oid original_oid;
git_tree *tree;
git_commit *commit;
git_signature *s;
git_oid_fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
git_oid_fromstr(&oid, "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
cl_git_pass(git_tree_lookup(&tree, _repo, &oid));
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
cl_git_pass(git_reference_name_to_id(&original_oid, _repo, "HEAD"));
cl_git_fail(git_commit_create(&oid, _repo, "HEAD", s, s,
NULL, "initial commit", tree, 0, NULL));
cl_git_pass(git_reference_name_to_id(&oid, _repo, "HEAD"));
cl_assert_equal_oid(&oid, &original_oid);
git_tree_free(tree);
git_commit_free(commit);
git_signature_free(s);
}
void assert_commit_summary(const char *expected, const char *given)
{
git_commit *dummy;
cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
dummy->raw_message = git__strdup(given);
cl_assert_equal_s(expected, git_commit_summary(dummy));
git_commit__free(dummy);
}
void assert_commit_body(const char *expected, const char *given)
{
git_commit *dummy;
cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
dummy->raw_message = git__strdup(given);
cl_assert_equal_s(expected, git_commit_body(dummy));
git_commit__free(dummy);
}
void test_commit_commit__summary(void)
{
assert_commit_summary("One-liner with no trailing newline", "One-liner with no trailing newline");
assert_commit_summary("One-liner with trailing newline", "One-liner with trailing newline\n");
assert_commit_summary("Trimmed leading&trailing newlines", "\n\nTrimmed leading&trailing newlines\n\n");
assert_commit_summary("First paragraph only", "\nFirst paragraph only\n\n(There are more!)");
assert_commit_summary("First paragraph with unwrapped trailing\tlines", "\nFirst paragraph\nwith unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
assert_commit_summary("\tLeading tabs", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
assert_commit_summary(" Leading Spaces", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
assert_commit_summary("Trailing tabs\tare removed", "Trailing tabs\tare removed\t\t");
assert_commit_summary("Trailing spaces are removed", "Trailing spaces are removed ");
assert_commit_summary("Trailing tabs", "Trailing tabs\t\n\nare removed");
assert_commit_summary("Trailing spaces", "Trailing spaces \n\nare removed");
assert_commit_summary("Newlines are replaced by spaces", "Newlines\nare\nreplaced by spaces\n");
assert_commit_summary(" Spaces after newlines are collapsed", "\n Spaces after newlines\n are\n collapsed\n "); /* newlines at the very beginning are ignored and not collapsed */
assert_commit_summary(" Spaces before newlines are collapsed", " \nSpaces before newlines \nare \ncollapsed \n");
assert_commit_summary(" Spaces around newlines are collapsed", " \n Spaces around newlines \n are \n collapsed \n ");
assert_commit_summary(" Trailing newlines are" , " \n Trailing newlines \n are \n\n collapsed \n ");
assert_commit_summary(" Trailing spaces are stripped", " \n Trailing spaces \n are stripped \n\n \n \t ");
assert_commit_summary("", "");
assert_commit_summary("", " ");
assert_commit_summary("", "\n");
assert_commit_summary("", "\n \n");
}
void test_commit_commit__body(void)
{
assert_commit_body(NULL, "One-liner with no trailing newline");
assert_commit_body(NULL, "One-liner with trailing newline\n");
assert_commit_body(NULL, "\n\nTrimmed leading&trailing newlines\n\n");
assert_commit_body("(There are more!)", "\nFirst paragraph only\n\n(There are more!)");
assert_commit_body("(Yes, unwrapped!)", "\nFirst paragraph\nwith unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
assert_commit_body("are preserved", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
assert_commit_body("are preserved", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
assert_commit_body(NULL, "Trailing tabs\tare removed\t\t");
assert_commit_body(NULL, "Trailing spaces are removed ");
assert_commit_body("are removed", "Trailing tabs\t\n\nare removed");
assert_commit_body("are removed", "Trailing spaces \n\nare removed");
assert_commit_body(NULL,"Newlines\nare\nreplaced by spaces\n");
assert_commit_body(NULL , "\n Spaces after newlines\n are\n collapsed\n "); /* newlines at the very beginning are ignored and not collapsed */
assert_commit_body(NULL , " \nSpaces before newlines \nare \ncollapsed \n");
assert_commit_body(NULL , " \n Spaces around newlines \n are \n collapsed \n ");
assert_commit_body("collapsed" , " \n Trailing newlines \n are \n\n collapsed \n ");
assert_commit_body(NULL, " \n Trailing spaces \n are stripped \n\n \n \t ");
assert_commit_body(NULL , "");
assert_commit_body(NULL , " ");
assert_commit_body(NULL , "\n");
assert_commit_body(NULL , "\n \n");
}
+60
View File
@@ -0,0 +1,60 @@
#include "clar_libgit2.h"
static git_repository *_repo;
static git_commit *commit;
void test_commit_parent__initialize(void)
{
git_oid oid;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
git_oid_fromstr(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
}
void test_commit_parent__cleanup(void)
{
git_commit_free(commit);
commit = NULL;
git_repository_free(_repo);
_repo = NULL;
}
static void assert_nth_gen_parent(unsigned int gen, const char *expected_oid)
{
git_commit *parent = NULL;
int error;
error = git_commit_nth_gen_ancestor(&parent, commit, gen);
if (expected_oid != NULL) {
cl_assert_equal_i(0, error);
cl_assert_equal_i(0, git_oid_streq(git_commit_id(parent), expected_oid));
} else
cl_assert_equal_i(GIT_ENOTFOUND, error);
git_commit_free(parent);
}
/*
* $ git show be35~0
* commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
*
* $ git show be35~1
* commit 9fd738e8f7967c078dceed8190330fc8648ee56a
*
* $ git show be35~3
* commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
*
* $ git show be35~42
* fatal: ambiguous argument 'be35~42': unknown revision or path not in the working tree.
*/
void test_commit_parent__can_retrieve_nth_generation_parent(void)
{
assert_nth_gen_parent(0, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
assert_nth_gen_parent(1, "9fd738e8f7967c078dceed8190330fc8648ee56a");
assert_nth_gen_parent(3, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
assert_nth_gen_parent(42, NULL);
}
+553
View File
@@ -0,0 +1,553 @@
#include "clar_libgit2.h"
#include <git2/types.h>
#include "commit.h"
#include "signature.h"
/* Fixture setup */
static git_repository *g_repo;
void test_commit_parse__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_commit_parse__cleanup(void)
{
cl_git_sandbox_cleanup();
}
/* Header parsing */
typedef struct {
const char *line;
const char *header;
} parse_test_case;
static parse_test_case passing_header_cases[] = {
{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
{ "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
{ "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " },
{ "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" },
{ "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " },
{ "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " },
{ "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " },
{ "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " },
{ NULL, NULL }
};
static parse_test_case failing_header_cases[] = {
{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " },
{ "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
{ "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " },
{ "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " },
{ "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " },
{ "", "tree " },
{ "", "" },
{ NULL, NULL }
};
void test_commit_parse__header(void)
{
git_oid oid;
parse_test_case *testcase;
for (testcase = passing_header_cases; testcase->line != NULL; testcase++)
{
const char *line = testcase->line;
const char *line_end = line + strlen(line);
cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
cl_assert(line == line_end);
}
for (testcase = failing_header_cases; testcase->line != NULL; testcase++)
{
const char *line = testcase->line;
const char *line_end = line + strlen(line);
cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
}
}
/* Signature parsing */
typedef struct {
const char *string;
const char *header;
const char *name;
const char *email;
git_time_t time;
int offset;
} passing_signature_test_case;
passing_signature_test_case passing_signature_cases[] = {
{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 12345, 0},
{"author Vicent Marti <> 12345 \n", "author ", "Vicent Marti", "", 12345, 0},
{"author Vicent Marti <tanoku@gmail.com> 231301 +1020\n", "author ", "Vicent Marti", "tanoku@gmail.com", 231301, 620},
{"author Vicent Marti with an outrageously long name which will probably overflow the buffer <tanoku@gmail.com> 12345 \n", "author ", "Vicent Marti with an outrageously long name which will probably overflow the buffer", "tanoku@gmail.com", 12345, 0},
{"author Vicent Marti <tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com> 12345 \n", "author ", "Vicent Marti", "tanokuwithaveryveryverylongemailwhichwillprobablyvoverflowtheemailbuffer@gmail.com", 12345, 0},
{"committer Vicent Marti <tanoku@gmail.com> 123456 +0000 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
{"committer Vicent Marti <tanoku@gmail.com> 123456 +0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 60},
{"committer Vicent Marti <tanoku@gmail.com> 123456 -0100 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, -60},
/* Parse a signature without an author field */
{"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
/* Parse a signature without an author field */
{"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
/* Parse a signature with an empty author field */
{"committer <tanoku@gmail.com> 123456 -0100 \n", "committer ", "", "tanoku@gmail.com", 123456, -60},
/* Parse a signature with an empty email field */
{"committer Vicent Marti <> 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60},
/* Parse a signature with an empty email field */
{"committer Vicent Marti < > 123456 -0100 \n", "committer ", "Vicent Marti", "", 123456, -60},
/* Parse a signature with empty name and email */
{"committer <> 123456 -0100 \n", "committer ", "", "", 123456, -60},
/* Parse a signature with empty name and email */
{"committer <> 123456 -0100 \n", "committer ", "", "", 123456, -60},
/* Parse a signature with empty name and email */
{"committer < > 123456 -0100 \n", "committer ", "", "", 123456, -60},
/* Parse an obviously invalid signature */
{"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60},
/* Parse an obviously invalid signature */
{"committer foo<@bar> 123456 -0100 \n", "committer ", "foo", "@bar", 123456, -60},
/* Parse an obviously invalid signature */
{"committer <>\n", "committer ", "", "", 0, 0},
{"committer Vicent Marti <tanoku@gmail.com> 123456 -1500 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
{"committer Vicent Marti <tanoku@gmail.com> 123456 +0163 \n", "committer ", "Vicent Marti", "tanoku@gmail.com", 123456, 0},
{"author Vicent Marti <tanoku@gmail.com>\n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
/* a variety of dates */
{"author Vicent Marti <tanoku@gmail.com> 0 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0, 0},
{"author Vicent Marti <tanoku@gmail.com> 1234567890 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 1234567890, 0},
{"author Vicent Marti <tanoku@gmail.com> 2147483647 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0x7fffffff, 0},
{"author Vicent Marti <tanoku@gmail.com> 4294967295 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 0xffffffff, 0},
{"author Vicent Marti <tanoku@gmail.com> 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0},
{"author Vicent Marti <tanoku@gmail.com> 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0},
{NULL,NULL,NULL,NULL,0,0}
};
typedef struct {
const char *string;
const char *header;
} failing_signature_test_case;
failing_signature_test_case failing_signature_cases[] = {
{"committer Vicent Marti tanoku@gmail.com> 123456 -0100 \n", "committer "},
{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "author "},
{"author Vicent Marti <tanoku@gmail.com> 12345 \n", "committer "},
{"author Vicent Marti 12345 \n", "author "},
{"author Vicent Marti <broken@email 12345 \n", "author "},
{"committer Vicent Marti ><\n", "committer "},
{"author ", "author "},
{NULL, NULL,}
};
void test_commit_parse__signature(void)
{
passing_signature_test_case *passcase;
failing_signature_test_case *failcase;
for (passcase = passing_signature_cases; passcase->string != NULL; passcase++)
{
const char *str = passcase->string;
size_t len = strlen(passcase->string);
struct git_signature person = {0};
cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
cl_assert_equal_s(passcase->name, person.name);
cl_assert_equal_s(passcase->email, person.email);
cl_assert_equal_i((int)passcase->time, (int)person.when.time);
cl_assert_equal_i(passcase->offset, person.when.offset);
git__free(person.name); git__free(person.email);
}
for (failcase = failing_signature_cases; failcase->string != NULL; failcase++)
{
const char *str = failcase->string;
size_t len = strlen(failcase->string);
git_signature person = {0};
cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n'));
git__free(person.name); git__free(person.email);
}
}
static char *failing_commit_cases[] = {
/* empty commit */
"",
/* random garbage */
"asd97sa9du902e9a0jdsuusad09as9du098709aweu8987sd\n",
/* broken endlines 1 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\r\n\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\r\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\r\n\
\r\n\
a test commit with broken endlines\r\n",
/* broken endlines 2 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\
\
another test commit with broken endlines",
/* starting endlines */
"\ntree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a test commit with a starting endline\n",
/* corrupted commit 1 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent 05452d6349abcd67aa396df",
/* corrupted commit 2 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent ",
/* corrupted commit 3 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
parent ",
/* corrupted commit 4 */
"tree f6c0dad3c7b3481caa9d73db21f91964894a945b\n\
par",
};
static char *passing_commit_cases[] = {
/* simple commit with no message */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n",
/* simple commit, no parent */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works\n",
/* simple commit, no parent, no newline in message */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works",
/* simple commit, 1 parent */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works\n",
/* simple commit with GPG signature */
"tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
gpgsig -----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----\n\
\n\
a simple commit which works\n",
/* some tools create two author entries */
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
author Helpful Coworker <helpful@coworker> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
a simple commit which works",
};
static int parse_commit(git_commit **out, const char *buffer)
{
git_commit *commit;
git_odb_object fake_odb_object;
int error;
commit = (git_commit*)git__malloc(sizeof(git_commit));
memset(commit, 0x0, sizeof(git_commit));
commit->object.repo = g_repo;
memset(&fake_odb_object, 0x0, sizeof(git_odb_object));
fake_odb_object.buffer = (char *)buffer;
fake_odb_object.cached.size = strlen(fake_odb_object.buffer);
error = git_commit__parse(commit, &fake_odb_object);
*out = commit;
return error;
}
void test_commit_parse__entire_commit(void)
{
const int failing_commit_count = ARRAY_SIZE(failing_commit_cases);
const int passing_commit_count = ARRAY_SIZE(passing_commit_cases);
int i;
git_commit *commit;
for (i = 0; i < failing_commit_count; ++i) {
cl_git_fail(parse_commit(&commit, failing_commit_cases[i]));
git_commit__free(commit);
}
for (i = 0; i < passing_commit_count; ++i) {
cl_git_pass(parse_commit(&commit, passing_commit_cases[i]));
if (!i)
cl_assert_equal_s("", git_commit_message(commit));
else
cl_assert(git__prefixcmp(
git_commit_message(commit), "a simple commit which works") == 0);
git_commit__free(commit);
}
}
/* query the details on a parsed commit */
void test_commit_parse__details0(void) {
static const char *commit_ids[] = {
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */
};
const size_t commit_count = sizeof(commit_ids) / sizeof(const char *);
unsigned int i;
for (i = 0; i < commit_count; ++i) {
git_oid id;
git_commit *commit;
const git_signature *author, *committer;
const char *message;
git_time_t commit_time;
unsigned int parents, p;
git_commit *parent = NULL, *old_parent = NULL;
git_oid_fromstr(&id, commit_ids[i]);
cl_git_pass(git_commit_lookup(&commit, g_repo, &id));
message = git_commit_message(commit);
author = git_commit_author(commit);
committer = git_commit_committer(commit);
commit_time = git_commit_time(commit);
parents = git_commit_parentcount(commit);
cl_assert_equal_s("Scott Chacon", author->name);
cl_assert_equal_s("schacon@gmail.com", author->email);
cl_assert_equal_s("Scott Chacon", committer->name);
cl_assert_equal_s("schacon@gmail.com", committer->email);
cl_assert(message != NULL);
cl_assert(commit_time > 0);
cl_assert(parents <= 2);
for (p = 0;p < parents;p++) {
if (old_parent != NULL)
git_commit_free(old_parent);
old_parent = parent;
cl_git_pass(git_commit_parent(&parent, commit, p));
cl_assert(parent != NULL);
cl_assert(git_commit_author(parent) != NULL); /* is it really a commit? */
}
git_commit_free(old_parent);
git_commit_free(parent);
cl_git_fail(git_commit_parent(&parent, commit, parents));
git_commit_free(commit);
}
}
void test_commit_parse__leading_lf(void)
{
git_commit *commit;
const char *buffer =
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
\n\
\n\
This commit has a few LF at the start of the commit message";
const char *message =
"This commit has a few LF at the start of the commit message";
const char *raw_message =
"\n\
\n\
This commit has a few LF at the start of the commit message";
cl_git_pass(parse_commit(&commit, buffer));
cl_assert_equal_s(message, git_commit_message(commit));
cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
git_commit__free(commit);
}
void test_commit_parse__only_lf(void)
{
git_commit *commit;
const char *buffer =
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent e90810b8df3e80c413d903f631643c716887138d\n\
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
\n\
\n\
\n";
const char *message = "";
const char *raw_message = "\n\n";
cl_git_pass(parse_commit(&commit, buffer));
cl_assert_equal_s(message, git_commit_message(commit));
cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
git_commit__free(commit);
}
void test_commit_parse__arbitrary_field(void)
{
git_commit *commit;
git_buf buf = GIT_BUF_INIT;
const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----";
cl_git_pass(parse_commit(&commit, passing_commit_cases[4]));
cl_git_pass(git_commit_header_field(&buf, commit, "tree"));
cl_assert_equal_s("6b79e22d69bf46e289df0345a14ca059dfc9bdf6", buf.ptr);
git_buf_clear(&buf);
cl_git_pass(git_commit_header_field(&buf, commit, "parent"));
cl_assert_equal_s("34734e478d6cf50c27c9d69026d93974d052c454", buf.ptr);
git_buf_clear(&buf);
cl_git_pass(git_commit_header_field(&buf, commit, "gpgsig"));
cl_assert_equal_s(gpgsig, buf.ptr);
git_buf_clear(&buf);
cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "awesomeness"));
cl_git_fail_with(GIT_ENOTFOUND, git_commit_header_field(&buf, commit, "par"));
git_commit__free(commit);
cl_git_pass(parse_commit(&commit, passing_commit_cases[0]));
cl_git_pass(git_commit_header_field(&buf, commit, "committer"));
cl_assert_equal_s("Vicent Marti <tanoku@gmail.com> 1273848544 +0200", buf.ptr);
git_buf_dispose(&buf);
git_commit__free(commit);
}
void test_commit_parse__extract_signature(void)
{
git_odb *odb;
git_oid commit_id;
git_buf signature = GIT_BUF_INIT, signed_data = GIT_BUF_INIT;
const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----";
const char *data = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which works\n";
const char *oneline_signature = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
author Some User <someuser@gmail.com> 1454537944 -0700\n\
committer Some User <someuser@gmail.com> 1454537944 -0700\n\
gpgsig bad\n\
\n\
corrupt signature\n";
const char *oneline_data = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
author Some User <someuser@gmail.com> 1454537944 -0700\n\
committer Some User <someuser@gmail.com> 1454537944 -0700\n\
\n\
corrupt signature\n";
cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[4], strlen(passing_commit_cases[4]), GIT_OBJECT_COMMIT));
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
cl_assert_equal_s(gpgsig, signature.ptr);
cl_assert_equal_s(data, signed_data.ptr);
git_buf_clear(&signature);
git_buf_clear(&signed_data);
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, "gpgsig"));
cl_assert_equal_s(gpgsig, signature.ptr);
cl_assert_equal_s(data, signed_data.ptr);
/* Try to parse a tree */
cl_git_pass(git_oid_fromstr(&commit_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
cl_assert_equal_i(GIT_ERROR_INVALID, git_error_last()->klass);
/* Try to parse an unsigned commit */
cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[1], strlen(passing_commit_cases[1]), GIT_OBJECT_COMMIT));
cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
cl_assert_equal_i(GIT_ERROR_OBJECT, git_error_last()->klass);
/* Parse the commit with a single-line signature */
git_buf_clear(&signature);
git_buf_clear(&signed_data);
cl_git_pass(git_odb_write(&commit_id, odb, oneline_signature, strlen(oneline_signature), GIT_OBJECT_COMMIT));
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
cl_assert_equal_s("bad", signature.ptr);
cl_assert_equal_s(oneline_data, signed_data.ptr);
git_buf_dispose(&signature);
git_buf_dispose(&signed_data);
}
+148
View File
@@ -0,0 +1,148 @@
#include "clar_libgit2.h"
#include "signature.h"
static int try_build_signature(const char *name, const char *email, git_time_t time, int offset)
{
git_signature *sign;
int error = 0;
if ((error = git_signature_new(&sign, name, email, time, offset)) < 0)
return error;
git_signature_free((git_signature *)sign);
return error;
}
static void assert_name_and_email(
const char *expected_name,
const char *expected_email,
const char *name,
const char *email)
{
git_signature *sign;
cl_git_pass(git_signature_new(&sign, name, email, 1234567890, 60));
cl_assert_equal_s(expected_name, sign->name);
cl_assert_equal_s(expected_email, sign->email);
git_signature_free(sign);
}
void test_commit_signature__leading_and_trailing_spaces_are_trimmed(void)
{
assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com ");
assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com \n");
assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " \t nulltoken \n", " \n emeric.fermas@gmail.com \n");
}
void test_commit_signature__leading_and_trailing_crud_is_trimmed(void)
{
assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", "\"nulltoken\"", "\"emeric.fermas@gmail.com\"");
assert_name_and_email("nulltoken w", "emeric.fermas@gmail.com", "nulltoken w.", "emeric.fermas@gmail.com");
assert_name_and_email("nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com", "nulltoken \xe2\x98\xba", "emeric.fermas@gmail.com");
}
void test_commit_signature__timezone_does_not_read_oob(void)
{
const char *header = "A <a@example.com> 1461698487 +1234", *header_end;
git_signature *sig;
/* Let the buffer end midway between the timezone offeset's "+12" and "34" */
header_end = header + strlen(header) - 2;
sig = git__calloc(1, sizeof(git_signature));
cl_assert(sig);
cl_git_pass(git_signature__parse(sig, &header, header_end, NULL, '\0'));
cl_assert_equal_s(sig->name, "A");
cl_assert_equal_s(sig->email, "a@example.com");
cl_assert_equal_i(sig->when.time, 1461698487);
cl_assert_equal_i(sig->when.offset, 12);
git_signature_free(sig);
}
void test_commit_signature__angle_brackets_in_names_are_not_supported(void)
{
cl_git_fail(try_build_signature("<Phil Haack", "phil@haack", 1234567890, 60));
cl_git_fail(try_build_signature("Phil>Haack", "phil@haack", 1234567890, 60));
cl_git_fail(try_build_signature("<Phil Haack>", "phil@haack", 1234567890, 60));
}
void test_commit_signature__angle_brackets_in_email_are_not_supported(void)
{
cl_git_fail(try_build_signature("Phil Haack", ">phil@haack", 1234567890, 60));
cl_git_fail(try_build_signature("Phil Haack", "phil@>haack", 1234567890, 60));
cl_git_fail(try_build_signature("Phil Haack", "<phil@haack>", 1234567890, 60));
}
void test_commit_signature__create_empties(void)
{
/* can not create a signature with empty name or email */
cl_git_pass(try_build_signature("nulltoken", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature("", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature(" ", "emeric.fermas@gmail.com", 1234567890, 60));
cl_git_fail(try_build_signature("nulltoken", "", 1234567890, 60));
cl_git_fail(try_build_signature("nulltoken", " ", 1234567890, 60));
}
void test_commit_signature__create_one_char(void)
{
/* creating a one character signature */
assert_name_and_email("x", "foo@bar.baz", "x", "foo@bar.baz");
}
void test_commit_signature__create_two_char(void)
{
/* creating a two character signature */
assert_name_and_email("xx", "foo@bar.baz", "xx", "foo@bar.baz");
}
void test_commit_signature__create_zero_char(void)
{
/* creating a zero character signature */
git_signature *sign;
cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60));
cl_assert(sign == NULL);
}
void test_commit_signature__from_buf(void)
{
git_signature *sign;
cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 +0200"));
cl_assert_equal_s("Test User", sign->name);
cl_assert_equal_s("test@test.tt", sign->email);
cl_assert_equal_i(1461698487, sign->when.time);
cl_assert_equal_i(120, sign->when.offset);
git_signature_free(sign);
}
void test_commit_signature__from_buf_with_neg_zero_offset(void)
{
git_signature *sign;
cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 -0000"));
cl_assert_equal_s("Test User", sign->name);
cl_assert_equal_s("test@test.tt", sign->email);
cl_assert_equal_i(1461698487, sign->when.time);
cl_assert_equal_i(0, sign->when.offset);
cl_assert_equal_i('-', sign->when.sign);
git_signature_free(sign);
}
void test_commit_signature__pos_and_neg_zero_offsets_dont_match(void)
{
git_signature *with_neg_zero;
git_signature *with_pos_zero;
cl_git_pass(git_signature_from_buffer(&with_neg_zero, "Test User <test@test.tt> 1461698487 -0000"));
cl_git_pass(git_signature_from_buffer(&with_pos_zero, "Test User <test@test.tt> 1461698487 +0000"));
cl_assert(!git_signature__equal(with_neg_zero, with_pos_zero));
git_signature_free((git_signature *)with_neg_zero);
git_signature_free((git_signature *)with_pos_zero);
}
+424
View File
@@ -0,0 +1,424 @@
#include "clar_libgit2.h"
#include "git2/sys/commit.h"
static const char *committer_name = "Vicent Marti";
static const char *committer_email = "vicent@github.com";
static const char *commit_message = "This commit has been created in memory\n\
This is a commit created in memory and it will be written back to disk\n";
static const char *tree_id_str = "1810dff58d8a660512d4832e740f692884338ccd";
static const char *parent_id_str = "8496071c1b46c854b31185ea97743be6a8774479";
static const char *root_commit_message = "This is a root commit\n\
This is a root commit and should be the only one in this branch\n";
static const char *root_reflog_message = "commit (initial): This is a root commit \
This is a root commit and should be the only one in this branch";
static char *head_old;
static git_reference *head, *branch;
static git_commit *commit;
/* Fixture setup */
static git_repository *g_repo;
void test_commit_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_commit_write__cleanup(void)
{
git_reference_free(head);
head = NULL;
git_reference_free(branch);
branch = NULL;
git_commit_free(commit);
commit = NULL;
git__free(head_old);
head_old = NULL;
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
}
/* write a new commit object from memory to disk */
void test_commit_write__from_memory(void)
{
git_oid tree_id, parent_id, commit_id;
git_signature *author, *committer;
const git_signature *author1, *committer1;
git_commit *parent;
git_tree *tree;
git_oid_fromstr(&tree_id, tree_id_str);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
git_oid_fromstr(&parent_id, parent_id_str);
cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id));
/* create signatures */
cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
g_repo,
NULL, /* do not update the HEAD */
author,
committer,
NULL,
commit_message,
tree,
1, parent));
git_object_free((git_object *)parent);
git_object_free((git_object *)tree);
git_signature_free(committer);
git_signature_free(author);
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
/* Check attributes were set correctly */
author1 = git_commit_author(commit);
cl_assert(author1 != NULL);
cl_assert_equal_s(committer_name, author1->name);
cl_assert_equal_s(committer_email, author1->email);
cl_assert(author1->when.time == 987654321);
cl_assert(author1->when.offset == 90);
committer1 = git_commit_committer(commit);
cl_assert(committer1 != NULL);
cl_assert_equal_s(committer_name, committer1->name);
cl_assert_equal_s(committer_email, committer1->email);
cl_assert(committer1->when.time == 123456789);
cl_assert(committer1->when.offset == 60);
cl_assert_equal_s(commit_message, git_commit_message(commit));
}
void test_commit_write__into_buf(void)
{
git_oid tree_id;
git_signature *author, *committer;
git_tree *tree;
git_commit *parent;
git_oid parent_id;
git_buf commit = GIT_BUF_INIT;
git_oid_fromstr(&tree_id, tree_id_str);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
/* create signatures */
cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
git_oid_fromstr(&parent_id, parent_id_str);
cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id));
cl_git_pass(git_commit_create_buffer(&commit, g_repo, author, committer,
NULL, root_commit_message, tree, 1, (const git_commit **) &parent));
cl_assert_equal_s(commit.ptr,
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
author Vicent Marti <vicent@github.com> 987654321 +0130\n\
committer Vicent Marti <vicent@github.com> 123456789 +0100\n\
\n\
This is a root commit\n\
This is a root commit and should be the only one in this branch\n\
");
git_buf_dispose(&commit);
git_tree_free(tree);
git_commit_free(parent);
git_signature_free(author);
git_signature_free(committer);
}
/* create a root commit */
void test_commit_write__root(void)
{
git_oid tree_id, commit_id;
const git_oid *branch_oid;
git_signature *author, *committer;
const char *branch_name = "refs/heads/root-commit-branch";
git_tree *tree;
git_reflog *log;
const git_reflog_entry *entry;
git_oid_fromstr(&tree_id, tree_id_str);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
/* create signatures */
cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
/* First we need to update HEAD so it points to our non-existant branch */
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_assert(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC);
head_old = git__strdup(git_reference_symbolic_target(head));
cl_assert(head_old != NULL);
git_reference_free(head);
cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1, NULL));
cl_git_pass(git_commit_create_v(
&commit_id, /* out id */
g_repo,
"HEAD",
author,
committer,
NULL,
root_commit_message,
tree,
0));
git_object_free((git_object *)tree);
git_signature_free(author);
/*
* The fact that creating a commit works has already been
* tested. Here we just make sure it's our commit and that it was
* written as a root commit.
*/
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
cl_assert(git_commit_parentcount(commit) == 0);
cl_git_pass(git_reference_lookup(&branch, g_repo, branch_name));
branch_oid = git_reference_target(branch);
cl_assert_equal_oid(branch_oid, &commit_id);
cl_assert_equal_s(root_commit_message, git_commit_message(commit));
cl_git_pass(git_reflog_read(&log, g_repo, branch_name));
cl_assert_equal_i(1, git_reflog_entrycount(log));
entry = git_reflog_entry_byindex(log, 0);
cl_assert_equal_s(committer->email, git_reflog_entry_committer(entry)->email);
cl_assert_equal_s(committer->name, git_reflog_entry_committer(entry)->name);
cl_assert_equal_s(root_reflog_message, git_reflog_entry_message(entry));
git_signature_free(committer);
git_reflog_free(log);
}
static int create_commit_from_ids(
git_oid *result,
const git_oid *tree_id,
const git_oid *parent_id)
{
git_signature *author, *committer;
const git_oid *parent_ids[1];
int ret;
cl_git_pass(git_signature_new(
&committer, committer_name, committer_email, 123456789, 60));
cl_git_pass(git_signature_new(
&author, committer_name, committer_email, 987654321, 90));
parent_ids[0] = parent_id;
ret = git_commit_create_from_ids(
result,
g_repo,
NULL,
author,
committer,
NULL,
root_commit_message,
tree_id,
1,
parent_ids);
git_signature_free(committer);
git_signature_free(author);
return ret;
}
void test_commit_write__can_write_invalid_objects(void)
{
git_oid expected_id, tree_id, parent_id, commit_id;
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
/* this is a valid tree and parent */
git_oid_fromstr(&tree_id, tree_id_str);
git_oid_fromstr(&parent_id, parent_id_str);
git_oid_fromstr(&expected_id, "c8571bbec3a72c4bcad31648902e5a453f1adece");
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
cl_assert_equal_oid(&expected_id, &commit_id);
/* this is a wholly invented tree id */
git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890");
git_oid_fromstr(&parent_id, parent_id_str);
git_oid_fromstr(&expected_id, "996008340b8e68d69bf3c28d7c57fb7ec3c8e202");
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
cl_assert_equal_oid(&expected_id, &commit_id);
/* this is a wholly invented parent id */
git_oid_fromstr(&tree_id, tree_id_str);
git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890");
git_oid_fromstr(&expected_id, "d78f660cab89d9791ca6714b57978bf2a7e709fd");
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
cl_assert_equal_oid(&expected_id, &commit_id);
/* these are legitimate objects, but of the wrong type */
git_oid_fromstr(&tree_id, parent_id_str);
git_oid_fromstr(&parent_id, tree_id_str);
git_oid_fromstr(&expected_id, "5d80c07414e3f18792949699dfcacadf7748f361");
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
cl_assert_equal_oid(&expected_id, &commit_id);
}
void test_commit_write__can_validate_objects(void)
{
git_oid tree_id, parent_id, commit_id;
/* this is a valid tree and parent */
git_oid_fromstr(&tree_id, tree_id_str);
git_oid_fromstr(&parent_id, parent_id_str);
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
/* this is a wholly invented tree id */
git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890");
git_oid_fromstr(&parent_id, parent_id_str);
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
/* this is a wholly invented parent id */
git_oid_fromstr(&tree_id, tree_id_str);
git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890");
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
/* these are legitimate objects, but of the wrong type */
git_oid_fromstr(&tree_id, parent_id_str);
git_oid_fromstr(&parent_id, tree_id_str);
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
}
void test_commit_write__attach_signature_checks_objects(void)
{
const char *sig = "magic word: pretty please";
const char *badtree = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which does not work\n";
const char *badparent = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which does not work\n";
git_oid id;
cl_git_fail_with(-1, git_commit_create_with_signature(&id, g_repo, badtree, sig, "magicsig"));
cl_git_fail_with(-1, git_commit_create_with_signature(&id, g_repo, badparent, sig, "magicsig"));
}
void test_commit_write__attach_singleline_signature(void)
{
const char *sig = "magic word: pretty please";
const char *data = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which works\n";
const char *complete = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
magicsig magic word: pretty please\n\
\n\
a simple commit which works\n";
git_oid id;
git_odb *odb;
git_odb_object *obj;
cl_git_pass(git_commit_create_with_signature(&id, g_repo, data, sig, "magicsig"));
cl_git_pass(git_repository_odb(&odb, g_repo));
cl_git_pass(git_odb_read(&obj, odb, &id));
cl_assert_equal_s(complete, git_odb_object_data(obj));
git_odb_object_free(obj);
git_odb_free(odb);
}
void test_commit_write__attach_multiline_signature(void)
{
const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----";
const char *data = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
\n\
a simple commit which works\n";
const char *complete = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
gpgsig -----BEGIN PGP SIGNATURE-----\n\
Version: GnuPG v1.4.12 (Darwin)\n\
\n\
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
cpxtDQQMGYFpXK/71stq\n\
=ozeK\n\
-----END PGP SIGNATURE-----\n\
\n\
a simple commit which works\n";
git_oid one, two;
git_odb *odb;
git_odb_object *obj;
cl_git_pass(git_commit_create_with_signature(&one, g_repo, data, gpgsig, "gpgsig"));
cl_git_pass(git_commit_create_with_signature(&two, g_repo, data, gpgsig, NULL));
cl_assert(!git_oid_cmp(&one, &two));
cl_git_pass(git_repository_odb(&odb, g_repo));
cl_git_pass(git_odb_read(&obj, odb, &one));
cl_assert_equal_s(complete, git_odb_object_data(obj));
git_odb_object_free(obj);
git_odb_free(odb);
}