Commit 152efee2 by Edward Thomson Committed by GitHub

Merge pull request #3865 from libgit2/ethomson/leaks

Fix leaks, some warnings and an error
parents a37624ec df87648a
......@@ -54,6 +54,13 @@ v0.24 + 1
### Breaking API changes
* `git_packbuilder_object_count` and `git_packbuilder_written` now
return a `size_t` instead of a `uint32_t` for more thorough
compatibility with the rest of the library.
* `git_packbuiler_progress` now provides explicitly sized `uint32_t`
values instead of `unsigned int`.
v0.24
-------
......
......@@ -196,7 +196,7 @@ GIT_EXTERN(int) git_packbuilder_foreach(git_packbuilder *pb, git_packbuilder_for
* @param pb the packbuilder
* @return the number of objects in the packfile
*/
GIT_EXTERN(uint32_t) git_packbuilder_object_count(git_packbuilder *pb);
GIT_EXTERN(size_t) git_packbuilder_object_count(git_packbuilder *pb);
/**
* Get the number of objects the packbuilder has already written out
......@@ -204,13 +204,13 @@ GIT_EXTERN(uint32_t) git_packbuilder_object_count(git_packbuilder *pb);
* @param pb the packbuilder
* @return the number of objects which have already been written
*/
GIT_EXTERN(uint32_t) git_packbuilder_written(git_packbuilder *pb);
GIT_EXTERN(size_t) git_packbuilder_written(git_packbuilder *pb);
/** Packbuilder progress notification function */
typedef int (*git_packbuilder_progress)(
int stage,
unsigned int current,
unsigned int total,
uint32_t current,
uint32_t total,
void *payload);
/**
......
......@@ -53,7 +53,10 @@ static int patch_image_init_fromstr(
for (start = in; start < in + in_len; start = end) {
end = memchr(start, '\n', in_len);
if (end < in + in_len)
if (end == NULL)
end = in + in_len;
else if (end < in + in_len)
end++;
line = git_pool_mallocz(&out->pool, 1);
......@@ -97,7 +100,7 @@ static bool match_hunk(
git_diff_line *preimage_line = git_vector_get(&preimage->lines, i);
git_diff_line *image_line = git_vector_get(&image->lines, linenum + i);
if (preimage_line->content_len != preimage_line->content_len ||
if (preimage_line->content_len != image_line->content_len ||
memcmp(preimage_line->content, image_line->content, image_line->content_len) != 0) {
match = 0;
break;
......
......@@ -289,6 +289,7 @@ static int crlf_check(
ca.eol = check_eol(attr_values[1]); /* eol */
}
ca.auto_crlf = GIT_AUTO_CRLF_DEFAULT;
ca.safe_crlf = GIT_SAFE_CRLF_DEFAULT;
/*
* Use the core Git logic to see if we should perform CRLF for this file
......
......@@ -2160,12 +2160,12 @@ static int read_reuc(git_index *index, const char *buffer, size_t size)
if (git__strtol64(&tmp, buffer, &endptr, 8) < 0 ||
!endptr || endptr == buffer || *endptr ||
tmp < 0) {
tmp < 0 || tmp > UINT32_MAX) {
index_entry_reuc_free(lost);
return index_error_invalid("reading reuc entry stage");
}
lost->mode[i] = tmp;
lost->mode[i] = (uint32_t)tmp;
len = (endptr + 1) - buffer;
if (size <= len) {
......
......@@ -42,8 +42,8 @@ typedef struct git_pobject {
* me */
void *delta_data;
unsigned long delta_size;
unsigned long z_delta_size;
size_t delta_size;
size_t z_delta_size;
int written:1,
recursing:1,
......@@ -66,10 +66,11 @@ struct git_packbuilder {
uint32_t nr_objects,
nr_deltified,
nr_alloc,
nr_written,
nr_remaining;
size_t nr_alloc;
git_pobject *object_list;
git_oidmap *object_ix;
......@@ -85,13 +86,13 @@ struct git_packbuilder {
git_cond progress_cond;
/* configs */
uint64_t delta_cache_size;
uint64_t max_delta_cache_size;
uint64_t cache_max_small_delta_size;
uint64_t big_file_threshold;
uint64_t window_memory_limit;
size_t delta_cache_size;
size_t max_delta_cache_size;
size_t cache_max_small_delta_size;
size_t big_file_threshold;
size_t window_memory_limit;
int nr_threads; /* nr of threads to use */
unsigned int nr_threads; /* nr of threads to use */
git_packbuilder_progress progress_cb;
void *progress_cb_payload;
......
......@@ -284,7 +284,7 @@ static int create_binary(
size_t b_datalen)
{
git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT;
unsigned long delta_data_len;
size_t delta_data_len;
int error;
/* The git_delta function accepts unsigned long only */
......@@ -310,7 +310,7 @@ static int create_binary(
if (error == 0) {
error = git_zstream_deflatebuf(
&delta, delta_data, (size_t)delta_data_len);
&delta, delta_data, delta_data_len);
git__free(delta_data);
} else if (error == GIT_EBUFS) {
......
......@@ -897,7 +897,7 @@ done:
*out_len = (path - path_start);
*out = git__strndup(path_start, *out_len);
return (out == NULL) ? -1 : 0;
return (*out == NULL) ? -1 : 0;
}
static int check_filenames(git_patch_parsed *patch)
......
......@@ -264,7 +264,7 @@ cleanup:
* the stack could remove directories name limits, but at the cost of doing
* repeated malloc/frees inside the loop below, so let's not do it now.
*/
static int find_ceiling_dir_offset(
static size_t find_ceiling_dir_offset(
const char *path,
const char *ceiling_directories)
{
......@@ -278,7 +278,7 @@ static int find_ceiling_dir_offset(
min_len = (size_t)(git_path_root(path) + 1);
if (ceiling_directories == NULL || min_len == 0)
return (int)min_len;
return min_len;
for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
......@@ -305,7 +305,7 @@ static int find_ceiling_dir_offset(
}
}
return (int)(max_len <= min_len ? min_len : max_len);
return (max_len <= min_len ? min_len : max_len);
}
/*
......@@ -362,7 +362,7 @@ static int find_repo(
dev_t initial_device = 0;
int min_iterations;
bool in_dot_git;
int ceiling_offset;
size_t ceiling_offset = 0;
git_buf_free(repo_path);
......
......@@ -18,18 +18,16 @@ static void assert_at_end(git_iterator *i, bool verbose)
void expect_iterator_items(
git_iterator *i,
int expected_flat,
size_t expected_flat,
const char **expected_flat_paths,
int expected_total,
size_t expected_total,
const char **expected_total_paths)
{
const git_index_entry *entry;
int count, error;
size_t count;
int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES);
bool v = false;
if (expected_flat < 0) { v = true; expected_flat = -expected_flat; }
if (expected_total < 0) { v = true; expected_total = -expected_total; }
int error;
if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees");
......
extern void expect_iterator_items(
git_iterator *i,
int expected_flat,
size_t expected_flat,
const char **expected_flat_paths,
int expected_total,
size_t expected_total,
const char **expected_total_paths);
extern void expect_advance_over(
......
#include "clar_libgit2.h"
#include "fileops.h"
#include "sysdir.h"
#include <ctype.h>
static void clear_git_env(void)
{
cl_setenv("GIT_DIR", NULL);
cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
cl_setenv("GIT_INDEX_FILE", NULL);
cl_setenv("GIT_NAMESPACE", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_WORK_TREE", NULL);
cl_setenv("GIT_COMMON_DIR", NULL);
}
void test_repo_env__initialize(void)
{
clear_git_env();
}
void test_repo_env__cleanup(void)
{
cl_git_sandbox_cleanup();
if (git_path_isdir("attr"))
git_futils_rmdir_r("attr", NULL, GIT_RMDIR_REMOVE_FILES);
if (git_path_isdir("testrepo.git"))
git_futils_rmdir_r("testrepo.git", NULL, GIT_RMDIR_REMOVE_FILES);
if (git_path_isdir("peeled.git"))
git_futils_rmdir_r("peeled.git", NULL, GIT_RMDIR_REMOVE_FILES);
clear_git_env();
}
static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char *fmt, ...)
{
int ret;
va_list args;
git_buf buf = GIT_BUF_INIT;
va_start(args, fmt);
cl_git_pass(git_buf_vprintf(&buf, fmt, args));
va_end(args);
ret = cl_setenv(name, git_buf_cstr(&buf));
git_buf_free(&buf);
return ret;
}
/* Helper functions for test_repo_open__env, passing through the file and line
* from the caller rather than those of the helper. The expression strings
* distinguish between the possible failures within the helper. */
static void env_pass_(const char *path, const char *file, int line)
{
git_repository *repo;
cl_git_pass_(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_git_pass_(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line);
cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line);
cl_assert_at_line(!git_repository_is_bare(repo), file, line);
git_repository_free(repo);
}
#define env_pass(path) env_pass_((path), __FILE__, __LINE__)
#define cl_git_fail_at_line(expr, file, line) clar__assert((expr) < 0, file, line, "Expected function call to fail: " #expr, NULL, 1)
static void env_fail_(const char *path, const char *file, int line)
{
git_repository *repo;
cl_git_fail_at_line(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_git_fail_at_line(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
}
#define env_fail(path) env_fail_((path), __FILE__, __LINE__)
static void env_cd_(
const char *path,
void (*passfail_)(const char *, const char *, int),
const char *file, int line)
{
git_buf cwd_buf = GIT_BUF_INIT;
cl_git_pass(git_path_prettify_dir(&cwd_buf, ".", NULL));
cl_must_pass(p_chdir(path));
passfail_(NULL, file, line);
cl_must_pass(p_chdir(git_buf_cstr(&cwd_buf)));
git_buf_free(&cwd_buf);
}
#define env_cd_pass(path) env_cd_((path), env_pass_, __FILE__, __LINE__)
#define env_cd_fail(path) env_cd_((path), env_fail_, __FILE__, __LINE__)
static void env_check_objects_(bool a, bool t, bool p, const char *file, int line)
{
git_repository *repo;
git_oid oid_a, oid_t, oid_p;
git_object *object;
cl_git_pass(git_oid_fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d"));
cl_git_pass(git_oid_fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
cl_git_pass(git_oid_fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07"));
cl_git_pass_(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
if (a) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
git_object_free(object);
} else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
}
if (t) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
git_object_free(object);
} else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
}
if (p) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
git_object_free(object);
} else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
}
git_repository_free(repo);
}
#define env_check_objects(a, t, t2) env_check_objects_((a), (t), (t2), __FILE__, __LINE__)
void test_repo_env__open(void)
{
git_repository *repo = NULL;
git_buf repo_dir_buf = GIT_BUF_INIT;
const char *repo_dir = NULL;
git_index *index = NULL;
const char *t_obj = "testrepo.git/objects";
const char *p_obj = "peeled.git/objects";
clear_git_env();
cl_fixture_sandbox("attr");
cl_fixture_sandbox("testrepo.git");
cl_fixture_sandbox("peeled.git");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_git_pass(git_path_prettify_dir(&repo_dir_buf, "attr", NULL));
repo_dir = git_buf_cstr(&repo_dir_buf);
/* GIT_DIR that doesn't exist */
cl_setenv("GIT_DIR", "does-not-exist");
env_fail(NULL);
/* Explicit start_path overrides GIT_DIR */
env_pass("attr");
env_pass("attr/.git");
env_pass("attr/sub");
env_pass("attr/sub/sub");
/* GIT_DIR with relative paths */
cl_setenv("GIT_DIR", "attr/.git");
env_pass(NULL);
cl_setenv("GIT_DIR", "attr");
env_fail(NULL);
cl_setenv("GIT_DIR", "attr/sub");
env_fail(NULL);
cl_setenv("GIT_DIR", "attr/sub/sub");
env_fail(NULL);
/* GIT_DIR with absolute paths */
cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir);
env_pass(NULL);
cl_setenv("GIT_DIR", repo_dir);
env_fail(NULL);
cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir);
env_fail(NULL);
cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir);
env_fail(NULL);
cl_setenv("GIT_DIR", NULL);
/* Searching from the current directory */
env_cd_pass("attr");
env_cd_pass("attr/.git");
env_cd_pass("attr/sub");
env_cd_pass("attr/sub/sub");
/* A ceiling directory blocks searches from ascending into that
* directory, but doesn't block the start_path itself. */
cl_setenv("GIT_CEILING_DIRECTORIES", repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir);
env_cd_pass("attr");
env_cd_pass("attr/sub");
env_cd_fail("attr/sub/sub");
/* Multiple ceiling directories */
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "123%c%s/sub%cabc",
GIT_PATH_LIST_SEPARATOR, repo_dir, GIT_PATH_LIST_SEPARATOR);
env_cd_pass("attr");
env_cd_pass("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
/* Index files */
cl_setenv("GIT_INDEX_FILE", cl_fixture("gitgit.index"));
cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
cl_git_pass(git_repository_index(&index, repo));
cl_assert_equal_s(git_index_path(index), cl_fixture("gitgit.index"));
cl_assert_equal_i(git_index_entrycount(index), 1437);
git_index_free(index);
git_repository_free(repo);
cl_setenv("GIT_INDEX_FILE", NULL);
/* Namespaces */
cl_setenv("GIT_NAMESPACE", "some-namespace");
cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
cl_assert_equal_s(git_repository_get_namespace(repo), "some-namespace");
git_repository_free(repo);
cl_setenv("GIT_NAMESPACE", NULL);
/* Object directories and alternates */
env_check_objects(true, false, false);
cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
env_check_objects(false, true, false);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj);
env_check_objects(true, true, false);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", p_obj);
env_check_objects(false, false, true);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", p_obj);
env_check_objects(false, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
"%s%c%s", t_obj, GIT_PATH_LIST_SEPARATOR, p_obj);
env_check_objects(true, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
"%s%c%s", p_obj, GIT_PATH_LIST_SEPARATOR, t_obj);
env_check_objects(true, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_fixture_cleanup("peeled.git");
cl_fixture_cleanup("testrepo.git");
cl_fixture_cleanup("attr");
git_buf_free(&repo_dir_buf);
clear_git_env();
}
......@@ -3,26 +3,6 @@
#include "sysdir.h"
#include <ctype.h>
static void clear_git_env(void)
{
cl_setenv("GIT_DIR", NULL);
cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
cl_setenv("GIT_INDEX_FILE", NULL);
cl_setenv("GIT_NAMESPACE", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_WORK_TREE", NULL);
cl_setenv("GIT_COMMON_DIR", NULL);
}
static git_buf cwd_backup_buf = GIT_BUF_INIT;
void test_repo_open__initialize(void)
{
if (!git_buf_is_allocated(&cwd_backup_buf))
cl_git_pass(git_path_prettify_dir(&cwd_backup_buf, ".", NULL));
clear_git_env();
}
void test_repo_open__cleanup(void)
{
......@@ -30,15 +10,6 @@ void test_repo_open__cleanup(void)
if (git_path_isdir("alternate"))
git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
if (git_path_isdir("attr"))
git_futils_rmdir_r("attr", NULL, GIT_RMDIR_REMOVE_FILES);
if (git_path_isdir("testrepo.git"))
git_futils_rmdir_r("testrepo.git", NULL, GIT_RMDIR_REMOVE_FILES);
if (git_path_isdir("peeled.git"))
git_futils_rmdir_r("peeled.git", NULL, GIT_RMDIR_REMOVE_FILES);
cl_must_pass(p_chdir(git_buf_cstr(&cwd_backup_buf)));
clear_git_env();
}
void test_repo_open__bare_empty_repo(void)
......@@ -432,230 +403,3 @@ void test_repo_open__force_bare(void)
git_repository_free(barerepo);
}
static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char *fmt, ...)
{
int ret;
va_list args;
git_buf buf = GIT_BUF_INIT;
va_start(args, fmt);
cl_git_pass(git_buf_vprintf(&buf, fmt, args));
va_end(args);
ret = cl_setenv(name, git_buf_cstr(&buf));
git_buf_free(&buf);
return ret;
}
/* Helper functions for test_repo_open__env, passing through the file and line
* from the caller rather than those of the helper. The expression strings
* distinguish between the possible failures within the helper. */
static void env_pass_(const char *path, const char *file, int line)
{
git_repository *repo;
cl_git_pass_(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_git_pass_(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line);
cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line);
cl_assert_at_line(!git_repository_is_bare(repo), file, line);
git_repository_free(repo);
}
#define env_pass(path) env_pass_((path), __FILE__, __LINE__)
#define cl_git_fail_at_line(expr, file, line) clar__assert((expr) < 0, file, line, "Expected function call to fail: " #expr, NULL, 1)
static void env_fail_(const char *path, const char *file, int line)
{
git_repository *repo;
cl_git_fail_at_line(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
cl_git_fail_at_line(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
}
#define env_fail(path) env_fail_((path), __FILE__, __LINE__)
static void env_cd_(
const char *path,
void (*passfail_)(const char *, const char *, int),
const char *file, int line)
{
git_buf cwd_buf = GIT_BUF_INIT;
cl_git_pass(git_path_prettify_dir(&cwd_buf, ".", NULL));
cl_must_pass(p_chdir(path));
passfail_(NULL, file, line);
cl_must_pass(p_chdir(git_buf_cstr(&cwd_buf)));
}
#define env_cd_pass(path) env_cd_((path), env_pass_, __FILE__, __LINE__)
#define env_cd_fail(path) env_cd_((path), env_fail_, __FILE__, __LINE__)
static void env_check_objects_(bool a, bool t, bool p, const char *file, int line)
{
git_repository *repo;
git_oid oid_a, oid_t, oid_p;
git_object *object;
cl_git_pass(git_oid_fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d"));
cl_git_pass(git_oid_fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
cl_git_pass(git_oid_fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07"));
cl_git_pass_(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
if (a) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
git_object_free(object);
} else
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
if (t) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
git_object_free(object);
} else
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
if (p) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
git_object_free(object);
} else
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
git_repository_free(repo);
}
#define env_check_objects(a, t, t2) env_check_objects_((a), (t), (t2), __FILE__, __LINE__)
void test_repo_open__env(void)
{
git_repository *repo = NULL;
git_buf repo_dir_buf = GIT_BUF_INIT;
const char *repo_dir = NULL;
git_index *index = NULL;
const char *t_obj = "testrepo.git/objects";
const char *p_obj = "peeled.git/objects";
cl_fixture_sandbox("attr");
cl_fixture_sandbox("testrepo.git");
cl_fixture_sandbox("peeled.git");
cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
cl_git_pass(git_path_prettify_dir(&repo_dir_buf, "attr", NULL));
repo_dir = git_buf_cstr(&repo_dir_buf);
/* GIT_DIR that doesn't exist */
cl_setenv("GIT_DIR", "does-not-exist");
env_fail(NULL);
/* Explicit start_path overrides GIT_DIR */
env_pass("attr");
env_pass("attr/.git");
env_pass("attr/sub");
env_pass("attr/sub/sub");
/* GIT_DIR with relative paths */
cl_setenv("GIT_DIR", "attr/.git");
env_pass(NULL);
cl_setenv("GIT_DIR", "attr");
env_fail(NULL);
cl_setenv("GIT_DIR", "attr/sub");
env_fail(NULL);
cl_setenv("GIT_DIR", "attr/sub/sub");
env_fail(NULL);
/* GIT_DIR with absolute paths */
cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir);
env_pass(NULL);
cl_setenv("GIT_DIR", repo_dir);
env_fail(NULL);
cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir);
env_fail(NULL);
cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir);
env_fail(NULL);
cl_setenv("GIT_DIR", NULL);
/* Searching from the current directory */
env_cd_pass("attr");
env_cd_pass("attr/.git");
env_cd_pass("attr/sub");
env_cd_pass("attr/sub/sub");
/* A ceiling directory blocks searches from ascending into that
* directory, but doesn't block the start_path itself. */
cl_setenv("GIT_CEILING_DIRECTORIES", repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir);
env_cd_pass("attr");
env_cd_pass("attr/sub");
env_cd_fail("attr/sub/sub");
/* Multiple ceiling directories */
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "123%c%s/sub%cabc",
GIT_PATH_LIST_SEPARATOR, repo_dir, GIT_PATH_LIST_SEPARATOR);
env_cd_pass("attr");
env_cd_pass("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub",
repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir);
env_cd_pass("attr");
env_cd_fail("attr/sub");
env_cd_fail("attr/sub/sub");
cl_setenv("GIT_CEILING_DIRECTORIES", NULL);
/* Index files */
cl_setenv("GIT_INDEX_FILE", cl_fixture("gitgit.index"));
cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
cl_git_pass(git_repository_index(&index, repo));
cl_assert_equal_s(git_index_path(index), cl_fixture("gitgit.index"));
cl_assert_equal_i(git_index_entrycount(index), 1437);
git_index_free(index);
git_repository_free(repo);
cl_setenv("GIT_INDEX_FILE", NULL);
/* Namespaces */
cl_setenv("GIT_NAMESPACE", "some-namespace");
cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
cl_assert_equal_s(git_repository_get_namespace(repo), "some-namespace");
git_repository_free(repo);
cl_setenv("GIT_NAMESPACE", NULL);
/* Object directories and alternates */
env_check_objects(true, false, false);
cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
env_check_objects(false, true, false);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj);
env_check_objects(true, true, false);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", p_obj);
env_check_objects(false, false, true);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", t_obj);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", p_obj);
env_check_objects(false, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv("GIT_OBJECT_DIRECTORY", NULL);
cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
"%s%c%s", t_obj, GIT_PATH_LIST_SEPARATOR, p_obj);
env_check_objects(true, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES",
"%s%c%s", p_obj, GIT_PATH_LIST_SEPARATOR, t_obj);
env_check_objects(true, true, true);
cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL);
cl_fixture_cleanup("peeled.git");
cl_fixture_cleanup("testrepo.git");
cl_fixture_cleanup("attr");
}
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