Hash :
0ba7a031
Author :
Date :
2010-10-07T00:42:55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include "test_lib.h"
#include "test_helpers.h"
#include "commit.h"
#include <git/odb.h>
#include <git/commit.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/sample-odb";
static const char *commit_ids[] = {
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
};
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
BEGIN_TEST(writenew_test)
git_odb *db;
git_repository *repo;
git_commit *commit, *parent;
git_tree *tree;
git_oid id;
/* char hex_oid[41]; */
git_person author = {"Vicent Marti", "vicent@github.com", 123456789};
git_person committer = {"Vicent Marti", "vicent@github.com", 987654321};
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
must_be_true(repo != NULL);
/* Create commit in memory */
commit = git_commit_new(repo);
must_be_true(commit != NULL);
/* Add new parent */
git_oid_mkstr(&id, commit_ids[4]);
parent = git_commit_lookup(repo, &id);
must_be_true(parent != NULL);
git_commit_add_parent(commit, parent);
/* Set other attributes */
git_commit_set_committer(commit, &committer);
git_commit_set_author(commit, &author);
git_commit_set_message(commit,
"This commit has been created in memory\n\
This is a commit created in memory and it will be written back to disk\n");
/* add new tree */
git_oid_mkstr(&id, tree_oid);
tree = git_tree_lookup(repo, &id);
must_be_true(tree != NULL);
git_commit_set_tree(commit, tree);
/* Test it has no OID */
must_be_true(git_commit_id(commit) == NULL);
/* Write to disk */
must_pass(git_object_write((git_object *)commit));
/* Show new SHA1 */
/*
git_oid_fmt(hex_oid, git_commit_id(commit));
hex_oid[40] = 0;
printf("Written new commit, SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)commit));
git_repository_free(repo);
git_odb_close(db);
END_TEST
BEGIN_TEST(writeback_test)
git_odb *db;
git_repository *repo;
git_oid id;
git_commit *commit, *parent;
const char *message;
/* char hex_oid[41]; */
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
must_be_true(repo != NULL);
git_oid_mkstr(&id, commit_ids[0]);
commit = git_commit_lookup(repo, &id);
must_be_true(commit != NULL);
message = git_commit_message(commit);
/*
git_oid_fmt(hex_oid, git_commit_id(commit));
hex_oid[40] = 0;
printf("Old SHA1: %s\n", hex_oid);
*/
git_commit_set_message(commit, "This is a new test message. Cool!\n");
git_oid_mkstr(&id, commit_ids[4]);
parent = git_commit_lookup(repo, &id);
must_be_true(parent != NULL);
git_commit_add_parent(commit, parent);
must_pass(git_object_write((git_object *)commit));
/*
git_oid_fmt(hex_oid, git_commit_id(commit));
hex_oid[40] = 0;
printf("New SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)commit));
git_repository_free(repo);
git_odb_close(db);
END_TEST