pack.c 2.06 KB
Newer Older
Ben Straub committed
1 2 3 4 5
#include "clar_libgit2.h"

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
6
#include "ref_helpers.h"
Ben Straub committed
7 8 9 10 11

static const char *loose_tag_ref_name = "refs/tags/e90810b";

static git_repository *g_repo;

12
void test_refs_pack__initialize(void)
Ben Straub committed
13 14 15 16
{
   g_repo = cl_git_sandbox_init("testrepo");
}

17
void test_refs_pack__cleanup(void)
Ben Straub committed
18 19 20 21
{
   cl_git_sandbox_cleanup();
}

22 23 24 25 26 27 28 29
void packall()
{
	git_refdb *refdb;
	
	cl_git_pass(git_repository_refdb(&refdb, g_repo));
	cl_git_pass(git_refdb_compress(refdb));
}

30
void test_refs_pack__empty(void)
Ben Straub committed
31 32 33 34 35 36 37 38
{
   // 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);

39
	packall();
Ben Straub committed
40 41
}

42
void test_refs_pack__loose(void)
Ben Straub committed
43 44 45 46 47 48 49
{
   // 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));
50
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
51
	cl_assert_equal_s(reference->name, loose_tag_ref_name);
Ben Straub committed
52 53 54 55 56 57 58
	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
	 */
59
	packall();
Ben Straub committed
60 61 62

	/* Ensure the packed-refs file exists */
	cl_git_pass(git_buf_joinpath(&temp_path, g_repo->path_repository, GIT_PACKEDREFS_FILE));
Vicent Martí committed
63
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
64 65 66

	/* 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));
67
	cl_assert(reference_is_packed(reference));
Vicent Martí committed
68
	cl_assert_equal_s(reference->name, loose_tag_ref_name);
Ben Straub committed
69 70 71

	/* 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));
Vicent Martí committed
72
	cl_assert(!git_path_exists(temp_path.ptr));
Ben Straub committed
73 74 75 76

	git_reference_free(reference);
	git_buf_free(&temp_path);
}