pack.c 2.8 KB
Newer Older
Ben Straub committed
1 2
#include "clar_libgit2.h"

3
#include "futils.h"
Ben Straub committed
4
#include "git2/reflog.h"
5
#include "git2/refdb.h"
Ben Straub committed
6
#include "reflog.h"
7
#include "refs.h"
8
#include "ref_helpers.h"
Ben Straub committed
9 10 11 12 13

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

static git_repository *g_repo;

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

19
void test_refs_pack__cleanup(void)
Ben Straub committed
20 21 22 23
{
   cl_git_sandbox_cleanup();
}

24
static void packall(void)
25 26
{
	git_refdb *refdb;
27

28 29
	cl_git_pass(git_repository_refdb(&refdb, g_repo));
	cl_git_pass(git_refdb_compress(refdb));
30
	git_refdb_free(refdb);
31 32
}

33
void test_refs_pack__empty(void)
Ben Straub committed
34
{
35
	/* create a packfile for an empty folder */
Ben Straub committed
36 37
	git_buf temp_path = GIT_BUF_INIT;

38
	cl_git_pass(git_buf_join_n(&temp_path, '/', 3, git_repository_path(g_repo), GIT_REFS_HEADS_DIR, "empty_dir"));
39
	cl_git_pass(git_futils_mkdir_r(temp_path.ptr, GIT_REFS_DIR_MODE));
40
	git_buf_dispose(&temp_path);
Ben Straub committed
41

42
	packall();
Ben Straub committed
43 44
}

45
void test_refs_pack__loose(void)
Ben Straub committed
46
{
Russell Belfer committed
47
	/* create a packfile from all the loose refs in a repo */
Ben Straub committed
48 49 50 51 52
	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));
53
	cl_assert(reference_is_packed(reference) == 0);
Vicent Martí committed
54
	cl_assert_equal_s(reference->name, loose_tag_ref_name);
Ben Straub committed
55 56 57 58 59 60 61
	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
	 */
62
	packall();
Ben Straub committed
63 64

	/* Ensure the packed-refs file exists */
65
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE));
Vicent Martí committed
66
	cl_assert(git_path_exists(temp_path.ptr));
Ben Straub committed
67 68 69

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

	/* Ensure the known ref has been removed from the loose folder structure */
74
	cl_git_pass(git_buf_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name));
Vicent Martí committed
75
	cl_assert(!git_path_exists(temp_path.ptr));
Ben Straub committed
76 77

	git_reference_free(reference);
78
	git_buf_dispose(&temp_path);
Ben Straub committed
79
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93

void test_refs_pack__symbolic(void)
{
	/* create a packfile from loose refs skipping symbolic refs */
	int i;
	git_oid head;
	git_reference *ref;
	char name[128];

	cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD"));

	/* make a bunch of references */

	for (i = 0; i < 100; ++i) {
94
		p_snprintf(name, sizeof(name), "refs/heads/symbolic-%03d", i);
95
		cl_git_pass(git_reference_symbolic_create(
96
			&ref, g_repo, name, "refs/heads/master", 0, NULL));
97 98
		git_reference_free(ref);

99
		p_snprintf(name, sizeof(name), "refs/heads/direct-%03d", i);
100
		cl_git_pass(git_reference_create(&ref, g_repo, name, &head, 0, NULL));
101 102 103 104 105
		git_reference_free(ref);
	}

	packall();
}