Commit 6fd195d7 by Vicent Marti

Change git_repository initialization to use a path

The constructor to git_repository is now called

	'git_repository_open(path)'

and takes a path to a git repository instead of an existing ODB object.
Unit tests have been updated accordingly and the two test repositories
have been merged into one.

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent d80e9d55
......@@ -53,6 +53,13 @@ int gitfo_write(git_file fd, void *buf, size_t cnt)
return GIT_SUCCESS;
}
int gitfo_isdir(const char *path)
{
struct stat st;
return (path && gitfo_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) ?
GIT_SUCCESS : GIT_ENOTFOUND;
}
int gitfo_exists(const char *path)
{
return access(path, F_OK);
......
......@@ -57,6 +57,7 @@ typedef struct { /* file io buffer */
extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
extern int gitfo_creat(const char *path, int mode);
extern int gitfo_isdir(const char *path);
#define gitfo_close(fd) close(fd)
extern int gitfo_read(git_file fd, void *buf, size_t cnt);
......
#ifndef INCLUDE_git_index_h__
#define INCLUDE_git_index_h__
#include <stdint.h>
#include "common.h"
#include "oid.h"
......@@ -16,18 +17,50 @@ GIT_BEGIN_DECL
/** Memory representation of an index file. */
typedef struct git_index git_index;
/** Time used in a git index entry */
typedef struct {
uint32_t seconds;
uint32_t nanoseconds;
} git_index_time;
/** Memory representation of a file entry in the index. */
typedef struct git_index_entry git_index_entry;
typedef struct git_index_entry {
git_index_time ctime;
git_index_time mtime;
uint32_t dev;
uint32_t ino;
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t file_size;
git_oid oid;
uint16_t flags;
uint16_t flags_extended;
char *path;
} git_index_entry;
/**
* Create a new Git index object as a memory representation
* of the Git index file in 'index_path'.
*
* The argument 'working_dir' is the root path of the indexed
* files in the index and is used to calculate the relative path
* when inserting new entries from existing files on disk.
*
* If 'working _dir' is NULL (e.g for bare repositories), the
* methods working on on-disk files will fail.
*
* @param index_path the path to the index file in disk
* @param working_dir working dir for the git repository
* @return the index object; NULL if the index could not be created
*/
GIT_EXTERN(git_index *) git_index_alloc(const char *index_path);
GIT_EXTERN(git_index *) git_index_alloc(const char *index_path, const char *working_dir);
/**
* Clear the contents (all the entries) of an index object.
......@@ -83,6 +116,19 @@ GIT_EXTERN(int) git_index_find(git_index *index, const char *path);
*/
GIT_EXTERN(int) git_index_add(git_index *index, const char *path, int stage);
/**
* Get a pointer to one of the entries in the index
*
* This entry can be modified, and the changes will be written
* back to disk on the next write() call.
*
* @param index an existing index object
* @param n the position of the entry
* @return a pointer to the entry; NULL if out of bounds
*/
GIT_EXTERN(git_index_entry *) git_index_get(git_index *index, int n);
/** @} */
GIT_END_DECL
#endif
......@@ -4,6 +4,7 @@
#include "common.h"
#include "odb.h"
#include "commit.h"
#include "index.h"
/**
* @file git/repository.h
......@@ -15,15 +16,28 @@
GIT_BEGIN_DECL
/**
* Allocate a new repository object.
* Open a git repository.
*
* TODO: specify the repository's path instead
* of its object database
* The 'path' argument must point to an existing git repository
* folder, e.g.
*
* @param odb an existing object database to back the repo
* /path/to/my_repo/.git/ (normal repository)
* objects/
* index
* HEAD
*
* /path/to/bare_repo/ (bare repository)
* objects/
* index
* HEAD
*
* The method will automatically detect if 'path' is a normal
* or bare repository or fail is 'path' is neither.
*
* @param path the path to the repository
* @return the new repository handle; NULL on error
*/
GIT_EXTERN(git_repository *) git_repository_alloc(git_odb *odb);
GIT_EXTERN(git_repository *) git_repository_open(const char *path);
/**
......@@ -58,6 +72,15 @@ GIT_EXTERN(git_object *) git_repository_lookup(git_repository *repo, const git_o
GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo);
/**
* Get the Index file of a Git repository
*
* @param repo a repository object
* @return a pointer to the Index object;
* NULL if the index cannot be opened
*/
GIT_EXTERN(git_index *) git_repository_index(git_repository *rpeo);
/**
* Create a new in-memory repository object with
* the given type.
*
......
......@@ -97,7 +97,7 @@ static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
static git_index_tree *read_tree_internal(const char **, const char *, git_index_tree *);
git_index *git_index_alloc(const char *index_path)
git_index *git_index_alloc(const char *index_path, const char *work_dir)
{
git_index *index;
......@@ -116,6 +116,9 @@ git_index *git_index_alloc(const char *index_path)
return NULL;
}
if (work_dir != NULL)
index->working_path = git__strdup(work_dir);
/* Check if index file is stored on disk already */
if (gitfo_exists(index->index_file_path) == 0)
index->on_disk = 1;
......@@ -126,6 +129,9 @@ git_index *git_index_alloc(const char *index_path)
void git_index_clear(git_index *index)
{
unsigned int i;
assert(index);
for (i = 0; i < index->entry_count; ++i)
free(index->entries[i].path);
......@@ -139,6 +145,9 @@ void git_index_clear(git_index *index)
void git_index_free(git_index *index)
{
if (index == NULL)
return;
git_index_clear(index);
free(index->entries);
index->entries = NULL;
......@@ -213,6 +222,11 @@ int git_index_write(git_index *index)
return 0;
}
git_index_entry *git_index_get(git_index *index, int n)
{
return (n >= 0 && (unsigned int)n < index->entry_count) ? &index->entries[n] : NULL;
}
int git_index_add(git_index *index, const char *filename, int stage)
{
git_index_entry entry;
......@@ -225,7 +239,7 @@ int git_index_add(git_index *index, const char *filename, int stage)
if (path_length < GIT_IDXENTRY_NAMEMASK)
entry.flags |= path_length;
else
entry.flags |= path_length;
entry.flags |= GIT_IDXENTRY_NAMEMASK;;
if (stage < 0 || stage > 3)
return GIT_ERROR;
......
......@@ -12,31 +12,6 @@
#define GIT_IDXENTRY_VALID (0x8000)
#define GIT_IDXENTRY_STAGESHIFT 12
typedef struct {
uint32_t seconds;
uint32_t nanoseconds;
} git_index_time;
struct git_index_entry {
git_index_time ctime;
git_index_time mtime;
uint32_t dev;
uint32_t ino;
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t file_size;
git_oid oid;
uint16_t flags;
uint16_t flags_extended;
char *path;
};
struct git_index_tree {
char *name;
......@@ -53,6 +28,8 @@ typedef struct git_index_tree git_index_tree;
struct git_index {
char *index_file_path;
char *working_path;
time_t last_modified;
git_index_entry *entries;
......
......@@ -28,6 +28,7 @@
#include "repository.h"
#include "commit.h"
#include "tag.h"
#include "fileops.h"
static const int default_table_size = 32;
static const double max_load_factor = 0.65;
......@@ -43,7 +44,7 @@ static const size_t object_sizes[] = {
};
uint32_t git_object_hash(const void *key)
uint32_t git__objtable_hash(const void *key)
{
uint32_t r;
git_oid *id;
......@@ -53,7 +54,7 @@ uint32_t git_object_hash(const void *key)
return r;
}
int git_object_haskey(void *object, const void *key)
int git__objtable_haskey(void *object, const void *key)
{
git_object *obj;
git_oid *oid;
......@@ -64,7 +65,62 @@ int git_object_haskey(void *object, const void *key)
return (git_oid_cmp(oid, &obj->id) == 0);
}
git_repository *git_repository_alloc(git_odb *odb)
static int parse_repository_folders(git_repository *repo, const char *repository_path)
{
char path_aux[GIT_PATH_MAX];
int path_len, i;
if (gitfo_isdir(repository_path) < 0)
return GIT_ERROR;
path_len = strlen(repository_path);
strcpy(path_aux, repository_path);
if (path_aux[path_len - 1] != '/') {
path_aux[path_len] = '/';
path_aux[path_len + 1] = 0;
path_len = path_len + 1;
}
repo->path_repository = git__strdup(path_aux);
/* objects database */
strcpy(path_aux + path_len, "objects/");
if (gitfo_isdir(path_aux) < 0)
return GIT_ERROR;
repo->path_odb = git__strdup(path_aux);
/* index file */
strcpy(path_aux + path_len, "index");
if (gitfo_exists(path_aux) < 0)
return GIT_ERROR;
repo->path_index = git__strdup(path_aux);
/* HEAD file */
strcpy(path_aux + path_len, "HEAD");
if (gitfo_exists(path_aux) < 0)
return GIT_ERROR;
i = path_len - 2;
while (path_aux[i] != '/')
i--;
if (strcmp(path_aux, "/.git/") == 0) {
repo->is_bare = 0;
path_aux[i + 1] = 0;
repo->path_workdir = git__strdup(path_aux);
} else {
repo->is_bare = 1;
repo->path_workdir = NULL;
}
return GIT_SUCCESS;
}
git_repository *git_repository__alloc()
{
git_repository *repo = git__malloc(sizeof(git_repository));
if (!repo)
......@@ -74,15 +130,30 @@ git_repository *git_repository_alloc(git_odb *odb)
repo->objects = git_hashtable_alloc(
default_table_size,
git_object_hash,
git_object_haskey);
git__objtable_hash,
git__objtable_haskey);
if (repo->objects == NULL) {
free(repo);
return NULL;
}
repo->db = odb; /* TODO: create ODB manually! */
return repo;
}
git_repository *git_repository_open(const char *path)
{
git_repository *repo;
repo = git_repository__alloc();
if (repo == NULL)
return NULL;
if (parse_repository_folders(repo, path) < 0 ||
git_odb_open(&repo->db, repo->path_odb) < 0) {
git_repository_free(repo);
return NULL;
}
return repo;
}
......@@ -92,6 +163,11 @@ void git_repository_free(git_repository *repo)
git_hashtable_iterator it;
git_object *object;
free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
git_hashtable_iterator_init(repo->objects, &it);
while ((object = (git_object *)
......@@ -99,10 +175,21 @@ void git_repository_free(git_repository *repo)
git_object_free(object);
git_hashtable_free(repo->objects);
/* TODO: free odb */
git_odb_close(repo->db);
git_index_free(repo->index);
free(repo);
}
git_index *git_repository_index(git_repository *repo)
{
if (repo->index == NULL) {
repo->index = git_index_alloc(repo->path_index, repo->path_workdir);
assert(repo->index && repo->index->on_disk);
}
return repo->index;
}
static int source_resize(git_odb_source *src)
{
size_t write_offset, new_size;
......
......@@ -7,6 +7,7 @@
#include "git/repository.h"
#include "hashtable.h"
#include "index.h"
typedef struct {
git_rawobj raw;
......@@ -24,7 +25,15 @@ struct git_object {
struct git_repository {
git_odb *db;
git_index *index;
git_hashtable *objects;
char *path_repository;
char *path_index;
char *path_odb;
char *path_workdir;
unsigned is_bare:1;
};
......
P pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack
......@@ -2,8 +2,6 @@
#include "test_helpers.h"
#include <git/odb.h>
#define ODB_FOLDER "../resources/pack-odb"
static const char *packed_objects[] = {
"0266163a49e280c4f5ed1e08facd36a2bd716bcf",
"53fc32d17276939fc79ed05badaef2db09990016",
......
......@@ -2,9 +2,6 @@
#include "test_helpers.h"
#include <git/odb.h>
#define PACK_ODB_FOLDER "../resources/pack-odb"
#define LOOSE_ODB_FOLDER "../resources/sample-odb"
static const char *packed_objects[] = {
"0266163a49e280c4f5ed1e08facd36a2bd716bcf",
"53fc32d17276939fc79ed05badaef2db09990016",
......@@ -161,7 +158,7 @@ BEGIN_TEST(readheader_packed_test)
unsigned int i;
git_odb *db;
must_pass(git_odb_open(&db, PACK_ODB_FOLDER));
must_pass(git_odb_open(&db, ODB_FOLDER));
for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
git_oid id;
......@@ -186,7 +183,7 @@ BEGIN_TEST(readheader_loose_test)
unsigned int i;
git_odb *db;
must_pass(git_odb_open(&db, LOOSE_ODB_FOLDER));
must_pass(git_odb_open(&db, ODB_FOLDER));
for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) {
git_oid id;
......
......@@ -6,8 +6,6 @@
#include <git/commit.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/pack-odb";
static char *test_commits_broken[] = {
/* empty commit */
......@@ -225,11 +223,8 @@ BEGIN_TEST(parse_buffer_test)
int i;
git_repository *repo;
git_odb *db;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
for (i = 0; i < broken_commit_count; ++i) {
......@@ -265,6 +260,4 @@ BEGIN_TEST(parse_buffer_test)
}
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -7,7 +7,6 @@
#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 */
......@@ -21,15 +20,11 @@ BEGIN_TEST(query_details_test)
const size_t commit_count = sizeof(commit_ids) / sizeof(const char *);
unsigned int i;
git_odb *db;
git_repository *repo;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
for (i = 0; i < commit_count; ++i) {
git_oid id;
git_commit *commit;
......@@ -59,5 +54,4 @@ BEGIN_TEST(query_details_test)
}
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -6,7 +6,6 @@
#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 */
......@@ -21,16 +20,13 @@ static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
#define COMMITTER_EMAIL "vicent@github.com"
BEGIN_TEST(writenew_test)
git_odb *db;
git_repository *repo;
git_commit *commit, *parent;
git_tree *tree;
git_oid id;
/* char hex_oid[41]; */
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
/* Create commit in memory */
......@@ -71,27 +67,22 @@ This is a commit created in memory and it will be written back to disk\n");
printf("Written new commit, SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)commit));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit));
//git_person_free(&author);
//git_person_free(&committer);
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);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id, commit_ids[0]);
......@@ -123,8 +114,7 @@ BEGIN_TEST(writeback_test)
printf("New SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)commit));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit));
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -6,7 +6,6 @@
#include <git/commit.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/sample-odb";
/*
$ git log --oneline --graph --decorate
* a4a7dce (HEAD, br2) Merge branch 'master' into br2
......@@ -91,15 +90,12 @@ static int test_walk(git_revwalk *walk, git_commit *start_from,
}
BEGIN_TEST(simple_walk_test)
git_odb *db;
git_oid id;
git_repository *repo;
git_revwalk *walk;
git_commit *head = NULL;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
walk = git_revwalk_alloc(repo);
......@@ -130,5 +126,4 @@ BEGIN_TEST(simple_walk_test)
git_revwalk_free(walk);
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -5,7 +5,7 @@
#include <git/odb.h>
#include <git/index.h>
#define TEST_INDEX_PATH "../resources/index"
#define TEST_INDEX_PATH "../resources/testrepo.git/index"
#define TEST_INDEX2_PATH "../resources/gitgit.index"
#define TEST_INDEX_ENTRY_COUNT 109
......@@ -29,7 +29,7 @@ struct test_entry TEST_ENTRIES[] = {
BEGIN_TEST(index_loadempty_test)
git_index *index;
index = git_index_alloc("in-memory-index");
index = git_index_alloc("in-memory-index", NULL);
must_be_true(index != NULL);
must_be_true(index->on_disk == 0);
......@@ -46,7 +46,7 @@ BEGIN_TEST(index_load_test)
git_index *index;
unsigned int i;
index = git_index_alloc(TEST_INDEX_PATH);
index = git_index_alloc(TEST_INDEX_PATH, NULL);
must_be_true(index != NULL);
must_be_true(index->on_disk);
......@@ -70,7 +70,7 @@ END_TEST
BEGIN_TEST(index2_load_test)
git_index *index;
index = git_index_alloc(TEST_INDEX2_PATH);
index = git_index_alloc(TEST_INDEX2_PATH, NULL);
must_be_true(index != NULL);
must_be_true(index->on_disk);
......@@ -88,7 +88,7 @@ BEGIN_TEST(index_find_test)
git_index *index;
unsigned int i;
index = git_index_alloc(TEST_INDEX_PATH);
index = git_index_alloc(TEST_INDEX_PATH, NULL);
must_be_true(index != NULL);
must_pass(git_index_read(index));
......@@ -104,7 +104,7 @@ BEGIN_TEST(index_findempty_test)
git_index *index;
unsigned int i;
index = git_index_alloc("fake-index");
index = git_index_alloc("fake-index", NULL);
must_be_true(index != NULL);
for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
......
......@@ -5,7 +5,7 @@
#include <git/odb.h>
#include <git/index.h>
#define TEST_INDEX_PATH "../resources/index"
#define TEST_INDEX_PATH "../resources/testrepo.git/index"
int filecmp(const char *filename1, const char *filename2)
{
......@@ -35,7 +35,7 @@ BEGIN_TEST(index_load_test)
git_index *index;
git_filelock out_file;
index = git_index_alloc(TEST_INDEX_PATH);
index = git_index_alloc(TEST_INDEX_PATH, NULL);
must_be_true(index != NULL);
must_pass(git_index_read(index));
must_be_true(index->on_disk);
......
......@@ -36,7 +36,7 @@ BEGIN_TEST(index_sort_test)
git_index *index;
unsigned int i;
index = git_index_alloc(TEST_INDEX_PATH);
index = git_index_alloc(TEST_INDEX_PATH, NULL);
must_be_true(index != NULL);
must_pass(git_index_read(index));
......@@ -54,7 +54,7 @@ END_TEST
BEGIN_TEST(index_sort_empty_test)
git_index *index;
index = git_index_alloc("fake-index");
index = git_index_alloc("fake-index", NULL);
must_be_true(index != NULL);
git_index__sort(index);
......
......@@ -6,21 +6,17 @@
#include <git/commit.h>
#include <git/tag.h>
static const char *odb_dir = "../resources/pack-odb";
static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
BEGIN_TEST(readtag)
git_odb *db;
git_repository *repo;
git_tag *tag1, *tag2;
git_commit *commit;
git_oid id1, id2, id_commit;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id1, tag1_id);
......@@ -44,5 +40,4 @@ BEGIN_TEST(readtag)
must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -6,19 +6,15 @@
#include <git/tag.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/pack-odb";
static const char *tag_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
BEGIN_TEST(tag_writeback_test)
git_odb *db;
git_oid id;
git_repository *repo;
git_tag *tag;
/* char hex_oid[41]; */
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id, tag_id);
......@@ -36,8 +32,7 @@ BEGIN_TEST(tag_writeback_test)
printf("TAG New SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)tag));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag));
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -6,18 +6,14 @@
#include <git/commit.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/sample-odb";
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
BEGIN_TEST(tree_entry_access_test)
git_odb *db;
git_oid id;
git_repository *repo;
git_tree *tree;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id, tree_oid);
......@@ -34,19 +30,15 @@ BEGIN_TEST(tree_entry_access_test)
must_be_true(git_tree_entry_byindex(tree, -1) == NULL);
git_repository_free(repo);
git_odb_close(db);
END_TEST
BEGIN_TEST(tree_read_test)
git_odb *db;
git_oid id;
git_repository *repo;
git_tree *tree;
git_tree_entry *entry;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id, tree_oid);
......@@ -61,9 +53,7 @@ BEGIN_TEST(tree_read_test)
must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);
must_be_true(git_tree_entry_2object(entry) != NULL);
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -6,22 +6,17 @@
#include <git/commit.h>
#include <git/revwalk.h>
static const char *odb_dir = "../resources/sample-odb";
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
BEGIN_TEST(tree_in_memory_add_test)
const unsigned int entry_count = 128;
git_odb *db;
git_repository *repo;
git_tree *tree;
unsigned int i;
git_oid entry_id;
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
tree = git_tree_new(repo);
......@@ -36,16 +31,14 @@ BEGIN_TEST(tree_in_memory_add_test)
must_be_true(git_tree_entrycount(tree) == entry_count);
must_pass(git_object_write((git_object *)tree));
must_pass(remove_loose_object(odb_dir, (git_object *)tree));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
git_object_free((git_object *)tree);
git_repository_free(repo);
git_odb_close(db);
END_TEST
BEGIN_TEST(tree_add_entry_test)
git_odb *db;
git_oid id;
git_repository *repo;
git_tree *tree;
......@@ -53,9 +46,7 @@ BEGIN_TEST(tree_add_entry_test)
unsigned int i;
/* char hex_oid[41]; */
must_pass(git_odb_open(&db, odb_dir));
repo = git_repository_alloc(db);
repo = git_repository_open(REPOSITORY_FOLDER);
must_be_true(repo != NULL);
git_oid_mkstr(&id, tree_oid);
......@@ -92,10 +83,7 @@ BEGIN_TEST(tree_add_entry_test)
printf("TREE New SHA1: %s\n", hex_oid);
*/
must_pass(remove_loose_object(odb_dir, (git_object *)tree));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
git_object_free((git_object *)tree);
git_repository_free(repo);
git_odb_close(db);
END_TEST
......@@ -84,18 +84,23 @@ int remove_object_files(const char *odb_dir, object_data *d)
return 0;
}
int remove_loose_object(const char *odb_dir, git_object *object)
int remove_loose_object(const char *repository_folder, git_object *object)
{
static const char *objects_folder = "objects/";
char *ptr, *full_path, *top_folder;
int path_length;
int path_length, objects_length;
assert(repository_folder && object);
assert(odb_dir && object);
objects_length = strlen(objects_folder);
path_length = strlen(repository_folder);
ptr = full_path = git__malloc(path_length + objects_length + GIT_OID_HEXSZ + 3);
path_length = strlen(odb_dir);
ptr = full_path = git__malloc(path_length + GIT_OID_HEXSZ + 3);
strcpy(ptr, repository_folder);
strcpy(ptr + path_length, objects_folder);
strcpy(ptr, odb_dir);
ptr = top_folder = ptr + path_length;
ptr = top_folder = ptr + path_length + objects_length;
*ptr++ = '/';
git_oid_pathfmt(ptr, git_object_id(object));
ptr += GIT_OID_HEXSZ + 1;
......
......@@ -29,6 +29,8 @@
#include "test_lib.h"
#include <git/odb.h>
#define ODB_FOLDER "../resources/testrepo.git/objects/"
#define REPOSITORY_FOLDER "../resources/testrepo.git/"
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment