tree.c 4.23 KB
Newer Older
1 2 3 4 5 6
#include "clar_libgit2.h"

#include "git2/checkout.h"
#include "repository.h"

static git_repository *g_repo;
7 8
static git_checkout_opts g_opts;
static git_object *g_object;
9 10 11 12

void test_checkout_tree__initialize(void)
{
	g_repo = cl_git_sandbox_init("testrepo");
13

Ben Straub committed
14
	GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTS_VERSION);
15
	g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
16 17 18 19
}

void test_checkout_tree__cleanup(void)
{
20
	git_object_free(g_object);
21
	g_object = NULL;
22

23 24 25 26 27
	cl_git_sandbox_cleanup();
}

void test_checkout_tree__cannot_checkout_a_non_treeish(void)
{
28 29 30
	/* blob */
	cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));

31
	cl_git_fail(git_checkout_tree(g_repo, g_object, NULL));
32 33 34 35 36 37
}

void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
{
	char *entries[] = { "ab/de/" };

38 39
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;
40 41 42 43 44

	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));

	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));

45
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
46 47 48 49 50

	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
void test_checkout_tree__can_checkout_and_remove_directory(void)
{
	git_reference *head;
	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));

	// Checkout brach "subtrees" and update HEAD, so that HEAD matches the current working tree
	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_git_pass(git_reference_symbolic_set_target(head, "refs/heads/subtrees"));
	git_reference_free(head);
	
	cl_assert_equal_i(true, git_path_isdir("./testrepo/ab/"));
	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/fgh/1.txt"));

	// Checkout brach "master" and update HEAD, so that HEAD matches the current working tree
	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_git_pass(git_reference_symbolic_set_target(head, "refs/heads/master"));
	git_reference_free(head);

	// This directory should no longer exist
	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
}

78 79 80 81
void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
{
	char *entries[] = { "de/" };

82 83
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;
84 85

	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab"));
86

87
	cl_assert_equal_i(false, git_path_isdir("./testrepo/de/"));
88

89
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
90

91 92
	cl_assert_equal_i(true, git_path_isfile("./testrepo/de/2.txt"));
	cl_assert_equal_i(true, git_path_isfile("./testrepo/de/fgh/1.txt"));
93
}
94

95
static void progress(const char *path, size_t cur, size_t tot, void *payload)
96
{
Ben Straub committed
97
	bool *was_called = (bool*)payload;
Ben Straub committed
98
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
Ben Straub committed
99
	*was_called = true;
100 101 102 103
}

void test_checkout_tree__calls_progress_callback(void)
{
Ben Straub committed
104
	bool was_called = 0;
105

106
	g_opts.progress_cb = progress;
Ben Straub committed
107
	g_opts.progress_payload = &was_called;
108 109

	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
110

111
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
112

Ben Straub committed
113
	cl_assert_equal_i(was_called, true);
114
}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
{
  git_oid master_oid;
  git_oid chomped_oid;
  git_commit* p_master_commit;
  git_commit* p_chomped_commit;
  git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

  git_oid_fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
  git_oid_fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d");
  cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid));
  cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid));

  /* A GIT_CHECKOUT_DEFAULT checkout is not allowed to add any file to the
   * working tree from the index as it is supposed to be a dry run. */
  opts.checkout_strategy = GIT_CHECKOUT_DEFAULT;
  git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts);
  cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt"));
}