Commit 4cba39ac by Vicent Martí

Merge pull request #615 from benstraub/port-all-tests-to-clar

Port all tests to clar
parents 5afe95d2 b1731215
......@@ -51,8 +51,7 @@ SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
# Build options
OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
OPTION (BUILD_TESTS "Build Tests" ON)
OPTION (BUILD_CLAR "Build Tests using the Clar suite" OFF)
OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
OPTION (TAGS "Generate tags" OFF)
# Platform specific compilation flags
......@@ -64,10 +63,11 @@ IF (MSVC)
IF (STDCALL)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
ENDIF ()
# TODO: bring back /RTC1 /RTCc
SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
SET(WIN_RC "src/win32/git2.rc")
# Precompiled headers
ELSE ()
SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
......@@ -127,29 +127,10 @@ INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
# Tests
IF (BUILD_TESTS)
SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
INCLUDE_DIRECTORIES(tests)
FILE(GLOB SRC_TEST tests/t??-*.c)
ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
IF (WIN32)
TARGET_LINK_LIBRARIES(libgit2_test ws2_32)
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
TARGET_LINK_LIBRARIES(libgit2_test socket nsl)
ENDIF ()
ENABLE_TESTING()
ADD_TEST(libgit2_test libgit2_test)
ENDIF ()
IF (BUILD_CLAR)
FIND_PACKAGE(PythonInterp REQUIRED)
SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
......
......@@ -70,7 +70,7 @@ The following CMake variables are declared:
- `INSTALL_LIB`: Where to install libraries to.
- `INSTALL_INC`: Where to install headers to.
- `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON)
- `BUILD_TESTS`: Build the libgit2 test suite (defaults to ON)
- `BUILD_CLAR`: Build [Clar](https://github.com/tanoku/clar)-based test suite (defaults to ON)
- `THREADSAFE`: Build libgit2 with threading support (defaults to OFF)
Language Bindings
......
......@@ -8,7 +8,7 @@ void test_buf_basic__resize(void)
git_buf buf1 = GIT_BUF_INIT;
git_buf_puts(&buf1, test_string);
cl_assert(git_buf_oom(&buf1) == 0);
cl_assert(strcmp(git_buf_cstr(&buf1), test_string) == 0);
cl_assert_strequal(git_buf_cstr(&buf1), test_string);
git_buf_puts(&buf1, test_string);
cl_assert(strlen(git_buf_cstr(&buf1)) == strlen(test_string) * 2);
......@@ -20,10 +20,10 @@ void test_buf_basic__printf(void)
git_buf buf2 = GIT_BUF_INIT;
git_buf_printf(&buf2, "%s %s %d ", "shoop", "da", 23);
cl_assert(git_buf_oom(&buf2) == 0);
cl_assert(strcmp(git_buf_cstr(&buf2), "shoop da 23 ") == 0);
cl_assert_strequal(git_buf_cstr(&buf2), "shoop da 23 ");
git_buf_printf(&buf2, "%s %d", "woop", 42);
cl_assert(git_buf_oom(&buf2) == 0);
cl_assert(strcmp(git_buf_cstr(&buf2), "shoop da 23 woop 42") == 0);
cl_assert_strequal(git_buf_cstr(&buf2), "shoop da 23 woop 42");
git_buf_free(&buf2);
}
......@@ -21,7 +21,7 @@ static int mv_read_cb(const char *name, const char *value, void *data)
if (!strcmp(name, _name))
(*n)++;
return 0;
return GIT_SUCCESS;
}
void test_config_multivar__foreach(void)
......
......@@ -26,9 +26,9 @@ void test_config_new__write_new_config(void)
cl_git_pass(git_config_add_file(config, file, 0));
cl_git_pass(git_config_get_string(config, "color.ui", &out));
cl_assert(strcmp(out, "auto") == 0);
cl_assert_strequal(out, "auto");
cl_git_pass(git_config_get_string(config, "core.editor", &out));
cl_assert(strcmp(out, "ed") == 0);
cl_assert_strequal(out, "ed");
git_config_free(config);
......
......@@ -28,9 +28,9 @@ void test_config_read__case_sensitive(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));
cl_git_pass(git_config_get_string(cfg, "this.that.other", &str));
cl_assert(!strcmp(str, "true"));
cl_assert_strequal(str, "true");
cl_git_pass(git_config_get_string(cfg, "this.That.other", &str));
cl_assert(!strcmp(str, "yes"));
cl_assert_strequal(str, "yes");
cl_git_pass(git_config_get_bool(cfg, "this.that.other", &i));
cl_assert(i == 1);
......@@ -55,7 +55,7 @@ void test_config_read__multiline_value(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));
cl_git_pass(git_config_get_string(cfg, "this.That.and", &str));
cl_assert(!strcmp(str, "one one one two two three three"));
cl_assert_strequal(str, "one one one two two three three");
git_config_free(cfg);
}
......@@ -71,7 +71,7 @@ void test_config_read__subsection_header(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));
cl_git_pass(git_config_get_string(cfg, "section.subsection.var", &str));
cl_assert(!strcmp(str, "hello"));
cl_assert_strequal(str, "hello");
/* The subsection is transformed to lower-case */
cl_must_fail(git_config_get_string(cfg, "section.subSectIon.var", &str));
......@@ -171,10 +171,10 @@ void test_config_read__prefixes(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
cl_git_pass(git_config_get_string(cfg, "remote.ab.url", &str));
cl_assert(strcmp(str, "http://example.com/git/ab") == 0);
cl_assert_strequal(str, "http://example.com/git/ab");
cl_git_pass(git_config_get_string(cfg, "remote.abba.url", &str));
cl_assert(strcmp(str, "http://example.com/git/abba") == 0);
cl_assert_strequal(str, "http://example.com/git/abba");
git_config_free(cfg);
}
......
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "test_lib.h"
#include "test_helpers.h"
#include "clar_libgit2.h"
#include "hashtable.h"
#include "hash.h"
// Helpers
typedef struct _aux_object {
int __bulk;
git_oid id;
int visited;
int __bulk;
git_oid id;
int visited;
} table_item;
static uint32_t hash_func(const void *key, int hash_id)
{
uint32_t r;
const git_oid *id = key;
uint32_t r;
const git_oid *id = (const git_oid *)key;
memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
return r;
memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
return r;
}
static int hash_cmpkey(const void *a, const void *b)
{
return git_oid_cmp(a, b);
return git_oid_cmp((const git_oid*)a, (const git_oid*)b);
}
BEGIN_TEST(table0, "create a new hashtable")
git_hashtable *table = NULL;
table = git_hashtable_alloc(55, hash_func, hash_cmpkey);
must_be_true(table != NULL);
must_be_true(table->size_mask + 1 == 64);
git_hashtable_free(table);
END_TEST
BEGIN_TEST(table1, "fill the hashtable with random entries")
const int objects_n = 32;
int i;
table_item *objects;
git_hashtable *table = NULL;
table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
must_be_true(table != NULL);
objects = git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
/* populate the hash table */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
/* make sure all the inserted objects can be found */
for (i = 0; i < objects_n; ++i) {
git_oid id;
table_item *ob;
git_hash_buf(&id, &i, sizeof(int));
ob = (table_item *)git_hashtable_lookup(table, &id);
must_be_true(ob != NULL);
must_be_true(ob == &(objects[i]));
}
/* make sure we cannot find inexisting objects */
for (i = 0; i < 50; ++i) {
int hash_id;
git_oid id;
hash_id = (rand() % 50000) + objects_n;
git_hash_buf(&id, &hash_id, sizeof(int));
must_be_true(git_hashtable_lookup(table, &id) == NULL);
}
git_hashtable_free(table);
git__free(objects);
END_TEST
void test_hash_table__new(void)
{
// create a new hashtable
git_hashtable *table = NULL;
BEGIN_TEST(table2, "make sure the table resizes automatically")
table = git_hashtable_alloc(55, hash_func, hash_cmpkey);
cl_assert(table != NULL);
cl_assert(table->size_mask + 1 == 64);
const int objects_n = 64;
int i;
unsigned int old_size;
table_item *objects;
git_hashtable *table = NULL;
git_hashtable_free(table);
}
table = git_hashtable_alloc(objects_n, hash_func, hash_cmpkey);
must_be_true(table != NULL);
void test_hash_table__fill(void)
{
// fill the hashtable with random entries
const int objects_n = 32;
int i;
table_item *objects;
git_hashtable *table = NULL;
table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
cl_assert(table != NULL);
objects = (table_item *)git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
/* populate the hash table */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
/* make sure all the inserted objects can be found */
for (i = 0; i < objects_n; ++i) {
git_oid id;
table_item *ob;
git_hash_buf(&id, &i, sizeof(int));
ob = (table_item *)git_hashtable_lookup(table, &id);
cl_assert(ob != NULL);
cl_assert(ob == &(objects[i]));
}
/* make sure we cannot find inexisting objects */
for (i = 0; i < 50; ++i) {
int hash_id;
git_oid id;
hash_id = (rand() % 50000) + objects_n;
git_hash_buf(&id, &hash_id, sizeof(int));
cl_assert(git_hashtable_lookup(table, &id) == NULL);
}
git_hashtable_free(table);
git__free(objects);
}
objects = git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
old_size = table->size_mask + 1;
void test_hash_table__resize(void)
{
// make sure the table resizes automatically
const int objects_n = 64;
int i;
unsigned int old_size;
table_item *objects;
git_hashtable *table = NULL;
/* populate the hash table -- should be automatically resized */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
table = git_hashtable_alloc(objects_n, hash_func, hash_cmpkey);
cl_assert(table != NULL);
must_be_true(table->size_mask > old_size);
objects = (table_item*)git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
/* make sure all the inserted objects can be found */
for (i = 0; i < objects_n; ++i) {
git_oid id;
table_item *ob;
old_size = table->size_mask + 1;
git_hash_buf(&id, &i, sizeof(int));
ob = (table_item *)git_hashtable_lookup(table, &id);
/* populate the hash table -- should be automatically resized */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
must_be_true(ob != NULL);
must_be_true(ob == &(objects[i]));
}
cl_assert(table->size_mask > old_size);
git_hashtable_free(table);
git__free(objects);
/* make sure all the inserted objects can be found */
for (i = 0; i < objects_n; ++i) {
git_oid id;
table_item *ob;
END_TEST
git_hash_buf(&id, &i, sizeof(int));
ob = (table_item *)git_hashtable_lookup(table, &id);
BEGIN_TEST(tableit0, "iterate through all the contents of the table")
cl_assert(ob != NULL);
cl_assert(ob == &(objects[i]));
}
const int objects_n = 32;
int i;
table_item *objects, *ob;
git_hashtable_free(table);
git__free(objects);
}
git_hashtable *table = NULL;
table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
must_be_true(table != NULL);
void test_hash_table__iterate(void)
{
// iterate through all the contents of the table
objects = git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
const int objects_n = 32;
int i;
table_item *objects, *ob;
/* populate the hash table */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
must_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
git_hashtable *table = NULL;
GIT_HASHTABLE_FOREACH_VALUE(table, ob, ob->visited = 1);
table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
cl_assert(table != NULL);
/* make sure all nodes have been visited */
for (i = 0; i < objects_n; ++i)
must_be_true(objects[i].visited);
objects = git__malloc(objects_n * sizeof(table_item));
memset(objects, 0x0, objects_n * sizeof(table_item));
git_hashtable_free(table);
git__free(objects);
END_TEST
/* populate the hash table */
for (i = 0; i < objects_n; ++i) {
git_hash_buf(&(objects[i].id), &i, sizeof(int));
cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
}
GIT_HASHTABLE_FOREACH_VALUE(table, ob, ob->visited = 1);
BEGIN_SUITE(hashtable)
ADD_TEST(table0);
ADD_TEST(table1);
ADD_TEST(table2);
ADD_TEST(tableit0);
END_SUITE
/* make sure all nodes have been visited */
for (i = 0; i < objects_n; ++i)
{
cl_assert(objects[i].visited);
}
git_hashtable_free(table);
git__free(objects);
}
#include "clar_libgit2.h"
#include "index.h"
static const int index_entry_count = 109;
static const int index_entry_count_2 = 1437;
#define TEST_INDEX_PATH cl_fixture("testrepo.git/index")
#define TEST_INDEX2_PATH cl_fixture("gitgit.index")
#define TEST_INDEXBIG_PATH cl_fixture("big.index")
// Suite data
struct test_entry {
int index;
char path[128];
git_off_t file_size;
git_time_t mtime;
};
static struct test_entry test_entries[] = {
{4, "Makefile", 5064, 0x4C3F7F33},
{62, "tests/Makefile", 2631, 0x4C3F7F33},
{36, "src/index.c", 10014, 0x4C43368D},
{6, "git.git-authors", 2709, 0x4C3F7F33},
{48, "src/revobject.h", 1448, 0x4C3F7FE2}
};
// Helpers
static void copy_file(const char *src, const char *dst)
{
git_buf source_buf = GIT_BUF_INIT;
git_file dst_fd;
int error = GIT_ERROR;
cl_git_pass(git_futils_readbuffer(&source_buf, src));
dst_fd = git_futils_creat_withpath(dst, 0777, 0666);
if (dst_fd < 0)
goto cleanup;
cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size));
cleanup:
git_buf_free(&source_buf);
p_close(dst_fd);
}
static void files_are_equal(const char *a, const char *b)
{
git_buf buf_a = GIT_BUF_INIT;
git_buf buf_b = GIT_BUF_INIT;
int pass;
if (git_futils_readbuffer(&buf_a, a) < GIT_SUCCESS)
cl_assert(0);
if (git_futils_readbuffer(&buf_b, b) < GIT_SUCCESS) {
git_buf_free(&buf_a);
cl_assert(0);
}
pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size));
git_buf_free(&buf_a);
git_buf_free(&buf_b);
}
// Fixture setup and teardown
void test_index_tests__initialize(void)
{
}
void test_index_tests__cleanup(void)
{
}
void test_index_tests__empty_index(void)
{
git_index *index;
cl_git_pass(git_index_open(&index, "in-memory-index"));
cl_assert(index->on_disk == 0);
cl_assert(git_index_entrycount(index) == 0);
cl_assert(index->entries.sorted);
git_index_free(index);
}
void test_index_tests__default_test_index(void)
{
git_index *index;
unsigned int i;
git_index_entry **entries;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count);
cl_assert(index->entries.sorted);
entries = (git_index_entry **)index->entries.contents;
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
git_index_entry *e = entries[test_entries[i].index];
cl_assert_strequal(e->path, test_entries[i].path);
cl_assert(e->mtime.seconds == test_entries[i].mtime);
cl_assert(e->file_size == test_entries[i].file_size);
}
git_index_free(index);
}
void test_index_tests__gitgit_index(void)
{
git_index *index;
cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH));
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count_2);
cl_assert(index->entries.sorted);
cl_assert(index->tree != NULL);
git_index_free(index);
}
void test_index_tests__find_in_existing(void)
{
git_index *index;
unsigned int i;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
int idx = git_index_find(index, test_entries[i].path);
cl_assert(idx == test_entries[i].index);
}
git_index_free(index);
}
void test_index_tests__find_in_empty(void)
{
git_index *index;
unsigned int i;
cl_git_pass(git_index_open(&index, "fake-index"));
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
int idx = git_index_find(index, test_entries[i].path);
cl_assert(idx == GIT_ENOTFOUND);
}
git_index_free(index);
}
void test_index_tests__write(void)
{
git_index *index;
copy_file(TEST_INDEXBIG_PATH, "index_rewrite");
cl_git_pass(git_index_open(&index, "index_rewrite"));
cl_assert(index->on_disk);
cl_git_pass(git_index_write(index));
files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite");
git_index_free(index);
p_unlink("index_rewrite");
}
void test_index_tests__sort0(void)
{
// sort the entires in an index
/*
* TODO: This no longer applies:
* index sorting in Git uses some specific changes to the way
* directories are sorted.
*
* We need to specificially check for this by creating a new
* index, adding entries in random order and then
* checking for consistency
*/
}
void test_index_tests__sort1(void)
{
// sort the entires in an empty index
git_index *index;
cl_git_pass(git_index_open(&index, "fake-index"));
/* FIXME: this test is slightly dumb */
cl_assert(index->entries.sorted);
git_index_free(index);
}
void test_index_tests__add(void)
{
git_index *index;
git_filebuf file = GIT_FILEBUF_INIT;
git_repository *repo;
git_index_entry *entry;
git_oid id1;
/* Intialize a new repository */
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
/* Ensure we're the only guy in the room */
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
/* Create a new file in the working directory */
cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0));
cl_git_pass(git_filebuf_write(&file, "hey there\n", 10));
cl_git_pass(git_filebuf_commit(&file, 0666));
/* Store the expected hash of the file/blob
* This has been generated by executing the following
* $ echo "hey there" | git hash-object --stdin
*/
cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
/* Add the new file to the index */
cl_git_pass(git_index_add(index, "test.txt", 0));
/* Wow... it worked! */
cl_assert(git_index_entrycount(index) == 1);
entry = git_index_get(index, 0);
/* And the built-in hashing mechanism worked as expected */
cl_assert(git_oid_cmp(&id1, &entry->oid) == 0);
git_index_free(index);
git_repository_free(repo);
}
......@@ -28,6 +28,6 @@ void test_network_createremotethenload__cleanup(void)
void test_network_createremotethenload__parsing(void)
{
cl_assert(!strcmp(git_remote_name(_remote), "origin"));
cl_assert(!strcmp(git_remote_url(_remote), url));
cl_assert_strequal(git_remote_name(_remote), "origin");
cl_assert_strequal(git_remote_url(_remote), url);
}
......@@ -27,8 +27,8 @@ void test_network_remotes__cleanup(void)
void test_network_remotes__parsing(void)
{
cl_assert(!strcmp(git_remote_name(_remote), "test"));
cl_assert(!strcmp(git_remote_url(_remote), "git://github.com/libgit2/libgit2"));
cl_assert_strequal(git_remote_name(_remote), "test");
cl_assert_strequal(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
}
void test_network_remotes__parsing_ssh_remote(void)
......@@ -53,24 +53,24 @@ void test_network_remotes__unsupported_transport_methods_are_unsupported(void)
void test_network_remotes__refspec_parsing(void)
{
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/test/*"));
cl_assert_strequal(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_strequal(git_refspec_dst(_refspec), "refs/remotes/test/*");
}
void test_network_remotes__set_fetchspec(void)
{
cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*"));
_refspec = git_remote_fetchspec(_remote);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/*"));
cl_assert_strequal(git_refspec_src(_refspec), "refs/*");
cl_assert_strequal(git_refspec_dst(_refspec), "refs/*");
}
void test_network_remotes__set_pushspec(void)
{
cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*"));
_refspec = git_remote_pushspec(_remote);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/*"));
cl_assert_strequal(git_refspec_src(_refspec), "refs/*");
cl_assert_strequal(git_refspec_dst(_refspec), "refs/*");
}
void test_network_remotes__save(void)
......@@ -90,13 +90,13 @@ void test_network_remotes__save(void)
_refspec = git_remote_fetchspec(_remote);
cl_assert(_refspec != NULL);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/upstream/*"));
cl_assert_strequal(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_strequal(git_refspec_dst(_refspec), "refs/remotes/upstream/*");
_refspec = git_remote_pushspec(_remote);
cl_assert(_refspec != NULL);
cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*"));
cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/heads/*"));
cl_assert_strequal(git_refspec_src(_refspec), "refs/heads/*");
cl_assert_strequal(git_refspec_dst(_refspec), "refs/heads/*");
}
void test_network_remotes__fnmatch(void)
......@@ -111,7 +111,7 @@ void test_network_remotes__transform(void)
memset(ref, 0x0, sizeof(ref));
cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master"));
cl_assert(!strcmp(ref, "refs/remotes/test/master"));
cl_assert_strequal(ref, "refs/remotes/test/master");
}
void test_network_remotes__transform_r(void)
......@@ -119,7 +119,7 @@ void test_network_remotes__transform_r(void)
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master"));
cl_assert(!strcmp(git_buf_cstr(&buf), "refs/remotes/test/master"));
cl_assert_strequal(git_buf_cstr(&buf), "refs/remotes/test/master");
git_buf_free(&buf);
}
......
......@@ -33,11 +33,11 @@ void test_notes_notes__1(void)
cl_git_pass(git_note_read(&_note, _repo, NULL, &oid));
cl_assert(!strcmp(git_note_message(_note), "hello world\n"));
cl_assert_strequal(git_note_message(_note), "hello world\n");
cl_assert(!git_oid_cmp(git_note_oid(_note), &note_oid));
cl_git_pass(git_blob_lookup(&_blob, _repo, &note_oid));
cl_assert(!strcmp(git_note_message(_note), git_blob_rawcontent(_blob)));
cl_assert_strequal(git_note_message(_note), git_blob_rawcontent(_blob));
cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
......
......@@ -87,7 +87,7 @@ void test_object_raw_compare__compare_fmt_oids(void)
/* Format produced the right result */
out[GIT_OID_HEXSZ] = '\0';
cl_assert(strcmp(exp, out) == 0);
cl_assert_strequal(exp, out);
}
void test_object_raw_compare__compare_allocfmt_oids(void)
......@@ -100,7 +100,7 @@ void test_object_raw_compare__compare_allocfmt_oids(void)
out = git_oid_allocfmt(&in);
cl_assert(out);
cl_assert(strcmp(exp, out) == 0);
cl_assert_strequal(exp, out);
git__free(out);
}
......@@ -120,5 +120,5 @@ void test_object_raw_compare__compare_pathfmt_oids(void)
/* Format produced the right result */
out[GIT_OID_HEXSZ + 1] = '\0';
cl_assert(strcmp(exp2, out) == 0);
cl_assert_strequal(exp2, out);
}
......@@ -45,7 +45,7 @@ void test_object_raw_convert__succeed_on_oid_to_string_conversion(void)
/* returns out as hex formatted c-string */
str = git_oid_tostr(out, sizeof(out), &in);
cl_assert(str && str == out && *(str+GIT_OID_HEXSZ) == '\0');
cl_assert(strcmp(exp, out) == 0);
cl_assert_strequal(exp, out);
}
void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void)
......@@ -66,7 +66,7 @@ void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void)
/* returns big as hex formatted c-string */
str = git_oid_tostr(big, sizeof(big), &in);
cl_assert(str && str == big && *(str+GIT_OID_HEXSZ) == '\0');
cl_assert(strcmp(exp, big) == 0);
cl_assert_strequal(exp, big);
/* check tail material is untouched */
cl_assert(str && str == big && *(str+GIT_OID_HEXSZ+1) == 'X');
......
......@@ -6,9 +6,13 @@
#include "data.h"
static int hash_object(git_oid *oid, git_rawobj *obj)
static void hash_object_pass(git_oid *oid, git_rawobj *obj)
{
return git_odb_hash(oid, obj->data, obj->len, obj->type);
cl_git_pass(git_odb_hash(oid, obj->data, obj->len, obj->type));
}
static void hash_object_fail(git_oid *oid, git_rawobj *obj)
{
cl_git_fail(git_odb_hash(oid, obj->data, obj->len, obj->type));
}
static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
......@@ -74,28 +78,28 @@ void test_object_raw_hash__hash_junk_data(void)
/* invalid types: */
junk_obj.data = some_data;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJ__EXT1;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJ__EXT2;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJ_OFS_DELTA;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJ_REF_DELTA;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
/* data can be NULL only if len is zero: */
junk_obj.type = GIT_OBJ_BLOB;
junk_obj.data = NULL;
cl_git_pass(hash_object(&id, &junk_obj));
hash_object_pass(&id, &junk_obj);
cl_assert(git_oid_cmp(&id, &id_zero) == 0);
junk_obj.len = 1;
cl_git_fail(hash_object(&id, &junk_obj));
hash_object_fail(&id, &junk_obj);
}
void test_object_raw_hash__hash_commit_object(void)
......@@ -103,7 +107,7 @@ void test_object_raw_hash__hash_commit_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, commit_id));
cl_git_pass(hash_object(&id2, &commit_obj));
hash_object_pass(&id2, &commit_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -112,7 +116,7 @@ void test_object_raw_hash__hash_tree_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, tree_id));
cl_git_pass(hash_object(&id2, &tree_obj));
hash_object_pass(&id2, &tree_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -121,7 +125,7 @@ void test_object_raw_hash__hash_tag_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, tag_id));
cl_git_pass(hash_object(&id2, &tag_obj));
hash_object_pass(&id2, &tag_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -130,7 +134,7 @@ void test_object_raw_hash__hash_zero_length_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, zero_id));
cl_git_pass(hash_object(&id2, &zero_obj));
hash_object_pass(&id2, &zero_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -139,7 +143,7 @@ void test_object_raw_hash__hash_one_byte_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, one_id));
cl_git_pass(hash_object(&id2, &one_obj));
hash_object_pass(&id2, &one_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -148,7 +152,7 @@ void test_object_raw_hash__hash_two_byte_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, two_id));
cl_git_pass(hash_object(&id2, &two_obj));
hash_object_pass(&id2, &two_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -157,6 +161,6 @@ void test_object_raw_hash__hash_multi_byte_object(void)
git_oid id1, id2;
cl_git_pass(git_oid_fromstr(&id1, some_id));
cl_git_pass(hash_object(&id2, &some_obj));
hash_object_pass(&id2, &some_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
......@@ -6,19 +6,19 @@
void test_object_raw_type2string__convert_type_to_string(void)
{
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_BAD), ""));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ__EXT1), ""));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_COMMIT), "commit"));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_TREE), "tree"));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_BLOB), "blob"));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_TAG), "tag"));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ__EXT2), ""));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_OFS_DELTA), "OFS_DELTA"));
cl_assert(!strcmp(git_object_type2string(GIT_OBJ_REF_DELTA), "REF_DELTA"));
cl_assert(!strcmp(git_object_type2string(-2), ""));
cl_assert(!strcmp(git_object_type2string(8), ""));
cl_assert(!strcmp(git_object_type2string(1234), ""));
cl_assert_strequal(git_object_type2string(GIT_OBJ_BAD), "");
cl_assert_strequal(git_object_type2string(GIT_OBJ__EXT1), "");
cl_assert_strequal(git_object_type2string(GIT_OBJ_COMMIT), "commit");
cl_assert_strequal(git_object_type2string(GIT_OBJ_TREE), "tree");
cl_assert_strequal(git_object_type2string(GIT_OBJ_BLOB), "blob");
cl_assert_strequal(git_object_type2string(GIT_OBJ_TAG), "tag");
cl_assert_strequal(git_object_type2string(GIT_OBJ__EXT2), "");
cl_assert_strequal(git_object_type2string(GIT_OBJ_OFS_DELTA), "OFS_DELTA");
cl_assert_strequal(git_object_type2string(GIT_OBJ_REF_DELTA), "REF_DELTA");
cl_assert_strequal(git_object_type2string(-2), "");
cl_assert_strequal(git_object_type2string(8), "");
cl_assert_strequal(git_object_type2string(1234), "");
}
void test_object_raw_type2string__convert_string_to_type(void)
......
#include "clar_libgit2.h"
#include "tag.h"
static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755";
static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static git_repository *g_repo;
// Helpers
static void ensure_tag_pattern_match(git_repository *repo,
const char *pattern,
const size_t expected_matches)
{
git_strarray tag_list;
int error = GIT_SUCCESS;
if ((error = git_tag_list_match(&tag_list, pattern, repo)) < GIT_SUCCESS)
goto exit;
if (tag_list.count != expected_matches)
error = GIT_ERROR;
exit:
git_strarray_free(&tag_list);
cl_git_pass(error);
}
// Fixture setup and teardown
void test_object_tag_read__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tag_read__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tag_read__parse(void)
{
// read and parse a tag from the repository
git_tag *tag1, *tag2;
git_commit *commit;
git_oid id1, id2, id_commit;
git_oid_fromstr(&id1, tag1_id);
git_oid_fromstr(&id2, tag2_id);
git_oid_fromstr(&id_commit, tagged_commit);
cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
cl_assert_strequal(git_tag_name(tag1), "test");
cl_assert(git_tag_type(tag1) == GIT_OBJ_TAG);
cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
cl_assert(tag2 != NULL);
cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
cl_git_pass(git_tag_target((git_object **)&commit, tag2));
cl_assert(commit != NULL);
cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_tag_free(tag1);
git_tag_free(tag2);
git_commit_free(commit);
}
void test_object_tag_read__list(void)
{
// list all tag names from the repository
git_strarray tag_list;
cl_git_pass(git_tag_list(&tag_list, g_repo));
cl_assert(tag_list.count == 3);
git_strarray_free(&tag_list);
}
void test_object_tag_read__list_pattern(void)
{
// list all tag names from the repository matching a specified pattern
ensure_tag_pattern_match(g_repo, "", 3);
ensure_tag_pattern_match(g_repo, "*", 3);
ensure_tag_pattern_match(g_repo, "t*", 1);
ensure_tag_pattern_match(g_repo, "*b", 2);
ensure_tag_pattern_match(g_repo, "e", 0);
ensure_tag_pattern_match(g_repo, "e90810b", 1);
ensure_tag_pattern_match(g_repo, "e90810[ab]", 1);
}
void test_object_tag_read__parse_without_tagger(void)
{
// read and parse a tag without a tagger field
git_repository *bad_tag_repo;
git_tag *bad_tag;
git_commit *commit;
git_oid id, id_commit;
// TODO: This is a little messy
cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git")));
git_oid_fromstr(&id, bad_tag_id);
git_oid_fromstr(&id_commit, badly_tagged_commit);
cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id));
cl_assert(bad_tag != NULL);
cl_assert_strequal(git_tag_name(bad_tag), "e90810b");
cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0);
cl_assert(bad_tag->tagger == NULL);
cl_git_pass(git_tag_target((git_object **)&commit, bad_tag));
cl_assert(commit != NULL);
cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_tag_free(bad_tag);
git_commit_free(commit);
git_repository_free(bad_tag_repo);
}
#include "clar_libgit2.h"
static const char* tagger_name = "Vicent Marti";
static const char* tagger_email = "vicent@github.com";
static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static git_repository *g_repo;
// Helpers
#ifndef GIT_WIN32
#include "odb.h"
static void locate_loose_object(const char *repository_folder,
git_object *object,
char **out,
char **out_folder)
{
static const char *objects_folder = "objects/";
char *ptr, *full_path, *top_folder;
int path_length, objects_length;
assert(repository_folder && object);
objects_length = strlen(objects_folder);
path_length = strlen(repository_folder);
ptr = full_path = git__malloc(path_length + objects_length + GIT_OID_HEXSZ + 3);
strcpy(ptr, repository_folder);
strcpy(ptr + path_length, objects_folder);
ptr = top_folder = ptr + path_length + objects_length;
*ptr++ = '/';
git_oid_pathfmt(ptr, git_object_id(object));
ptr += GIT_OID_HEXSZ + 1;
*ptr = 0;
*out = full_path;
if (out_folder)
*out_folder = top_folder;
}
static void loose_object_mode(const char *repository_folder, git_object *object)
{
char *object_path;
struct stat st;
locate_loose_object(repository_folder, object, &object_path, NULL);
cl_git_pass(p_stat(object_path, &st));
free(object_path);
cl_assert((st.st_mode & 0777) == GIT_OBJECT_FILE_MODE);
}
#endif
// Fixture setup and teardown
void test_object_tag_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tag_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tag_write__basic(void)
{
// write a tag to the repository and read it again
git_tag *tag;
git_oid target_id, tag_id;
git_signature *tagger;
const git_signature *tagger1;
git_reference *ref_tag;
git_object *target;
git_oid_fromstr(&target_id, tagged_commit);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_pass(git_tag_create(
&tag_id, /* out id */
g_repo,
"the-tag",
target,
tagger,
tagger_message,
0));
git_object_free(target);
git_signature_free(tagger);
cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
cl_assert(git_oid_cmp(git_tag_target_oid(tag), &target_id) == 0);
/* Check attributes were set correctly */
tagger1 = git_tag_tagger(tag);
cl_assert(tagger1 != NULL);
cl_assert_strequal(tagger1->name, tagger_name);
cl_assert_strequal(tagger1->email, tagger_email);
cl_assert(tagger1->when.time == 123456789);
cl_assert(tagger1->when.offset == 60);
cl_assert_strequal(git_tag_message(tag), tagger_message);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
cl_git_pass(git_reference_delete(ref_tag));
#ifndef GIT_WIN32
// TODO: Get this to work on Linux
//loose_object_mode("testrepo/", (git_object *)tag);
#endif
git_tag_free(tag);
}
void test_object_tag_write__overwrite(void)
{
// Attempt to write a tag bearing the same name than an already existing tag
git_oid target_id, tag_id;
git_signature *tagger;
git_object *target;
git_oid_fromstr(&target_id, tagged_commit);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_fail(git_tag_create(
&tag_id, /* out id */
g_repo,
"e90810b",
target,
tagger,
tagger_message,
0));
git_object_free(target);
git_signature_free(tagger);
}
void test_object_tag_write__replace(void)
{
// Replace an already existing tag
git_oid target_id, tag_id, old_tag_id;
git_signature *tagger;
git_reference *ref_tag;
git_object *target;
git_oid_fromstr(&target_id, tagged_commit);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
git_oid_cpy(&old_tag_id, git_reference_oid(ref_tag));
git_reference_free(ref_tag);
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_pass(git_tag_create(
&tag_id, /* out id */
g_repo,
"e90810b",
target,
tagger,
tagger_message,
1));
git_object_free(target);
git_signature_free(tagger);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &old_tag_id) != 0);
git_reference_free(ref_tag);
}
void test_object_tag_write__lightweight(void)
{
// write a lightweight tag to the repository and read it again
git_oid target_id, object_id;
git_reference *ref_tag;
git_object *target;
git_oid_fromstr(&target_id, tagged_commit);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
cl_git_pass(git_tag_create_lightweight(
&object_id,
g_repo,
"light-tag",
target,
0));
git_object_free(target);
cl_assert(git_oid_cmp(&object_id, &target_id) == 0);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag"));
cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &target_id) == 0);
cl_git_pass(git_tag_delete(g_repo, "light-tag"));
git_reference_free(ref_tag);
}
void test_object_tag_write__lightweight_over_existing(void)
{
// Attempt to write a lightweight tag bearing the same name than an already existing tag
git_oid target_id, object_id, existing_object_id;
git_object *target;
git_oid_fromstr(&target_id, tagged_commit);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
cl_git_fail(git_tag_create_lightweight(
&object_id,
g_repo,
"e90810b",
target,
0));
git_oid_fromstr(&existing_object_id, tag2_id);
cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0);
git_object_free(target);
}
void test_object_tag_write__delete(void)
{
// Delete an already existing tag
git_reference *ref_tag;
cl_git_pass(git_tag_delete(g_repo, "e90810b"));
cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
git_reference_free(ref_tag);
}
......@@ -18,7 +18,7 @@ static void diff_cmp(const git_tree_diff_data *a, const git_tree_diff_data *b)
cl_assert(a->status - b->status == 0);
cl_assert(strcmp(a->path, b->path) == 0);
cl_assert_strequal(a->path, b->path);
}
static int diff_cb(const git_tree_diff_data *diff, void *data)
......
......@@ -24,7 +24,10 @@ void test_object_tree_frompath__cleanup(void)
cl_fixture_cleanup("testrepo.git");
}
static void assert_tree_from_path(git_tree *root, const char *path, git_error expected_result, const char *expected_raw_oid)
static void assert_tree_from_path(git_tree *root,
const char *path,
git_error expected_result,
const char *expected_raw_oid)
{
git_tree *containing_tree = NULL;
......@@ -35,7 +38,7 @@ static void assert_tree_from_path(git_tree *root, const char *path, git_error ex
cl_assert(containing_tree != NULL && expected_result == GIT_SUCCESS);
cl_assert(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid) == GIT_SUCCESS);
cl_git_pass(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid));
git_tree_free(containing_tree);
}
......
#include "clar_libgit2.h"
#include "tree.h"
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static git_repository *g_repo;
// Fixture setup and teardown
void test_object_tree_read__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_read__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_read__loaded(void)
{
// acces randomly the entries on a loaded tree
git_oid id;
git_tree *tree;
git_oid_fromstr(&id, tree_oid);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert(git_tree_entry_byname(tree, "README") != NULL);
cl_assert(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
cl_assert(git_tree_entry_byname(tree, "") == NULL);
cl_assert(git_tree_entry_byindex(tree, 0) != NULL);
cl_assert(git_tree_entry_byindex(tree, 2) != NULL);
cl_assert(git_tree_entry_byindex(tree, 3) == NULL);
cl_assert(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL);
git_tree_free(tree);
}
void test_object_tree_read__two(void)
{
// read a tree from the repository
git_oid id;
git_tree *tree;
const git_tree_entry *entry;
git_object *obj;
git_oid_fromstr(&id, tree_oid);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert(git_tree_entrycount(tree) == 3);
/* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_TREE) == 0);
cl_assert(obj != NULL);
git_object_free(obj);
obj = NULL;
cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE);
cl_assert(obj == NULL);
entry = git_tree_entry_byname(tree, "README");
cl_assert(entry != NULL);
cl_assert_strequal(git_tree_entry_name(entry), "README");
cl_git_pass(git_tree_entry_2object(&obj, g_repo, entry));
cl_assert(obj != NULL);
git_object_free(obj);
git_tree_free(tree);
}
#include "clar_libgit2.h"
#include "tree.h"
static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
static git_repository *g_repo;
// Helpers
static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
{
static const char *indent = " ";
git_tree *tree;
unsigned int i;
if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
return GIT_ERROR;
for (i = 0; i < git_tree_entrycount(tree); ++i) {
const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
char entry_oid[40];
git_oid_fmt(entry_oid, &entry->oid);
printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);
if (entry->attr == S_IFDIR) {
if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
git_tree_free(tree);
return GIT_ERROR;
}
}
}
git_tree_free(tree);
return GIT_SUCCESS;
}
static void locate_loose_object(const char *repository_folder,
git_object *object,
char **out,
char **out_folder)
{
static const char *objects_folder = "objects/";
char *ptr, *full_path, *top_folder;
int path_length, objects_length;
assert(repository_folder && object);
objects_length = strlen(objects_folder);
path_length = strlen(repository_folder);
ptr = full_path = git__malloc(path_length + objects_length + GIT_OID_HEXSZ + 3);
strcpy(ptr, repository_folder);
strcpy(ptr + path_length, objects_folder);
ptr = top_folder = ptr + path_length + objects_length;
*ptr++ = '/';
git_oid_pathfmt(ptr, git_object_id(object));
ptr += GIT_OID_HEXSZ + 1;
*ptr = 0;
*out = full_path;
if (out_folder)
*out_folder = top_folder;
}
static int loose_object_mode(const char *repository_folder, git_object *object)
{
char *object_path;
struct stat st;
locate_loose_object(repository_folder, object, &object_path, NULL);
if (p_stat(object_path, &st) < 0)
return 0;
free(object_path);
return st.st_mode;
}
static int loose_object_dir_mode(const char *repository_folder, git_object *object)
{
char *object_path;
size_t pos;
struct stat st;
locate_loose_object(repository_folder, object, &object_path, NULL);
pos = strlen(object_path);
while (pos--) {
if (object_path[pos] == '/') {
object_path[pos] = 0;
break;
}
}
if (p_stat(object_path, &st) < 0)
return 0;
free(object_path);
return st.st_mode;
}
// Fixture setup and teardown
void test_object_tree_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#if 0
void xtest_object_tree_write__print(void)
{
// write a tree from an index
git_index *index;
git_oid tree_oid;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_tree_create_fromindex(&tree_oid, index));
cl_git_pass(print_tree(g_repo, &tree_oid, 0));
}
#endif
void test_object_tree_write__from_memory(void)
{
// write a tree from a memory
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, rid, id2;
git_oid_fromstr(&id, first_tree);
git_oid_fromstr(&id2, second_tree);
git_oid_fromstr(&bid, blob_oid);
//create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
cl_git_fail(git_treebuilder_insert(NULL, builder, "", &bid, 0100644));
cl_git_fail(git_treebuilder_insert(NULL, builder, "/", &bid, 0100644));
cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt", &bid, 0100644));
cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
cl_git_pass(git_treebuilder_write(&rid, g_repo, builder));
cl_assert(git_oid_cmp(&rid, &id2) == 0);
git_treebuilder_free(builder);
git_tree_free(tree);
}
void test_object_tree_write__subtree(void)
{
// write a hierarchical tree from a memory
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, subtree_id, id2, id3;
git_oid id_hiearar;
git_oid_fromstr(&id, first_tree);
git_oid_fromstr(&id2, second_tree);
git_oid_fromstr(&id3, third_tree);
git_oid_fromstr(&bid, blob_oid);
//create subtree
cl_git_pass(git_treebuilder_create(&builder, NULL));
cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644));
cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder));
git_treebuilder_free(builder);
// create parent tree
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000));
cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder));
git_treebuilder_free(builder);
git_tree_free(tree);
cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0);
// check data is correct
cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar));
cl_assert(2 == git_tree_entrycount(tree));
#ifndef GIT_WIN32
// TODO: fix these
//cl_assert((loose_object_dir_mode("testrepo", (git_object *)tree) & 0777) == GIT_OBJECT_DIR_MODE);
//cl_assert((loose_object_mode("testrespo", (git_object *)tree) & 0777) == GIT_OBJECT_FILE_MODE);
#endif
git_tree_free(tree);
}
......@@ -11,7 +11,7 @@ void test_refs_crashes__double_free(void)
cl_git_pass(git_reference_lookup(&ref2, repo, REFNAME));
cl_git_pass(git_reference_delete(ref));
/* reference is gone from disk, so reloading it will fail */
cl_must_fail(git_reference_reload(ref2));
cl_git_fail(git_reference_reload(ref2));
git_repository_free(repo);
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *current_head_target = "refs/heads/master";
static git_repository *g_repo;
void test_ref_create__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_ref_create__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_ref_create__symbolic(void)
{
// create a new symbolic reference
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_repository *repo2;
git_oid id;
git_buf ref_path = GIT_BUF_INIT;
const char *new_head_tracker = "another-head-tracker";
git_oid_fromstr(&id, current_master_tip);
/* Retrieve the physical path to the symbolic ref for further cleaning */
cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker));
git_buf_free(&ref_path);
/* Create and write the new symbolic reference */
cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
cl_assert_strequal(looked_up_ref->name, new_head_tracker);
/* ...peeled.. */
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);
/* ...and that it points to the current master tip */
cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
/* Similar test with a fresh new repository */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker));
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
git_repository_free(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
}
void test_ref_create__deep_symbolic(void)
{
// create a deep symbolic reference
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_oid id;
git_buf ref_path = GIT_BUF_INIT;
const char *new_head_tracker = "deep/rooted/tracker";
git_oid_fromstr(&id, current_master_tip);
cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker));
cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0));
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
git_buf_free(&ref_path);
}
void test_ref_create__oid(void)
{
// create a new OID reference
git_reference *new_reference, *looked_up_ref;
git_repository *repo2;
git_oid id;
git_buf ref_path = GIT_BUF_INIT;
const char *new_head = "refs/heads/new-head";
git_oid_fromstr(&id, current_master_tip);
/* Retrieve the physical path to the symbolic ref for further cleaning */
cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head));
/* Create and write the new object id reference */
cl_git_pass(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID);
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
cl_assert_strequal(looked_up_ref->name, new_head);
/* ...and that it points to the current master tip */
cl_assert(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
git_reference_free(looked_up_ref);
/* Similar test with a fresh new repository */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head));
cl_assert(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
git_repository_free(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_buf_free(&ref_path);
}
void test_ref_create__oid_unknown(void)
{
// Can not create a new OID reference which targets at an unknown id
git_reference *new_reference, *looked_up_ref;
git_oid id;
const char *new_head = "refs/heads/new-head";
git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644");
/* Create and write the new object id reference */
cl_git_fail(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0));
/* Ensure the reference can't be looked-up... */
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head));
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static git_repository *g_repo;
void test_refs_delete__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_delete__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_delete__packed_loose(void)
{
// deleting a ref which is both packed and loose should remove both tracks in the filesystem
git_reference *looked_up_ref, *another_looked_up_ref;
git_buf temp_path = GIT_BUF_INIT;
/* Ensure the loose reference exists on the file system */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, packed_test_head_name));
cl_git_pass(git_path_exists(temp_path.ptr));
/* Lookup the reference */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's the loose version that has been found */
cl_assert(git_reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is deleted... */
cl_git_pass(git_reference_delete(looked_up_ref));
/* Looking up the reference once again should not retrieve it */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure the loose reference doesn't exist any longer on the file system */
cl_git_pass(!git_path_exists(temp_path.ptr));
git_reference_free(another_looked_up_ref);
git_buf_free(&temp_path);
}
void test_refs_delete__packed_only(void)
{
// can delete a just packed reference
git_reference *ref;
git_oid id;
const char *new_ref = "refs/heads/new_ref";
git_oid_fromstr(&id, current_master_tip);
/* Create and write the new object id reference */
cl_git_pass(git_reference_create_oid(&ref, g_repo, new_ref, &id, 0));
git_reference_free(ref);
/* Lookup the reference */
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a loose reference */
cl_assert(git_reference_is_packed(ref) == 0);
/* Pack all existing references */
cl_git_pass(git_reference_packall(g_repo));
/* Reload the reference from disk */
cl_git_pass(git_reference_reload(ref));
/* Ensure it's a packed reference */
cl_assert(git_reference_is_packed(ref) == 1);
/* This should pass */
cl_git_pass(git_reference_delete(ref));
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static git_repository *g_repo;
void test_refs_list__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_list__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_list__all(void)
{
// try to list all the references in our test repo
git_strarray ref_list;
cl_git_pass(git_reference_listall(&ref_list, g_repo, GIT_REF_LISTALL));
/*{
unsigned short i;
for (i = 0; i < ref_list.count; ++i)
printf("# %s\n", ref_list.strings[i]);
}*/
/* We have exactly 9 refs in total if we include the packed ones:
* there is a reference that exists both in the packfile and as
* loose, but we only list it once */
cl_assert(ref_list.count == 9);
git_strarray_free(&ref_list);
}
void test_refs_list__symbolic_only(void)
{
// try to list only the symbolic references
git_strarray ref_list;
cl_git_pass(git_reference_listall(&ref_list, g_repo, GIT_REF_SYMBOLIC));
cl_assert(ref_list.count == 0); /* no symrefs in the test repo */
git_strarray_free(&ref_list);
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
// Helpers
static void ensure_refname_normalized(int is_oid_ref,
const char *input_refname,
const char *expected_refname)
{
char buffer_out[GIT_REFNAME_MAX];
if (is_oid_ref)
cl_git_pass(git_reference__normalize_name_oid(buffer_out, sizeof(buffer_out), input_refname));
else
cl_git_pass(git_reference__normalize_name(buffer_out, sizeof(buffer_out), input_refname));
if (expected_refname)
cl_assert(0 == strcmp(buffer_out, expected_refname));
}
static void ensure_refname_invalid(int is_oid_ref, const char *input_refname)
{
char buffer_out[GIT_REFNAME_MAX];
if (is_oid_ref)
cl_git_fail(git_reference__normalize_name_oid(buffer_out, sizeof(buffer_out), input_refname));
else
cl_git_fail(git_reference__normalize_name(buffer_out, sizeof(buffer_out), input_refname));
}
#define OID_REF 1
#define SYM_REF 0
void test_refs_normalize__direct(void)
{
// normalize a direct (OID) reference name
ensure_refname_invalid(OID_REF, "a");
ensure_refname_invalid(OID_REF, "");
ensure_refname_invalid(OID_REF, "refs/heads/a/");
ensure_refname_invalid(OID_REF, "refs/heads/a.");
ensure_refname_invalid(OID_REF, "refs/heads/a.lock");
ensure_refname_normalized(OID_REF, "refs/dummy/a", NULL);
ensure_refname_normalized(OID_REF, "refs/stash", NULL);
ensure_refname_normalized(OID_REF, "refs/tags/a", "refs/tags/a");
ensure_refname_normalized(OID_REF, "refs/heads/a/b", "refs/heads/a/b");
ensure_refname_normalized(OID_REF, "refs/heads/a./b", "refs/heads/a./b");
ensure_refname_invalid(OID_REF, "refs/heads/foo?bar");
ensure_refname_invalid(OID_REF, "refs/heads\foo");
ensure_refname_normalized(OID_REF, "refs/heads/v@ation", "refs/heads/v@ation");
ensure_refname_normalized(OID_REF, "refs///heads///a", "refs/heads/a");
ensure_refname_invalid(OID_REF, "refs/heads/.a/b");
ensure_refname_invalid(OID_REF, "refs/heads/foo/../bar");
ensure_refname_invalid(OID_REF, "refs/heads/foo..bar");
ensure_refname_invalid(OID_REF, "refs/heads/./foo");
ensure_refname_invalid(OID_REF, "refs/heads/v@{ation");
}
void test_refs_normalize__symbolic(void)
{
// normalize a symbolic reference name
ensure_refname_normalized(SYM_REF, "a", "a");
ensure_refname_normalized(SYM_REF, "a/b", "a/b");
ensure_refname_normalized(SYM_REF, "refs///heads///a", "refs/heads/a");
ensure_refname_invalid(SYM_REF, "");
ensure_refname_invalid(SYM_REF, "heads\foo");
}
/* Ported from JGit, BSD licence.
* See https://github.com/spearce/JGit/commit/e4bf8f6957bbb29362575d641d1e77a02d906739 */
void test_refs_normalize__jgit_suite(void)
{
// tests borrowed from JGit
/* EmptyString */
ensure_refname_invalid(SYM_REF, "");
ensure_refname_invalid(SYM_REF, "/");
/* MustHaveTwoComponents */
ensure_refname_invalid(OID_REF, "master");
ensure_refname_normalized(SYM_REF, "heads/master", "heads/master");
/* ValidHead */
ensure_refname_normalized(SYM_REF, "refs/heads/master", "refs/heads/master");
ensure_refname_normalized(SYM_REF, "refs/heads/pu", "refs/heads/pu");
ensure_refname_normalized(SYM_REF, "refs/heads/z", "refs/heads/z");
ensure_refname_normalized(SYM_REF, "refs/heads/FoO", "refs/heads/FoO");
/* ValidTag */
ensure_refname_normalized(SYM_REF, "refs/tags/v1.0", "refs/tags/v1.0");
/* NoLockSuffix */
ensure_refname_invalid(SYM_REF, "refs/heads/master.lock");
/* NoDirectorySuffix */
ensure_refname_invalid(SYM_REF, "refs/heads/master/");
/* NoSpace */
ensure_refname_invalid(SYM_REF, "refs/heads/i haz space");
/* NoAsciiControlCharacters */
{
char c;
char buffer[GIT_REFNAME_MAX];
for (c = '\1'; c < ' '; c++) {
strncpy(buffer, "refs/heads/mast", 15);
strncpy(buffer + 15, (const char *)&c, 1);
strncpy(buffer + 16, "er", 2);
buffer[18 - 1] = '\0';
ensure_refname_invalid(SYM_REF, buffer);
}
}
/* NoBareDot */
ensure_refname_invalid(SYM_REF, "refs/heads/.");
ensure_refname_invalid(SYM_REF, "refs/heads/..");
ensure_refname_invalid(SYM_REF, "refs/heads/./master");
ensure_refname_invalid(SYM_REF, "refs/heads/../master");
/* NoLeadingOrTrailingDot */
ensure_refname_invalid(SYM_REF, ".");
ensure_refname_invalid(SYM_REF, "refs/heads/.bar");
ensure_refname_invalid(SYM_REF, "refs/heads/..bar");
ensure_refname_invalid(SYM_REF, "refs/heads/bar.");
/* ContainsDot */
ensure_refname_normalized(SYM_REF, "refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
ensure_refname_invalid(SYM_REF, "refs/heads/master..pu");
/* NoMagicRefCharacters */
ensure_refname_invalid(SYM_REF, "refs/heads/master^");
ensure_refname_invalid(SYM_REF, "refs/heads/^master");
ensure_refname_invalid(SYM_REF, "^refs/heads/master");
ensure_refname_invalid(SYM_REF, "refs/heads/master~");
ensure_refname_invalid(SYM_REF, "refs/heads/~master");
ensure_refname_invalid(SYM_REF, "~refs/heads/master");
ensure_refname_invalid(SYM_REF, "refs/heads/master:");
ensure_refname_invalid(SYM_REF, "refs/heads/:master");
ensure_refname_invalid(SYM_REF, ":refs/heads/master");
/* ShellGlob */
ensure_refname_invalid(SYM_REF, "refs/heads/master?");
ensure_refname_invalid(SYM_REF, "refs/heads/?master");
ensure_refname_invalid(SYM_REF, "?refs/heads/master");
ensure_refname_invalid(SYM_REF, "refs/heads/master[");
ensure_refname_invalid(SYM_REF, "refs/heads/[master");
ensure_refname_invalid(SYM_REF, "[refs/heads/master");
ensure_refname_invalid(SYM_REF, "refs/heads/master*");
ensure_refname_invalid(SYM_REF, "refs/heads/*master");
ensure_refname_invalid(SYM_REF, "*refs/heads/master");
/* ValidSpecialCharacters */
ensure_refname_normalized(SYM_REF, "refs/heads/!", "refs/heads/!");
ensure_refname_normalized(SYM_REF, "refs/heads/\"", "refs/heads/\"");
ensure_refname_normalized(SYM_REF, "refs/heads/#", "refs/heads/#");
ensure_refname_normalized(SYM_REF, "refs/heads/$", "refs/heads/$");
ensure_refname_normalized(SYM_REF, "refs/heads/%", "refs/heads/%");
ensure_refname_normalized(SYM_REF, "refs/heads/&", "refs/heads/&");
ensure_refname_normalized(SYM_REF, "refs/heads/'", "refs/heads/'");
ensure_refname_normalized(SYM_REF, "refs/heads/(", "refs/heads/(");
ensure_refname_normalized(SYM_REF, "refs/heads/)", "refs/heads/)");
ensure_refname_normalized(SYM_REF, "refs/heads/+", "refs/heads/+");
ensure_refname_normalized(SYM_REF, "refs/heads/,", "refs/heads/,");
ensure_refname_normalized(SYM_REF, "refs/heads/-", "refs/heads/-");
ensure_refname_normalized(SYM_REF, "refs/heads/;", "refs/heads/;");
ensure_refname_normalized(SYM_REF, "refs/heads/<", "refs/heads/<");
ensure_refname_normalized(SYM_REF, "refs/heads/=", "refs/heads/=");
ensure_refname_normalized(SYM_REF, "refs/heads/>", "refs/heads/>");
ensure_refname_normalized(SYM_REF, "refs/heads/@", "refs/heads/@");
ensure_refname_normalized(SYM_REF, "refs/heads/]", "refs/heads/]");
ensure_refname_normalized(SYM_REF, "refs/heads/_", "refs/heads/_");
ensure_refname_normalized(SYM_REF, "refs/heads/`", "refs/heads/`");
ensure_refname_normalized(SYM_REF, "refs/heads/{", "refs/heads/{");
ensure_refname_normalized(SYM_REF, "refs/heads/|", "refs/heads/|");
ensure_refname_normalized(SYM_REF, "refs/heads/}", "refs/heads/}");
// This is valid on UNIX, but not on Windows
// hence we make in invalid due to non-portability
//
ensure_refname_invalid(SYM_REF, "refs/heads/\\");
/* UnicodeNames */
/*
* Currently this fails.
* ensure_refname_normalized(SYM_REF, "refs/heads/\u00e5ngstr\u00f6m", "refs/heads/\u00e5ngstr\u00f6m");
*/
/* RefLogQueryIsValidRef */
ensure_refname_invalid(SYM_REF, "refs/heads/master@{1}");
ensure_refname_invalid(SYM_REF, "refs/heads/master@{1.hour.ago}");
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *ref_name = "refs/heads/other";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_branch_name = "refs/heads/branch";
static const char *ref_test_name = "refs/heads/test";
static git_repository *g_repo;
void test_ref_overwrite__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_ref_overwrite__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_ref_overwrite__symbolic(void)
{
// Overwrite an existing symbolic reference
git_reference *ref, *branch_ref;
/* The target needds to exist and we need to check the name has changed */
cl_git_pass(git_reference_create_symbolic(&branch_ref, g_repo, ref_branch_name, ref_master_name, 0));
cl_git_pass(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_branch_name, 0));
git_reference_free(ref);
/* Ensure it points to the right place*/
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
cl_assert_strequal(git_reference_target(ref), ref_branch_name);
git_reference_free(ref);
/* Ensure we can't create it unless we force it to */
cl_git_fail(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_master_name, 0));
cl_git_pass(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_master_name, 1));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
cl_assert_strequal(git_reference_target(ref), ref_master_name);
git_reference_free(ref);
git_reference_free(branch_ref);
}
void test_ref_overwrite__object_id(void)
{
// Overwrite an existing object id reference
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
git_reference_free(ref);
/* Create it */
cl_git_pass(git_reference_create_oid(&ref, g_repo, ref_name, &id, 0));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_test_name));
cl_assert(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
git_reference_free(ref);
/* Ensure we can't overwrite unless we force it */
cl_git_fail(git_reference_create_oid(&ref, g_repo, ref_name, &id, 0));
cl_git_pass(git_reference_create_oid(&ref, g_repo, ref_name, &id, 1));
git_reference_free(ref);
/* Ensure it has been overwritten */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(!git_oid_cmp(&id, git_reference_oid(ref)));
git_reference_free(ref);
}
void test_ref_overwrite__object_id_with_symbolic(void)
{
// Overwrite an existing object id reference with a symbolic one
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
git_reference_free(ref);
cl_git_pass(git_reference_create_oid(&ref, g_repo, ref_name, &id, 0));
git_reference_free(ref);
cl_git_fail(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_master_name, 0));
cl_git_pass(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_master_name, 1));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REF_SYMBOLIC);
cl_assert_strequal(git_reference_target(ref), ref_master_name);
git_reference_free(ref);
}
void test_ref_overwrite__symbolic_with_object_id(void)
{
// Overwrite an existing symbolic reference with an object id one
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REF_OID);
git_oid_cpy(&id, git_reference_oid(ref));
git_reference_free(ref);
/* Create the symbolic ref */
cl_git_pass(git_reference_create_symbolic(&ref, g_repo, ref_name, ref_master_name, 0));
git_reference_free(ref);
/* It shouldn't overwrite unless we tell it to */
cl_git_fail(git_reference_create_oid(&ref, g_repo, ref_name, &id, 0));
cl_git_pass(git_reference_create_oid(&ref, g_repo, ref_name, &id, 1));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REF_OID);
cl_assert(!git_oid_cmp(git_reference_oid(ref), &id));
git_reference_free(ref);
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static git_repository *g_repo;
void test_ref_pack__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_ref_pack__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_ref_pack__empty(void)
{
// create a packfile for an empty folder
git_buf temp_path = GIT_BUF_INIT;
cl_git_pass(git_buf_join_n(&temp_path, '/', 3, g_repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir"));
cl_git_pass(git_futils_mkdir_r(temp_path.ptr, NULL, GIT_REFS_DIR_MODE));
git_buf_free(&temp_path);
cl_git_pass(git_reference_packall(g_repo));
}
void test_ref_pack__loose(void)
{
// create a packfile from all the loose rn a repo
git_reference *reference;
git_buf temp_path = GIT_BUF_INIT;
/* Ensure a known loose ref can be looked up */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert_strequal(reference->name, loose_tag_ref_name);
git_reference_free(reference);
/*
* We are now trying to pack also a loose reference
* called `points_to_blob`, to make sure we can properly
* pack weak tags
*/
cl_git_pass(git_reference_packall(g_repo));
/* Ensure the packed-refs file exists */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE));
cl_git_pass(git_path_exists(temp_path.ptr));
/* Ensure the known ref can still be looked up but is now packed */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_is_packed(reference));
cl_assert_strequal(reference->name, loose_tag_ref_name);
/* Ensure the known ref has been removed from the loose folder structure */
cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, loose_tag_ref_name));
cl_git_pass(!git_path_exists(temp_path.ptr));
git_reference_free(reference);
git_buf_free(&temp_path);
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
static const char *head_tracker_sym_ref_name = "head-tracker";
static const char *current_head_target = "refs/heads/master";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *packed_head_name = "refs/heads/packed";
static const char *packed_test_head_name = "refs/heads/packed-test";
static git_repository *g_repo;
void test_ref_read__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_ref_read__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_ref_read__loose_tag(void)
{
// lookup a loose tag reference
git_reference *reference;
git_object *object;
git_buf ref_name_from_tag_name = GIT_BUF_INIT;
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert_strequal(reference->name, loose_tag_ref_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_oid(reference), GIT_OBJ_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJ_TAG);
/* Ensure the name of the tag matches the name of the reference */
cl_git_pass(git_buf_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)));
cl_assert_strequal(ref_name_from_tag_name.ptr, loose_tag_ref_name);
git_buf_free(&ref_name_from_tag_name);
git_object_free(object);
git_reference_free(reference);
}
void test_ref_read__nonexisting_tag(void)
{
// lookup a loose tag reference that doesn't exist
git_reference *reference;
cl_git_fail(git_reference_lookup(&reference, g_repo, non_existing_tag_ref_name));
git_reference_free(reference);
}
void test_ref_read__symbolic(void)
{
// lookup a symbolic reference
git_reference *reference, *resolved_ref;
git_object *object;
git_oid id;
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert_strequal(reference->name, GIT_HEAD_FILE);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
git_oid_fromstr(&id, current_master_tip);
cl_assert(git_oid_cmp(&id, git_object_id(object)) == 0);
git_object_free(object);
git_reference_free(reference);
git_reference_free(resolved_ref);
}
void test_ref_read__nested_symbolic(void)
{
// lookup a nested symbolic reference
git_reference *reference, *resolved_ref;
git_object *object;
git_oid id;
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_assert(git_reference_type(reference) & GIT_REF_SYMBOLIC);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert_strequal(reference->name, head_tracker_sym_ref_name);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_oid(resolved_ref), GIT_OBJ_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
git_oid_fromstr(&id, current_master_tip);
cl_assert(git_oid_cmp(&id, git_object_id(object)) == 0);
git_object_free(object);
git_reference_free(reference);
git_reference_free(resolved_ref);
}
void test_ref_read__head_then_master(void)
{
// lookup the HEAD and resolve the master branch
git_reference *reference, *resolved_ref, *comp_base_ref;
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_git_pass(git_reference_resolve(&comp_base_ref, reference));
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_git_pass(git_oid_cmp(git_reference_oid(comp_base_ref), git_reference_oid(resolved_ref)));
git_reference_free(reference);
git_reference_free(resolved_ref);
cl_git_pass(git_reference_lookup(&reference, g_repo, current_head_target));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_git_pass(git_oid_cmp(git_reference_oid(comp_base_ref), git_reference_oid(resolved_ref)));
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(comp_base_ref);
}
void test_ref_read__master_then_head(void)
{
// lookup the master branch and then the HEAD
git_reference *reference, *master_ref, *resolved_ref;
cl_git_pass(git_reference_lookup(&master_ref, g_repo, current_head_target));
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_git_pass(git_oid_cmp(git_reference_oid(master_ref), git_reference_oid(resolved_ref)));
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(master_ref);
}
void test_ref_read__packed(void)
{
// lookup a packed reference
git_reference *reference;
git_object *object;
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference));
cl_assert_strequal(reference->name, packed_head_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_oid(reference), GIT_OBJ_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJ_COMMIT);
git_object_free(object);
git_reference_free(reference);
}
void test_ref_read__loose_first(void)
{
// assure that a loose reference is looked up before a packed reference
git_reference *reference;
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
cl_assert(git_reference_type(reference) & GIT_REF_OID);
cl_assert(git_reference_is_packed(reference) == 0);
cl_assert_strequal(reference->name, packed_test_head_name);
git_reference_free(reference);
}
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *new_ref = "refs/heads/test-reflog";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *commit_msg = "commit: bla bla";
static git_repository *g_repo;
// helpers
static void assert_signature(git_signature *expected, git_signature *actual)
{
cl_assert(actual);
cl_assert_strequal(expected->name, actual->name);
cl_assert_strequal(expected->email, actual->email);
cl_assert(expected->when.offset == actual->when.offset);
cl_assert(expected->when.time == actual->when.time);
}
// Fixture setup and teardown
void test_refs_reflog__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_reflog__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_reflog__write_then_read(void)
{
// write a reflog for a given reference and ensure it can be read back
git_repository *repo2;
git_reference *ref, *lookedup_ref;
git_oid oid;
git_signature *committer;
git_reflog *reflog;
git_reflog_entry *entry;
char oid_str[GIT_OID_HEXSZ+1];
/* Create a new branch pointing at the HEAD */
git_oid_fromstr(&oid, current_master_tip);
cl_git_pass(git_reference_create_oid(&ref, g_repo, new_ref, &oid, 0));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
cl_git_pass(git_reflog_write(ref, NULL, committer, NULL));
cl_git_fail(git_reflog_write(ref, NULL, committer, "no ancestor NULL for an existing reflog"));
cl_git_fail(git_reflog_write(ref, NULL, committer, "no\nnewline"));
cl_git_pass(git_reflog_write(ref, &oid, committer, commit_msg));
/* Reopen a new instance of the repository */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
/* Lookup the preivously created branch */
cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref));
/* Read and parse the reflog for this branch */
cl_git_pass(git_reflog_read(&reflog, lookedup_ref));
cl_assert(reflog->entries.length == 2);
entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 0);
assert_signature(committer, entry->committer);
git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old);
cl_assert_strequal("0000000000000000000000000000000000000000", oid_str);
git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur);
cl_assert_strequal(current_master_tip, oid_str);
cl_assert(entry->msg == NULL);
entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 1);
assert_signature(committer, entry->committer);
git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old);
cl_assert_strequal(current_master_tip, oid_str);
git_oid_tostr(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur);
cl_assert_strequal(current_master_tip, oid_str);
cl_assert_strequal(commit_msg, entry->msg);
git_signature_free(committer);
git_reflog_free(reflog);
git_repository_free(repo2);
git_reference_free(ref);
git_reference_free(lookedup_ref);
}
void test_refs_reflog__dont_write_bad(void)
{
// avoid writing an obviously wrong reflog
git_reference *ref;
git_oid oid;
git_signature *committer;
/* Create a new branch pointing at the HEAD */
git_oid_fromstr(&oid, current_master_tip);
cl_git_pass(git_reference_create_oid(&ref, g_repo, new_ref, &oid, 0));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
/* Write the reflog for the new branch */
cl_git_pass(git_reflog_write(ref, NULL, committer, NULL));
/* Try to update the reflog with wrong information:
* It's no new reference, so the ancestor OID cannot
* be NULL. */
cl_git_fail(git_reflog_write(ref, NULL, committer, NULL));
git_signature_free(committer);
git_reference_free(ref);
}
#include "clar_libgit2.h"
#include "odb.h"
#include "repository.h"
#define TEMP_REPO_FOLDER "temprepo/"
#define DISCOVER_FOLDER TEMP_REPO_FOLDER "discover.git"
#define SUB_REPOSITORY_FOLDER_NAME "sub_repo"
#define SUB_REPOSITORY_FOLDER DISCOVER_FOLDER "/" SUB_REPOSITORY_FOLDER_NAME
#define SUB_REPOSITORY_FOLDER_SUB SUB_REPOSITORY_FOLDER "/sub"
#define SUB_REPOSITORY_FOLDER_SUB_SUB SUB_REPOSITORY_FOLDER_SUB "/subsub"
#define SUB_REPOSITORY_FOLDER_SUB_SUB_SUB SUB_REPOSITORY_FOLDER_SUB_SUB "/subsubsub"
#define REPOSITORY_ALTERNATE_FOLDER DISCOVER_FOLDER "/alternate_sub_repo"
#define REPOSITORY_ALTERNATE_FOLDER_SUB REPOSITORY_ALTERNATE_FOLDER "/sub"
#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB "/subsub"
#define REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/subsubsub"
#define ALTERNATE_MALFORMED_FOLDER1 DISCOVER_FOLDER "/alternate_malformed_repo1"
#define ALTERNATE_MALFORMED_FOLDER2 DISCOVER_FOLDER "/alternate_malformed_repo2"
#define ALTERNATE_MALFORMED_FOLDER3 DISCOVER_FOLDER "/alternate_malformed_repo3"
#define ALTERNATE_NOT_FOUND_FOLDER DISCOVER_FOLDER "/alternate_not_found_repo"
static void ensure_repository_discover(const char *start_path,
const char *ceiling_dirs,
const char *expected_path)
{
char found_path[GIT_PATH_MAX];
cl_git_pass(git_repository_discover(found_path, sizeof(found_path), start_path, 0, ceiling_dirs));
//across_fs is always 0 as we can't automate the filesystem change tests
cl_assert_strequal(found_path, expected_path);
}
static void write_file(const char *path, const char *content)
{
git_file file;
int error;
if (git_path_exists(path) == GIT_SUCCESS) {
cl_git_pass(p_unlink(path));
}
file = git_futils_creat_withpath(path, 0777, 0666);
cl_assert(file >= 0);
error = p_write(file, content, strlen(content) * sizeof(char));
p_close(file);
cl_git_pass(error);
}
//no check is performed on ceiling_dirs length, so be sure it's long enough
static void append_ceiling_dir(git_buf *ceiling_dirs, const char *path)
{
git_buf pretty_path = GIT_BUF_INIT;
char ceiling_separator[2] = { GIT_PATH_LIST_SEPARATOR, '\0' };
cl_git_pass(git_path_prettify_dir(&pretty_path, path, NULL));
if (ceiling_dirs->size > 0)
git_buf_puts(ceiling_dirs, ceiling_separator);
git_buf_puts(ceiling_dirs, pretty_path.ptr);
git_buf_free(&pretty_path);
cl_git_pass(git_buf_lasterror(ceiling_dirs));
}
void test_repo_discover__0(void)
{
// test discover
git_repository *repo;
git_buf ceiling_dirs_buf = GIT_BUF_INIT;
const char *ceiling_dirs;
char repository_path[GIT_PATH_MAX];
char sub_repository_path[GIT_PATH_MAX];
char found_path[GIT_PATH_MAX];
const mode_t mode = 0777;
git_futils_mkdir_r(DISCOVER_FOLDER, NULL, mode);
append_ceiling_dir(&ceiling_dirs_buf, TEMP_REPO_FOLDER);
ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
cl_assert(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
cl_git_pass(git_repository_init(&repo, DISCOVER_FOLDER, 1));
cl_git_pass(git_repository_discover(repository_path, sizeof(repository_path), DISCOVER_FOLDER, 0, ceiling_dirs));
git_repository_free(repo);
cl_git_pass(git_repository_init(&repo, SUB_REPOSITORY_FOLDER, 0));
cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
cl_git_pass(git_repository_discover(sub_repository_path, sizeof(sub_repository_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
cl_git_pass(git_futils_mkdir_r(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, NULL, mode));
ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, ceiling_dirs, sub_repository_path);
cl_git_pass(git_futils_mkdir_r(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, NULL, mode));
write_file(REPOSITORY_ALTERNATE_FOLDER "/" DOT_GIT, "gitdir: ../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB "/" DOT_GIT, "gitdir: ../../../" SUB_REPOSITORY_FOLDER_NAME "/" DOT_GIT);
write_file(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB "/" DOT_GIT, "gitdir: ../../../../");
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path);
cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER1, NULL, mode));
write_file(ALTERNATE_MALFORMED_FOLDER1 "/" DOT_GIT, "Anything but not gitdir:");
cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER2, NULL, mode));
write_file(ALTERNATE_MALFORMED_FOLDER2 "/" DOT_GIT, "gitdir:");
cl_git_pass(git_futils_mkdir_r(ALTERNATE_MALFORMED_FOLDER3, NULL, mode));
write_file(ALTERNATE_MALFORMED_FOLDER3 "/" DOT_GIT, "gitdir: \n\n\n");
cl_git_pass(git_futils_mkdir_r(ALTERNATE_NOT_FOUND_FOLDER, NULL, mode));
write_file(ALTERNATE_NOT_FOUND_FOLDER "/" DOT_GIT, "gitdir: a_repository_that_surely_does_not_exist");
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs));
append_ceiling_dir(&ceiling_dirs_buf, SUB_REPOSITORY_FOLDER);
ceiling_dirs = git_buf_cstr(&ceiling_dirs_buf);
//this must pass as ceiling_directories cannot predent the current
//working directory to be checked
cl_git_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs));
cl_git_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs));
//.gitfile redirection should not be affected by ceiling directories
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB, ceiling_dirs, sub_repository_path);
ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER_SUB_SUB_SUB, ceiling_dirs, repository_path);
cl_git_pass(git_futils_rmdir_r(TEMP_REPO_FOLDER, 1));
git_repository_free(repo);
git_buf_free(&ceiling_dirs_buf);
}
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