|
Packit Service |
20376f |
#include "clar_libgit2.h"
|
|
Packit Service |
20376f |
#include "posix.h"
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static git_repository *repo;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_object_commit_commitstagedfile__initialize(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
cl_fixture("treebuilder");
|
|
Packit Service |
20376f |
cl_git_pass(git_repository_init(&repo, "treebuilder/", 0));
|
|
Packit Service |
20376f |
cl_assert(repo != NULL);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_object_commit_commitstagedfile__cleanup(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_repository_free(repo);
|
|
Packit Service |
20376f |
repo = NULL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_fixture_cleanup("treebuilder");
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_index *index;
|
|
Packit Service |
20376f |
const git_index_entry *entry;
|
|
Packit Service |
20376f |
git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
|
|
Packit Service |
20376f |
git_signature *signature;
|
|
Packit Service |
20376f |
git_tree *tree;
|
|
Packit Service |
20376f |
git_buf buffer;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* The test below replicates the following git scenario
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* $ echo "test" > test.txt
|
|
Packit Service |
20376f |
* $ git hash-object test.txt
|
|
Packit Service |
20376f |
* 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* $ git add .
|
|
Packit Service |
20376f |
* $ git commit -m "Initial commit"
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* $ git log
|
|
Packit Service |
20376f |
* commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
|
|
Packit Service |
20376f |
* Author: nulltoken <emeric.fermas@gmail.com>
|
|
Packit Service |
20376f |
* Date: Wed Dec 14 08:29:03 2011 +0100
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* Initial commit
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* $ git show 1fe3 --format=raw
|
|
Packit Service |
20376f |
* commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
|
|
Packit Service |
20376f |
* tree 2b297e643c551e76cfa1f93810c50811382f9117
|
|
Packit Service |
20376f |
* author nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
|
|
Packit Service |
20376f |
* committer nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* Initial commit
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* diff --git a/test.txt b/test.txt
|
|
Packit Service |
20376f |
* new file mode 100644
|
|
Packit Service |
20376f |
* index 0000000..9daeafb
|
|
Packit Service |
20376f |
* --- /dev/null
|
|
Packit Service |
20376f |
* +++ b/test.txt
|
|
Packit Service |
20376f |
* @@ -0,0 +1 @@
|
|
Packit Service |
20376f |
* +test
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* $ git ls-tree 2b297
|
|
Packit Service |
20376f |
* 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d"));
|
|
Packit Service |
20376f |
cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117"));
|
|
Packit Service |
20376f |
cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4"));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Add a new file to the index
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
cl_git_mkfile("treebuilder/test.txt", "test\n");
|
|
Packit Service |
20376f |
cl_git_pass(git_repository_index(&index, repo));
|
|
Packit Service |
20376f |
cl_git_pass(git_index_add_bypath(index, "test.txt"));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
entry = git_index_get_byindex(index, 0);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert(git_oid_cmp(&expected_blob_oid, &entry->id) == 0);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Information about index entry should match test file
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
struct stat st;
|
|
Packit Service |
20376f |
cl_must_pass(p_lstat("treebuilder/test.txt", &st);;
|
|
Packit Service |
20376f |
cl_assert(entry->file_size == st.st_size);
|
|
Packit Service |
20376f |
#ifndef _WIN32
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Windows doesn't populate these fields, and the signage is
|
|
Packit Service |
20376f |
* wrong in the Windows version of the struct, so lets avoid
|
|
Packit Service |
20376f |
* the "comparing signed and unsigned" compilation warning in
|
|
Packit Service |
20376f |
* that case.
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
cl_assert(entry->uid == st.st_uid);
|
|
Packit Service |
20376f |
cl_assert(entry->gid == st.st_gid);
|
|
Packit Service |
20376f |
#endif
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Build the tree from the index
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
cl_git_pass(git_index_write_tree(&tree_oid, index));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Commit the staged file
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
|
|
Packit Service |
20376f |
cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(&buffer, 0, sizeof(git_buf));
|
|
Packit Service |
20376f |
cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0, '#'));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_create_v(
|
|
Packit Service |
20376f |
&commit_oid,
|
|
Packit Service |
20376f |
repo,
|
|
Packit Service |
20376f |
"HEAD",
|
|
Packit Service |
20376f |
signature,
|
|
Packit Service |
20376f |
signature,
|
|
Packit Service |
20376f |
NULL,
|
|
Packit Service |
20376f |
buffer.ptr,
|
|
Packit Service |
20376f |
tree,
|
|
Packit Service |
20376f |
0));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_buf_free(&buffer);
|
|
Packit Service |
20376f |
git_signature_free(signature);
|
|
Packit Service |
20376f |
git_tree_free(tree);
|
|
Packit Service |
20376f |
git_index_free(index);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void assert_commit_tree_has_n_entries(git_commit *c, int count)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_tree *tree;
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_tree(&tree, c));
|
|
Packit Service |
20376f |
cl_assert_equal_i(count, git_tree_entrycount(tree));
|
|
Packit Service |
20376f |
git_tree_free(tree);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void assert_commit_is_head_(git_commit *c, const char *file, int line)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_commit *head;
|
|
Packit Service |
20376f |
cl_git_pass(git_revparse_single((git_object **)&head, repo, "HEAD"));
|
|
Packit Service |
20376f |
clar__assert(git_oid_equal(git_commit_id(c), git_commit_id(head)), file, line, "Commit is not the HEAD", NULL, 1);
|
|
Packit Service |
20376f |
git_commit_free(head);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
#define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__LINE__)
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void test_object_commit_commitstagedfile__amend_commit(void)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_index *index;
|
|
Packit Service |
20376f |
git_oid old_oid, new_oid, tree_oid;
|
|
Packit Service |
20376f |
git_commit *old_commit, *new_commit;
|
|
Packit Service |
20376f |
git_tree *tree;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* make a commit */
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_mkfile("treebuilder/myfile", "This is a file\n");
|
|
Packit Service |
20376f |
cl_git_pass(git_repository_index(&index, repo));
|
|
Packit Service |
20376f |
cl_git_pass(git_index_add_bypath(index, "myfile"));
|
|
Packit Service |
20376f |
cl_repo_commit_from_index(&old_oid, repo, NULL, 0, "first commit");
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_lookup(&old_commit, repo, &old_oid));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert_equal_i(0, git_commit_parentcount(old_commit));
|
|
Packit Service |
20376f |
assert_commit_tree_has_n_entries(old_commit, 1);
|
|
Packit Service |
20376f |
assert_commit_is_head(old_commit);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* let's amend the message of the HEAD commit */
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_amend(
|
|
Packit Service |
20376f |
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* fail because the commit isn't the tip of the branch anymore */
|
|
Packit Service |
20376f |
cl_git_fail(git_commit_amend(
|
|
Packit Service |
20376f |
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert_equal_i(0, git_commit_parentcount(new_commit));
|
|
Packit Service |
20376f |
assert_commit_tree_has_n_entries(new_commit, 1);
|
|
Packit Service |
20376f |
assert_commit_is_head(new_commit);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_commit_free(old_commit);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
old_commit = new_commit;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* let's amend the tree of that last commit */
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_mkfile("treebuilder/anotherfile", "This is another file\n");
|
|
Packit Service |
20376f |
cl_git_pass(git_index_add_bypath(index, "anotherfile"));
|
|
Packit Service |
20376f |
cl_git_pass(git_index_write_tree(&tree_oid, index));
|
|
Packit Service |
20376f |
cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
|
|
Packit Service |
20376f |
cl_assert_equal_i(2, git_tree_entrycount(tree));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* fail to amend on a ref which does not exist */
|
|
Packit Service |
20376f |
cl_git_fail_with(GIT_ENOTFOUND, git_commit_amend(
|
|
Packit Service |
20376f |
&new_oid, old_commit, "refs/heads/nope", NULL, NULL, NULL, "Initial commit", tree));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_amend(
|
|
Packit Service |
20376f |
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree));
|
|
Packit Service |
20376f |
git_tree_free(tree);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
cl_assert_equal_i(0, git_commit_parentcount(new_commit));
|
|
Packit Service |
20376f |
assert_commit_tree_has_n_entries(new_commit, 2);
|
|
Packit Service |
20376f |
assert_commit_is_head(new_commit);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* cleanup */
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git_commit_free(old_commit);
|
|
Packit Service |
20376f |
git_commit_free(new_commit);
|
|
Packit Service |
20376f |
git_index_free(index);
|
|
Packit Service |
20376f |
}
|