Commit ef9905c9 by Ben Straub

checkout: introduce git_checkout_opts

Refactor checkout into several more-sensible
entry points, which consolidates common options
into a single structure that may be passed around.
parent dc03369c
......@@ -21,14 +21,44 @@
*/
GIT_BEGIN_DECL
#define GIT_CHECKOUT_OVERWRITE_EXISTING 0
#define GIT_CHECKOUT_SKIP_EXISTING 1
typedef struct git_checkout_opts {
git_indexer_stats stats;
int existing_file_action;
int apply_filters;
int dir_mode;
int file_open_mode;
} git_checkout_opts;
#define GIT_CHECKOUT_DEFAULT_OPTS { \
{0}, \
GIT_CHECKOUT_OVERWRITE_EXISTING, \
true, \
GIT_DIR_MODE, \
O_CREAT|O_TRUNC|O_WRONLY \
}
/**
* Updates files in the working tree to match the index.
*
* @param repo repository to check out (must be non-bare)
* @param opts specifies checkout options (may be NULL)
* @return 0 on success, GIT_ERROR otherwise (use git_error_last for information about the error)
*/
GIT_EXTERN(int) git_checkout_index(git_repository *repo, git_checkout_opts *opts);
/**
* Updates files in the working tree to match the version in the index.
* Updates files in the working tree to match the commit pointed to by HEAD.
*
* @param repo repository to check out (must be non-bare)
* @param stats pointer to structure that receives progress information (may be NULL)
* @param opts specifies checkout options (may be NULL)
* @return 0 on success, GIT_ERROR otherwise (use git_error_last for information about the error)
*/
GIT_EXTERN(int) git_checkout_force(git_repository *repo, git_indexer_stats *stats);
GIT_EXTERN(int) git_checkout_head(git_repository *repo, git_checkout_opts *opts);
/** @} */
GIT_END_DECL
......
......@@ -27,7 +27,7 @@ GIT_BEGIN_DECL
typedef struct tree_walk_data
{
git_indexer_stats *stats;
git_checkout_opts *opts;
git_repository *repo;
git_odb *odb;
bool do_symlinks;
......@@ -120,21 +120,21 @@ static int checkout_walker(const char *path, const git_tree_entry *entry, void *
}
git_buf_free(&fnbuf);
data->stats->processed++;
data->opts->stats.processed++;
return retcode;
}
int git_checkout_force(git_repository *repo, git_indexer_stats *stats)
int git_checkout_index(git_repository *repo, git_checkout_opts *opts)
{
int retcode = GIT_ERROR;
git_indexer_stats dummy_stats;
git_checkout_opts default_opts = GIT_CHECKOUT_DEFAULT_OPTS;
git_tree *tree;
tree_walk_data payload;
git_config *cfg;
assert(repo);
if (!stats) stats = &dummy_stats;
if (!opts) opts = &default_opts;
if (git_repository_is_bare(repo)) {
giterr_set(GITERR_INVALID, "Checkout is not allowed for bare repositories");
......@@ -150,12 +150,12 @@ int git_checkout_force(git_repository *repo, git_indexer_stats *stats)
git_config_free(cfg);
}
stats->total = stats->processed = 0;
payload.stats = stats;
opts->stats.total = opts->stats.processed = 0;
payload.opts = opts;
payload.repo = repo;
if (git_repository_odb(&payload.odb, repo) < 0) return GIT_ERROR;
/* TODO: stats->total is never calculated. */
/* TODO: opts->stats.total is never calculated. */
if (!git_repository_head_tree(&tree, repo)) {
/* Checkout the files */
......@@ -170,4 +170,11 @@ int git_checkout_force(git_repository *repo, git_indexer_stats *stats)
}
int git_checkout_head(git_repository *repo, git_checkout_opts *opts)
{
/* TODO */
return -1;
}
GIT_END_DECL
......@@ -161,20 +161,20 @@ static int update_head_to_remote(git_repository *repo, git_remote *remote)
static int setup_remotes_and_fetch(git_repository *repo,
const char *origin_url,
git_indexer_stats *stats)
git_indexer_stats *fetch_stats)
{
int retcode = GIT_ERROR;
git_remote *origin = NULL;
git_off_t bytes = 0;
git_indexer_stats dummy_stats;
if (!stats) stats = &dummy_stats;
if (!fetch_stats) fetch_stats = &dummy_stats;
/* Create the "origin" remote */
if (!git_remote_add(&origin, repo, "origin", origin_url)) {
/* Connect and download everything */
if (!git_remote_connect(origin, GIT_DIR_FETCH)) {
if (!git_remote_download(origin, &bytes, stats)) {
if (!git_remote_download(origin, &bytes, fetch_stats)) {
/* Create "origin/foo" branches for all remote branches */
if (!git_remote_update_tips(origin, NULL)) {
/* Point HEAD to the same ref as the remote's head */
......@@ -209,18 +209,21 @@ static bool path_is_okay(const char *path)
static int clone_internal(git_repository **out,
const char *origin_url,
const char *path,
git_indexer_stats *stats,
git_indexer_stats *fetch_stats,
int is_bare)
{
int retcode = GIT_ERROR;
git_repository *repo = NULL;
git_indexer_stats dummy_stats;
if (!fetch_stats) fetch_stats = &dummy_stats;
if (!path_is_okay(path)) {
return GIT_ERROR;
}
if (!(retcode = git_repository_init(&repo, path, is_bare))) {
if ((retcode = setup_remotes_and_fetch(repo, origin_url, stats)) < 0) {
if ((retcode = setup_remotes_and_fetch(repo, origin_url, fetch_stats)) < 0) {
/* Failed to fetch; clean up */
git_repository_free(repo);
git_futils_rmdir_r(path, GIT_DIRREMOVAL_FILES_AND_DIRS);
......@@ -236,25 +239,25 @@ static int clone_internal(git_repository **out,
int git_clone_bare(git_repository **out,
const char *origin_url,
const char *dest_path,
git_indexer_stats *stats)
git_indexer_stats *fetch_stats)
{
assert(out && origin_url && dest_path);
return clone_internal(out, origin_url, dest_path, stats, 1);
return clone_internal(out, origin_url, dest_path, fetch_stats, 1);
}
int git_clone(git_repository **out,
const char *origin_url,
const char *workdir_path,
git_indexer_stats *stats)
git_indexer_stats *fetch_stats,
git_checkout_opts *checkout_opts)
{
int retcode = GIT_ERROR;
assert(out && origin_url && workdir_path);
if (!(retcode = clone_internal(out, origin_url, workdir_path, stats, 0))) {
git_indexer_stats checkout_stats;
retcode = git_checkout_force(*out, &checkout_stats);
if (!(retcode = clone_internal(out, origin_url, workdir_path, fetch_stats, 0))) {
retcode = git_checkout_head(*out, checkout_opts);
}
return retcode;
......
......@@ -3,10 +3,6 @@
#include "git2/checkout.h"
#include "repository.h"
#define DO_LOCAL_TEST 0
#define DO_LIVE_NETWORK_TESTS 1
#define LIVE_REPO_URL "http://github.com/libgit2/node-gitteh"
static git_repository *g_repo;
......@@ -42,12 +38,12 @@ void test_checkout_checkout__bare(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_fail(git_checkout_force(g_repo, NULL));
cl_git_fail(git_checkout_index(g_repo, NULL));
}
void test_checkout_checkout__default(void)
{
cl_git_pass(git_checkout_force(g_repo, NULL));
cl_git_pass(git_checkout_index(g_repo, NULL));
test_file_contents("./testrepo/README", "hey there\n");
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
test_file_contents("./testrepo/new.txt", "my new file\n");
......@@ -61,7 +57,7 @@ void test_checkout_checkout__crlf(void)
"README text eol=cr\n"
"new.txt text eol=lf\n";
cl_git_mkfile("./testrepo/.gitattributes", attributes);
cl_git_pass(git_checkout_force(g_repo, NULL));
cl_git_pass(git_checkout_index(g_repo, NULL));
/* test_file_contents("./testrepo/README", "hey there\n"); */
/* test_file_contents("./testrepo/new.txt", "my new file\n"); */
/* test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n"); */
......@@ -84,7 +80,7 @@ void test_checkout_checkout__symlinks(void)
{
/* First try with symlinks forced on */
enable_symlinks(true);
cl_git_pass(git_checkout_force(g_repo, NULL));
cl_git_pass(git_checkout_index(g_repo, NULL));
#ifdef GIT_WIN32
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
......@@ -105,7 +101,7 @@ void test_checkout_checkout__symlinks(void)
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo");
enable_symlinks(false);
cl_git_pass(git_checkout_force(g_repo, NULL));
cl_git_pass(git_checkout_index(g_repo, NULL));
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
}
......@@ -3,8 +3,8 @@
#include "git2/clone.h"
#include "repository.h"
#define DO_LIVE_NETWORK_TESTS 0
#define DO_LOCAL_TEST 0
#define DO_LIVE_NETWORK_TESTS 1
#define LIVE_REPO_URL "http://github.com/libgit2/node-gitteh"
......@@ -67,7 +67,7 @@ static void build_local_file_url(git_buf *out, const char *fixture)
void test_clone_clone__bad_url(void)
{
/* Clone should clean up the mess if the URL isn't a git repository */
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL));
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL, NULL));
cl_assert(!git_path_exists("./foo"));
cl_git_fail(git_clone_bare(&g_repo, "not_a_repo", "./foo.git", NULL));
cl_assert(!git_path_exists("./foo.git"));
......@@ -80,7 +80,7 @@ void test_clone_clone__local(void)
build_local_file_url(&src, cl_fixture("testrepo.git"));
#if DO_LOCAL_TEST
cl_git_pass(git_clone(&g_repo, git_buf_cstr(&src), "./local", NULL));
cl_git_pass(git_clone(&g_repo, git_buf_cstr(&src), "./local", NULL, NULL));
git_repository_free(g_repo);
git_futils_rmdir_r("./local", GIT_DIRREMOVAL_FILES_AND_DIRS);
cl_git_pass(git_clone_bare(&g_repo, git_buf_cstr(&src), "./local.git", NULL));
......@@ -96,7 +96,7 @@ void test_clone_clone__network_full(void)
#if DO_LIVE_NETWORK_TESTS
git_remote *origin;
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test2", NULL));
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test2", NULL, NULL));
cl_assert(!git_repository_is_bare(g_repo));
cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
git_futils_rmdir_r("./test2", GIT_DIRREMOVAL_FILES_AND_DIRS);
......@@ -121,19 +121,19 @@ void test_clone_clone__already_exists(void)
#if DO_LIVE_NETWORK_TESTS
/* Should pass with existing-but-empty dir */
p_mkdir("./foo", GIT_DIR_MODE);
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL));
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL));
git_repository_free(g_repo); g_repo = NULL;
git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS);
#endif
/* Should fail with a file */
cl_git_mkfile("./foo", "Bar!");
cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL));
cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL));
git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS);
/* Should fail with existing-and-nonempty dir */
p_mkdir("./foo", GIT_DIR_MODE);
cl_git_mkfile("./foo/bar", "Baz!");
cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL));
cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL));
git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS);
}
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