Commit 3aa443a9 by nulltoken

checkout: introduce git_checkout_tree()

parent e8776d30
...@@ -32,10 +32,16 @@ typedef struct git_checkout_opts { ...@@ -32,10 +32,16 @@ typedef struct git_checkout_opts {
int dir_mode; /* default is 0755 */ int dir_mode; /* default is 0755 */
int file_mode; /* default is 0644 */ int file_mode; /* default is 0644 */
int file_open_flags; /* default is O_CREAT | O_TRUNC | O_WRONLY */ int file_open_flags; /* default is O_CREAT | O_TRUNC | O_WRONLY */
/* when not NULL, arrays of fnmatch pattern specifying
* which paths should be taken into account
*/
git_strarray *paths;
} git_checkout_opts; } git_checkout_opts;
/** /**
* Updates files in the working tree to match the commit pointed to by HEAD. * Updates files in the index and the working tree to match the content of the
* commit pointed at by HEAD.
* *
* @param repo repository to check out (must be non-bare) * @param repo repository to check out (must be non-bare)
* @param opts specifies checkout options (may be NULL) * @param opts specifies checkout options (may be NULL)
...@@ -49,7 +55,9 @@ GIT_EXTERN(int) git_checkout_head( ...@@ -49,7 +55,9 @@ GIT_EXTERN(int) git_checkout_head(
git_indexer_stats *stats); git_indexer_stats *stats);
/** /**
* Updates files in the working tree to match a commit pointed to by a ref. * Updates files in the index and the working tree to match the content of the
* commit pointed at by the reference.
*
* *
* @param ref reference to follow to a commit * @param ref reference to follow to a commit
* @param opts specifies checkout options (may be NULL) * @param opts specifies checkout options (may be NULL)
...@@ -62,6 +70,23 @@ GIT_EXTERN(int) git_checkout_reference( ...@@ -62,6 +70,23 @@ GIT_EXTERN(int) git_checkout_reference(
git_checkout_opts *opts, git_checkout_opts *opts,
git_indexer_stats *stats); git_indexer_stats *stats);
/**
* Updates files in the index and working tree to match the content of the
* tree pointed at by the treeish.
*
* @param repo repository to check out (must be non-bare)
* @param treeish a commit, tag or tree which content will be used to update
* the working directory
* @param opts specifies checkout options (may be NULL)
* @param stats structure through which progress information is reported
* @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
* about the error)
*/
GIT_EXTERN(int) git_checkout_tree(
git_repository *repo,
git_object *treeish,
git_checkout_opts *opts,
git_indexer_stats *stats);
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -235,9 +235,8 @@ int git_clone(git_repository **out, ...@@ -235,9 +235,8 @@ int git_clone(git_repository **out,
assert(out && origin_url && workdir_path); assert(out && origin_url && workdir_path);
if (!(retcode = clone_internal(out, origin_url, workdir_path, fetch_stats, 0))) { if (!(retcode = clone_internal(out, origin_url, workdir_path, fetch_stats, 0)))
retcode = git_checkout_head(*out, checkout_opts, checkout_stats); retcode = git_checkout_head(*out, checkout_opts, checkout_stats);
}
return retcode; return retcode;
} }
...@@ -165,37 +165,21 @@ int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters) ...@@ -165,37 +165,21 @@ int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters)
return 0; return 0;
} }
static int unfiltered_blob_contents(git_buf *out, git_repository *repo, const git_oid *blob_id) int git_filter_blob_content(git_buf *out, git_blob *blob, const char *hintpath)
{ {
int retcode = GIT_ERROR; git_buf unfiltered = GIT_BUF_INIT;
git_blob *blob; git_vector filters = GIT_VECTOR_INIT;
int retcode;
if (!(retcode = git_blob_lookup(&blob, repo, blob_id)))
{
retcode = git_blob__getbuf(out, blob);
git_blob_free(blob);
}
return retcode; retcode = git_blob__getbuf(&unfiltered, blob);
}
int git_filter_blob_contents(git_buf *out, git_repository *repo, const git_oid *oid, const char *path) git_buf_clear(out);
{
int retcode = GIT_ERROR;
git_buf unfiltered = GIT_BUF_INIT; if (git_filters_load(&filters, git_object_owner((git_object *)blob), hintpath, GIT_FILTER_TO_WORKTREE) >= 0)
if (!unfiltered_blob_contents(&unfiltered, repo, oid)) {
git_vector filters = GIT_VECTOR_INIT;
if (git_filters_load(&filters,
repo, path, GIT_FILTER_TO_WORKTREE) >= 0) {
git_buf_clear(out);
retcode = git_filters_apply(out, &unfiltered, &filters); retcode = git_filters_apply(out, &unfiltered, &filters);
}
git_filters_free(&filters);
}
git_filters_free(&filters);
git_buf_free(&unfiltered); git_buf_free(&unfiltered);
return retcode; return retcode;
} }
...@@ -124,11 +124,10 @@ extern int git_text_is_binary(git_text_stats *stats); ...@@ -124,11 +124,10 @@ extern int git_text_is_binary(git_text_stats *stats);
* Get the content of a blob after all filters have been run. * Get the content of a blob after all filters have been run.
* *
* @param out buffer to receive the contents * @param out buffer to receive the contents
* @param repo repository containing the blob * @param hintpath path to the blob's output file, relative to the workdir root.
* @param oid object id for the blob * Used to determine what git filters should be applied to the content.
* @param path path to the blob's output file, relative to the workdir root
* @return 0 on success, an error code otherwise * @return 0 on success, an error code otherwise
*/ */
extern int git_filter_blob_contents(git_buf *out, git_repository *repo, const git_oid *oid, const char *path); extern int git_filter_blob_content(git_buf *out, git_blob *blob, const char *hintpath);
#endif #endif
...@@ -5,21 +5,28 @@ ...@@ -5,21 +5,28 @@
static git_repository *g_repo; static git_repository *g_repo;
static git_object *g_treeish;
static git_checkout_opts g_opts;
void test_checkout_checkout__initialize(void) void test_checkout_tree__initialize(void)
{ {
const char *attributes = "* text eol=lf\n"; memset(&g_opts, 0, sizeof(g_opts));
g_repo = cl_git_sandbox_init("testrepo"); g_repo = cl_git_sandbox_init("testrepo");
cl_git_mkfile("./testrepo/.gitattributes", attributes);
cl_git_rewritefile(
"./testrepo/.gitattributes",
"* text eol=lf\n");
cl_git_pass(git_repository_head_tree((git_tree **)&g_treeish, g_repo));
} }
void test_checkout_checkout__cleanup(void) void test_checkout_tree__cleanup(void)
{ {
git_object_free(g_treeish);
cl_git_sandbox_cleanup(); cl_git_sandbox_cleanup();
} }
static void test_file_contents(const char *path, const char *expectedcontents) static void test_file_contents(const char *path, const char *expectedcontents)
{ {
int fd; int fd;
...@@ -37,70 +44,105 @@ static void test_file_contents(const char *path, const char *expectedcontents) ...@@ -37,70 +44,105 @@ static void test_file_contents(const char *path, const char *expectedcontents)
cl_assert_equal_s(buffer, expectedcontents); cl_assert_equal_s(buffer, expectedcontents);
} }
void test_checkout_tree__cannot_checkout_a_bare_repository(void)
void test_checkout_checkout__bare(void)
{ {
cl_git_sandbox_cleanup(); test_checkout_tree__cleanup();
memset(&g_opts, 0, sizeof(g_opts));
g_repo = cl_git_sandbox_init("testrepo.git"); g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_fail(git_checkout_head(g_repo, NULL, NULL)); cl_git_pass(git_repository_head_tree((git_tree **)&g_treeish, g_repo));
cl_git_fail(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
} }
void test_checkout_checkout__default(void) void test_checkout_tree__update_the_content_of_workdir_with_missing_files(void)
{ {
cl_git_pass(git_checkout_head(g_repo, NULL, NULL)); cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
test_file_contents("./testrepo/README", "hey there\n"); test_file_contents("./testrepo/README", "hey there\n");
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n"); test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
test_file_contents("./testrepo/new.txt", "my new file\n"); test_file_contents("./testrepo/new.txt", "my new file\n");
} }
void test_checkout_tree__honor_the_specified_pathspecs(void)
{
git_strarray paths;
char *entries[] = { "*.txt" };
paths.strings = entries;
paths.count = 1;
g_opts.paths = &paths;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
void test_checkout_checkout__crlf(void) cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
test_file_contents("./testrepo/new.txt", "my new file\n");
}
static void set_config_entry_to(const char *entry_name, bool value)
{
git_config *cfg;
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_git_pass(git_config_set_bool(cfg, entry_name, value));
git_config_free(cfg);
}
static void set_core_autocrlf_to(bool value)
{
set_config_entry_to("core.autocrlf", value);
}
void test_checkout_tree__honor_the_gitattributes_directives(void)
{ {
const char *attributes = const char *attributes =
"branch_file.txt text eol=crlf\n" "branch_file.txt text eol=crlf\n"
"new.txt text eol=lf\n"; "new.txt text eol=lf\n";
git_config *cfg;
cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false));
cl_git_mkfile("./testrepo/.gitattributes", attributes); cl_git_mkfile("./testrepo/.gitattributes", attributes);
set_core_autocrlf_to(false);
cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
test_file_contents("./testrepo/README", "hey there\n"); test_file_contents("./testrepo/README", "hey there\n");
test_file_contents("./testrepo/new.txt", "my new file\n"); test_file_contents("./testrepo/new.txt", "my new file\n");
test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n"); test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
} }
void test_checkout_tree__honor_coreautocrlf_setting_set_to_true(void)
void test_checkout_checkout__win32_autocrlf(void)
{ {
#ifdef GIT_WIN32 #ifdef GIT_WIN32
git_config *cfg;
const char *expected_readme_text = "hey there\r\n"; const char *expected_readme_text = "hey there\r\n";
cl_must_pass(p_unlink("./testrepo/.gitattributes")); cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_git_pass(git_repository_config__weakptr(&cfg, g_repo)); set_core_autocrlf_to(true);
cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", true));
cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
test_file_contents("./testrepo/README", expected_readme_text); test_file_contents("./testrepo/README", expected_readme_text);
#endif #endif
} }
static void set_repo_symlink_handling_cap_to(bool value)
static void enable_symlinks(bool enable)
{ {
git_config *cfg; set_config_entry_to("core.symlinks", value);
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_git_pass(git_config_set_bool(cfg, "core.symlinks", enable));
git_config_free(cfg);
} }
void test_checkout_checkout__symlinks(void) void test_checkout_tree__honor_coresymlinks_setting_set_to_true(void)
{ {
/* First try with symlinks forced on */ set_repo_symlink_handling_cap_to(true);
enable_symlinks(true);
cl_git_pass(git_checkout_head(g_repo, NULL, NULL)); cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
#ifdef GIT_WIN32 #ifdef GIT_WIN32
test_file_contents("./testrepo/link_to_new.txt", "new.txt"); test_file_contents("./testrepo/link_to_new.txt", "new.txt");
...@@ -116,56 +158,67 @@ void test_checkout_checkout__symlinks(void) ...@@ -116,56 +158,67 @@ void test_checkout_checkout__symlinks(void)
test_file_contents("./testrepo/link_to_new.txt", "my new file\n"); test_file_contents("./testrepo/link_to_new.txt", "my new file\n");
} }
#endif #endif
}
/* Now with symlinks forced off */ void test_checkout_tree__honor_coresymlinks_setting_set_to_false(void)
cl_git_sandbox_cleanup(); {
g_repo = cl_git_sandbox_init("testrepo"); set_repo_symlink_handling_cap_to(false);
enable_symlinks(false);
cl_git_pass(git_checkout_head(g_repo, NULL, NULL)); cl_git_pass(git_checkout_tree(g_repo, g_treeish, NULL, NULL));
test_file_contents("./testrepo/link_to_new.txt", "new.txt"); test_file_contents("./testrepo/link_to_new.txt", "new.txt");
} }
void test_checkout_checkout__existing_file_skip(void) void test_checkout_tree__options_skip_existing_file(void)
{ {
git_checkout_opts opts = {0};
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!"); cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING; g_opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING;
cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
test_file_contents("./testrepo/new.txt", "This isn't what's stored!"); test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
} }
void test_checkout_checkout__existing_file_overwrite(void) void test_checkout_tree__options_overwrite_existing_file(void)
{ {
git_checkout_opts opts = {0};
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!"); cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING; g_opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
test_file_contents("./testrepo/new.txt", "my new file\n"); test_file_contents("./testrepo/new.txt", "my new file\n");
} }
void test_checkout_checkout__disable_filters(void) void test_checkout_tree__options_disable_filters(void)
{ {
git_checkout_opts opts = {0};
cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n"); cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
/* TODO cl_git_pass(git_checkout_head(g_repo, &opts, NULL));*/
/* TODO test_file_contents("./testrepo/new.txt", "my new file\r\n");*/ g_opts.disable_filters = false;
opts.disable_filters = true; cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
test_file_contents("./testrepo/new.txt", "my new file\r\n");
p_unlink("./testrepo/new.txt");
g_opts.disable_filters = true;
cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
test_file_contents("./testrepo/new.txt", "my new file\n"); test_file_contents("./testrepo/new.txt", "my new file\n");
} }
void test_checkout_checkout__dir_modes(void) void test_checkout_tree__options_dir_modes(void)
{ {
#ifndef GIT_WIN32 #ifndef GIT_WIN32
git_checkout_opts opts = {0};
struct stat st; struct stat st;
git_reference *ref; git_oid oid;
git_commit *commit;
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/dir")); cl_git_pass(git_reference_name_to_oid(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
g_opts.dir_mode = 0701;
cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &g_opts, NULL));
opts.dir_mode = 0701;
cl_git_pass(git_checkout_reference(ref, &opts, NULL));
cl_git_pass(p_stat("./testrepo/a", &st)); cl_git_pass(p_stat("./testrepo/a", &st));
cl_assert_equal_i(st.st_mode & 0777, 0701); cl_assert_equal_i(st.st_mode & 0777, 0701);
...@@ -173,34 +226,43 @@ void test_checkout_checkout__dir_modes(void) ...@@ -173,34 +226,43 @@ void test_checkout_checkout__dir_modes(void)
cl_git_pass(p_stat("./testrepo/a/b.txt", &st)); cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
cl_assert_equal_i(st.st_mode & 0777, 0755); cl_assert_equal_i(st.st_mode & 0777, 0755);
git_reference_free(ref); git_commit_free(commit);
#endif #endif
} }
void test_checkout_checkout__override_file_modes(void) void test_checkout_tree__options_override_file_modes(void)
{ {
#ifndef GIT_WIN32 #ifndef GIT_WIN32
git_checkout_opts opts = {0};
struct stat st; struct stat st;
opts.file_mode = 0700; g_opts.file_mode = 0700;
cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
cl_git_pass(p_stat("./testrepo/new.txt", &st)); cl_git_pass(p_stat("./testrepo/new.txt", &st));
cl_assert_equal_i(st.st_mode & 0777, 0700); cl_assert_equal_i(st.st_mode & 0777, 0700);
#endif #endif
} }
void test_checkout_checkout__open_flags(void) void test_checkout_tree__options_open_flags(void)
{ {
git_checkout_opts opts = {0};
cl_git_mkfile("./testrepo/new.txt", "hi\n"); cl_git_mkfile("./testrepo/new.txt", "hi\n");
opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
cl_git_pass(git_checkout_head(g_repo, &opts, NULL)); g_opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
cl_git_pass(git_checkout_tree(g_repo, g_treeish, &g_opts, NULL));
test_file_contents("./testrepo/new.txt", "hi\nmy new file\n"); test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
} }
void test_checkout_checkout__detached_head(void) void test_checkout_tree__cannot_checkout_a_non_treeish(void)
{ {
/* TODO: write this when git_checkout_commit is implemented. */ git_oid oid;
git_blob *blob;
cl_git_pass(git_oid_fromstr(&oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
cl_git_pass(git_blob_lookup(&blob, g_repo, &oid));
cl_git_fail(git_checkout_tree(g_repo, (git_object *)blob, NULL, NULL));
git_blob_free(blob);
} }
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